:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-2: #334155;
  --muted: #64748b;
  --primary: #0f766e;
  --primary-dark: #0b5a53;
  --primary-soft: #e6f3f1;
  --accent: #1d4ed8;
  --win: #16a34a;
  --win-soft: #e9f7ef;
  --loss: #dc2626;
  --gold: #b45309;
  --gold-soft: #fef3c7;
  --radius: 12px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
}

body {
  padding-top: calc(58px + var(--safe-top));
  padding-bottom: calc(72px + var(--safe-bottom));
  max-width: 560px;
  margin: 0 auto;
}

/* ---------- Header ---------- */
.app-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 40;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: var(--safe-top);
}
.header-inner {
  max-width: 560px;
  margin: 0 auto;
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}
.title-block { display: flex; flex-direction: column; line-height: 1.1; }
.title-block h1 {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.3px;
  color: var(--text);
}
.title-block .subtitle {
  font-size: 10px;
  letter-spacing: 1.5px;
  color: var(--primary);
  font-weight: 700;
  margin-top: 2px;
  text-transform: uppercase;
}
.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  cursor: pointer;
  transition: all .15s ease;
}
.icon-btn:active { transform: scale(.92); color: var(--loss); border-color: var(--loss); }

.header-actions { display: flex; align-items: center; gap: 8px; }

/* Sync header button + status dot */
#syncBtn { position: relative; }
#syncBtn .sync-dot {
  position: absolute; top: 6px; right: 6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted); border: 2px solid var(--surface);
}
#syncBtn.s-off .sync-dot { background: var(--muted); }
#syncBtn.s-on { color: var(--primary); border-color: var(--primary); }
#syncBtn.s-on .sync-dot { background: var(--win); }
#syncBtn.s-connecting .sync-dot { background: var(--gold); animation: pulse 1s ease-in-out infinite; }
#syncBtn.s-error .sync-dot { background: var(--loss); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

/* ---------- Panels ---------- */
.tab-panel { display: none; padding: 16px 14px 24px; animation: fade .2s ease; }
.tab-panel.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.section-title {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 700; letter-spacing: -0.1px;
  color: var(--text);
  margin: 2px 2px 14px;
}
.section-title .pill {
  margin-left: auto;
  font-size: 10.5px; font-weight: 700; letter-spacing: .3px;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 4px 10px; border-radius: 6px;
}

/* ---------- Progress banner ---------- */
.progress-banner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.progress-banner .pb-top {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 10px;
}
.progress-banner .pb-label { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: .4px; text-transform: uppercase; }
.progress-banner .pb-count { font-size: 14px; font-weight: 700; color: var(--text); }
.progress-banner .pb-count b { color: var(--primary); }
.progress-track { height: 7px; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 999px; transition: width .4s ease; }

/* ---------- Slot groups ---------- */
.slot-group { margin-bottom: 20px; }
.slot-header {
  display: flex; align-items: center; gap: 10px;
  margin: 0 2px 10px;
}
.slot-badge {
  display: inline-grid; place-items: center;
  width: 30px; height: 30px;
  background: var(--primary);
  border-radius: 8px;
  font-weight: 800; font-size: 13px; color: #fff;
}
.slot-header .slot-text { font-size: 12.5px; font-weight: 700; color: var(--text); }
.slot-header .slot-sub { font-size: 11px; color: var(--muted); font-weight: 500; }

/* ---------- Match card ---------- */
.match-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  position: relative;
  box-shadow: var(--shadow-sm);
  transition: border-color .2s ease, transform .1s ease;
}
.match-card:active { transform: scale(.995); }
.match-card.played { border-color: var(--border-strong); }
.match-card .court-tag {
  position: absolute; top: 13px; right: 14px;
  font-size: 10px; font-weight: 700; letter-spacing: .3px;
  color: var(--muted); text-transform: uppercase;
}
.team-row {
  display: flex; align-items: center; gap: 11px;
  padding: 6px 2px;
}
.team-row .chip {
  flex-shrink: 0;
  width: 30px; height: 30px; border-radius: 8px;
  display: grid; place-items: center;
  font-weight: 700; font-size: 11px; color: #fff;
}
.team-row .tname { font-size: 14px; font-weight: 600; flex: 1; line-height: 1.2; color: var(--text-2); }
.team-row .tscore {
  font-size: 18px; font-weight: 800; min-width: 26px; text-align: center;
  color: var(--muted);
}
.team-row.winner .tname { color: var(--win); font-weight: 700; }
.team-row.winner .tscore { color: var(--win); }
.team-row.winner::after {
  content: '✓'; color: var(--win); font-weight: 800; font-size: 13px; margin-left: -4px;
}
.team-row.loser .tname, .team-row.loser .tscore { opacity: .5; }
.vs-divider {
  display: flex; align-items: center; gap: 8px;
  padding: 1px 2px; margin: 1px 0;
}
.vs-divider .line { flex: 1; height: 1px; background: var(--border); }
.vs-divider .vs { font-size: 10px; font-weight: 700; color: var(--muted); letter-spacing: .5px; }

.match-footer {
  margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.match-footer .status { font-size: 11px; font-weight: 600; letter-spacing: .2px; }
.match-footer .status.pending { color: var(--muted); }
.match-footer .status.done { color: var(--win); }
.btn-score {
  font-size: 12px; font-weight: 700; letter-spacing: .2px;
  padding: 8px 16px; border-radius: 8px;
  border: 1px solid var(--primary);
  color: #fff; background: var(--primary);
  cursor: pointer; transition: all .15s ease;
}
.btn-score:active { transform: scale(.95); background: var(--primary-dark); }
.btn-score.edit { border-color: var(--border-strong); color: var(--text-2); background: var(--surface); }
.btn-score.edit:active { background: var(--surface-2); }

/* ---------- Standings table ---------- */
.standings-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.st-row {
  display: grid;
  grid-template-columns: 30px 1fr 34px 34px 48px;
  align-items: center;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.st-row:last-child { border-bottom: none; }
.st-row.head {
  font-size: 10px; font-weight: 700; letter-spacing: .4px;
  color: var(--muted); text-transform: uppercase;
  background: var(--surface-2);
}
.st-row .rank {
  font-weight: 700; font-size: 14px; text-align: center;
  color: var(--muted);
}
.st-row.qualified .rank { color: var(--primary); }
.st-row.qualified { background: var(--primary-soft); }
.st-team { display: flex; align-items: center; gap: 8px; min-width: 0; }
.st-team .chip { width: 24px; height: 24px; border-radius: 6px; display: grid; place-items: center; font-size: 10px; font-weight: 700; color: #fff; flex-shrink: 0; }
.st-team .nm { font-weight: 600; font-size: 12.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st-row .num { text-align: center; font-weight: 600; color: var(--muted); font-size: 12.5px; }
.st-row .pts { text-align: center; font-weight: 800; color: var(--primary); font-size: 14px; }
.qualify-line {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; font-size: 10px; font-weight: 700;
  letter-spacing: .5px; color: var(--primary); text-transform: uppercase;
  background: var(--surface-2);
}
.qualify-line .line { flex: 1; height: 1px; background: var(--border-strong); }

.legend { margin-top: 14px; font-size: 11.5px; color: var(--muted); line-height: 1.7; padding: 0 4px; }
.legend b { color: var(--text-2); font-weight: 600; }

/* ---------- Knockout ---------- */
.ko-stage { margin-bottom: 20px; }
.ko-label {
  font-size: 12px; font-weight: 700; letter-spacing: .2px;
  margin: 0 2px 10px;
  display: flex; align-items: center; gap: 8px;
  color: var(--text);
}
.ko-label .tag {
  font-size: 10px; padding: 3px 9px; border-radius: 6px;
  background: var(--primary-soft); color: var(--primary);
  letter-spacing: .3px; font-weight: 700;
}
.ko-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}
.ko-card.final { border-color: var(--gold); background: var(--gold-soft); }
.ko-card.final .ko-label .tag { background: #fde68a; color: var(--gold); }
.ko-slot {
  display: flex; align-items: center; gap: 10px; padding: 8px 2px;
}
.ko-slot .seed {
  font-size: 10px; font-weight: 700; color: var(--muted);
  width: 56px; flex-shrink: 0; letter-spacing: .2px; text-transform: uppercase;
}
.ko-slot .chip { width: 28px; height: 28px; border-radius: 7px; display: grid; place-items: center; font-size: 10px; font-weight: 700; color: #fff; flex-shrink: 0; }
.ko-slot .nm { flex: 1; font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.ko-slot .sc { font-size: 17px; font-weight: 800; color: var(--muted); min-width: 24px; text-align: center; }
.ko-slot.empty .nm { color: var(--muted); font-style: italic; font-weight: 500; }
.ko-slot.winner .nm { color: var(--win); font-weight: 700; }
.ko-slot.winner .sc { color: var(--win); }
.ko-champion {
  margin-top: 16px;
  background: var(--primary);
  border-radius: var(--radius); padding: 20px; text-align: center;
  color: #fff; box-shadow: var(--shadow-md);
}
.ko-champion .ttl { font-size: 11px; font-weight: 700; letter-spacing: 1.5px; opacity: .85; }
.ko-champion .nm { font-size: 20px; font-weight: 800; margin-top: 5px; }
.ko-locked {
  text-align: center; padding: 36px 20px; color: var(--muted);
  font-size: 13px; line-height: 1.7;
}
.ko-locked b { color: var(--text-2); }
.ko-locked svg { opacity: .35; margin-bottom: 12px; }

/* ---------- Teams tab ---------- */
.team-list-card {
  display: flex; align-items: center; gap: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
}
.team-list-card .chip { width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; font-size: 15px; font-weight: 800; color: #fff; flex-shrink: 0; }
.team-list-card .info { flex: 1; }
.team-list-card .info .players { font-size: 15px; font-weight: 700; color: var(--text); }
.team-list-card .info .code { font-size: 11px; color: var(--muted); font-weight: 500; letter-spacing: .3px; margin-top: 1px; }
.team-list-card .rec { text-align: right; }
.team-list-card .rec .wl { font-size: 15px; font-weight: 800; }
.team-list-card .rec .wl .w { color: var(--win); }
.team-list-card .rec .wl .l { color: var(--loss); }
.team-list-card .rec .lbl { font-size: 9px; color: var(--muted); letter-spacing: .5px; text-transform: uppercase; margin-top: 1px; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  max-width: 560px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.nav-btn {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 10px 0 9px;
  background: none; border: none; cursor: pointer;
  color: var(--muted); transition: color .15s ease;
}
.nav-btn span { font-size: 10px; font-weight: 600; letter-spacing: .2px; }
.nav-btn.active { color: var(--primary); }
.nav-btn:active { transform: scale(.92); }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: none; }
.modal.open { display: block; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .45); animation: fade .2s ease; }
.modal-card {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-width: 560px; margin: 0 auto;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 18px 18px calc(20px + var(--safe-bottom));
  box-shadow: 0 -8px 30px rgba(15, 23, 42, .15);
  animation: slideUp .26s cubic-bezier(.16,1,.3,1);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }
.modal-card::before {
  content: ''; display: block; width: 40px; height: 4px;
  background: var(--border-strong); border-radius: 999px;
  margin: -6px auto 16px;
}
.modal-head { text-align: center; margin-bottom: 18px; }
.modal-meta { font-size: 11px; font-weight: 700; letter-spacing: .3px; color: var(--primary); text-transform: uppercase; }
.modal-head h2 { font-size: 18px; font-weight: 700; margin-top: 4px; color: var(--text); }
.score-rows { display: flex; flex-direction: column; gap: 10px; }
.score-input-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px;
  transition: border-color .15s ease, background .15s ease;
}
.score-input-row.lead { border-color: var(--win); background: var(--win-soft); }
.score-input-row .chip { width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; font-size: 12px; font-weight: 700; color: #fff; flex-shrink: 0; }
.score-input-row .nm { flex: 1; font-size: 14px; font-weight: 600; color: var(--text-2); min-width: 0; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button {
  width: 36px; height: 36px; border-radius: 9px;
  background: var(--surface); border: 1px solid var(--border-strong);
  color: var(--text); font-size: 20px; font-weight: 600;
  cursor: pointer; display: grid; place-items: center;
  transition: all .12s ease;
}
.stepper button:active { transform: scale(.9); background: var(--primary); color: #fff; border-color: var(--primary); }
.stepper input {
  width: 48px; height: 40px; text-align: center;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 9px; color: var(--text);
  font-size: 19px; font-weight: 800; font-family: inherit;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input:focus { outline: none; border-color: var(--primary); }
.score-hint { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 4px; min-height: 16px; font-weight: 500; }
.score-hint.valid { color: var(--win); font-weight: 600; }
.score-hint.error { color: var(--loss); font-weight: 600; }
.modal-actions { display: flex; gap: 10px; margin-top: 10px; }
.btn {
  flex: 1; padding: 13px; border-radius: 10px;
  font-size: 13px; font-weight: 700; letter-spacing: .2px;
  cursor: pointer; border: 1px solid transparent;
  font-family: inherit; transition: all .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-ghost { background: var(--surface); border-color: var(--border-strong); color: var(--text-2); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:disabled { opacity: .5; background: var(--border-strong); color: var(--surface); cursor: not-allowed; }
.btn-danger { background: var(--loss); color: #fff; }
.btn-danger:disabled { opacity: .5; background: var(--border-strong); color: var(--surface); cursor: not-allowed; }
#modalClear { flex: 0 0 auto; padding: 13px 16px; }

/* ---------- Reset modal ---------- */
.modal-meta.danger { color: var(--loss); }
.reset-text { text-align: center; font-size: 13px; color: var(--text-2); line-height: 1.6; margin-bottom: 16px; }
.reset-text b { color: var(--loss); font-weight: 700; letter-spacing: .5px; }
.reset-input {
  width: 100%; height: 48px; text-align: center;
  background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: var(--radius); color: var(--text);
  font-size: 17px; font-weight: 700; letter-spacing: 2px; font-family: inherit;
  text-transform: uppercase; margin-bottom: 16px;
}
.reset-input:focus { outline: none; border-color: var(--loss); background: var(--surface); }
.reset-input::placeholder { color: var(--muted); font-weight: 500; letter-spacing: 1px; }

/* ---------- Sync modal ---------- */
.sync-toggle-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; margin-bottom: 16px;
}
.sync-toggle-label { font-size: 14px; font-weight: 700; color: var(--text); }
.sync-toggle-sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.switch { position: relative; display: inline-block; width: 46px; height: 28px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; cursor: pointer;
  background: var(--border-strong); border-radius: 999px; transition: background .2s ease;
}
.switch .slider::before {
  content: ''; position: absolute; height: 22px; width: 22px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: transform .2s ease;
  box-shadow: var(--shadow-sm);
}
.switch input:checked + .slider { background: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(18px); }

.sync-body { transition: opacity .2s ease; }
.sync-field-label { display: block; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; margin: 0 2px 6px; }
.sync-input {
  width: 100%; height: 44px; padding: 0 14px;
  background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: 10px; color: var(--text);
  font-size: 14px; font-weight: 600; font-family: inherit;
}
.sync-input:focus { outline: none; border-color: var(--primary); background: var(--surface); }
.sync-input::placeholder { color: var(--muted); font-weight: 500; }
.sync-body .sync-input { margin-bottom: 14px; }
select.sync-input { -webkit-appearance: none; appearance: none; cursor: pointer; }
.sync-hint { font-size: 12px; color: var(--muted); line-height: 1.5; margin: 14px 2px 0; }

.sync-servers-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: 16px 2px 8px;
  font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .4px;
}
.sync-refresh {
  font-size: 11px; font-weight: 700; color: var(--primary);
  background: none; border: none; cursor: pointer; padding: 2px 4px; letter-spacing: .2px;
}
.sync-server-list { display: flex; flex-direction: column; gap: 8px; }
.sync-server {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px;
}
.sync-server.selected { border-color: var(--primary); background: var(--primary-soft); }
.sync-server .sv-info { flex: 1; min-width: 0; }
.sync-server .sv-name { font-size: 13px; font-weight: 700; color: var(--text); }
.sync-server .sv-url { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.sv-action {
  flex-shrink: 0; font-size: 12px; font-weight: 700;
  padding: 7px 13px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--primary); color: #fff; background: var(--primary);
}
.sv-action:active { transform: scale(.95); }
.sv-action.is-disconnect { background: var(--surface); color: var(--loss); border-color: var(--loss); }
.sync-empty { font-size: 12.5px; color: var(--muted); text-align: center; padding: 14px; }

.sync-add { display: flex; gap: 8px; margin-top: 10px; }
.sync-add .sync-input { flex: 1; }
.sync-add .btn { flex: 0 0 auto; padding: 0 16px; height: 44px; }

.sync-status {
  display: flex; align-items: center; gap: 8px;
  margin-top: 16px; padding: 11px 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; font-size: 12.5px; font-weight: 600; color: var(--text-2);
}

/* Status dots (shared) */
.dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; background: var(--muted); }
.dot.online { background: var(--win); }
.dot.offline { background: var(--loss); }
.dot.checking { background: var(--gold); animation: pulse 1s ease-in-out infinite; }
.dot.connected { background: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.dot.off { background: var(--muted); }
.dot.unknown { background: var(--border-strong); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + var(--safe-bottom));
  transform: translateX(-50%) translateY(16px);
  background: var(--text); border: 1px solid var(--text);
  color: #fff; font-size: 13px; font-weight: 600;
  padding: 11px 18px; border-radius: 10px;
  box-shadow: var(--shadow-md); z-index: 70;
  opacity: 0; pointer-events: none; transition: all .3s ease;
  max-width: 85%; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Empty ---------- */
.empty-note { text-align: center; color: var(--muted); font-size: 13px; padding: 20px; line-height: 1.6; }
.empty-note b { color: var(--text-2); }
