:root {
  --fifa-red: #E30613;
  --fifa-gold: #D4AF37;
  --fifa-blue: #003DA5;
  --fifa-green: #00A651;
  --fifa-yellow: #FCE51A;
  --bg: #F4F4F4;
  --surface: #FFFFFF;
  --text: #1A1A1A;
  --text-muted: #555555;
  --border: #D9D9D9;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);

  --state-faltante-bg: #ECECEC;
  --state-faltante-fg: #777777;
  --state-tenida-bg: var(--fifa-green);
  --state-tenida-fg: #FFFFFF;
  --state-repetida-bg: var(--fifa-yellow);
  --state-repetida-fg: #1A1A1A;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  color: var(--text);
  background: var(--bg);
}

a {
  color: var(--fifa-blue);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

button, .btn {
  font: inherit;
  border: none;
  border-radius: 6px;
  padding: 8px 16px;
  background: var(--fifa-blue);
  color: white;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
}
button:hover, .btn:hover { background: #002b73; }
button:active, .btn:active { transform: translateY(1px); }
button.secondary { background: #555; }
button.secondary:hover { background: #333; }
button.danger { background: var(--fifa-red); }

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: white;
}
input:focus {
  outline: 2px solid var(--fifa-blue);
  outline-offset: 1px;
}

/* ===== Login ===== */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 15% 20%, rgba(227,6,19,0.20), transparent 35%),
    radial-gradient(circle at 85% 15%, rgba(0,166,81,0.20), transparent 38%),
    radial-gradient(circle at 80% 85%, rgba(0,61,165,0.20), transparent 40%),
    radial-gradient(circle at 20% 80%, rgba(252,229,26,0.20), transparent 40%),
    var(--bg);
  padding: 24px;
}
.login-card {
  background: var(--surface);
  width: 100%;
  max-width: 380px;
  padding: 32px 28px;
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.login-card h1 {
  margin: 0 0 4px;
  font-size: 24px;
  color: var(--fifa-blue);
}
.login-card .subtitle {
  margin: 0 0 24px;
  color: var(--text-muted);
  font-size: 14px;
}
.field { margin-bottom: 14px; }
.field label {
  display: block;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--text-muted);
}
.login-card button {
  width: 100%;
  padding: 12px;
  margin-top: 6px;
}
.error-msg {
  color: var(--fifa-red);
  font-size: 13px;
  min-height: 18px;
  margin-top: 6px;
}

/* ===== App layout ===== */
.app-header {
  background: var(--fifa-blue);
  color: white;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
}
.app-header .brand {
  font-weight: 700;
  letter-spacing: 0.3px;
}
.app-header .user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
}
.app-header button {
  background: rgba(255,255,255,0.18);
}
.app-header button:hover { background: rgba(255,255,255,0.30); }

.app-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 20px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.app-tabs button {
  background: transparent;
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px 8px 0 0;
  border-bottom: 3px solid transparent;
}
.app-tabs button:hover { background: rgba(0,0,0,0.04); }
.app-tabs button.active {
  border-bottom-color: var(--fifa-red);
  font-weight: 600;
}

.app-main {
  padding: 20px;
  max-width: 1280px;
  margin: 0 auto;
}

/* ===== Buscador ===== */
.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}
.search-bar input {
  flex: 1;
  min-width: 0;
}
.search-bar button { white-space: nowrap; }
.camera-btn {
  background: var(--fifa-red);
  padding: 8px 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.camera-btn:hover { background: #b80511; }
.camera-btn svg { display: block; position: relative; z-index: 1; }
.camera-btn {
  position: relative;
  overflow: hidden;
  /* Suprime el callout nativo de iOS y la selección durante el long press */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.camera-btn svg { pointer-events: none; }

/* ===== Grupo selector + grilla ===== */
.grupos-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}
.grupo-pill {
  font: inherit;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}
.grupo-pill:hover { background: #EEE; }
.grupo-pill.active {
  background: var(--fifa-blue);
  color: white;
  border-color: var(--fifa-blue);
}

.grupos-select {
  display: none;
  width: 100%;
  padding: 12px 14px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--fifa-blue);
  border-radius: 8px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'><path fill='%23003DA5' d='M6 8L0 0h12z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 12px;
  padding-right: 36px;
}
.grupos-select:focus {
  outline: 2px solid var(--fifa-blue);
  outline-offset: 1px;
}

.grupo-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 12px;
}
.grupo-header h2 {
  margin: 0;
  font-size: 22px;
  color: var(--fifa-blue);
}
.grupo-progress {
  font-size: 14px;
  color: var(--text-muted);
}

.figuritas-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 10px;
}
.figurita {
  background: var(--state-faltante-bg);
  color: var(--state-faltante-fg);
  border-radius: 8px;
  padding: 14px 10px;
  text-align: center;
  cursor: pointer;
  user-select: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 84px;
  font-weight: 600;
  transition: transform 0.05s ease, background 0.15s ease, color 0.15s ease;
  position: relative;
}
.figurita:hover { transform: translateY(-1px); }
.figurita .codigo { font-size: 15px; }
.figurita .estado-tag {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.85;
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.figurita.tenida {
  background: var(--state-tenida-bg);
  color: var(--state-tenida-fg);
}
.figurita.repetida {
  background: var(--state-repetida-bg);
  color: var(--state-repetida-fg);
}
.repetida-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}
.repetida-controls button {
  padding: 2px 8px;
  background: rgba(0,0,0,0.15);
  color: inherit;
  font-weight: 700;
  border-radius: 4px;
}
.repetida-controls button:hover { background: rgba(0,0,0,0.30); }
.repetida-controls .badge {
  font-weight: 700;
  min-width: 24px;
  text-align: center;
}

/* ===== Estadísticas ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--surface);
  border-radius: 10px;
  padding: 18px 16px;
  box-shadow: var(--shadow);
}
.stat-card .label {
  color: var(--text-muted);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.stat-card .value {
  font-size: 32px;
  font-weight: 700;
  margin-top: 6px;
  color: var(--fifa-blue);
}
.stat-card.highlight .value { color: var(--fifa-green); }

.progress-bar {
  height: 10px;
  background: var(--state-faltante-bg);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 8px;
}
.progress-bar > div {
  height: 100%;
  background: var(--fifa-green);
  transition: width 0.4s ease;
}

.por-grupo-list {
  background: var(--surface);
  border-radius: 10px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.por-grupo-list h3 {
  margin-top: 0;
  color: var(--fifa-blue);
}
.grupo-row {
  display: grid;
  grid-template-columns: 180px 1fr 80px;
  gap: 12px;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.grupo-row:last-child { border-bottom: none; }
.grupo-row .name { font-weight: 600; }
.grupo-row .count { text-align: right; color: var(--text-muted); }

.export-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 16px 0 24px;
}

/* ===== Modal de búsqueda ===== */
.dialog {
  border: none;
  border-radius: 12px;
  padding: 0;
  max-width: 420px;
  width: calc(100% - 32px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.18);
  color: var(--text);
  background: var(--surface);
}
.dialog::backdrop {
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
}
.dialog-form {
  display: flex;
  flex-direction: column;
  padding: 20px 22px 18px;
}
.dialog-title {
  margin: 0 0 4px;
  color: var(--fifa-blue);
  font-size: 22px;
}
.dialog-subtitle {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 12px;
}
.dialog-estado {
  font-size: 15px;
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--state-faltante-bg);
  color: var(--state-faltante-fg);
  margin-bottom: 12px;
}
.dialog-estado.tenida {
  background: var(--state-tenida-bg);
  color: var(--state-tenida-fg);
}
.dialog-estado.repetida {
  background: var(--state-repetida-bg);
  color: var(--state-repetida-fg);
}
.dialog-cantidad {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--text-muted);
}
.dialog-cantidad[hidden] { display: none; }
.btn-state:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  filter: grayscale(40%);
}
.dialog-cantidad input {
  width: 80px;
  padding: 6px 8px;
}
.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}
.btn-state {
  padding: 12px 6px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  color: white;
  background: var(--text-muted);
}
.btn-state.state-tenida { background: var(--state-tenida-bg); }
.btn-state.state-tenida:hover { background: #008a44; }
.btn-state.state-faltante { background: #888; color: white; }
.btn-state.state-faltante:hover { background: #555; }
.btn-state.state-repetida { background: var(--state-repetida-bg); color: var(--state-repetida-fg); }
.btn-state.state-repetida:hover { background: #e8d119; }
.btn-state.active-state { outline: 3px solid var(--fifa-blue); outline-offset: 2px; }
.dialog-cancel {
  margin-top: 10px;
  text-align: center;
}
.dialog-cancel button {
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
  text-decoration: underline;
}
.dialog-cancel button:hover { background: transparent; color: var(--fifa-red); }
.dialog-error {
  color: var(--fifa-red);
  font-size: 13px;
  margin-top: 8px;
  min-height: 18px;
}

/* ===== Modal cámara ===== */
.camera-dialog { max-width: 520px; }
.camera-stage {
  width: 100%;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.camera-stage canvas {
  width: 100%;
  height: auto;
  display: block;
}
.camera-status {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 10px;
  min-height: 20px;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(0,0,0,0.04);
}
.camera-status.level-ok { background: rgba(47, 191, 113, 0.12); color: #1b6e3f; }
.camera-actions {
  display: flex;
  gap: 8px;
}
.camera-actions button { flex: 1; }

/* ===== Importar batch ===== */
.import-dialog { max-width: 480px; }
.import-help {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 0 0 12px 0;
}
.import-help code {
  background: rgba(0,0,0,0.05);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
}
.import-file-label {
  display: block;
  margin-bottom: 12px;
}
.import-file-label input[type=file] {
  display: block;
  width: 100%;
  padding: 8px;
  border: 1px dashed #c0c0c0;
  border-radius: 6px;
  background: #fafafa;
  cursor: pointer;
}
.import-file-label input[type=file]:hover { border-color: var(--fifa-blue, #2563eb); }
.import-preview {
  margin-bottom: 12px;
  padding: 10px;
  background: #f7f7f7;
  border-radius: 6px;
  font-size: 13px;
}
.import-stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}
.import-stat {
  padding: 3px 9px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 12px;
}
.import-stat.valid { background: rgba(47, 191, 113, 0.18); color: #1b6e3f; }
.import-stat.invalid { background: rgba(244, 183, 64, 0.20); color: #8a5a00; }
.import-stat.total { background: rgba(0,0,0,0.08); color: #555; }
.import-invalid {
  margin-top: 8px;
  font-size: 12px;
}
.import-invalid summary { cursor: pointer; color: #555; }
.import-invalid code {
  display: block;
  margin-top: 4px;
  max-height: 100px;
  overflow-y: auto;
  background: #fff;
  padding: 6px;
  border-radius: 4px;
  word-break: break-all;
  font-size: 11px;
}
.import-result-line {
  padding: 4px 0;
  font-size: 13px;
}
.import-result-line.ok { color: #1b6e3f; }
.import-result-line.info { color: #555; }
.import-result-line.warn { color: #8a5a00; }
.import-result-line.muted { color: #888; font-size: 11px; margin-top: 6px; }

/* ===== Modo ráfaga ===== */
.rafaga-dialog {
  max-width: 560px;
  width: 100%;
}
.rafaga-stage {
  width: 100%;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rafaga-stage canvas {
  width: 100%;
  height: auto;
  display: block;
}
.rafaga-status {
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(0,0,0,0.04);
  margin-bottom: 8px;
  min-height: 28px;
}
.rafaga-status.level-ok { background: rgba(47, 191, 113, 0.12); color: #1b6e3f; }
.rafaga-status.level-warn { background: rgba(244, 183, 64, 0.16); color: #8a5a00; }
.rafaga-list-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  margin: 6px 0 4px;
}
.rafaga-count {
  background: #2fbf71;
  color: #fff;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  min-width: 20px;
  text-align: center;
}
.rafaga-list {
  list-style: none;
  padding: 0;
  margin: 0 0 10px 0;
  max-height: 38vh;
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
}
.rafaga-list:empty::after {
  content: "Esperando detecciones…";
  display: block;
  text-align: center;
  padding: 16px;
  color: #999;
  font-size: 13px;
}
.rafaga-item {
  position: relative;
  padding: 8px 38px 8px 10px;
  border-bottom: 1px solid #f1f1f1;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 4px 8px;
  align-items: center;
}
.rafaga-item.removing { opacity: 0.4; pointer-events: none; }
.rafaga-item-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid #ddd;
  background: #fff;
  color: #888;
  font-size: 14px;
  font-weight: bold;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rafaga-item-delete:hover,
.rafaga-item-delete:focus-visible {
  border-color: #e96676;
  color: #c4291f;
  background: #fff5f5;
}
.rafaga-item:last-child { border-bottom: none; }
.rafaga-item.saving { opacity: 0.6; }
.rafaga-item.state-tenida { background: rgba(47, 191, 113, 0.06); }
.rafaga-item.state-repetida { background: rgba(244, 183, 64, 0.10); }
.rafaga-item.state-faltante { background: rgba(233, 102, 118, 0.06); }
.rafaga-item-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
  grid-column: 1 / 2;
  grid-row: 1 / 2;
}
.rafaga-item-head strong { font-size: 15px; letter-spacing: 0.5px; }
.rafaga-item-nombre { font-size: 11px; color: #666; }
.rafaga-item-body {
  grid-column: 2 / 3;
  grid-row: 1 / 2;
  font-size: 12px;
  color: #444;
  text-align: right;
}
.rafaga-item-actions {
  grid-column: 1 / 3;
  grid-row: 2 / 3;
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}
.rafaga-item-actions button {
  font-size: 11px;
  padding: 4px 8px;
  border: 1px solid #d0d0d0;
  background: #fafafa;
  border-radius: 4px;
  cursor: pointer;
}
.rafaga-item-actions button:hover:not(:disabled) {
  border-color: #2fbf71;
  color: #1b6e3f;
}
.rafaga-item-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ===== Responsive ===== */
@media (max-width: 760px) {
  /* En pantallas angostas la barra de pills consume demasiado espacio;
     la reemplazamos por un <select> nativo (mejor UX en touch). */
  .grupos-bar { display: none; }
  .grupos-select { display: block; }
}

@media (max-width: 600px) {
  .app-header { padding: 10px 12px; }
  .app-tabs { padding: 6px 8px 0; }
  .app-main { padding: 12px; }
  .grupo-header h2 { font-size: 18px; }
  .figuritas-grid { grid-template-columns: repeat(auto-fill, minmax(95px, 1fr)); }
  .grupo-row { grid-template-columns: 1fr 1fr 60px; }
  .grupo-row .name { font-size: 13px; }
}
