:root {
  --felt: #0f5132; --felt-dark: #0b3d26; --ink: #1b1b1b; --red: #c62828;
  --gold: #f5c542; --paper: #fdfcf7; --you: #4aa3ff; --cpu: #ff9f5a;
  --cw: 46px; --ch: 66px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--felt-dark); color: #fff; font-family: -apple-system, system-ui, Roboto, sans-serif; }
body { min-height: 100dvh; touch-action: manipulation; }
#app { max-width: 640px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; background: var(--felt); padding-top: env(safe-area-inset-top); }

.scorebar { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: var(--felt-dark); font-size: 14px; gap: 8px; }
.scorebar .side { display: flex; flex-direction: column; line-height: 1.15; }
.scorebar .side b { font-size: 22px; }
.scorebar .you b { color: var(--you); } .scorebar .cpu b { color: var(--cpu); text-align: right; }
.scorebar .cpu { align-items: flex-end; }
.scorebar .mid { text-align: center; font-size: 12px; opacity: .8; }
.hand-pts { font-size: 11px; opacity: .8; }

.status { padding: 6px 12px; min-height: 40px; font-size: 14px; background: rgba(0,0,0,.18); display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.status.err { background: #7a1f1f; }

.section { padding: 8px 12px 4px; }
.label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .75; margin-bottom: 4px; display: flex; justify-content: space-between; }

.cpu-hand { display: flex; padding-left: 20px; height: 44px; }
.cpu-hand .card { width: 30px; height: 44px; margin-left: -16px; }

.table { flex: 1; min-height: 120px; }
.melds { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-start; }
.meld { display: flex; flex-direction: column; min-width: 68px; padding: 4px 6px; border-radius: 8px; background: rgba(0,0,0,.22); border: 2px solid transparent; cursor: pointer; }
.meld.you { border-color: rgba(74,163,255,.5); } .meld.cpu { border-color: rgba(255,159,90,.5); }
.meld.accepts { border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
.half { display: flex; align-items: center; min-height: 48px; padding-left: 18px; }
.half .card { width: 32px; height: 46px; margin-left: -18px; font-size: 13px; cursor: pointer; }
.half .slot { width: 32px; height: 46px; margin-left: -18px; border: 2px dashed rgba(255,255,255,.2); border-radius: 6px; }
.link { position: relative; height: 12px; margin: 3px -2px; display: flex; align-items: center; justify-content: center; }
.link::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 3px; margin-top: -1px; background: rgba(255,255,255,.45); border-radius: 2px; }
.link span { position: relative; font-size: 9px; text-transform: uppercase; letter-spacing: .08em; padding: 0 5px; background: var(--felt-dark); border-radius: 6px; color: rgba(255,255,255,.8); }
.meld.you .half.you .card { box-shadow: 0 0 0 2px rgba(74,163,255,.55); }
.half.cpu .card { box-shadow: 0 0 0 2px rgba(255,159,90,.55); }
.half.you .card { box-shadow: 0 0 0 2px rgba(74,163,255,.55); }
.empty { opacity: .55; font-size: 13px; padding: 4px 0; }

.piles { display: flex; gap: 12px; align-items: flex-start; }
.stock { flex: none; text-align: center; }
.stock .card { width: var(--cw); height: var(--ch); }
.stock .count { font-size: 11px; opacity: .8; margin-top: 2px; }
.discard { flex: 1; overflow-x: auto; display: flex; padding: 8px 4px 6px 28px; min-height: calc(var(--ch) + 16px); -webkit-overflow-scrolling: touch; }
.discard .card { margin-left: -26px; flex: none; }
.discard .card.tail { transform: translateY(-8px); outline: 3px solid var(--gold); z-index: 5; }
.discard .card.illegal { filter: brightness(.7); }

.card { position: relative; width: var(--cw); height: var(--ch); border-radius: 6px; background: var(--paper); border: 1px solid #bbb; color: var(--ink); display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-start; padding: 3px 0 0 4px; font: 700 15px/1.05 -apple-system, system-ui, sans-serif; box-shadow: 0 1px 3px rgba(0,0,0,.4); flex: none; }
.card.red { color: var(--red); }
.card .s { font-size: 1.15em; }
.card.back { background: repeating-linear-gradient(45deg, #1f4f9b, #1f4f9b 5px, #2a63bd 5px, #2a63bd 10px); border: 2px solid #fff; padding: 0; }
button.card { cursor: pointer; font-family: inherit; text-align: left; }
.card.sel { transform: translateY(-10px); outline: 3px solid var(--gold); z-index: 3; }
.card.must::after { content: "USE"; position: absolute; bottom: 2px; left: 0; right: 0; text-align: center; font-size: 10px; background: var(--gold); color: #000; border-radius: 3px; margin: 0 2px; }

.hand { display: flex; flex-wrap: wrap; gap: 14px 5px; padding-top: 12px; }

.actions { position: sticky; bottom: 0; background: var(--felt-dark); padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); display: flex; gap: 8px; z-index: 10; }
.btn { flex: 1; padding: 12px 6px; border: 0; border-radius: 10px; font-size: 16px; font-weight: 700; background: #e8e8e8; color: #111; }
.btn.primary { background: var(--gold); }
.btn:disabled { opacity: .35; }
.btn.small { flex: none; padding: 4px 10px; font-size: 12px; border-radius: 6px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal .box { background: var(--paper); color: var(--ink); border-radius: 14px; padding: 18px; width: 100%; max-width: 420px; }
.modal h2 { margin: 0 0 8px; }
.modal table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 15px; }
.modal td, .modal th { padding: 5px 4px; text-align: right; }
.modal td:first-child, .modal th:first-child { text-align: left; }
.modal tr.total td { border-top: 2px solid #999; font-weight: 700; }
.modal .btn { width: 100%; margin-top: 6px; }
.rules { font-size: 12px; opacity: .7; padding: 4px 12px 10px; }

/* Cards just drawn or picked up, until the player's next move */
.card.new { box-shadow: 0 0 0 3px #3ddc84, 0 0 12px 2px rgba(61,220,132,.8); background: #f1fff6; }
.card.new::before { content: "NEW"; position: absolute; top: -9px; right: -4px; font-size: 9px; font-weight: 800; background: #3ddc84; color: #04260f; border-radius: 4px; padding: 1px 4px; }

.modal .tally { font-size: 11px; font-weight: 400; color: #555; white-space: nowrap; }
.modal .key { font-size: 12px; color: #555; margin: 4px 0 8px; }

/* Hand rearranging */
.hand .card { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.card.ph { background: rgba(255,255,255,.12); border: 2px dashed rgba(255,255,255,.5); box-shadow: none; }
.hint { font-size: 11px; opacity: .6; margin-top: 4px; }
.sorts { display: inline-flex; align-items: center; gap: 4px; }

/* Start button */
.startwrap { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.startbox { pointer-events: auto; background: rgba(11,61,38,.94); border: 2px solid var(--gold); border-radius: 16px; padding: 20px 28px; text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.startbox h2 { margin: 0 0 4px; }
.startbox p { margin: 0 0 12px; opacity: .8; font-size: 14px; }
.btn.big { font-size: 22px; padding: 14px 40px; flex: none; }

/* Deal animation: cards slide in face-down from the stock, then flip */
:root { --backimg: repeating-linear-gradient(45deg, #1f4f9b, #1f4f9b 5px, #2a63bd 5px, #2a63bd 10px); }
.card.dealing { animation: dealIn 900ms ease-out both; animation-delay: calc(var(--i) * 110ms); }
.card.dealing > span { animation: dealFace 900ms step-end both; animation-delay: calc(var(--i) * 110ms); }
.card.dealing-back { animation: dealBack 600ms ease-out both; animation-delay: calc(var(--i) * 110ms); }
@keyframes dealIn {
  0%   { transform: translateY(-240px) scale(.6); opacity: 0; background: var(--backimg); border-color: #fff; }
  45%  { transform: none; opacity: 1; background: var(--backimg); border-color: #fff; }
  60%  { transform: rotateY(90deg); background: var(--backimg); border-color: #fff; }
  61%  { transform: rotateY(90deg); background: var(--paper); border-color: #bbb; }
  100% { transform: none; background: var(--paper); border-color: #bbb; }
}
@keyframes dealFace { 0%, 60% { visibility: hidden; } 61%, 100% { visibility: visible; } }
@keyframes dealBack { from { transform: translateY(-120px) scale(.6); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .card.dealing, .card.dealing > span, .card.dealing-back { animation-duration: 1ms; animation-delay: 0s; } }

/* Settings */
.setting { margin: 12px 0; }
.setting small { display: block; color: #555; font-size: 12px; margin: 2px 0 6px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.seg button { flex: 1; min-width: 70px; padding: 10px 6px; border-radius: 8px; border: 2px solid #bbb; background: #fff; color: #111; font-size: 14px; font-weight: 600; }
.seg button.on { border-color: #0f5132; background: #0f5132; color: #fff; }
.mid .btn.small { margin-top: 4px; }

/* What the computer just did */
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(255,159,90,0); } 50% { box-shadow: 0 0 0 5px rgba(255,159,90,.95); } }
.meld.flash { animation: pulse 1s ease-in-out 3; border-color: var(--cpu); }
.discard .card.flash { animation: pulse 1s ease-in-out 3; outline: 3px solid var(--cpu); }

/* Toast: messages near the thumb, above the action bar */
#toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(10px); max-width: min(92vw, 560px); padding: 10px 14px; border-radius: 10px; background: rgba(20,20,20,.94); color: #fff; font-size: 14px; line-height: 1.3; text-align: center; opacity: 0; pointer-events: none; transition: opacity .15s, transform .15s; z-index: 60; }
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.err { background: #8a1f1f; }

.btn.small.on { background: var(--gold); }
.status .btn.small { flex: none; font-size: 16px; padding: 4px 9px; }

/* Compact action bar, right-aligned for right-handed thumb use */
.actions { justify-content: flex-end; padding: 6px 12px calc(6px + env(safe-area-inset-bottom)); gap: 8px; }
.actions .btn { flex: none; min-width: 92px; padding: 9px 16px; font-size: 15px; border-radius: 9px; }
#toast { bottom: calc(64px + env(safe-area-inset-bottom)); }

/* Final-score summary: close with an X to inspect the table; revealed computer hand */
.modal .box { position: relative; }
.modal .x { position: absolute; top: 8px; right: 8px; width: 34px; height: 34px; border-radius: 50%; border: 0; background: #e4e4e4; color: #222; font-size: 17px; line-height: 1; cursor: pointer; }
.modal h2 { padding-right: 36px; }
.cpu-hand.reveal { height: 54px; padding-left: 14px; }
.cpu-hand.reveal .card { width: 36px; height: 50px; margin-left: -14px; font-size: 13px; }

.modal .held { margin: 6px 0 8px; font-size: 13px; }
.modal .held-cards { display: flex; flex-wrap: wrap; padding-left: 14px; margin-top: 6px; min-height: 44px; }
.modal .held-cards .card { width: 32px; height: 44px; margin-left: -14px; font-size: 12px; }
.modal .held-cards .none { padding-left: 0; opacity: .6; }

/* Hand: overlapped cards with big corner values (only the left edge of each card shows) */
.hand { gap: 16px 0; padding-left: 18px; }
.hand .card { width: 46px; height: 68px; margin-left: -18px; padding: 3px 0 0 4px; font-size: 22px; line-height: 1; box-shadow: -2px 1px 3px rgba(0,0,0,.35); }
.hand .card .s { font-size: 1.1em; }
.hand .card.sel { z-index: 4; }
.hand .card.new::before { right: auto; left: 2px; }

/* Keep every card's value strip visible: selection lifts a card instead of stacking it over neighbours */
.hand .card.sel { z-index: auto; transform: translateY(-14px); outline-offset: -1px; }
.hand .card.new { box-shadow: -2px 1px 3px rgba(0,0,0,.35), 0 0 0 2px #3ddc84; }
.hand .card.new::before { top: -10px; left: 1px; right: auto; font-size: 8px; padding: 1px 3px; }
.hand .card.must::after { right: auto; left: 0; width: 24px; margin: 0; bottom: 2px; font-size: 9px; }

/* ---- Tablet / desktop: use the whole screen ---- */
@media (min-width: 700px) {
  :root { --cw: 64px; --ch: 92px; }
  #app {
    max-width: 1280px;
    display: grid;
    grid-template-columns: clamp(300px, 34%, 440px) minmax(0, 1fr);
    grid-template-rows: auto auto auto 1fr auto auto;
    grid-template-areas:
      "score score"
      "status status"
      "piles cpu"
      "piles table"
      "hand hand"
      "actions actions";
    align-content: start;
  }
  .scorebar { grid-area: score; padding: 10px 20px; font-size: 16px; }
  .scorebar .side b { font-size: 28px; }
  .scorebar .mid { font-size: 14px; }
  .hand-pts { font-size: 13px; }
  .status { grid-area: status; padding: 8px 20px; font-size: 16px; min-height: 46px; }
  .s-cpu { grid-area: cpu; } .s-table { grid-area: table; } .s-piles { grid-area: piles; } .s-hand { grid-area: hand; }
  .actions { grid-area: actions; }
  .section { padding: 10px 20px 6px; }
  .label { font-size: 12px; }

  .cpu-hand { height: 62px; padding-left: 26px; }
  .cpu-hand .card { width: 42px; height: 60px; margin-left: -22px; }
  .cpu-hand.reveal { height: 76px; }
  .cpu-hand.reveal .card { width: 50px; height: 72px; margin-left: -20px; font-size: 17px; }

  .melds { gap: 14px; }
  .meld { padding: 6px 8px; }
  .half { min-height: 68px; padding-left: 26px; }
  .half .card, .half .slot { width: 46px; height: 66px; margin-left: -24px; font-size: 18px; }
  .link { height: 14px; } .link span { font-size: 10px; }

  .s-piles { border-right: 1px solid rgba(255,255,255,.08); }
  .piles { flex-direction: column; gap: 18px; }
  .stock { display: flex; align-items: center; gap: 12px; text-align: left; }
  .stock .count { font-size: 14px; margin: 0; }
  .discard { width: 100%; flex: none; padding: 14px 4px 10px 34px; min-height: calc(var(--ch) + 28px); }
  .discard .card { margin-left: -34px; }
  .discard .card.tail { transform: translateY(-12px); }

  .hand { padding-left: 14px; gap: 18px 0; min-height: 104px; }
  .hand .card { width: 66px; height: 96px; margin-left: -10px; font-size: 30px; padding: 5px 0 0 7px; }
  .hand .card.sel { transform: translateY(-16px); }
  .hand .card.new::before { font-size: 10px; top: -12px; }
  .hand .card.must::after { width: 38px; font-size: 11px; }

  .hint { font-size: 12px; }
  .rules { padding: 4px 20px 8px; font-size: 13px; }
  .actions { padding: 10px 20px calc(10px + env(safe-area-inset-bottom)); }
  .actions .btn { min-width: 130px; padding: 13px 22px; font-size: 18px; border-radius: 11px; }
  .btn.small { padding: 7px 14px; font-size: 14px; }
  #toast { bottom: calc(84px + env(safe-area-inset-bottom)); font-size: 17px; }
  .modal .box { max-width: 520px; }
}
/* Short landscape screens (iPad in landscape with Safari bars): tighten vertical space */
@media (min-width: 700px) and (max-height: 760px) {
  :root { --cw: 56px; --ch: 80px; }
  .scorebar { padding: 6px 20px; } .scorebar .side b { font-size: 24px; }
  .status { min-height: 38px; padding: 4px 20px; }
  .hand .card { height: 86px; width: 60px; font-size: 27px; }
  .half .card, .half .slot { width: 40px; height: 58px; }
  .half { min-height: 60px; }
  .hint { display: none; }
}

/* Decluttered: rules live in the ? help sheet; sort controls are small ghost chips */
.tools { display: inline-flex; gap: 6px; }
.status .btn.small { min-width: 34px; }
.sorts { display: inline-flex; gap: 4px; }
.chip { font: inherit; font-size: 11px; letter-spacing: .02em; text-transform: none; color: rgba(255,255,255,.75); background: transparent; border: 1px solid rgba(255,255,255,.25); border-radius: 12px; padding: 2px 9px; cursor: pointer; }
.chip.on { background: rgba(245,197,66,.25); border-color: var(--gold); color: #fff; }
.help ul { padding-left: 18px; margin: 8px 0 14px; font-size: 14px; line-height: 1.4; }
.help li { margin-bottom: 6px; }
.help .btn { width: 100%; }
@media (min-width: 700px) { .chip { font-size: 13px; padding: 4px 12px; } .help ul { font-size: 16px; } }

/* Toast sits at the top of the screen so it never covers your hand or the buttons */
#toast, #toast.show { bottom: auto; top: calc(8px + env(safe-area-inset-top)); }
#toast { transform: translateX(-50%) translateY(-10px); }
#toast.show { transform: translateX(-50%); }

/* Themes and card backs (chosen in Settings, applied as data attributes on <html>) */
:root { --back-a: #1f4f9b; --back-b: #2a63bd; }
.card.back { background: repeating-linear-gradient(45deg, var(--back-a), var(--back-a) 5px, var(--back-b) 5px, var(--back-b) 10px); }
:root[data-back="red"] { --back-a: #9b1f2a; --back-b: #c0323f; }
:root[data-back="black"] { --back-a: #1d1d1d; --back-b: #3d3d3d; }
:root[data-theme="blue"] { --felt: #14456b; --felt-dark: #0d2f4a; }
:root[data-theme="burgundy"] { --felt: #5c1f2e; --felt-dark: #40141f; }

.stats-table { width: 100%; border-collapse: collapse; margin: 6px 0 12px; }
.stats-table td { padding: 6px 4px; border-bottom: 1px solid #e4e1d4; }
.stats-table td:last-child { text-align: right; font-weight: 700; }

/* Bigger touch targets for the small controls */
.chip { min-height: 32px; padding: 5px 12px; font-size: 12px; }
.sorts { gap: 8px; }
.tools { gap: 8px; }
.status .btn.small { min-width: 40px; min-height: 36px; }
