:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #ffffff;
  --ink: #1f1d1d;
  --ink-soft: #3a3737;
  --muted: #6d6a64;
  --line: #dedbd2;
  --line-soft: #ece9e0;
  --yellow: #ffd400;
  --yellow-soft: #fff5b8;
  --blue: #225d9b;
  --danger: #b23b3b;
  --danger-soft: #f6e4e2;
  --ok: #2e7d4f;
  --radius: 10px;
  --shadow: 0 8px 24px rgba(31, 29, 29, 0.09);
  --display: Haettenschweiler, Impact, "Arial Narrow", "Arial Black", sans-serif;
  --body: "Segoe UI", Roboto, Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --nav-h: 64px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body);
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

h2 { font-family: var(--display); font-size: 1.7rem; line-height: 1.05; text-transform: uppercase; letter-spacing: 0.01em; }
h3 { font-size: 1rem; font-weight: 800; }

/* ---------- Splash / Login ---------- */
.splash {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  padding: 24px; text-align: center; background: var(--ink); color: #fff;
}
.splash p { color: rgba(255, 255, 255, 0.75); max-width: 30ch; }
.splash img { height: auto; }

.auth {
  min-height: 100dvh;
  display: grid; place-items: center;
  padding: max(20px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 50% -10%, rgba(255, 212, 0, 0.22), transparent 55%),
    var(--ink);
}
.auth-card {
  width: min(100%, 400px);
  background: var(--surface);
  border-radius: 16px;
  padding: 28px 24px 22px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
  display: flex; flex-direction: column; gap: 10px;
}
.auth-logo { align-self: center; height: auto; margin-top: -4px; }
.auth-card h1 { font-family: var(--display); font-size: 2.3rem; line-height: 1; text-align: center; text-transform: uppercase; }
.auth-sub { color: var(--muted); text-align: center; margin-bottom: 8px; }
.auth-foot { color: var(--muted); font-size: 0.85rem; text-align: center; margin-top: 6px; }
#loginForm { display: flex; flex-direction: column; gap: 14px; }

/* ---------- Formulários ---------- */
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 700; font-size: 0.9rem; min-width: 0; }
.field.compact { gap: 4px; }
.hint { color: var(--muted); font-weight: 500; font-size: 0.8rem; }

input, select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfbf8;
  padding: 0 12px;
  font-size: 1rem; /* 16px evita zoom no iOS */
  font-weight: 500;
}
input:focus, select:focus { border-color: var(--ink); outline: 3px solid rgba(255, 212, 0, 0.55); outline-offset: 0; }
input:disabled { background: #eee8d6; color: #a39b84; cursor: not-allowed; }

.password-wrap { position: relative; display: block; }
.password-wrap input { padding-right: 84px; }
.ghost-button {
  position: absolute; right: 4px; top: 4px; bottom: 4px;
  border: 0; border-radius: 7px; background: transparent;
  padding: 0 10px; font-weight: 800; font-size: 0.85rem; color: var(--blue);
}
.ghost-button:hover { background: var(--line-soft); }

.form-message { min-height: 1.2em; font-size: 0.9rem; font-weight: 600; color: var(--danger); }
.form-message.ok { color: var(--ok); }

.link-button {
  align-self: center; border: 0; background: none; color: var(--blue);
  font-weight: 700; padding: 8px; min-height: 40px;
}

/* ---------- Botões ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff; color: var(--ink); font-weight: 800;
  transition: background 120ms, transform 80ms;
}
.btn:hover { background: var(--line-soft); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--yellow); }
.btn.primary:hover { background: #000; }
.btn.danger { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 0.88rem; }
.btn.block { width: 100%; min-height: 52px; font-size: 1rem; }
.btn:disabled { opacity: 0.55; cursor: progress; }

/* ---------- Topo ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--ink); color: #fff;
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  width: min(100%, 1240px); margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 14px; min-height: 60px;
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.brand img { height: auto; filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.35)); }
.brand-name { display: block; font-family: var(--display); font-size: 1.7rem; line-height: 0.95; text-transform: uppercase; }
.brand-by { display: block; font-size: 0.72rem; color: rgba(255, 255, 255, 0.65); }

.sync {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 0.78rem; font-weight: 700; color: rgba(255, 255, 255, 0.8);
  padding: 6px 10px; border-radius: 999px; background: rgba(255, 255, 255, 0.1);
}
.sync i { width: 8px; height: 8px; border-radius: 50%; background: #5ad17f; }
.sync[data-state="saving"] i { background: var(--yellow); animation: pulse 1s infinite; }
.sync[data-state="offline"] i, .sync[data-state="error"] i { background: #ef6b6b; }
@keyframes pulse { 50% { opacity: 0.3; } }
@media (max-width: 520px) { .sync span { display: none; } .sync { padding: 8px; } }

.avatar {
  width: 42px; height: 42px; flex: 0 0 auto;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--yellow); color: var(--ink);
  font-weight: 900; font-size: 0.95rem;
}
.avatar.large { width: 54px; height: 54px; font-size: 1.15rem; }

/* ---------- Layout / navegação ---------- */
.layout { width: min(100%, 1240px); margin: 0 auto; padding: 14px 12px calc(var(--nav-h) + 22px + env(safe-area-inset-bottom)); }
.content { min-width: 0; display: flex; flex-direction: column; gap: 14px; }

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  display: flex;
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  padding: 4px 6px calc(4px + env(safe-area-inset-bottom));
}
.nav-item {
  position: relative;
  flex: 1; min-height: 52px; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: 0; border-radius: 10px; background: transparent;
  color: var(--muted); font-size: 0.72rem; font-weight: 800;
}
.nav-item svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-item.active { color: var(--ink); }
.nav-item.active svg { stroke: var(--ink); }
.nav-item.active::before {
  content: ""; position: absolute; top: -4px; left: 50%; width: 36px; height: 3px; border-radius: 3px; background: var(--yellow); transform: translateX(-50%);
}
body:not(.is-admin) .admin-only { display: none; }

@media (min-width: 900px) {
  .layout {
    display: grid; grid-template-columns: 210px minmax(0, 1fr); gap: 28px;
    padding: 28px 20px 40px; align-items: start;
  }
  .nav {
    position: sticky; top: 84px; left: auto; right: auto; bottom: auto;
    flex-direction: column; gap: 4px; padding: 8px;
    border: 1px solid var(--line); border-radius: 14px; background: var(--surface); backdrop-filter: none;
  }
  .nav-item { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; min-height: 46px; padding: 0 14px; font-size: 0.95rem; }
  .nav-item:hover { background: var(--line-soft); }
  .nav-item.active { background: var(--yellow); color: var(--ink); }
  .nav-item.active::before { display: none; }
  h2 { font-size: 2rem; }
}

/* ---------- Estrutura de telas ---------- */
.view { display: none; }
.view.active { display: block; }
.view-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.view-head p { color: var(--muted); font-size: 0.92rem; margin-top: 4px; }

.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--danger-soft); color: #7a2525; font-weight: 700; font-size: 0.9rem;
}
.banner.info { background: var(--yellow-soft); color: #3a3310; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); }

.tournament-bar {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; align-items: end;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px;
}
@media (min-width: 700px) { .tournament-bar { grid-template-columns: minmax(0, 1fr) minmax(220px, 320px); } }
.progress-text { font-size: 0.82rem; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.progress-track { height: 8px; border-radius: 8px; background: var(--line-soft); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--yellow); box-shadow: inset 0 0 0 1px rgba(31, 29, 29, 0.25); border-radius: 8px; transition: width 300ms ease; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  min-height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 999px;
  background: #fff; font-weight: 700; font-size: 0.88rem;
}
.chip.active { background: var(--ink); border-color: var(--ink); color: var(--yellow); }

.empty-state {
  padding: 28px 18px; border: 1px dashed var(--line); border-radius: 14px;
  color: var(--muted); text-align: center; background: #fbfbf8;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}

.card-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }

/* ---------- Partidas ---------- */
.section-title { font-family: var(--display); font-size: 1.2rem; text-transform: uppercase; margin: 6px 0 10px; }
.rounds-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); align-items: start; margin-bottom: 18px; }

.round { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.round-title {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 12px; background: var(--ink); color: var(--yellow);
  font-weight: 800; font-size: 0.92rem;
}
.round-title span { color: rgba(255, 255, 255, 0.7); font-size: 0.78rem; font-weight: 600; }

.match { padding: 10px 12px; border-top: 1px solid var(--line-soft); }
.round .match:first-of-type { border-top: 0; }
.match-meta { display: flex; justify-content: space-between; align-items: center; font-size: 0.76rem; color: var(--muted); font-weight: 700; margin-bottom: 4px; }
.status { padding: 2px 8px; border-radius: 999px; background: var(--line-soft); }
.match.is-done .status { background: #e1f1e7; color: var(--ok); }

.side {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 10px;
  padding: 5px 8px 5px 10px; border-left: 4px solid transparent; border-radius: 6px;
}
.side-text { min-width: 0; }
.side-name { display: block; font-weight: 700; overflow-wrap: anywhere; }
.side-hint { display: block; color: var(--muted); font-size: 0.74rem; }
.side-hint:empty { display: none; }
.side.is-winner { border-left-color: var(--yellow); background: var(--yellow-soft); }
.side.is-winner .side-name { font-weight: 900; }
.side.is-bye .side-name { color: var(--muted); font-style: italic; font-weight: 600; }

.score {
  min-width: 44px; text-align: center; font-weight: 900; font-size: 1.2rem; font-variant-numeric: tabular-nums;
}
.score-input { width: 60px; min-height: 46px; padding: 0 4px; text-align: center; font-weight: 900; font-size: 1.15rem; }

/* Chaveamento */
.bracket {
  display: flex; gap: 12px; overflow-x: auto; padding: 2px 2px 14px;
  scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch;
}
.bracket-col { flex: 0 0 min(86vw, 330px); scroll-snap-align: start; display: flex; flex-direction: column; gap: 10px; justify-content: space-around; }
.bracket-col .round { margin: 0; }
@media (min-width: 900px) { .bracket-col { flex-basis: 300px; } }

/* ---------- Classificação ---------- */
.standings-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 420px), 1fr)); align-items: start; }
.group-table { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.group-title {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 12px; background: var(--ink); color: var(--yellow); font-weight: 800;
}
.group-title span { color: rgba(255, 255, 255, 0.7); font-size: 0.78rem; font-weight: 600; }
.table-scroll { overflow-x: auto; }
.standings-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.standings-table th, .standings-table td { padding: 10px 8px; border-bottom: 1px solid var(--line-soft); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.standings-table tr:last-child td { border-bottom: 0; }
.standings-table th:first-child, .standings-table td:first-child { text-align: left; white-space: normal; padding-left: 12px; }
.standings-table th { color: var(--muted); font-size: 0.75rem; font-weight: 800; background: #fbfbf8; }
.standings-table td strong { font-size: 1rem; }
.standings-table tr.qualified td:first-child { box-shadow: inset 4px 0 0 var(--yellow); }
.pos {
  display: inline-grid; place-items: center; width: 26px; height: 26px; margin-right: 8px;
  border-radius: 50%; background: var(--line-soft); font-weight: 900; font-size: 0.82rem;
}
.pos.gold { background: var(--yellow); }
.pos.silver { background: #d9dadc; }
.pos.bronze { background: #e7c3a0; }
.legend { color: var(--muted); font-size: 0.8rem; margin-top: 12px; }
.qualified-list td:first-child { font-weight: 800; }
.qualified-list td:last-child { text-align: left; font-weight: 600; }

/* ---------- Criar torneio ---------- */
.setup-form { display: flex; flex-direction: column; gap: 14px; }
.form-row { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .form-row { grid-template-columns: 1fr 1fr; } }
.format-note { border: 1px solid #f1d76b; border-radius: var(--radius); background: var(--yellow-soft); color: #3a3310; padding: 11px 12px; font-size: 0.92rem; }
.participants-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.participants-header p { color: var(--muted); font-size: 0.88rem; margin-top: 2px; }
.participant-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.participant-list { display: grid; gap: 10px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .participant-list { grid-template-columns: 1fr 1fr; column-gap: 16px; } }
.participant-row { display: grid; grid-template-columns: 34px minmax(0, 1fr); align-items: center; gap: 10px; }
.participant-number { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; background: var(--line-soft); color: var(--muted); font-weight: 800; font-size: 0.85rem; }
.sticky-actions { position: sticky; bottom: calc(var(--nav-h) + 8px + env(safe-area-inset-bottom)); padding-top: 6px; }
@media (min-width: 900px) { .sticky-actions { position: static; } .sticky-actions .btn { width: auto; min-width: 260px; } }

/* ---------- Torneios salvos ---------- */
.tournament-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.tournament-card.current { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.tournament-card h3 { font-size: 1.05rem; overflow-wrap: anywhere; }
.meta { color: var(--muted); font-size: 0.84rem; }
.card-actions { display: flex; gap: 8px; margin-top: 4px; }

/* ---------- Usuários ---------- */
.user-list { display: flex; flex-direction: column; gap: 10px; }
.user-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
}
.user-row.inactive { opacity: 0.65; }
.user-main { min-width: 0; }
.user-main h3 { overflow-wrap: anywhere; }
.user-main p { color: var(--muted); font-size: 0.86rem; overflow-wrap: anywhere; }
.user-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.user-actions { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; }
@media (min-width: 900px) {
  .user-row { grid-template-columns: auto minmax(0, 1fr) auto; }
  .user-actions { grid-column: auto; justify-content: flex-end; }
}
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; background: var(--line-soft); font-size: 0.75rem; font-weight: 800; }
.badge.admin { background: var(--yellow); }
.badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.me { background: #dfeaf6; color: var(--blue); }

/* ---------- Diálogos ---------- */
.dialog { border: 0; border-radius: 16px; padding: 0; width: min(92vw, 440px); max-height: 92dvh; box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45); color: var(--ink); }
.dialog::backdrop { background: rgba(20, 18, 18, 0.62); }
.dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.dialog-body > p { color: var(--ink-soft); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.dialog-actions.stack { flex-direction: column; }
.dialog-actions.stack .btn { width: 100%; }
.account-head { display: flex; align-items: center; gap: 14px; }
.account-head p { color: var(--muted); font-size: 0.9rem; overflow-wrap: anywhere; }
.account-head .badge { margin-top: 6px; }
.install-box { background: var(--yellow-soft); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 10px; font-size: 0.9rem; }
.credentials { margin: 0; padding: 12px; background: #f4f4f1; border-radius: var(--radius); font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.9rem; white-space: pre-wrap; overflow-wrap: anywhere; user-select: all; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; z-index: 60;
  bottom: calc(var(--nav-h) + 18px + env(safe-area-inset-bottom));
  width: max-content; max-width: min(92vw, 460px); text-align: center;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  border-radius: 10px; background: #14201f; color: #fff; padding: 12px 16px; font-weight: 700;
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 900px) { .toast { bottom: 24px; } }

@media (max-width: 370px) {
  .brand-name { font-size: 1.45rem; }
  .score-input { width: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
