:root {
  --bg: #f5efe0;
  --bg-2: #eee6d1;
  --surface: #fbf6e8;
  --surface-2: #efe7d0;
  --border: #d9cdad;
  --border-soft: #e6ddc4;
  --ink: #1e1a14;
  --muted: #6b6354;
  --accent: #8a1a1a;
  --accent-soft: #a93838;

  --mark-have: #2c7a3a;
  --mark-not: #a11c1c;
  --mark-maybe: #8a6410;
  --envelope: #caa44a;

  --tap: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --font-ui: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-title: "Georgia", "Times New Roman", serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; overscroll-behavior: none; }
body {
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--bg);
  font-size: 16px;
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
textarea, input { user-select: text; -webkit-user-select: text; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  align-items: center;
  padding: calc(var(--safe-top) + 4px) 8px 4px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 0 var(--accent);
}
.title-wrap { text-align: center; line-height: 1.1; }
.title {
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 18px;
  color: var(--ink);
  letter-spacing: 0.01em;
}
.subtitle { display: none; }
.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--ink);
  width: var(--tap); height: var(--tap);
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:active { background: rgba(0,0,0,.06); }

/* ---------- Tabs ---------- */
.tabs {
  position: sticky;
  top: calc(var(--safe-top) + 50px);
  z-index: 20;
  display: flex;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 calc(var(--safe-right)) 0 calc(var(--safe-left));
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: var(--font-ui);
  font-weight: 500;
  font-size: 14px;
  padding: 10px 14px;
  min-height: var(--tap);
  white-space: nowrap;
  position: relative;
  cursor: pointer;
  flex: 1;
}
.tab[aria-selected="true"] { color: var(--ink); font-weight: 600; }
.tab[aria-selected="true"]::after {
  content: "";
  position: absolute;
  left: 12px; right: 12px; bottom: 0;
  height: 2px;
  background: var(--accent);
}

/* ---------- Paper main ---------- */
.paper {
  min-height: calc(100vh - 160px);
  padding: 12px calc(12px + var(--safe-right)) calc(100px + var(--safe-bottom)) calc(12px + var(--safe-left));
  background: var(--bg);
}

.panel { display: none; }
.panel.active { display: block; }

.panel-head h2 {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 2px 0 2px 0;
}
.panel-head .hint {
  margin: 0 0 10px 0;
  color: var(--muted);
  font-size: 13px;
}

/* ---------- Card grid ---------- */
.player-header-row {
  display: grid;
  align-items: center;
  padding: 6px 4px;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px 6px 0 0;
  border-bottom: 0;
  position: sticky;
  top: calc(var(--safe-top) + 50px + 44px);
  z-index: 4;
}
.player-header-row .card-name-cell { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; padding-left: 4px; }
.player-header-row .p-init {
  text-align: center;
  font-weight: 600;
  font-size: 12px;
  color: var(--ink);
  padding: 4px 0;
  min-height: 32px;
  display: flex; align-items: center; justify-content: center;
}

.card-row {
  display: grid;
  align-items: center;
  gap: 4px;
  min-height: var(--tap);
  padding: 4px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border-soft);
}
.card-row:last-of-type { border-bottom: 1px solid var(--border); border-radius: 0 0 6px 6px; }
.card-row.envelope {
  background: #fbf2d9;
}
.card-row.envelope .card-name { font-weight: 600; }
.card-row.accounted .card-name { color: var(--muted); text-decoration: line-through; text-decoration-color: rgba(0,0,0,0.25); }

.card-name-cell { display: flex; align-items: center; gap: 6px; overflow: hidden; padding-left: 4px; }
.card-swatch {
  width: 10px; height: 10px; border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.25);
}
.card-name {
  font-size: 15px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-envelope-badge {
  font-size: 10px;
  background: var(--envelope);
  color: #000;
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.note-btn {
  appearance: none; border: 0; background: transparent;
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
}
.note-btn.has-note { color: var(--accent); }
.note-btn svg { width: 16px; height: 16px; }

.mark-cell {
  min-height: var(--tap);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 20px;
  line-height: 1;
  color: var(--ink);
  border-radius: 4px;
}
.mark-cell:active { background: rgba(0,0,0,0.06); }
.mark-cell.have { color: var(--mark-have); }
.mark-cell.not { color: var(--mark-not); }
.mark-cell.maybe { color: var(--mark-maybe); }
.mark-cell.empty::after {
  content: "";
  width: 16px; height: 1px;
  background: rgba(0,0,0,0.15);
}

/* ---------- Buttons / chrome ---------- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 6px;
  padding: 8px 14px;
  min-height: var(--tap);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.05s, filter 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger  { background: var(--accent); color: #fff; }
.btn-ghost   { background: transparent; color: var(--ink); border: 1px solid var(--border); }
.btn:disabled { opacity: 0.5; pointer-events: none; }

/* ---------- Bottom bar ---------- */
.bottombar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 8px calc(10px + var(--safe-right)) calc(8px + var(--safe-bottom)) calc(10px + var(--safe-left));
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  z-index: 25;
}
.legend {
  display: flex; gap: 10px;
  font-size: 12px;
  color: var(--muted);
}
.lg { display: inline-flex; align-items: center; gap: 4px; }
.chip {
  width: 20px; height: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  font-weight: 700;
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.chip-have { color: var(--mark-have); }
.chip-not  { color: var(--mark-not); }
.chip-maybe{ color: var(--mark-maybe); }

/* ---------- Players ---------- */
.players-list { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 10px 0; }
.player-pill {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: var(--tap);
  cursor: pointer;
}
.player-pill .pname { font-weight: 500; flex: 1; }
.player-pill .pinit {
  font-weight: 600;
  padding: 3px 8px;
  background: var(--bg-2);
  color: var(--ink);
  border-radius: 4px;
  font-size: 11px;
  letter-spacing: 0.04em;
}
.player-pill.me .pinit {
  background: var(--accent);
  color: #fff;
}

.divider {
  margin: 16px 0 6px 0;
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-transform: uppercase;
  display: flex; align-items: center; gap: 8px;
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.player-notes-list { display: flex; flex-direction: column; gap: 8px; }
.player-note-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}
.player-note-card .head {
  display: flex; align-items: center; gap: 8px;
  font-weight: 600;
  margin-bottom: 6px;
  font-size: 14px;
}
.player-note-card textarea {
  width: 100%;
  min-height: 60px;
  border: 0;
  background: transparent;
  font-family: var(--font-ui);
  font-size: 15px;
  color: var(--ink);
  resize: vertical;
}
.player-note-card textarea:focus { outline: 0; }

/* ---------- Notepad ---------- */
.notepad {
  width: 100%;
  min-height: 45vh;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 10px;
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  outline: 0;
  resize: none;
}
.notepad::placeholder { color: rgba(0,0,0,0.35); }

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 10px;
}
.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  box-shadow: 0 8px 30px rgba(0,0,0,.25);
}
.modal-card h3 {
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 10px 0;
}
.modal-card p { margin: 4px 0 12px 0; color: var(--muted); font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; flex-wrap: wrap; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0; font-size: 14px; }
.stack { display: flex; flex-direction: column; gap: 4px; margin: 6px 0; font-size: 13px; color: var(--muted); }
.stack input, .stack textarea {
  font-family: var(--font-ui);
  font-size: 16px;
  padding: 10px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--ink);
  border-radius: 6px;
  width: 100%;
}
.stack input:focus, .stack textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.card-players { display: grid; grid-template-columns: 1fr; gap: 2px; margin: 10px 0; }
.card-player-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto auto;
  align-items: center;
  gap: 6px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border-soft);
}
.card-player-row:last-child { border-bottom: 0; }
.card-player-row .cp-name { font-weight: 500; font-size: 14px; }
.card-player-row .cp-mark {
  width: 36px; height: 36px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--muted);
}
.card-player-row .cp-mark.active { background: var(--bg-2); color: var(--ink); }
.card-player-row .cp-mark.have.active { color: #fff; background: var(--mark-have); border-color: var(--mark-have); }
.card-player-row .cp-mark.not.active { color: #fff; background: var(--mark-not); border-color: var(--mark-not); }
.card-player-row .cp-mark.maybe.active { color: #fff; background: var(--mark-maybe); border-color: var(--mark-maybe); }

/* ---------- Curtain (hide/show) ---------- */
.curtain {
  position: fixed;
  inset: 0;
  background: #1a1612;
  z-index: 200;
  display: flex; align-items: center; justify-content: center;
  color: #a79874;
  cursor: pointer;
  font-family: var(--font-ui);
}
.curtain-inner { text-align: center; }
.curtain-seal { display: none; }
.curtain-sub {
  font-size: 14px;
  color: #a79874;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 13px;
  z-index: 150;
  max-width: 80vw;
  text-align: center;
}

@media (min-width: 540px) {
  .title { font-size: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
