:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #222634;
  --border: #2c3140;
  --text: #e8eaf0;
  --muted: #9aa0b0;
  --accent: #3ddc84;
  --accent-blue: #4c9aff;
  --danger: #ff5c72;
  --gold: #ffd15c;
  --win: #3ddc84;
  --loss: #ff5c72;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.28);
  --maxw: 900px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #f0f2f8;
    --border: #e0e4ee;
    --text: #1a1d27;
    --muted: #667085;
    --shadow: 0 6px 20px rgba(20, 30, 60, 0.08);
  }
}

* { box-sizing: border-box; }
/* Ensure the [hidden] attribute always wins over component display rules
   (e.g. .field { display:flex } would otherwise keep hidden fields visible). */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}
.wrap { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 0 16px; }

/* Header */
.app-header {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.header-inner { padding-top: 12px; }
.brand { font-size: 1.35rem; margin: 4px 0 10px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; appearance: none; border: none; background: transparent;
  color: var(--muted); font-size: 0.95rem; font-weight: 600; padding: 10px 14px;
  cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

/* Layout */
main.wrap { padding-top: 20px; padding-bottom: 24px; min-height: 60vh; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.section-head h2 { margin: 0; font-size: 1.15rem; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}

/* Connection error banner */
.conn-error {
  background: color-mix(in srgb, var(--danger) 16%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
  color: var(--text); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; font-weight: 600;
}

/* Segmented control */
.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; margin-bottom: 16px; }
.seg { appearance: none; border: none; background: transparent; color: var(--muted); font-weight: 700; font-size: 0.9rem; padding: 8px 14px; border-radius: 8px; cursor: pointer; }
.seg.active { background: var(--accent); color: #08130c; }

/* Forms */
.form .row { margin-bottom: 14px; }
.row.two { display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; gap: 12px; margin-top: 14px; }
.side-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.side-title { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 700; }
.vs { font-weight: 700; color: var(--muted); text-align: center; align-self: center; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field.grow { flex: 1; }
.field input, .field select {
  font-size: 1rem; font-weight: 500; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; appearance: none;
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.form.inline { display: flex; align-items: flex-end; gap: 10px; }
.actions { display: flex; align-items: center; gap: 12px; margin-top: 14px; flex-wrap: wrap; }
.form-error { color: var(--danger); font-weight: 600; margin: 10px 0 0; }

/* Buttons */
.btn { appearance: none; font-size: 0.95rem; font-weight: 700; border-radius: 10px; padding: 10px 16px; cursor: pointer; border: 1px solid transparent; transition: transform 0.05s ease, filter 0.15s ease; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); color: #08130c; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.danger-ghost:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.btn.icon { padding: 6px 10px; font-size: 0.85rem; }

/* Leaderboard table */
.table-scroll { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
table.board { width: 100%; border-collapse: collapse; background: var(--surface); font-size: 0.95rem; min-width: 560px; }
table.board th, table.board td { padding: 12px 10px; text-align: left; border-bottom: 1px solid var(--border); }
table.board th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); position: sticky; top: 0; }
table.board td.num, table.board th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.board tbody tr:last-child td { border-bottom: none; }
table.board tbody tr:hover { background: var(--surface-2); }
.rank-col { width: 34px; text-align: center !important; color: var(--muted); }
.rating-badge { font-weight: 800; font-variant-numeric: tabular-nums; }
.name-cell { font-weight: 600; }
.rank-1 .rating-badge { color: var(--gold); }
.medal { margin-right: 4px; }

/* Form pills */
.form-pills { display: inline-flex; gap: 3px; }
.pill { width: 18px; height: 18px; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center; font-size: 0.65rem; font-weight: 800; color: #fff; }
.pill.w { background: var(--win); color: #08130c; }
.pill.l { background: var(--loss); }
.pill.none { background: var(--border); }

/* History */
.history-list { display: flex; flex-direction: column; gap: 10px; }
.match-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; display: flex; align-items: center; gap: 12px; }
.match-main { flex: 1; min-width: 0; }
.match-players { display: flex; align-items: center; gap: 8px; font-weight: 600; flex-wrap: wrap; }
.match-players .winner { color: var(--accent); }
.match-score { font-variant-numeric: tabular-nums; font-weight: 800; background: var(--surface-2); border-radius: 8px; padding: 2px 8px; }
.match-meta { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.tag { display: inline-block; font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.tag.doubles { color: var(--accent-blue); border-color: color-mix(in srgb, var(--accent-blue) 40%, transparent); }
.delta { font-size: 0.78rem; font-weight: 700; }
.delta.up { color: var(--win); }
.delta.down { color: var(--loss); }
.match-del { color: var(--muted); background: transparent; border: none; cursor: pointer; font-size: 1.1rem; padding: 4px 8px; border-radius: 8px; }
.match-del:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

/* Players list */
.player-list { list-style: none; padding: 0; margin: 16px 0 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.player-item { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.player-item .pname { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-item .prating { font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Footer */
.app-footer { padding: 20px 16px 32px; border-top: 1px solid var(--border); margin-top: 20px; }
.footer-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }

/* Empty */
.empty-note { color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: 12px; padding: 24px; text-align: center; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 10px; font-weight: 600; box-shadow: var(--shadow); z-index: 50; animation: toast-in 0.2s ease; max-width: 90vw; text-align: center; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Dialog */
.dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 0; max-width: 92vw; width: 360px; box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(0,0,0,0.5); }
.dialog-form { padding: 20px; }
.dialog-form h3 { margin: 0 0 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

@media (max-width: 560px) {
  .row.two { grid-template-columns: 1fr; }
  .vs { padding: 2px 0; }
  .brand { font-size: 1.2rem; }
}
