:root {
  --bg: #f4f6f6;
  --surface: #ffffff;
  --text: #15191c;
  --muted: #69737a;
  --border: #e0e5e7;
  --primary: #0f766e;
  --primary-text: #ffffff;
  --accent: #f59e0b;
  --danger: #dc2626;
  --success: #15803d;
  --joined-bg: #e6f4f1;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1214;
    --surface: #1a1f22;
    --text: #e9eef0;
    --muted: #97a2a8;
    --border: #2b3236;
    --primary: #14b8a6;
    --primary-text: #04201d;
    --accent: #fbbf24;
    --danger: #f87171;
    --success: #4ade80;
    --joined-bg: #12302c;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }
a { color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--primary);
  color: var(--primary-text);
}
.topbar h1 { margin: 0; font-size: 1.25rem; }
.home-link { text-decoration: none; }
.topbar .btn.ghost { color: var(--primary-text); border-color: rgba(255,255,255,.5); }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: max(32px, env(safe-area-inset-bottom));
}

h2 { margin: 0 0 6px; font-size: 1.1rem; }

.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin: 0 0 14px;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.card-head h2 { margin: 0 0 4px; overflow-wrap: anywhere; }
.block-card { text-decoration: none; }
.block-card.past { opacity: .6; }

.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); font-size: .9rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 8px 0 0; }
.center { text-align: center; }
.error { color: var(--danger); font-weight: 600; }
.warn { color: var(--danger); }
.spacer { height: 14px; }

input[type="text"], input[type="number"], input[type="date"], input[type="password"], select, textarea {
  width: 100%;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* avoid iOS zoom */
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.form label { display: block; font-weight: 600; font-size: .9rem; margin-top: 12px; }
.form label input, .form label select, .form label textarea { margin-top: 4px; font-weight: 400; }
.form label .muted { font-weight: 400; font-size: .8rem; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 600;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .85rem; }
.btn.block { display: flex; width: 100%; margin-top: 10px; }
.btn.icon { min-width: 34px; padding: 0; }
.btn.on { background: var(--joined-bg); border-color: var(--primary); color: var(--primary); }
.btn.danger-text { color: var(--danger); background: transparent; border-color: transparent; }
.actions { margin-top: 16px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; text-align: center; }
.stats div { background: var(--bg); border-radius: 8px; padding: 8px 4px; }
.stats strong { display: block; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.stats span { color: var(--muted); font-size: .8rem; }
.pay-info { margin: 12px 0 0; font-size: .9rem; overflow-wrap: anywhere; white-space: pre-line; }

.me-card .btn.block { margin-top: 10px; }

.tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.tab {
  flex: 1;
  font: inherit;
  font-weight: 600;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 99px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}
.tab.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.badge {
  display: inline-block;
  min-width: 1.4em;
  padding: 0 5px;
  border-radius: 99px;
  background: var(--danger);
  color: #fff;
  font-size: .75rem;
  line-height: 1.4em;
}

.date-list, .player-list { list-style: none; padding: 0; }
.date-list { padding: 4px 14px; }
.player-list { margin: 10px 0 0; }

.date-row, .player-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.date-row:last-child, .player-row:last-child { border-bottom: 0; }
.date-main { flex: 1; min-width: 0; }
.date-label { font-weight: 700; }
.date-actions, .org-actions { display: flex; gap: 6px; flex-shrink: 0; }
.date-row.past { opacity: .6; }
.date-row.cancelled .date-label { text-decoration: line-through; color: var(--muted); }
.date-row.cancelled .muted { color: var(--danger); }

.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chip {
  font-size: .78rem;
  padding: 2px 8px;
  border-radius: 99px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.chip.me { background: var(--joined-bg); border-color: var(--primary); font-weight: 600; }

.player-row .name { font-weight: 600; overflow-wrap: anywhere; }
.player-row .status { font-size: .85rem; font-weight: 600; text-align: right; max-width: 40%; }
.player-row.owes .status { color: var(--danger); }
.player-row.paid .status { color: var(--success); }
.player-row.credit .status { color: var(--accent); }

.toggle { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-weight: 600; font-size: .9rem; }
.toggle input { width: 20px; height: 20px; accent-color: var(--primary); }

.share .row { margin-top: 6px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 99px;
  font-weight: 600;
  z-index: 20;
  max-width: 90vw;
}

dialog {
  width: min(92vw, 380px);
  border: 0;
  border-radius: 16px;
  padding: 20px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h2 { font-size: 1.3rem; }

#me-form select { flex: 1; min-width: 0; width: auto; }
#me-form .new-name { margin-top: 8px; }
