:root {
  --felt: #0b6b3a;
  --felt-dark: #074d29;
  --gold: #e7c34a;
  --ink: #14201a;
  --correct: #1f9d55;
  --wrong: #d23b3b;
  --card-bg: #fffdf6;
}

* { box-sizing: border-box; }

/* The off-canvas settings drawer sits past the right edge; clip horizontal
   overflow so it never widens the page or pushes the header gear off-screen. */
html, body { overflow-x: hidden; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(circle at 50% 0%, #16352a, #0a1410);
  color: #eef3ef;
  min-height: 100vh;
  padding-bottom: 3rem;
}

header {
  text-align: center;
  padding: 1.4rem 1rem 0.4rem;
  position: relative;
}
header h1 { margin: 0; font-size: 1.9rem; letter-spacing: 0.5px; }
.tag { margin: 0.2rem 0 0; color: #9fc4ad; font-size: 0.95rem; }

/* Settings gear button */
.settings-btn {
  position: absolute; top: 1rem; right: 1rem; z-index: 30;
  width: 40px; height: 40px; border-radius: 50%; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(231,195,74,0.16); border: 1px solid rgba(231,195,74,0.55);
  color: var(--gold); line-height: 1;
  transition: background 0.15s, transform 0.2s;
}
.settings-btn:hover { background: rgba(231,195,74,0.28); transform: rotate(45deg); }

/* Settings drawer (slides in from the right) */
.settings-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 40;
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
}
.settings-backdrop.open { opacity: 1; pointer-events: auto; }
.settings-drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: 300px; max-width: 85vw;
  z-index: 50; background: #11261d; border-left: 1px solid #2c5a44;
  box-shadow: -10px 0 40px rgba(0,0,0,0.5); padding: 1.2rem 1.3rem 2rem;
  overflow-y: auto; transform: translateX(100%); transition: transform 0.28s ease;
}
.settings-drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 0.6rem;
}
.drawer-head h2 { margin: 0; font-size: 1.2rem; }
.drawer-section {
  color: var(--gold); font-size: 0.8rem; text-transform: uppercase;
  letter-spacing: 1.5px; margin: 1.3rem 0 0.7rem;
}
/* Stack the controls vertically inside the drawer */
.settings-drawer .toggles,
.settings-drawer .rules-bar {
  max-width: none; margin: 0; padding: 0; gap: 0.9rem;
  flex-direction: column; align-items: flex-start; justify-content: flex-start;
}
.settings-drawer .rule-select { justify-content: space-between; width: 100%; }

/* Goal bar */
.goal { max-width: 640px; margin: 1.2rem auto 0; padding: 0 1rem; }
.goal-bar {
  height: 14px; border-radius: 8px; background: rgba(255,255,255,0.12);
  overflow: hidden;
}
.goal-fill {
  height: 100%; width: 0;
  background: linear-gradient(90deg, var(--gold), #f6e08a);
  transition: width 0.4s ease;
}
.goal-fill.achieved { background: linear-gradient(90deg, #46e08a, #b6ffd2); }
.goal-label { text-align: center; font-size: 0.85rem; color: #cfe6d8; margin-top: 0.35rem; }

/* Toggle bar */
.toggles {
  max-width: 640px; margin: 1rem auto 0; padding: 0 1rem;
  display: flex; gap: 1.4rem; justify-content: center; flex-wrap: wrap;
}
.toggle { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; user-select: none; }
.toggle input { display: none; }
.switch {
  width: 42px; height: 24px; border-radius: 14px; background: rgba(255,255,255,0.18);
  position: relative; transition: background 0.2s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; transition: transform 0.2s;
}
.toggle input:checked + .switch { background: var(--gold); }
.toggle input:checked + .switch::after { transform: translateX(18px); }
.toggle-label { font-size: 0.92rem; color: #cfe6d8; }

/* Rules bar */
.rules-bar {
  max-width: 640px; margin: 0.7rem auto 0; padding: 0 1rem;
  display: flex; gap: 1.4rem; justify-content: center; flex-wrap: wrap;
}
.rule-select {
  display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: #9fc4ad;
}
.rule-select select {
  background: #0c1c15; color: #f0e3b4; border: 1px solid #2c5a44;
  border-radius: 8px; padding: 0.35rem 0.5rem; font-size: 0.85rem; font-weight: 600;
}
.rule-warn { color: #ffb38a; }

/* Timer bar */
.timer-wrap {
  height: 8px; border-radius: 6px; background: rgba(0,0,0,0.3);
  overflow: hidden; margin-bottom: 1rem;
}
.timer-fill {
  height: 100%; width: 100%; background: linear-gradient(90deg, #46e08a, var(--gold));
  transition: width 0.1s linear;
}
.timer-fill.danger { background: linear-gradient(90deg, #ff7a45, var(--wrong)); }

/* Bet-sizing panel */
.bet-panel {
  margin-bottom: 1rem; padding: 1rem; border-radius: 12px; text-align: center;
  background: rgba(231,195,74,0.08); border: 1px solid rgba(231,195,74,0.35);
}
.bet-label { font-size: 0.95rem; color: #f0e3b4; margin-bottom: 0.5rem; }
.bet-label strong { color: var(--gold); font-size: 1.15rem; }
.bet-option {
  display: inline-flex; align-items: center; gap: 0.4rem; cursor: pointer;
  font-size: 0.82rem; color: #cfe6d8; margin-bottom: 0.7rem;
}
.bet-option .dim { color: #8fb3a0; }
.bet-buttons { display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap; }
.bet-btn {
  flex: 1 1 0; min-width: 64px; padding: 0.7rem 0.4rem; font-weight: 700; font-size: 1rem;
  border: none; border-radius: 10px; cursor: pointer; color: var(--ink);
  background: linear-gradient(180deg, #f6e08a, #e7c34a); box-shadow: 0 3px 0 #b89a2c;
}
.bet-btn:hover { transform: translateY(-1px); }
.bet-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #b89a2c; }
.bet-feedback { min-height: 1.4rem; margin-top: 0.6rem; font-size: 0.9rem; }
.bet-feedback.correct { color: #6fe6a3; }
.bet-feedback.wrong { color: #ffb38a; }

/* Counting panel */
.count-panel {
  margin-top: 1.2rem; padding: 0.9rem 1rem; border-radius: 12px;
  background: rgba(0,0,0,0.22); border: 1px solid rgba(255,255,255,0.08);
}
.count-row { display: flex; align-items: center; gap: 1.2rem; justify-content: center; flex-wrap: wrap; }
.count-stat { text-align: center; }
.count-stat .lbl { display: block; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 1px; color: #9fc4ad; }
.count-stat b { font-size: 1.3rem; color: var(--gold); }
.count-quiz { display: flex; align-items: center; gap: 0.6rem; justify-content: center; margin-top: 0.8rem; flex-wrap: wrap; }
.count-quiz label { font-size: 0.95rem; color: #cfe6d8; }
.count-quiz input {
  width: 80px; padding: 0.5rem; border-radius: 8px; border: 1px solid #2c5a44;
  background: #0c1c15; color: #fff; font-size: 1.1rem; text-align: center; font-weight: 700;
}
.action-btn.small { flex: 0 0 auto; min-width: 0; padding: 0.55rem 1rem; font-size: 0.9rem; }
.count-feedback { text-align: center; min-height: 1.4rem; margin-top: 0.6rem; font-size: 0.9rem; }
.count-feedback.correct { color: #6fe6a3; }
.count-feedback.wrong { color: #ff9a9a; }

/* Table */
.table-felt {
  max-width: 640px;
  margin: 1.2rem auto;
  background: linear-gradient(180deg, var(--felt), var(--felt-dark));
  border: 6px solid #3c2a16;
  border-radius: 20px;
  padding: 1.4rem 1.2rem 1.6rem;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5), inset 0 0 60px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
}

/* Sidebar settings button only shows on desktop (mobile uses the header gear) */
.side-settings { display: none; }

/* Round over: the action buttons are disabled, so swap in Next hand
   (keeps it reachable and stops the outcome box pushing it off-screen). */
body.round-over .actions { display: none; }

.hand-area { text-align: center; }
.hand-label {
  text-transform: uppercase; letter-spacing: 2px; font-size: 0.72rem;
  color: var(--gold); margin-bottom: 0.5rem;
}
.cards { display: flex; gap: 0.5rem; justify-content: center; min-height: 96px; }
.total { margin-top: 0.45rem; font-size: 1.05rem; font-weight: 600; min-height: 1.3rem; }

/* Player hand(s) — supports multiple hands after a split */
#player-cards { flex-wrap: wrap; align-items: flex-start; gap: 1rem; }
.phand {
  padding: 0.5rem 0.6rem; border-radius: 12px; border: 2px solid transparent;
  background: rgba(0,0,0,0.12); transition: border-color 0.2s, box-shadow 0.2s;
}
.phand.active {
  border-color: var(--gold); box-shadow: 0 0 16px rgba(231,195,74,0.4);
}
.phand .cards { min-height: 0; }
.phand-total { margin-top: 0.4rem; font-size: 0.92rem; font-weight: 600; }
.phand-total .dbl { color: var(--gold); font-weight: 700; }
.result {
  margin-top: 0.4rem; font-weight: 800; font-size: 0.8rem; letter-spacing: 0.5px;
  padding: 2px 8px; border-radius: 6px; display: inline-block;
}
.result.win, .result.blackjack { background: rgba(31,157,85,0.3); color: #8ff0bb; }
.result.lose { background: rgba(210,59,59,0.3); color: #ffb0b0; }
.result.push { background: rgba(255,255,255,0.12); color: #e6e6e6; }
.muted { color: #9fc4ad; font-size: 0.85rem; }

.prompt { text-align: center; color: #bfe0cd; margin: 0.9rem 0; font-style: italic; }

/* Cards — the centerpiece. Fluidly sized: big on desktop, scales with the
   viewport on phones. aspect-ratio keeps them card-shaped at any size. */
.card {
  width: min(clamp(80px, 24vw, 124px), 16vh);
  aspect-ratio: 5 / 7;
  background: var(--card-bg); color: var(--ink);
  border-radius: 11px; position: relative; box-shadow: 0 6px 14px rgba(0,0,0,0.4);
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 0.4em 0.5em; font-weight: 700;
}
.card.red { color: #c4262e; }
.card .rank { font-size: clamp(1.4rem, 6vw, 2.2rem); line-height: 1; }
.card .suit { font-size: clamp(1.7rem, 7vw, 2.6rem); line-height: 1; align-self: flex-end; }
.card.back {
  background: repeating-linear-gradient(45deg, #7a1f1f, #7a1f1f 7px, #a13030 7px, #a13030 14px);
  border: 2px solid #fffdf6;
}

/* Feedback */
.feedback {
  margin: 1rem auto 0.4rem; min-height: 3.4rem; padding: 0.7rem 0.9rem;
  border-radius: 10px; font-size: 0.95rem; line-height: 1.4;
  background: rgba(255,255,255,0.06); text-align: center;
}
.feedback.correct { background: rgba(31,157,85,0.22); border: 1px solid var(--correct); }
.feedback.wrong { background: rgba(210,59,59,0.2); border: 1px solid var(--wrong); }

/* Never let the flex column squash these below their text height */
.feedback, .outcome { flex-shrink: 0; }

/* Round outcome — separate from the per-decision verdict so both stay visible */
.outcome {
  margin: 0.6rem auto 0; padding: 0.6rem 0.9rem; border-radius: 10px;
  text-align: center; font-size: 0.95rem; line-height: 1.4;
  background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.15);
}
.outcome.won { background: rgba(31,157,85,0.18); border-color: var(--correct); }
.outcome.lost { background: rgba(210,59,59,0.16); border-color: var(--wrong); }
.outcome.tie { background: rgba(255,255,255,0.08); }

/* Actions */
.actions { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; margin-top: 0.9rem; }
.action-btn {
  flex: 1 1 0; min-width: 110px; padding: 0.85rem 0.5rem; font-size: 1rem; font-weight: 700;
  border: none; border-radius: 10px; cursor: pointer; color: var(--ink);
  background: linear-gradient(180deg, #fff, #e2e8e3);
  transition: transform 0.08s, box-shadow 0.15s;
  box-shadow: 0 4px 0 #9aa79f;
}
.action-btn:hover:not(:disabled) { transform: translateY(-1px); }
.action-btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 2px 0 #9aa79f; }
.action-btn:disabled { opacity: 0.45; cursor: default; }
kbd {
  font-size: 0.7rem; background: rgba(0,0,0,0.15); border-radius: 4px;
  padding: 1px 5px; margin-left: 4px; vertical-align: middle;
}
.next-btn {
  display: block; margin: 1rem auto 0; padding: 0.7rem 1.6rem; font-weight: 700;
  border: 2px solid var(--gold); background: transparent; color: var(--gold);
  border-radius: 10px; cursor: pointer; font-size: 1rem;
}
.next-btn:hover { background: rgba(231,195,74,0.12); }

/* Stats */
.stats { max-width: 640px; margin: 1.2rem auto 0; display: flex; gap: 0.8rem; padding: 0 1rem; }
.stat {
  flex: 1; background: rgba(255,255,255,0.05); border-radius: 12px; padding: 0.8rem; text-align: center;
}
.stat .num { font-size: 1.7rem; font-weight: 800; color: var(--gold); }
.stat .num.pos { color: #6fe6a3; }
.stat .num.neg { color: #ff9a9a; }
.stat .lbl { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 1px; color: #9fc4ad; }

/* Breakdown */
.breakdown { max-width: 640px; margin: 1.2rem auto 0; padding: 0 1rem; }
.breakdown h2 { font-size: 1rem; margin: 0 0 0.6rem; }
.breakdown .hint { font-weight: 400; color: #8fb3a0; font-size: 0.8rem; }
.cats { display: flex; gap: 0.8rem; }
.cat {
  flex: 1; background: rgba(255,255,255,0.05); border-radius: 10px; padding: 0.7rem;
  display: flex; flex-direction: column; gap: 0.3rem; text-align: center;
}
.cat span { font-size: 0.78rem; color: #9fc4ad; }
.cat b { font-size: 1.1rem; color: var(--gold); }

footer { max-width: 640px; margin: 1.4rem auto 0; text-align: center; display: flex; gap: 1rem; justify-content: center; }
.link-btn {
  background: none; border: none; color: #9fc4ad; cursor: pointer; font-size: 0.9rem; text-decoration: underline;
}
.link-btn:hover { color: #fff; }
.rules-note { max-width: 560px; margin: 1rem auto 0; text-align: center; color: #6f8c7c; font-size: 0.78rem; line-height: 1.4; }

/* Modal + chart */
.modal {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  z-index: 50; overflow-y: auto; padding: 2rem 1rem;
}
.modal.open { display: block; }
.modal-inner {
  max-width: 720px; margin: 0 auto; background: #11261d; border: 1px solid #2c5a44;
  border-radius: 14px; padding: 1.6rem; position: relative;
}
.modal-close {
  position: absolute; top: 0.8rem; right: 0.8rem; background: none; border: none;
  color: #9fc4ad; font-size: 1.3rem; cursor: pointer;
}
table.chart { border-collapse: collapse; width: 100%; margin: 0.4rem 0 1.2rem; font-size: 0.82rem; }
table.chart th, table.chart td {
  border: 1px solid #2c5a44; text-align: center; padding: 5px 2px; font-weight: 700;
}
table.chart thead th, table.chart tbody th { background: #0c1c15; color: var(--gold); }
.a-h { background: #b8d6c4; color: #14201a; } /* hit */
.a-s { background: #e1b3b3; color: #14201a; } /* stand */
.a-d { background: #f0d98a; color: #14201a; } /* double */
.a-p { background: #a9c4ea; color: #14201a; } /* split */
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: 0.85rem; align-items: center; }
.legend span { padding: 2px 8px; border-radius: 4px; margin-right: 3px; font-weight: 700; }

/* Clickable matrix */
.chart-hint { color: #9fc4ad; font-size: 0.85rem; margin: 0 0 0.6rem; }
table.chart td.clickable { cursor: pointer; }
table.chart td.clickable:hover { outline: 2px solid var(--gold); outline-offset: -2px; }
table.chart td.selected { outline: 3px solid #fff; outline-offset: -3px; }
.chart-detail {
  margin-top: 1rem; padding: 0.9rem 1rem; border-radius: 10px; line-height: 1.5;
  background: rgba(231,195,74,0.1); border: 1px solid rgba(231,195,74,0.3);
  font-size: 0.92rem; min-height: 3rem;
}

/* Rulebook */
.modal-inner.rulebook { max-width: 640px; }
.rulebook h3 { color: var(--gold); margin: 1.3rem 0 0.4rem; font-size: 1.05rem; }
.rulebook p, .rulebook li { line-height: 1.55; color: #dce8e1; font-size: 0.95rem; }
.rulebook ul { margin: 0.3rem 0; padding-left: 1.3rem; }
.rulebook li { margin: 0.25rem 0; }
.rulebook .rule-foot { margin-top: 1.4rem; color: #9fc4ad; font-style: italic; }

@media (max-width: 560px) {
  body { padding-bottom: 2rem; }
  /* Compact header (gear is absolutely positioned, adds no height) */
  header { padding: 0.6rem 1rem 0.15rem; }
  header h1 { font-size: 1.3rem; }
  .tag { font-size: 0.78rem; margin-top: 0.1rem; }
  .settings-btn { top: 0.55rem; right: 0.6rem; width: 36px; height: 36px; }

  /* The whole gameplay loop lives in the felt and fills the first screen:
     dealer at the top, your cards in the middle, buttons pinned to the bottom.
     dvh tracks the real (toolbar-aware) viewport height. */
  .table-felt {
    margin: 0.4rem 0.5rem 0;
    padding: 0.7rem 0.6rem 0.9rem;
    border-width: 4px; border-radius: 14px;
  }

  /* Cards bounded by BOTH width and height so two rows always fit unscrolled */
  .cards { gap: 0.4rem; min-height: 0; }
  #player-cards { gap: 0.6rem; }
  .card { width: min(24vw, 13.5vh); border-radius: 9px; }
  .card .rank { font-size: min(6vw, 3.4vh); }
  .card .suit { font-size: min(7vw, 3.9vh); }

  /* Trim the chrome between the cards */
  .hand-label { margin-bottom: 0.2rem; font-size: 0.62rem; }
  .total { margin-top: 0.25rem; font-size: 0.92rem; min-height: 0; }
  .prompt { margin: 0.4rem 0; font-size: 0.88rem; }
  .feedback {
    min-height: 2.4rem; margin: 0.45rem auto 0;
    padding: 0.45rem 0.65rem; font-size: 0.84rem;
  }
  .outcome { font-size: 0.84rem; padding: 0.45rem 0.65rem; }

  /* Buttons pinned to the bottom of the felt = bottom of the first screen */
  .actions { gap: 0.5rem; margin-top: 0.7rem; }
  .action-btn { min-width: 44%; padding: 0.85rem 0.5rem; font-size: 1.05rem; }
  .next-btn { padding: 0.7rem 1.3rem; font-size: 1.05rem; margin-top: 0.5rem; }
  kbd { display: none; } /* keyboard hints aren't useful on touch */

  /* On mobile, make the round-over Next hand button full-width and chunky. */
  body.round-over .next-btn { width: 100%; padding: 1rem; font-size: 1.15rem; }

  /* Below-the-fold: stats wrap to a tidy grid */
  .stats { flex-wrap: wrap; gap: 0.5rem; }
  .stats .stat { flex: 1 1 28%; }
  .stat .num { font-size: 1.4rem; }
  .cats { flex-wrap: wrap; }
}

/* Desktop: game felt + right sidebar (stats, progress, settings), sized to
   one screen so nothing scrolls. Settings live next to the action, not at
   the far viewport edge. */
@media (min-width: 900px) {
  .settings-btn { display: none; } /* header gear hidden; use the sidebar button */
  header { padding: 1rem 1rem 0.3rem; }
  header h1 { font-size: 1.7rem; }

  .layout {
    display: flex; gap: 1.4rem; align-items: flex-start;
    max-width: 1040px; margin: 0.4rem auto 0; padding: 0 1.2rem;
  }
  .table-felt {
    flex: 1 1 auto; min-width: 0; margin: 0;
    padding: 1.1rem 1.1rem 1.3rem;
  }
  .card { width: min(124px, 15vh); }
  .prompt { margin: 0.5rem 0; }
  .feedback { min-height: 2.6rem; }
  .hand-label { margin-bottom: 0.3rem; }
  .total { margin-top: 0.3rem; }

  .sidebar {
    flex: 0 0 290px; display: flex; flex-direction: column; gap: 0.9rem;
    overflow-y: auto; min-height: 0; padding-top: 0.2rem;
  }
  .side-settings {
    display: block; width: 100%; cursor: pointer;
    background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.18);
    color: var(--gold); padding: 0.65rem; border-radius: 10px;
    font-size: 0.95rem; font-weight: 700; transition: background 0.15s;
  }
  .side-settings:hover { background: rgba(255,255,255,0.16); }

  /* Sidebar sections: drop centered max-width/margins, left-align */
  .sidebar .stats, .sidebar .goal, .sidebar .breakdown { max-width: none; margin: 0; padding: 0; }
  .sidebar .stats { flex-wrap: wrap; gap: 0.5rem; }
  .sidebar .stats .stat { flex: 1 1 44%; }
  .sidebar .breakdown h2 { font-size: 0.95rem; margin: 0 0 0.5rem; }
  .sidebar footer { margin: 0; flex-wrap: wrap; gap: 0.7rem; justify-content: flex-start; }
  .sidebar .rules-note { max-width: none; margin: 0; text-align: left; }
}
