* { box-sizing: border-box; }
:root { --ink: #2a1d12; --ivory: #fbf6ea; --paper: #f6efe0; --jade: #2e7d5b; --verm: #b8392b; --gold: #d8a94f; --blue: #1f4e9c; --mute: #7a6a58; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #1d231f; color: var(--ink);
  font-family: 'Baloo 2', 'Nunito', system-ui, 'Noto Sans', sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
}
#viewport { position: fixed; inset: 0; }
#stage { position: absolute; left: 0; top: 0; width: 1280px; height: 720px; transform-origin: 0 0; overflow: hidden; }
#game { position: absolute; inset: 0; width: 1280px; height: 720px; touch-action: none; display: block; }
.hidden { display: none !important; }

.overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: radial-gradient(ellipse at center, rgba(30,40,32,.35), rgba(16,20,16,.7)); animation: fade .22s ease; z-index: 10;
}
.overlay.clear { background: none; }
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
@keyframes pop { 0% { transform: scale(.8); opacity: 0 } 70% { transform: scale(1.03); opacity: 1 } 100% { transform: scale(1) } }
@keyframes bounce { 0%, 100% { transform: translateY(0) } 50% { transform: translateY(-5px) } }
@keyframes sway { 0%, 100% { transform: rotate(-1.5deg) } 50% { transform: rotate(1.5deg) } }
@keyframes rot { to { transform: rotate(360deg) } }

.loader { text-align: center; font-size: 24px; color: var(--ivory); font-weight: 800; }
.spin { width: 58px; height: 58px; margin: 0 auto 10px; border-radius: 50%; border: 6px solid rgba(251,246,234,.2); border-top-color: var(--jade); animation: rot .9s linear infinite; }
.loader .bar { width: 260px; height: 8px; border-radius: 6px; background: rgba(251,246,234,.2); margin: 8px auto 0; overflow: hidden; }
.loader .bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--jade), var(--gold)); transition: width .2s; }

/* ---------------------------------------------------------------- buttons */
.btn {
  font: inherit; font-weight: 800; font-size: 20px; color: #fffaf0; border: 0; cursor: pointer; position: relative; line-height: 1.1;
  padding: 9px 22px 10px; border-radius: 14px; box-shadow: 0 0 0 2px rgba(42,29,18,.85), 0 5px 0 1px rgba(42,29,18,.55);
  transition: transform .12s cubic-bezier(.3,1.6,.5,1), filter .15s; text-shadow: 0 1px 0 rgba(0,0,0,.25);
}
.btn:hover { transform: translateY(-2px); filter: brightness(1.07); }
.btn:active { transform: translateY(3px) scale(.98); box-shadow: 0 0 0 2px rgba(42,29,18,.85), 0 1px 0 1px rgba(42,29,18,.55); }
.btn:disabled { filter: grayscale(.85) brightness(.9); cursor: not-allowed; transform: none; opacity: .75; }
.btn.red { background: linear-gradient(#cf5a45, #9e2d20); }
.btn.jade { background: linear-gradient(#4c9a78, #266248); }
.btn.gold { background: linear-gradient(#ecc673, #c0902e); color: #3a2508; text-shadow: 0 1px 0 rgba(255,255,255,.35); }
.btn.ink { background: linear-gradient(#5a4a3c, #2f251c); }
.btn.small { font-size: 16px; padding: 6px 14px 7px; }
.btn.round { width: 50px; height: 50px; padding: 0; border-radius: 50%; font-size: 18px; }
.btn.round-sm { width: 54px; flex: none !important; padding: 0; font-size: 22px; }
.badge { position: absolute; top: -9px; right: -7px; background: var(--verm); color: #fff; border-radius: 12px; min-width: 22px; height: 22px; font-size: 14px; display: grid; place-items: center; padding: 0 6px; animation: bounce 1s infinite; }
.btn.toggle { min-width: 96px; font-size: 17px; padding: 5px 14px 6px; background: linear-gradient(#a79c8e, #6c6257); }
.btn.toggle.on { background: linear-gradient(#4c9a78, #266248); }
.seg { display: flex; gap: 0; border-radius: 12px; overflow: hidden; box-shadow: 0 0 0 2px rgba(42,29,18,.85); }
.seg button { font: inherit; font-weight: 800; font-size: 16px; border: 0; padding: 6px 12px; background: #e9e0cc; color: var(--ink); cursor: pointer; }
.seg button.on { background: var(--jade); color: #fff; }

/* ---------------------------------------------------------------- menu */
#menu { justify-content: center; }
.menu-col {
  width: 860px; display: flex; flex-direction: column; align-items: stretch; gap: 12px; padding: 16px 30px 20px; border-radius: 28px;
  background: rgba(251,246,234,.9); box-shadow: 0 0 0 2px rgba(42,29,18,.7), 0 20px 50px rgba(20,24,16,.35); animation: pop .4s ease both;
  backdrop-filter: blur(3px);
}
.logo { display: flex; align-items: baseline; justify-content: center; gap: 14px; line-height: .95; }
.logo .l1, .logo .l2 { font-size: 64px; font-weight: 800; display: inline-block; }
.logo .l1 { color: var(--verm); text-shadow: 0 3px 0 rgba(42,29,18,.25); }
.logo .l2 { color: var(--jade); text-shadow: 0 3px 0 rgba(42,29,18,.25); }
.tagline { margin: -4px 0 2px; text-align: center; font-size: 17px; font-weight: 700; color: var(--mute); font-style: italic; }
.modes { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.mode-card {
  position: relative; font: inherit; border: 0; cursor: pointer; border-radius: 20px; padding: 10px 12px 12px; text-align: center; color: var(--ink);
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 206px;
  background: linear-gradient(#fffdf6, #efe4cc); box-shadow: 0 0 0 2px rgba(42,29,18,.75), 0 6px 0 1px rgba(42,29,18,.35);
  transition: transform .15s cubic-bezier(.3,1.6,.5,1);
}
.mode-card:hover { transform: translateY(-4px); }
.mode-card:active { transform: translateY(2px) scale(.98); }
.mode-card canvas { width: 140px; height: 80px; }
.mode-card b { font-size: 26px; line-height: 1.05; }
.mode-card.classic b { color: var(--verm); }
.mode-card.connect b { color: var(--blue); }
.mode-card.triple b { color: var(--jade); }
.mode-card > span { font-size: 14.5px; font-weight: 700; color: var(--mute); line-height: 1.15; }
.mode-card small { margin-top: auto; font-size: 15px; font-weight: 800; color: #fffaf0; background: var(--ink); border-radius: 10px; padding: 2px 10px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mode-card.connect { background: linear-gradient(#f6f7ff, #dde3f1); }
.mode-card.triple { background: linear-gradient(#f5fbf4, #dcebd9); }
.mode-card .pick { position: absolute; top: 8px; right: 10px; width: 36px; height: 36px; border-radius: 10px; background: var(--ink); color: #fff; font-size: 20px; display: grid; place-items: center; }
.daily { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 8px 20px; }
.daily small { font-size: 16px; font-weight: 700; opacity: .9; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.daily.done { background: linear-gradient(#bfe0cc, #7fb596); }
.menu-row { display: flex; gap: 10px; }
.menu-row .btn { flex: 1; font-size: 16.5px; padding: 8px 6px 9px; min-width: 0; }
.menu-row.small { justify-content: center; margin-top: 10px; }
.menu-row.small .btn { flex: none; }
.icon-btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wallet-row { position: absolute; top: 14px; right: 18px; display: flex; gap: 10px; }
.wallet-row .pill { font-size: 22px; padding: 2px 16px; flex-direction: row; gap: 6px; }

/* ---------------------------------------------------------------- HUD */
#hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
#hud > * { pointer-events: auto; }
.hud-top { position: absolute; left: 14px; right: 14px; top: 10px; height: 56px; display: flex; align-items: center; gap: 14px; pointer-events: none; }
.hud-top > * { pointer-events: auto; }
.hud-title { display: flex; flex-direction: column; line-height: 1.05; background: rgba(251,246,234,.9); border-radius: 14px; padding: 4px 14px; box-shadow: 0 0 0 2px rgba(42,29,18,.6); max-width: 360px; }
.hud-title b { font-size: 20px; color: var(--verm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-title small { font-size: 14px; font-weight: 700; color: var(--mute); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hud[data-mode="connect"] .hud-title b { color: var(--blue); }
#hud[data-mode="triple"] .hud-title b { color: var(--jade); }
.pills { margin-left: auto; display: flex; gap: 8px; }
.pill { background: rgba(251,246,234,.92); color: var(--ink); font-weight: 800; padding: 2px 14px; border-radius: 16px; box-shadow: 0 0 0 2px rgba(42,29,18,.6); display: flex; flex-direction: column; align-items: center; line-height: 1.05; min-width: 74px; }
.pill small { font-size: 12px; color: var(--mute); text-transform: uppercase; letter-spacing: .5px; }
.pill b { font-size: 21px; color: var(--ink); font-variant-numeric: tabular-nums; }
#timebar { position: absolute; left: 220px; right: 24px; top: 70px; height: 10px; border-radius: 6px; background: rgba(251,246,234,.3); overflow: hidden; box-shadow: 0 0 0 2px rgba(0,0,0,.35); }
#timebar i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #e8b64c, #7fd6a0); transition: width .25s linear; }
#timebar i.low { background: linear-gradient(90deg, #ff6a4a, #ffb04a); animation: bounce .5s infinite; }
.toolbar { position: absolute; right: 16px; bottom: 12px; display: flex; gap: 10px; }
#hud[data-mode="triple"] .toolbar { flex-direction: column; bottom: 16px; right: 14px; }
.tool {
  position: relative; width: 78px; height: 66px; border: 0; border-radius: 16px; font: inherit; cursor: pointer; color: var(--ink);
  background: linear-gradient(#fffdf6, #eadfc6); box-shadow: 0 0 0 2px rgba(42,29,18,.75), 0 4px 0 1px rgba(42,29,18,.4);
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; transition: transform .12s;
}
#hud[data-mode="triple"] .tool { height: 58px; }
.tool:hover { transform: translateY(-2px); }
.tool:active { transform: translateY(2px) scale(.96); }
.tool .ic { font-size: 24px; }
.tool small { font-size: 13px; font-weight: 800; margin-top: 3px; max-width: 72px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool .n { position: absolute; top: -8px; right: -6px; min-width: 22px; height: 22px; border-radius: 11px; background: var(--jade); color: #fff; font-size: 14px; display: grid; place-items: center; padding: 0 5px; }
.tool.empty { opacity: .55; }
.tool.empty .n { background: #8a7d6e; }
.banner {
  position: absolute; left: 0; right: 0; margin: 0 auto; width: fit-content; bottom: 16px; max-width: 690px; text-align: center; color: #fffaf0; font-weight: 800; font-size: 18px;
  background: rgba(42,29,18,.86); border-radius: 14px; padding: 6px 16px; animation: pop .3s ease both; pointer-events: none !important; z-index: 6;
}
#hud[data-mode="triple"] .banner { bottom: auto; top: 76px; }
.toast { position: absolute; left: 0; right: 0; margin: 0 auto; width: fit-content; top: 96px; background: rgba(251,246,234,.97); color: var(--ink); font-weight: 800; font-size: 20px; padding: 7px 22px; border-radius: 16px; box-shadow: 0 0 0 2px rgba(42,29,18,.7), 0 8px 20px rgba(0,0,0,.25); animation: pop .3s ease both; z-index: 20; max-width: 900px; text-align: center; pointer-events: none; }

/* ---------------------------------------------------------------- modals */
.modal {
  background: var(--paper); border-radius: 24px; padding: 18px 30px 20px; min-width: 440px; max-width: 980px; text-align: center;
  box-shadow: 0 0 0 2px rgba(42,29,18,.8), 0 24px 60px rgba(0,0,0,.45); animation: pop .32s ease both; max-height: 700px; overflow-y: auto;
  background-image: radial-gradient(ellipse at 95% 100%, rgba(46,125,91,.1), transparent 55%), radial-gradient(ellipse at 0% 0%, rgba(184,57,43,.06), transparent 50%);
}
.modal.wide { width: 920px; }
.modal.tall { height: 690px; display: flex; flex-direction: column; }
.modal h2 { margin: 0 0 6px; font-size: 34px; line-height: 1.1; color: var(--verm); }
.modal .sub { margin: 0 0 10px; color: var(--mute); font-size: 17px; font-weight: 700; }
.stack { display: flex; flex-direction: column; gap: 12px; margin: 12px 0; }
.actions { display: flex; gap: 14px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.goal { font-size: 24px; font-weight: 800; color: var(--blue); margin: 8px 0; }

.layout-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; overflow-y: auto; padding: 4px 6px 8px; flex: 1; }
.lay { font: inherit; border: 0; cursor: pointer; border-radius: 14px; padding: 6px 6px 8px; background: #fffdf6; box-shadow: 0 0 0 2px rgba(42,29,18,.6); display: flex; flex-direction: column; align-items: center; gap: 1px; transition: transform .12s; }
.lay:hover { transform: translateY(-2px); }
.lay canvas { width: 150px; height: 84px; }
.lay b { font-size: 16px; line-height: 1.1; color: var(--ink); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lay small { font-size: 12.5px; font-weight: 700; color: var(--mute); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lay.won { background: #eef7ef; }
.lay.won small { color: var(--jade); }
.lay.locked { opacity: .7; background: #e9e2d4; }

.list { display: grid; gap: 10px; margin-top: 8px; text-align: left; }
.mission { display: grid; grid-template-columns: 1fr auto; gap: 14px; align-items: center; background: #fffdf6; border-radius: 14px; padding: 9px 14px; box-shadow: 0 0 0 2px rgba(42,29,18,.55); }
.mission b { font-size: 18px; }
.mission small { color: var(--mute); font-weight: 700; font-size: 13px; }
.m-bar { height: 9px; border-radius: 6px; background: #e8dfcc; overflow: hidden; margin: 4px 0 2px; }
.m-bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--jade), var(--gold)); }
.mission.done { background: #fff7e0; }
.mission.claimed { opacity: .6; }

.shop-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 8px; }
.skin { background: #fffdf6; border-radius: 16px; padding: 8px 8px 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; box-shadow: 0 0 0 2px rgba(42,29,18,.55); }
.skin canvas { width: 130px; height: 76px; }
.skin b { font-size: 17px; }
.skin .btn { width: 100%; }
.skin.using { box-shadow: 0 0 0 3px var(--jade); background: #eef7ef; }

.stats-body { overflow-y: auto; flex: 1; }
.stat-cards { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; margin-bottom: 10px; }
.stat-cards div, .stats div { background: #fffdf6; border-radius: 12px; padding: 6px 8px; box-shadow: 0 0 0 2px rgba(42,29,18,.5); }
.stat-cards b { display: block; font-size: 17px; color: var(--blue); line-height: 1.15; }
.stat-cards small, .stats small { color: var(--mute); font-size: 13px; font-weight: 700; display: block; line-height: 1.1; }
.stats-table { width: 100%; border-collapse: collapse; font-size: 16px; font-weight: 700; }
.stats-table th { text-align: left; color: var(--mute); font-size: 13px; text-transform: uppercase; padding: 4px 8px; position: sticky; top: 0; background: var(--paper); }
.stats-table td { padding: 4px 8px; border-top: 1px solid rgba(42,29,18,.12); text-align: left; }
.stats-table td:not(:first-child), .stats-table th:not(:first-child) { text-align: center; }

.tabs { display: flex; gap: 8px; justify-content: center; margin: 4px 0 6px; }
.tabs button { font: inherit; font-weight: 800; font-size: 17px; border: 0; border-radius: 12px; padding: 5px 16px; cursor: pointer; background: #e9e0cc; color: var(--ink); }
.tabs button.on { background: var(--verm); color: #fff; }
.howto { text-align: left; font-size: 18px; line-height: 1.4; margin: 8px 0; padding-left: 26px; font-weight: 600; }
.howto li { margin: 6px 0; }
.keys { color: var(--mute); font-size: 15px; font-weight: 700; margin: 4px 0; }
.set-grid { display: grid; grid-template-columns: 1fr auto; gap: 9px 24px; align-items: center; text-align: left; font-size: 18px; font-weight: 800; margin: 10px 0; }
select { font: inherit; font-size: 17px; font-weight: 700; padding: 5px 10px; border-radius: 12px; border: 2px solid rgba(42,29,18,.8); color: var(--ink); background: #fff; }

.quip { margin: 0 0 6px; font-size: 18px; font-weight: 700; color: #8a5a2a; font-style: italic; }
.big-score { font-size: 64px; font-weight: 800; color: var(--jade); line-height: 1; margin: 4px 0; font-variant-numeric: tabular-nums; }
#result.lost .big-score { color: var(--mute); }
.badge-new { display: inline-block; background: linear-gradient(#ecc673, #c0902e); color: #3a2508; font-weight: 800; font-size: 19px; padding: 3px 18px; border-radius: 14px; animation: sway 1s ease-in-out infinite; }
.stars { font-size: 60px; letter-spacing: 8px; line-height: 1; margin: 2px 0 4px; }
.stars span { display: inline-block; color: #ddd3c1; }
.stars span.on.pop { color: #e8b64c; animation: pop .45s ease both; text-shadow: 0 2px 0 rgba(42,29,18,.35); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 8px; margin: 10px 0; }
.stats b { display: block; font-size: 22px; color: var(--blue); }
.reward { font-size: 22px; font-weight: 800; color: #3f7d4f; }

/* ---------------------------------------------------------------- phones (landscape)
   The 1280×720 stage is shown at ~0.5× on a phone, so menus, modals and the HUD are zoomed by --k (set in
   main.js resize) to keep text ≥ ~12 CSS px and touch targets ≥ 44 px. The canvas board shrinks to make room. */
#stage { --k: 1; }
body.phone .menu-col, body.phone .modal, body.phone .hud-top, body.phone .toolbar, body.phone .banner,
body.phone .toast, body.phone .wallet-row, body.phone .loader { zoom: var(--k); }
body.phone .menu-col { width: calc(1250px / var(--k)); padding: 10px 18px 12px; gap: 8px; }
body.phone .logo .l1, body.phone .logo .l2 { font-size: 46px; }
body.phone .tagline { display: none; }
body.phone .mode-card { min-height: 0; padding: 6px 8px 8px; }
body.phone .mode-card canvas { width: 112px; height: 64px; }
body.phone .mode-card > span { font-size: 15.5px; }
body.phone .mode-card .pick { width: 44px; height: 44px; }
body.phone .btn, body.phone .menu-row .btn, body.phone .daily, body.phone .tabs button, body.phone .seg button { min-height: 56px; }
body.phone .btn.round { min-height: 0; width: 56px; height: 56px; }
body.phone #menu { justify-content: flex-end; padding-bottom: 6px; }
body.phone .wallet-row { top: 6px; right: 8px; }
body.phone .modal { min-width: 0; max-width: calc(1250px / var(--k)); max-height: calc(708px / var(--k)); padding: 12px 18px 14px; }
body.phone .modal.wide { width: calc(1250px / var(--k)); }
body.phone .modal.tall { height: calc(708px / var(--k)); }
body.phone .modal h2 { font-size: 28px; }
body.phone .pill small, body.phone .tool small, body.phone .lay small, body.phone .mission small,
body.phone .stat-cards small, body.phone .stats small, body.phone .stats-table th, body.phone .keys { font-size: 15px; }
body.phone .tool small { max-width: 76px; }
body.phone .tool .n { font-size: 15px; min-width: 24px; height: 24px; }
body.phone .hud-title small { font-size: 15px; }
body.phone .btn.small, body.phone .btn.toggle, body.phone .seg button, body.phone .tabs button { font-size: 16px; }
body.phone .layout-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
body.phone .lay canvas { width: 100%; height: auto; aspect-ratio: 150 / 84; }
body.phone .set-grid { grid-template-columns: 1fr auto 1fr auto; gap: 8px 14px; font-size: 16px; }
body.phone .stat-cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
body.phone .stars { font-size: 44px; }
body.phone .big-score { font-size: 48px; }
body.phone #timebar { top: calc(14px + 58px * var(--k)); }
body.phone .banner { left: calc(212px / var(--k)); right: auto; margin: 0; max-width: calc(640px / var(--k)); font-size: 17px; }
body.phone #hud[data-mode="triple"] .banner { left: 0; right: 0; margin: 0 auto; }

#rotate { display: none; }
@media (orientation: portrait) and (max-width: 820px) and (pointer: coarse) {
  #rotate { display: block; position: fixed; left: 0; right: 0; bottom: 0; background: rgba(46,125,91,.95); color: #fff; text-align: center; padding: 12px; font-weight: 800; z-index: 50; }
}
