:root {
  color-scheme: dark;
  --bg: #090d1a;
  --bg-glow: #17234a;
  --panel: rgba(19, 28, 51, .78);
  --panel-strong: rgba(24, 35, 62, .96);
  --line: rgba(170, 192, 255, .13);
  --text: #f0f4ff;
  --muted: #98a6c7;
  --cyan: #6be4ed;
  --green: #9bf0b4;
  --gold: #ffd477;
  --danger: #ff7087;
  --shadow: 0 24px 70px rgba(0, 0, 0, .4);
  font-family: 'DM Sans', 'Noto Sans SC', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; min-height: 100%; background: var(--bg); }
body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: grid;
  place-items: center;
  overflow-x: hidden;
  color: var(--text);
  background: radial-gradient(ellipse at 18% 8%, var(--bg-glow), transparent 45%), linear-gradient(145deg, #101830, var(--bg) 60%, #11102a);
  transition: background .3s ease, color .3s ease;
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

body[data-theme="day"] {
  color-scheme: light;
  --bg: #e9f0f6;
  --bg-glow: #c8e8f0;
  --panel: rgba(255, 255, 255, .78);
  --panel-strong: rgba(250, 253, 255, .97);
  --line: rgba(31, 62, 96, .14);
  --text: #15263a;
  --muted: #64768d;
  --cyan: #087f9c;
  --green: #167a48;
  --gold: #a9680a;
  --danger: #c63855;
  --shadow: 0 22px 60px rgba(37, 74, 110, .15);
  background: radial-gradient(ellipse at 18% 8%, #d0f0f4, transparent 45%), linear-gradient(145deg, #f8fbfd, #eaf2f8 60%, #eef0fb);
}

.app {
  --board-width: 520px;
  width: min(100%, 760px);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 26px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  transition: background .3s ease, border-color .3s ease;
}
.topbar { display: flex; align-items: center; justify-content: center; gap: 13px; position: relative; text-align: left; }
.brand-mark { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 16px; background: linear-gradient(145deg, rgba(107, 228, 237, .18), rgba(155, 240, 180, .12)); font-size: 25px; }
.brand-copy h1 { margin: 0; font-size: clamp(1.32rem, 4vw, 1.75rem); letter-spacing: -.035em; line-height: 1.2; background: linear-gradient(100deg, var(--cyan), var(--green)); color: transparent; background-clip: text; -webkit-background-clip: text; }
.tagline { margin: 4px 0 0; color: var(--muted); font-size: .84rem; }
.settings-shortcut { position: absolute; right: 0; top: 2px; }

.hud { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 18px 0 12px; }
.stat { min-width: 0; padding: 9px 12px; text-align: center; border: 1px solid var(--line); border-radius: 14px; background: rgba(127, 157, 213, .07); }
.stat-label { display: block; color: var(--muted); font-size: .72rem; }
.stat-value { display: block; margin-top: 1px; color: var(--green); font-size: 1.32rem; font-weight: 800; line-height: 1.2; font-variant-numeric: tabular-nums; }

.toolbar { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0 auto 10px; }
.icon-btn { min-width: 40px; height: 38px; padding: 0 10px; border: 1px solid var(--line); border-radius: 12px; color: var(--text); background: rgba(125, 157, 211, .09); cursor: pointer; font-size: 1rem; font-weight: 700; transition: transform .15s ease, background .2s ease, border-color .2s ease; touch-action: manipulation; }
.icon-btn:hover { transform: translateY(-1px); border-color: rgba(107, 228, 237, .42); background: rgba(107, 228, 237, .13); }
.icon-btn:active, .btn:active { transform: scale(.97); }
.icon-btn:focus-visible, .btn:focus-visible, .toggle-btn:focus-visible { outline: 3px solid rgba(107, 228, 237, .55); outline-offset: 2px; }
.icon-btn:disabled { opacity: .42; cursor: default; transform: none; }
.best-btn { min-width: 52px; font-size: .78rem; }
.toolbar-divider { width: 1px; height: 20px; margin: 0 2px; background: var(--line); }

.play-area { display: flex; flex-direction: column; align-items: center; }
.stage { width: min(100%, 620px); display: flex; justify-content: center; align-items: center; }
canvas { display: block; width: var(--board-width); height: var(--board-width); max-width: 100%; border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 12px 38px rgba(0, 0, 0, .22), inset 0 0 40px rgba(0, 0, 0, .12); cursor: crosshair; touch-action: manipulation; }
.game-info { width: 100%; max-width: 620px; text-align: center; }
.message { min-height: 1.45em; margin: 10px 0 2px; color: var(--green); font-size: .88rem; font-weight: 700; }
.hint, .build-tip { margin: 3px 0 0; color: var(--muted); font-size: .72rem; }
.controls { display: flex; justify-content: center; gap: 10px; margin-top: 12px; }
.btn { min-height: 43px; padding: 0 18px; border: 1px solid transparent; border-radius: 13px; cursor: pointer; font-size: .88rem; font-weight: 800; transition: transform .12s ease, opacity .2s ease, box-shadow .2s ease, background .2s ease; touch-action: manipulation; }
.btn-primary { color: #09232b; background: linear-gradient(105deg, #77e8e8, #a3efaf); box-shadow: 0 8px 20px rgba(69, 204, 195, .2); }
.btn-secondary { color: var(--text); border-color: var(--line); background: rgba(125, 157, 211, .11); }
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:disabled { opacity: .42; cursor: not-allowed; box-shadow: none; }
.build-tip { margin-top: 9px; text-align: center; }

.settings-panel { width: min(420px, calc(100vw - 32px)); max-height: min(90dvh, 620px); padding: 22px; border: 1px solid var(--line); border-radius: 22px; color: var(--text); background: var(--panel-strong); box-shadow: var(--shadow); }
.settings-panel::backdrop { background: rgba(4, 8, 18, .68); backdrop-filter: blur(5px); }
.settings-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 13px; }
.eyebrow { color: var(--cyan); font-size: .64rem; font-weight: 800; letter-spacing: .16em; }
.settings-heading h2 { margin: 3px 0 0; font-size: 1.35rem; }
.close-settings { min-width: 36px; height: 36px; font-size: 1.3rem; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 62px; border-top: 1px solid var(--line); }
.setting-row strong, .setting-row small { display: block; }
.setting-row strong { font-size: .88rem; }
.setting-row small { margin-top: 2px; color: var(--muted); font-size: .7rem; }
.toggle-btn { min-width: 66px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; color: var(--text); background: rgba(125, 157, 211, .1); cursor: pointer; font-size: .78rem; font-weight: 700; }
.volume-row { display: grid; grid-template-columns: 1fr minmax(95px, 140px) 42px; }
input[type="range"] { width: 100%; accent-color: var(--cyan); }
output { color: var(--muted); text-align: right; font-size: .75rem; font-variant-numeric: tabular-nums; }
.settings-actions { display: flex; gap: 10px; margin-top: 16px; }
.settings-actions .btn { flex: 1; padding: 0 10px; }

.app:fullscreen { width: 100%; height: 100%; max-width: none; max-height: 100dvh; overflow: auto; display: flex; flex-direction: column; justify-content: center; padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left)); border: 0; border-radius: 0; }
.app:fullscreen .stage { flex: 1 1 auto; min-height: 0; }
.app:fullscreen .toolbar { margin-top: 4px; margin-bottom: 6px; }

@media (max-width: 540px) {
  body { padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); }
  .app { padding: 14px; border-radius: 21px; }
  .brand-mark { width: 40px; height: 40px; border-radius: 13px; font-size: 21px; }
  .tagline { font-size: .72rem; }
  .settings-shortcut { min-width: 36px; height: 36px; }
  .hud { gap: 7px; margin: 13px 0 9px; }
  .stat { padding: 7px 5px; border-radius: 12px; }
  .stat-label { font-size: .66rem; }
  .stat-value { font-size: 1.12rem; }
  .toolbar { gap: 6px; margin-bottom: 8px; }
  .icon-btn { min-width: 36px; height: 35px; padding: 0 8px; border-radius: 11px; }
  .best-btn { min-width: 46px; }
  canvas { border-radius: 14px; }
  .message { font-size: .78rem; }
  .hint, .build-tip { font-size: .65rem; }
  .controls { gap: 7px; margin-top: 9px; }
  .btn { min-height: 41px; padding: 0 12px; font-size: .78rem; }
}

@media (orientation: landscape) and (max-height: 560px) {
  body { padding-top: max(2px, env(safe-area-inset-top)); padding-bottom: max(2px, env(safe-area-inset-bottom)); }
  .app { width: min(100%, 1050px); padding: 6px 12px; border-radius: 18px; display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: 'header header' 'hud toolbar' 'play controls' 'tip tip'; column-gap: 16px; align-items: center; }
  .topbar { grid-area: header; min-height: 32px; }
  .brand-mark { width: 30px; height: 30px; font-size: 16px; }
  .brand-copy h1 { font-size: 1.15rem; }
  .tagline { display: none; }
  .settings-shortcut { top: 0; min-width: 34px; height: 34px; }
  .hud { grid-area: hud; margin: 2px 0; gap: 5px; }
  .stat { padding: 3px 7px; }
  .stat-label { font-size: .58rem; }
  .stat-value { font-size: .9rem; }
  .toolbar { grid-area: toolbar; margin: 2px 0; gap: 4px; }
  .icon-btn { min-width: 32px; height: 30px; padding: 0 6px; }
  .best-btn { min-width: 40px; }
  .play-area { grid-area: play; min-width: 0; }
  .stage { width: min(100%, 620px); }
  .game-info { max-width: 100%; }
  .message { min-height: 1.15em; margin: 2px 0 0; font-size: .68rem; }
  .hint { display: none; }
  .controls { grid-area: controls; flex-direction: column; margin: 0; min-width: 140px; }
  .btn { min-height: 38px; }
  .build-tip { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

body.mobile-portrait { overflow: hidden; }
.orientation-gate[hidden] { display: none; }
.orientation-gate { position: fixed; z-index: 10000; inset: 0; display: grid; place-items: center; padding: max(22px, env(safe-area-inset-top)) max(22px, env(safe-area-inset-right)) max(22px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left)); color: var(--text); background: radial-gradient(ellipse at 50% 22%, #20335c, #090d1a 70%); }
.orientation-card { width: min(100%, 390px); padding: 30px 26px 24px; text-align: center; border: 1px solid rgba(170, 192, 255, .17); border-radius: 26px; background: rgba(19, 28, 51, .84); box-shadow: 0 24px 70px rgba(0, 0, 0, .4); backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px); }
.device-rotate { width: 76px; height: 64px; margin: 0 auto 21px; display: grid; place-items: center; }
.device-rotate span { position: relative; width: 31px; height: 55px; border: 3px solid var(--cyan); border-radius: 8px; box-shadow: 0 0 24px rgba(107, 228, 237, .2); transform: rotate(90deg); animation: rotate-phone 1.8s ease-in-out infinite alternate; }
.device-rotate span::after { content: ''; position: absolute; left: 50%; bottom: 3px; width: 4px; height: 4px; border-radius: 50%; background: var(--cyan); transform: translateX(-50%); }
.orientation-card .eyebrow { margin: 0 0 7px; }
.orientation-card h2 { margin: 0; font-size: 1.45rem; }
.orientation-card > p:not(.eyebrow) { margin: 10px auto 19px; max-width: 310px; color: var(--muted); font-size: .88rem; line-height: 1.75; }
.orientation-card .btn { min-height: 46px; padding-inline: 24px; font-size: .92rem; }
.orientation-card .orientation-hint { min-height: 1.4em; margin: 13px 0 0; font-size: .74rem; }
@keyframes rotate-phone { from { transform: rotate(0deg); } to { transform: rotate(90deg); } }
@media (prefers-reduced-motion: reduce) { .device-rotate span { animation: none; transform: rotate(90deg); } }

/* Embedded browsers may reject orientation locking; rotate the game without fullscreen. */
body.portrait-landscape-fallback { position: fixed; inset: 0; width: 100vw; height: 100dvh; min-height: 0; padding: 0; display: block; overflow: hidden; }
body.portrait-landscape-fallback .app { position: fixed; top: 0; left: 100vw; width: 100dvh; height: 100vw; min-height: 0; max-width: none; max-height: none; overflow: hidden; padding: 3px 6px; border: 0; border-radius: 0; display: grid; grid-template-columns: minmax(150px, 1.3fr) auto auto; grid-template-rows: 40px minmax(0, 1fr); grid-template-areas: 'header hud toolbar' 'play play controls'; column-gap: 8px; row-gap: 1px; align-items: center; transform: rotate(90deg); transform-origin: top left; }
body.portrait-landscape-fallback .topbar { grid-area: header; min-height: 38px; gap: 7px; }
body.portrait-landscape-fallback .brand-mark { width: 30px; height: 30px; border-radius: 9px; font-size: 16px; }
body.portrait-landscape-fallback .brand-copy h1 { font-size: 1.08rem; }
body.portrait-landscape-fallback .tagline { display: none; }
body.portrait-landscape-fallback .settings-shortcut { top: 0; min-width: 31px; height: 30px; padding: 0 4px; border-radius: 8px; font-size: .9rem; }
body.portrait-landscape-fallback .hud { grid-area: hud; margin: 0; gap: 4px; }
body.portrait-landscape-fallback .stat { padding: 3px 5px; border-radius: 8px; }
body.portrait-landscape-fallback .stat-label { font-size: .62rem; }
body.portrait-landscape-fallback .stat-value { margin: 0; font-size: .88rem; }
body.portrait-landscape-fallback .toolbar { grid-area: toolbar; margin: 0; gap: 4px; }
body.portrait-landscape-fallback .icon-btn { min-width: 32px; height: 31px; padding: 0 5px; border-radius: 8px; }
body.portrait-landscape-fallback .best-btn { min-width: 39px; }
body.portrait-landscape-fallback .play-area { grid-area: play; min-width: 0; }
body.portrait-landscape-fallback .play-area { align-self: stretch; justify-content: center; }
body.portrait-landscape-fallback .stage { width: min(100%, calc(100dvh - 8px)); flex: 1 1 auto; }
body.portrait-landscape-fallback .game-info { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
body.portrait-landscape-fallback .hint,
body.portrait-landscape-fallback .build-tip { display: none; }
body.portrait-landscape-fallback .controls { position: absolute; left: 80%; top: 57%; grid-area: auto; display: flex; flex-direction: column; justify-content: center; gap: 4px; width: auto; min-width: 0; margin: 0; transform: translate(-50%, -50%); }
body.portrait-landscape-fallback .btn { flex: 0 0 44px; width: 70px; min-height: 36px; padding: 0 4px; border-radius: 8px; font-size: .66rem; }
