:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1a2230;
  --muted: #5c6b82;
  --accent: #2d70b3;
  --accent-dark: #1f5490;
  --good: #2e9e5b;
  --line: #e2e8f2;
  --shadow: 0 6px 24px rgba(24, 44, 80, 0.08);
}

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

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 1.25rem; margin: 0; letter-spacing: -0.02em; }
.logo {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #4a97e0);
  color: #fff;
  font-size: 1.4rem;
  font-style: italic;
  font-weight: 700;
}
.scoreboard { display: flex; gap: 28px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; }
.stat-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.stat-value { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---- Game layout ---- */
.game-area {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 18px;
  padding: 18px;
  min-height: 0;
}
.calc-pane {
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-height: 480px;
}
.calculator { width: 100%; height: 100%; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

/* ---- Cards ---- */
.card {
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 18px;
}
.how-to h2 { margin: 0 0 8px; font-size: 1rem; }
.how-to p { margin: 0 0 12px; color: var(--muted); font-size: 0.9rem; }
.how-to ol { margin: 0; padding-left: 18px; color: var(--ink); font-size: 0.88rem; line-height: 1.5; }
.how-to li { margin-bottom: 6px; }
.how-to code {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.82em;
  background: #eef3fb;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
}
.swatch {
  display: inline-block;
  width: 12px; height: 12px;
  background: var(--accent);
  border-radius: 3px;
  vertical-align: middle;
}
.swatch.green { background: #388c46; }

/* ---- Buttons ---- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease;
  width: 100%;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-primary:disabled { opacity: 0.5; cursor: default; }
.btn-secondary { background: #eef3fb; color: var(--accent-dark); border: 1px solid var(--line); }
.btn-secondary:hover { background: #e2ebf7; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--ink); background: #f0f3f9; }
.btn-lg { padding: 14px 18px; font-size: 1.02rem; }

/* ---- Menu ---- */
.brand { cursor: pointer; }
.menu-card { width: min(440px, 100%); }
.tagline { margin: 0 0 20px; color: var(--muted); font-size: 0.95rem; line-height: 1.5; }
.field-label {
  display: block;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 6px;
}
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: #eef3fb;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 8px;
}
.seg {
  border: none;
  background: transparent;
  border-radius: 7px;
  padding: 9px 0;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.seg.active { background: var(--panel); color: var(--accent-dark); box-shadow: 0 1px 3px rgba(24, 44, 80, 0.12); }
.diff-hint { margin: 0 0 18px; font-size: 0.82rem; color: var(--muted); min-height: 1.1em; }
.text-input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 0.95rem;
  margin-bottom: 14px;
  font-family: inherit;
}
.text-input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.code-input { text-transform: uppercase; letter-spacing: 0.2em; font-weight: 700; text-align: center; }
.menu-divider {
  display: flex; align-items: center; text-align: center;
  color: var(--muted); font-size: 0.78rem; margin: 20px 0 16px;
}
.menu-divider::before, .menu-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--line);
}
.menu-divider span { padding: 0 12px; }
.menu-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.join-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; margin-top: 12px; }
.join-row .text-input { margin-bottom: 0; }
.join-row .btn { width: auto; padding-left: 22px; padding-right: 22px; }
.menu-error { margin: 12px 0 0; color: #c0392b; font-size: 0.85rem; }

/* ---- Lobby ---- */
.lobby-card { width: min(460px, 100%); }
.lobby-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 4px; }
.room-code {
  font-size: 2rem; font-weight: 800; letter-spacing: 0.18em;
  color: var(--accent); background: none; border: none; cursor: pointer;
  font-family: "SF Mono", Menlo, Consolas, monospace; padding: 0;
}
.room-code::after { content: " ⧉"; font-size: 1rem; color: var(--muted); }
.room-code.copied::after { content: " copied"; font-size: 0.8rem; color: var(--good); }
.lobby-diff {
  background: #eef3fb; color: var(--accent-dark); font-weight: 700;
  font-size: 0.8rem; padding: 5px 10px; border-radius: 20px;
}
.lobby-diff-seg { margin-top: 12px; }
.lobby-rounds { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.lobby-rounds .field-label { margin: 0 auto 0 0; }
.lobby-rounds .step { padding: 4px 12px; min-width: 0; width: auto; flex: none; }
.rounds-val { font-weight: 800; min-width: 2ch; text-align: center; }
.lobby-share { color: var(--muted); font-size: 0.85rem; margin: 10px 0 18px; }
.lobby-wait { text-align: center; color: var(--muted); font-size: 0.9rem; margin: 4px 0 14px; }
.lobby-card .btn-lg { margin-bottom: 10px; }

/* ---- Player lists ---- */
.player-list { list-style: none; margin: 0 0 18px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.player-list li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px 12px; background: #f7f9fd; border: 1px solid var(--line); border-radius: 8px;
  font-size: 0.9rem;
}
.p-name { font-weight: 600; }
.you { font-size: 0.7rem; color: var(--accent); font-weight: 700; }
.host-tag { font-size: 0.65rem; color: #fff; background: var(--muted); padding: 1px 6px; border-radius: 10px; vertical-align: middle; }
.p-total { font-weight: 800; font-variant-numeric: tabular-nums; }
.p-round { font-size: 0.72rem; color: var(--muted); margin-left: 8px; }

/* ---- In-game room panel ---- */
.room-panel { padding: 14px; }
.room-panel-head { font-size: 0.85rem; color: var(--muted); margin-bottom: 10px; }
.room-panel-head strong { color: var(--ink); letter-spacing: 0.1em; }
.player-list.live { margin-bottom: 0; }
.standings { text-align: left; margin-bottom: 18px; }

/* ---- Result ---- */
.result { display: flex; flex-direction: column; gap: 10px; }
.result-score { font-size: 1.35rem; font-weight: 800; color: var(--good); }
.result-detail { font-size: 0.88rem; color: var(--muted); }
.result-reveal {
  font-size: 0.95rem;
  padding: 12px;
  background: #f0f5fc;
  border-radius: 10px;
  border: 1px solid var(--line);
  overflow-x: auto;
}
.reveal-formula { font-weight: 600; }
.reveal-plain { margin-top: 6px; font-family: "SF Mono", Menlo, Consolas, monospace; color: var(--muted); font-size: 0.85rem; }

/* ---- Centered screens (menu / lobby / end) ---- */
.center-screen {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}
.end-card { width: min(560px, 100%); text-align: center; }
.end-card h2 { margin: 0 0 8px; font-size: 1.5rem; }
.end-total {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  margin-bottom: 18px;
}
.breakdown { display: flex; flex-direction: column; gap: 6px; margin-bottom: 20px; text-align: left; }
.breakdown-row {
  display: grid;
  grid-template-columns: 64px 1fr auto auto auto;
  gap: 10px;
  align-items: center;
  padding: 9px 12px;
  background: #f7f9fd;
  border-radius: 8px;
  border: 1px solid var(--line);
}
.bd-round { font-weight: 700; color: var(--muted); font-size: 0.85rem; }
.bd-formula {
  font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 0.82rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bd-time { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bd-score { font-weight: 800; font-variant-numeric: tabular-nums; }
.bd-share {
  border: 1px solid var(--line); background: var(--panel); color: var(--accent-dark);
  border-radius: 7px; padding: 5px 10px; font-size: 0.75rem; font-weight: 700; cursor: pointer;
}
.bd-share:hover { background: #eef3fb; }
.bd-share.copied { background: var(--good); color: #fff; border-color: var(--good); }

/* ---- Share plaintext box ---- */
.share-box { text-align: left; margin: -6px 0 18px; }
.share-text {
  width: 100%;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.5;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f7f9fd;
  color: var(--ink);
  resize: vertical;
}
.share-text:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

/* ---- Challenge banner ---- */
.challenge-banner {
  background: linear-gradient(135deg, #eaf2fd, #f3ecfb);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--accent-dark);
  box-shadow: var(--shadow);
}

/* ---- Responsive ---- */
@media (max-width: 860px) {
  .game-area { grid-template-columns: 1fr; }
  .calc-pane { min-height: 420px; }
}
@media (max-width: 520px) {
  .breakdown-row { grid-template-columns: 52px 1fr auto; row-gap: 4px; }
  .breakdown-row .bd-time { grid-column: 2; }
  .breakdown-row .bd-share { grid-column: 3; }
}
