/* Tema: pista de atletismo à noite. Azul de pista, linhas brancas de raia,
   âmbar de placar de cronometragem para tudo que é "match". */
:root {
  --track: #12202b;      /* fundo */
  --lane: #1a2c3a;       /* superfícies */
  --lane-2: #223747;     /* superfície elevada / hover */
  --line: #2b4354;       /* bordas */
  --chalk: #eef1f3;      /* texto */
  --muted: #8ea3b3;
  --amber: #ffb020;      /* match, threshold */
  --amber-ink: #1d1500;
  --ok: #5fd39a;
  --err: #ff6b5e;

  --display: "Bahnschrift", "DIN Alternate", "DIN Condensed", "Arial Narrow", system-ui, sans-serif;
  --body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --r-sm: 4px;
  --r-md: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--track);
  color: var(--chalk);
  font: 15px/1.5 var(--body);
  min-height: 100vh;
}
h2, h3 { font-family: var(--display); font-weight: 600; margin: 0 0 .5rem; letter-spacing: .01em; }
h2 { font-size: 1.6rem; line-height: 1.15; }
h3 { font-size: 1.15rem; }
p { margin: 0 0 .75rem; }
.num { font-family: var(--display); font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }

/* ------------------------------------------------------------ topo ---- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .9rem clamp(16px, 3vw, 32px);
  border-bottom: 1px solid var(--line);
  background: var(--track);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: .6rem; }
/* marca: um visor de enquadramento de rosto */
.brand-mark {
  width: 22px; height: 22px; border: 2px solid var(--amber); border-radius: 3px;
  box-shadow: inset 0 0 0 4px var(--track), inset 0 0 0 7px var(--amber);
}
.brand-name { font-family: var(--display); font-size: 1.25rem; font-weight: 700; }

/* abas como raias: a selecionada ganha a linha âmbar embaixo */
.tabs { display: flex; gap: .25rem; }
.tabs button {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  font: 600 1rem var(--display); padding: .45rem .9rem;
  border-bottom: 3px solid transparent;
}
.tabs button:hover { color: var(--chalk); }
.tabs button[aria-selected="true"] { color: var(--chalk); border-bottom-color: var(--amber); }

.event-picker { display: flex; gap: .5rem; margin-left: auto; }
select, input[type="text"] {
  background: var(--lane); color: var(--chalk); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: .45rem .6rem; font: inherit; min-width: 0;
}
select { max-width: 260px; }

.new-event {
  display: flex; gap: .5rem; flex-wrap: wrap;
  padding: .75rem clamp(16px, 3vw, 32px); background: var(--lane); border-bottom: 1px solid var(--line);
}
.new-event input { flex: 1 1 240px; }

/* --------------------------------------------------------- botões ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: 600 .95rem var(--body); padding: .5rem .95rem; border-radius: var(--r-sm);
  cursor: pointer; border: 1px solid transparent; text-decoration: none; white-space: nowrap;
}
.btn.primary { background: var(--amber); color: var(--amber-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--chalk); border-color: var(--line); }
.btn.ghost:hover { background: var(--lane-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------------------------------------------------------- layout ---- */
main { padding: clamp(16px, 3vw, 32px); }
.tab-panel { display: grid; grid-template-columns: 300px 1fr; gap: clamp(16px, 3vw, 40px); align-items: start; }
.tab-panel[hidden] { display: none; }
#tab-debug { display: block; }
#tab-debug[hidden] { display: none; }
.side { position: sticky; top: 84px; }
.work { min-width: 0; }
.hint { color: var(--muted); font-size: .88rem; max-width: 60ch; }
.msg { min-height: 1.4em; font-size: .9rem; color: var(--muted); }
.msg.warn { color: var(--amber); }
.msg.err { color: var(--err); }
.empty { color: var(--muted); padding: 3rem 0; }

@media (max-width: 820px) {
  .tab-panel { grid-template-columns: 1fr; }
  .side { position: static; }
  .event-picker { margin-left: 0; width: 100%; }
  .event-picker select { flex: 1; max-width: none; }
}

/* ------------------------------------------------------- fotógrafo ---- */
.event-title { font-size: 1.9rem; overflow-wrap: anywhere; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); margin: 1rem 0 1.25rem;
  border: 1px solid var(--line); border-radius: var(--r-md); overflow: hidden; }
.stats div { background: var(--lane); padding: .7rem .8rem; }
.stats dt { color: var(--muted); font-size: .8rem; }
.stats dd { margin: 0; font: 600 1.7rem/1.1 var(--display); font-variant-numeric: tabular-nums; }

.dropzone {
  display: flex; flex-direction: column; justify-content: center; gap: .3rem;
  min-height: 200px; padding: 2rem; cursor: pointer;
  border: 2px dashed var(--line); border-radius: var(--r-md);
  /* faixas de raia sutis no fundo */
  background: repeating-linear-gradient(90deg, transparent 0 79px, rgba(238,241,243,.035) 79px 81px), var(--lane);
}
.dropzone:hover, .dropzone.over { border-color: var(--amber); }
.dropzone.over { background-color: var(--lane-2); }
.dz-title { font: 600 1.5rem var(--display); }
.dz-sub { color: var(--muted); }
.dz-touch, .short { display: none; }
@media (hover: none) and (pointer: coarse) {
  .dz-mouse { display: none; }
  .dz-touch { display: inline; }
}

.batch { margin-top: 1.5rem; }
.batch-head { display: flex; justify-content: space-between; margin-bottom: .4rem; font-weight: 600; }
.bar { height: 6px; background: var(--lane-2); border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--amber); transition: width .25s; }
.bar-fill.done { background: var(--ok); }

.upload-list { list-style: none; margin: 1rem 0 0; padding: 0; display: grid; gap: 1px;
  background: var(--line); border: 1px solid var(--line); border-radius: var(--r-md); overflow: hidden; }
.upload-list li {
  display: grid; grid-template-columns: 1fr 120px minmax(120px, auto); gap: 1rem; align-items: center;
  padding: .55rem .8rem; background: var(--lane); font-size: .9rem;
}
.upload-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-list .state { color: var(--muted); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.upload-list .state.ok { color: var(--ok); }
.upload-list .state.err { color: var(--err); }
.upload-list .state.dup { color: var(--muted); }
.upload-list .bar { height: 4px; }
.bar-fill.working { background: repeating-linear-gradient(90deg, var(--amber) 0 10px, #c98a12 10px 20px);
  background-size: 40px 100%; animation: crawl .8s linear infinite; }
@keyframes crawl { to { background-position: 40px 0; } }

/* ----------------------------------------------------- participante ---- */
.selfie-box { background: var(--lane); border: 1px solid var(--line); border-radius: var(--r-md);
  overflow: hidden; min-height: 260px; display: grid; place-items: center; }
.selfie-empty { padding: 1.5rem; color: var(--muted); text-align: center; }
/* preview espelhado (como um espelho), mas a foto capturada não é */
#cam-video { width: 100%; aspect-ratio: 4/3; object-fit: cover; transform: scaleX(-1); }
.selfie-box .frame { width: fit-content; }
.selfie-box .frame img { max-height: 55vh; width: auto; max-width: 100%; }
.selfie-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin: .8rem 0 .4rem; }

/* "frame" = imagem + camada de retângulos por cima, em % do tamanho da foto,
   então funciona em qualquer escala (thumb, selfie, modal). */
.frame { position: relative; display: block; line-height: 0; }
.frame img { display: block; width: 100%; height: auto; }
.facebox { position: absolute; border: 2px solid var(--amber); border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.45); pointer-events: none; }
.facebox.other { border-style: dashed; border-color: rgba(238,241,243,.6); border-width: 1.5px; }

.threshold { margin-top: 1.25rem; }
.threshold-head { display: flex; justify-content: space-between; align-items: baseline; }
.threshold-head label { font-weight: 600; }
.threshold-head output { font-size: 1.6rem; color: var(--amber); }
.threshold-scale { display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); }
.thr-input { width: 100%; accent-color: var(--amber); margin: .3rem 0; }
.threshold.compact { margin: 0; min-width: 220px; }
.threshold.compact output { font-size: 1.3rem; }

.results-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  flex-wrap: wrap; margin-bottom: 1rem; }
.result-count { margin: 0; font: 600 1.35rem var(--display); }
.result-count b { color: var(--amber); font-weight: 700; }

/* Masonry: N colunas flex; app.js distribui os cards na coluna mais baixa. */
.gallery { --gap: 12px; display: flex; gap: var(--gap); align-items: flex-start; }
.gallery-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--gap); }
.card { position: relative; cursor: pointer; border-radius: var(--r-sm); overflow: hidden;
  background: var(--lane); border: 0; padding: 0; color: inherit; text-align: left; }
.card:hover img { filter: brightness(1.08); }
/* score como número de peito: retângulo branco, dígitos grossos */
.bib {
  position: absolute; left: 8px; bottom: 8px; line-height: 1;
  background: var(--chalk); color: #10202c; border-radius: 3px;
  font: 700 1.05rem var(--display); font-variant-numeric: tabular-nums;
  padding: .3rem .45rem .25rem; border-top: 3px solid var(--amber);
}
.gallery-empty { color: var(--muted); padding: 2rem 0; max-width: 55ch; }

/* ------------------------------------------------------------ debug ---- */
.debug-intro { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; align-items: end; margin-bottom: 1.5rem; }
.debug-intro p { color: var(--muted); max-width: 62ch; margin: 0; }
.debug-intro em { color: var(--chalk); font-style: normal; font-weight: 600; }
.debug-actions { display: flex; gap: 1.25rem; align-items: end; flex-wrap: wrap; }

/* A "linha de chegada": eixo de score com os rostos posicionados. */
.finish { margin: 0 0 2rem; }
.ruler {
  position: relative; min-height: 170px; border-radius: var(--r-md);
  background:
    repeating-linear-gradient(90deg, rgba(238,241,243,.06) 0 1px, transparent 1px 10%),
    var(--lane);
  border: 1px solid var(--line);
  overflow: hidden;
}
.ruler .accepted { position: absolute; top: 0; bottom: 0; right: 0; background: rgba(255,176,32,.07); }
.ruler .finish-line {
  position: absolute; top: 0; bottom: 0; width: 0; border-left: 3px solid var(--amber);
}
.ruler .finish-line span {
  position: absolute; top: 8px; left: 6px; white-space: nowrap;
  font: 700 .9rem var(--display); color: var(--amber);
}
.ruler .dot {
  position: absolute; width: var(--dot, 34px); height: var(--dot, 34px); margin-left: calc(var(--dot, 34px) / -2);
  border-radius: 50%; border: 2px solid var(--muted); background: var(--lane-2) no-repeat;
  cursor: pointer; padding: 0; transition: transform .1s;
}
.ruler .dot.above { border-color: var(--amber); }
.ruler .dot:hover, .ruler .dot:focus-visible { transform: scale(1.6); z-index: 2; }
/* 3 colunas (1fr auto 1fr): o "0.5" fica no centro exato, alinhado com a régua */
.ruler-axis { display: grid; grid-template-columns: 1fr auto 1fr; font: 600 .95rem var(--display);
  color: var(--muted); padding: .35rem 0 0; }
.ruler-axis span:last-child { text-align: right; }
.ruler-axis small { font: .75rem var(--body); margin-left: .35rem; }

.debug-grid { display: grid; grid-template-columns: minmax(260px, 1fr) 2fr; gap: 2.5rem; }
@media (max-width: 900px) { .debug-grid { grid-template-columns: 1fr; } }

.timing-bar { display: flex; height: 26px; border-radius: var(--r-sm); overflow: hidden; background: var(--lane-2); margin: .5rem 0 .8rem; }
.timing-bar div { min-width: 3px; }
.timing-legend { list-style: none; padding: 0; margin: 0 0 1rem; display: grid; gap: .3rem; }
.timing-legend li { display: flex; align-items: center; gap: .5rem; font-size: .9rem; }
.timing-legend .sw { width: 12px; height: 12px; border-radius: 2px; flex: none; }
.timing-legend .ms { margin-left: auto; font-family: var(--display); font-variant-numeric: tabular-nums; }

.debug-list { list-style: none; margin: 0; padding: 0; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.debug-list li { display: flex; gap: .55rem; align-items: center; background: var(--lane);
  border-radius: var(--r-sm); padding: .4rem; cursor: pointer; border-left: 3px solid var(--line); }
.debug-list li.above { border-left-color: var(--amber); }
.debug-list li:hover { background: var(--lane-2); }
.face-crop { width: 46px; height: 46px; border-radius: 3px; flex: none; background: var(--lane-2) no-repeat; }
.debug-list .score { font: 700 1.2rem var(--display); font-variant-numeric: tabular-nums; }
.debug-list li.above .score { color: var(--amber); }
.debug-list .meta { font-size: .75rem; color: var(--muted); line-height: 1.3; }

/* ------------------------------------------------------------ modal ---- */
.modal { border: 0; padding: 0; background: transparent; max-width: min(96vw, 1400px); max-height: 94vh; }
.modal::backdrop { background: rgba(6, 12, 18, .88); }
.modal-inner { display: flex; flex-direction: column; gap: .75rem; }
.modal-stage { display: grid; place-items: center; min-height: 0; }
.modal-frame img { max-height: calc(94vh - 70px); width: auto; max-width: 100%; margin: 0 auto; }
.modal-frame { width: fit-content; margin: 0 auto; }
.modal-bar { display: flex; gap: .6rem; align-items: center; justify-content: flex-end; color: var(--chalk); }
.modal-info { margin-right: auto; min-width: 0; display: flex; align-items: baseline; gap: .6rem; }
.modal-info strong { font-size: 1.35rem; color: var(--amber); white-space: nowrap; }
.modal-info span { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
body.modal-open { overflow: hidden; } /* página de trás não rola com o modal aberto */

/* ----------------------------------------------- participante mobile ---- */
@media (max-width: 820px) {
  /* header rola junto: sticky ele comeria ~150px da tela */
  .topbar { position: static; gap: .6rem 1rem; }
  .tabs { width: 100%; }
  .tabs button { flex: 1; }

  /* Painel da selfie vira uma barra compacta grudada no topo, para ajustar
     o threshold enquanto rola a galeria:
       [selfie] [Enviar selfie] [Usar webcam]
       [selfie] mensagem
       [Semelhança mínima 0.40 ━━━━●━━━━━━━]           */
  #tab-participant .selfie-side {
    position: sticky; top: 0; z-index: 5;
    display: grid; grid-template-columns: 76px 1fr; gap: 6px 12px; align-items: center;
    margin: calc(-1 * clamp(16px, 3vw, 32px)) calc(-1 * clamp(16px, 3vw, 32px)) 0;
    padding: 10px clamp(16px, 3vw, 32px);
    background: var(--track); border-bottom: 1px solid var(--line);
  }
  #tab-participant .selfie-box { grid-row: span 2; min-height: 0; aspect-ratio: 1; width: 76px; border-radius: var(--r-sm); }
  #tab-participant .selfie-box .frame img { max-height: 76px; }
  /* sem selfie: visor de enquadramento vazio no lugar do texto */
  .selfie-empty { width: 40px; height: 48px; padding: 0; border: 2px dashed var(--line); border-radius: 50% / 45%; }
  .selfie-empty p { display: none; }
  .selfie-actions { margin: 0; gap: .4rem; }
  .selfie-actions .btn { padding: .4rem .7rem; font-size: .88rem; }
  #selfie-msg { grid-column: 2; min-height: 0; margin: 0; font-size: .8rem; line-height: 1.3; }
  #selfie-msg:empty { display: none; }
  #tab-participant .threshold { grid-column: 1 / -1; margin: 0; display: flex; align-items: center; gap: .75rem; }
  #tab-participant .threshold-head { gap: .5rem; flex: none; }
  #tab-participant .threshold-head label { font-size: .88rem; }
  #tab-participant .threshold-head output { font-size: 1.2rem; min-width: 2.6em; }
  #tab-participant .threshold-scale { display: none; }
  #tab-participant .thr-input { flex: 1; margin: 0; }

  /* webcam aberta: painel expande e solta do topo para o vídeo ter espaço */
  #tab-participant .selfie-side.cam-on { position: static; grid-template-columns: 1fr; }
  #tab-participant .selfie-side.cam-on .selfie-box { grid-row: auto; width: 100%; aspect-ratio: auto; }

  .results-head { margin: 0 0 .75rem; flex-wrap: nowrap; }
  .zip-ext { display: none; }
  .result-count { font-size: 1rem; }
  #zip-btn { padding: .4rem .7rem; font-size: .85rem; }
  .gallery { --gap: 8px; }
  .bib { left: 6px; bottom: 6px; font-size: .9rem; padding: .22rem .35rem .18rem; }

}

/* ------------------------------------------- fotógrafo + debug mobile ---- */
@media (max-width: 820px) {
  /* Fotógrafo: o painel lateral "se desmonta" (display: contents) e seus
     filhos entram na grade da aba, reordenados: título, stats, upload e
     por último o texto explicativo. Assim o upload fica na primeira tela. */
  #tab-photographer .side { display: contents; }
  #tab-photographer .event-title { order: 1; font-size: 1.35rem; margin: 0; }
  #tab-photographer .stats { order: 2; margin: 0; grid-template-columns: repeat(4, 1fr); }
  #tab-photographer .work { order: 3; }
  #tab-photographer .hint { order: 4; margin: 0; }
  /* coluna flex com o valor empurrado para a base: se um rótulo quebrar em
     2 linhas, os números continuam alinhados entre as 4 células */
  .stats div { padding: .5rem .55rem; display: flex; flex-direction: column; }
  .stats dt { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .stats dd { margin-top: auto; }
  .stats dt { font-size: .72rem; line-height: 1.2; }
  .stats dd { font-size: 1.3rem; margin-top: .15rem; }
  .dropzone { min-height: 0; padding: 1.25rem; }
  .dz-title { font-size: 1.2rem; }

  /* progresso total grudado no topo enquanto rola a lista de fotos */
  .batch { margin-top: 1rem; }
  .batch-top {
    position: sticky; top: 0; z-index: 5;
    margin: 0 calc(-1 * clamp(16px, 3vw, 32px));
    padding: 10px clamp(16px, 3vw, 32px);
    background: var(--track);
  }
  /* linha de upload em 2 níveis:  nome ........ status
                                   ━━━━━━━━━━━━━━━━━━━━  */
  .upload-list li {
    grid-template-columns: 1fr auto; grid-template-areas: "name state" "bar bar";
    gap: 6px 12px; padding: .6rem .75rem;
  }
  .upload-list .name { grid-area: name; }
  .upload-list .state { grid-area: state; font-size: .85rem; }
  .upload-list li .bar { grid-area: bar; }

  /* Debug: o bloco de introdução também se desmonta, para que os controles
     (selfie + threshold) possam grudar no topo sobre a régua. */
  .debug-intro { display: contents; }
  .debug-intro > div:first-child { margin-bottom: 1rem; }
  .debug-intro h2 { font-size: 1.35rem; }
  .debug-actions {
    position: sticky; top: 0; z-index: 5; flex-wrap: nowrap; align-items: center; gap: .75rem;
    margin: 0 calc(-1 * clamp(16px, 3vw, 32px)) 1rem;
    padding: 10px clamp(16px, 3vw, 32px);
    background: var(--track); border-bottom: 1px solid var(--line);
  }
  .debug-actions .btn { padding: .4rem .7rem; font-size: .88rem; }
  .long { display: none; }
  .short { display: inline; }
  .threshold.compact { flex: 1; min-width: 0; display: flex; align-items: center; gap: .6rem; }
  .threshold.compact .threshold-head label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .threshold.compact .thr-input { flex: 1; margin: 0; }
  .finish { margin-bottom: 1.5rem; }
  .ruler-axis { font-size: .85rem; }
  .debug-grid { gap: 1.5rem; }
  .debug-list { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

/* ----------------------------------------------------- modal mobile ---- */
/* Celular (retrato) ou celular deitado (pouca altura): tela cheia, fundo
   sólido, foto centralizada no espaço livre e barra de ações embaixo. */
@media (max-width: 820px), (max-height: 500px) {
  .modal {
    width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0;
    background: #08121a;
  }
  .modal-inner { height: 100%; gap: 0; }
  .modal-stage { flex: 1; padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
  .modal-bar {
    flex-wrap: wrap; gap: .6rem;
    padding: .75rem 16px calc(.75rem + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line); background: var(--track);
  }
  /* retrato: score em cima, nome do arquivo embaixo (lado a lado cortava o nome) */
  .modal-info { flex: 1 1 100%; flex-direction: column; align-items: flex-start; gap: .1rem; }
  .modal-bar .btn { flex: 1; padding: .7rem 1rem; } /* alvos grandes para o dedo */
  /* altura livre = tela - barra (~140px: score + nome + botões + área do gesto) */
  .modal-frame img {
    max-width: 100vw;
    max-height: calc(100dvh - 140px - env(safe-area-inset-bottom) - env(safe-area-inset-top));
  }
}
/* celular deitado: legenda e botões numa linha só, sobra mais altura para a foto */
@media (max-height: 500px) {
  .modal-info { flex: 1 1 auto; flex-direction: row; align-items: baseline; gap: .6rem; }
  .modal-bar { flex-wrap: nowrap; padding-top: .5rem; padding-bottom: calc(.5rem + env(safe-area-inset-bottom)); }
  .modal-bar .btn { flex: none; padding: .45rem .9rem; }
  .modal-frame img { max-height: calc(100dvh - 60px - env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
