:root {
  --bg: #f7fbff;
  --surface: #ffffff;
  --surface-soft: #f2f7ff;
  --ink: #202734;
  --muted: #6c7583;
  --line: #c5ccd8;
  --major-line: #6b7380;
  --blue: #3c61c9;
  --blue-soft: #dce9ff;
  --blue-mid: #82a4eb;
  --danger: #df5a56;
  --shadow: 0 14px 35px rgba(61, 87, 127, .15);
  --safe-bottom: max(14px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: radial-gradient(circle at 50% -20%, #ffffff 0, var(--bg) 50%, #edf4fd 100%);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", Tahoma, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
}
button, input { font: inherit; }
button { border: 0; cursor: pointer; }
svg { width: 1em; height: 1em; fill: currentColor; }

.app-shell { width: min(100%, 560px); min-height: 100dvh; margin: 0 auto; padding: max(12px, env(safe-area-inset-top)) 13px var(--safe-bottom); position: relative; overflow: hidden; }
.game-view { display: flex; min-height: calc(100dvh - max(12px, env(safe-area-inset-top)) - var(--safe-bottom)); flex-direction: column; }
.topbar { min-height: 59px; display: flex; align-items: center; gap: 12px; }
.game-title { font-size: 22px; letter-spacing: .1px; color: #4f5762; flex: 1; white-space: nowrap; }
.game-title strong { color: var(--blue); font-weight: 800; }
.top-actions { display: flex; align-items: center; gap: 7px; }
.icon-button { width: 41px; height: 41px; border-radius: 13px; background: transparent; color: #49515c; display: grid; place-items: center; font-size: 31px; transition: .18s ease; }
.icon-button:active { background: #e7effc; transform: scale(.92); }
.icon-button svg { width: 27px; height: 27px; }

.scoreboard { display: grid; grid-template-columns: 1fr 1.25fr 1fr; align-items: end; padding: 5px 8px 10px; gap: 6px; }
.meta-left { color: #616a75; font-size: 16px; align-self: end; padding-bottom: 2px; }
.score-area { text-align: center; }
.score { color: var(--blue); font-size: 23px; font-weight: 700; line-height: 1.05; }
.score-area small { color: #505964; display: inline-block; margin-top: 5px; font-size: 17px; }
.time-area { display: flex; justify-content: end; align-items: center; color: #606873; font-size: 17px; gap: 4px; padding-bottom: 1px; }
.pause-button { width: 29px; height: 29px; border: 1.8px solid #69727d; border-radius: 50%; color: #59626e; background: transparent; padding: 5px; display: grid; place-items: center; }
.pause-button svg { width: 18px; height: 18px; }

.board-wrap { display: flex; justify-content: center; align-items: center; padding: 0 clamp(0px, 1vw, 7px); }
.sudoku-board { aspect-ratio: 1; width: min(100%, 502px); background: var(--surface); display: grid; grid-template-columns: repeat(9, 1fr); border: 2.2px solid var(--major-line); box-shadow: 0 6px 18px rgba(40,55,81,.10); }
.cell { min-width: 0; min-height: 0; position: relative; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); display: grid; place-items: center; background: var(--surface); color: #2a3038; font-size: clamp(24px, 8.9vw, 44px); font-weight: 450; transition: background .12s ease, color .12s ease, transform .12s ease; }
.cell:nth-child(3n) { border-right: 2px solid var(--major-line); }
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+19):nth-child(-n+27), .cell:nth-child(n+46):nth-child(-n+54) { border-bottom: 2px solid var(--major-line); }
.cell.given { font-weight: 500; color: #252a31; }
.cell.user { color: var(--blue); font-weight: 540; }
.cell.related { background: #edf4ff; }
.cell.same-number { background: #d9e7ff; }
.cell.selected { background: #94b8f7; color: #1f3e9b; }
.cell.selected.same-number { background: #7da6ed; color: #1f3e9b; }
.cell.selected .notes { color: #3155ac; }
.cell.wrong { color: var(--danger); background: #fff0f0; animation: shake .3s ease both; }
.cell.completed { animation: pop .25s ease both; }
.notes { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); align-self: stretch; justify-self: stretch; padding: 4px; color: #4668bb; font-size: clamp(8px, 2.6vw, 15px); font-weight: 600; line-height: 1; }
.notes span { display: grid; place-items: center; }
@keyframes shake { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
@keyframes pop { 50% { transform: scale(1.15); } }

.controls { display: grid; grid-template-columns: repeat(5, 1fr); align-items: start; gap: 4px; padding: 21px 4px 14px; }
.tool-button { color: #525b65; background: transparent; display: flex; flex-direction: column; align-items: center; gap: 5px; padding: 0; font-size: clamp(11px, 3.5vw, 15px); line-height: 1; white-space: nowrap; }
.tool-button:active .tool-icon { transform: scale(.9); }
.tool-icon { width: 43px; height: 43px; display: grid; place-items: center; position: relative; transition: .14s ease; }
.tool-icon svg { width: 40px; height: 40px; }
.tool-icon.with-badge b { position: absolute; right: -3px; top: -5px; width: 23px; height: 23px; display: grid; place-items: center; color: white; background: var(--blue); border: 2px solid var(--bg); border-radius: 50%; font-size: 13px; }
.pencil-switch em { position: absolute; top: -9px; right: -16px; color: white; background: #7a838f; border-radius: 5px; font-style: normal; padding: 2px 4px; font-weight: 700; font-size: 12px; }
.pencil-switch.active em { background: var(--blue); }
.tool-button.active { color: var(--blue); }

.number-pad { padding: 4px 0 0; display: grid; grid-template-columns: repeat(9, 1fr); gap: 7px; }
.number-pad button { min-width: 0; aspect-ratio: .74; max-height: 92px; background: linear-gradient(#fcfdff, #eaf1fa); border-radius: 15px; box-shadow: 0 3px 7px rgba(50,75,110,.16), inset 0 1px 0 rgba(255,255,255,.9); color: var(--blue); display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: clamp(30px, 8vw, 54px); font-weight: 450; line-height: .95; padding-top: 2px; transition: .14s ease; }
.number-pad button small { min-height: 10px; margin-top: 4px; color: #555d66; font-size: 11px; font-weight: 500; }
.number-pad button:disabled { opacity: .32; }
.number-pad button:active { transform: translateY(2px) scale(.95); box-shadow: inset 0 2px 5px rgba(50,75,110,.14); }
.offline-pill { position: fixed; left: 50%; transform: translateX(-50%); top: 12px; background: #253b6a; color: #fff; padding: 7px 12px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow); z-index: 100; }

.screen-overlay { position: fixed; inset: 0; z-index: 30; display: grid; place-items: end center; padding: 12px; background: rgba(20, 28, 44, .42); backdrop-filter: blur(3px); transition: opacity .2s ease, visibility .2s ease; }
.screen-overlay.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.newgame-overlay { background: linear-gradient(to bottom, rgba(31,47,79,.07), rgba(18,31,58,.32)); padding: 0; align-items: end; }
.new-game-sheet { width: min(100%, 560px); min-height: min(84dvh, 760px); background: linear-gradient(148deg, #ffffff, #f5f8fc); border-radius: 31px 31px 0 0; padding: 16px 22px max(20px, env(safe-area-inset-bottom)); box-shadow: 0 -10px 40px rgba(21,37,67,.20); animation: sheet-in .35s ease both; max-height: 95dvh; overflow-y: auto; }
@keyframes sheet-in { from { transform: translateY(60px); } to { transform: translateY(0); } }
.sheet-handle { width: 46px; height: 5px; background: #d3dae4; border-radius: 20px; margin: 0 auto 10px; }
.new-game-sheet h1 { text-align: center; margin: 6px 0 2px; color: #353c46; font-size: 34px; letter-spacing: .2px; }
.weekly-rate { text-align: center; margin: 0 0 23px; color: #606974; font-size: 20px; }
.weekly-rate strong { color: var(--blue); }
.difficulty-list { display: grid; gap: 12px; }
.difficulty-button { width: 100%; min-height: 73px; padding: 0 24px; display: flex; align-items: center; justify-content: space-between; text-align: left; background: rgba(255,255,255,.82); color: #313944; border-radius: 18px; box-shadow: 0 3px 12px rgba(81,102,138,.10), inset 0 1px 0 #fff; font-size: 28px; transition: .15s ease; }
.difficulty-button:active { transform: scale(.985); background: #edf4ff; }
.difficulty-button .difficulty-small { color: #737b85; font-size: 18px; margin-left: auto; }
.difficulty-button .expert-star { color: #e6b81d; font-size: 25px; }
.difficulty-button .last-score { color: #6c7580; font-size: 16px; }
.mode-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 15px; }
.mode-button { min-height: 63px; border-radius: 17px; background: rgba(255,255,255,.78); color: #454e59; font-size: 24px; box-shadow: 0 3px 12px rgba(81,102,138,.08), inset 0 1px 0 #fff; }
.mode-button span { font-size: 13px; display: block; margin-top: 2px; }
.mode-button.disabled { color: #adb4bd; cursor: not-allowed; }
.install-row { margin-top: 16px; text-align: center; }
.install-button { width: 100%; padding: 14px; background: #e7efff; color: #3158b9; border-radius: 15px; font-weight: 700; }

.modal-card { position: relative; width: min(100%, 430px); background: #fff; border-radius: 25px; padding: 28px 24px 24px; text-align: center; box-shadow: var(--shadow); animation: modal-in .22s ease both; }
@keyframes modal-in { from { opacity: .2; transform: translateY(12px) scale(.96); } to { opacity: 1; transform: translateY(0) scale(1); } }
.modal-card h2 { margin: 5px 0 8px; font-size: 28px; color: #303844; }
.modal-card p { color: var(--muted); margin: 0 0 20px; font-size: 17px; }
.pause-card { padding-top: 35px; }
.pause-symbol { color: var(--blue); font-size: 56px; line-height: 1; font-weight: 800; }
.primary-button, .secondary-button { width: 100%; min-height: 53px; border-radius: 15px; font-weight: 750; font-size: 19px; }
.primary-button { background: linear-gradient(135deg, #4571d3, #3155b9); color: white; box-shadow: 0 8px 20px rgba(49,85,185,.25); }
.secondary-button { background: #edf3fb; color: #4b596f; margin-top: 10px; }
.close-modal { position: absolute; right: 14px; top: 10px; width: 35px; height: 35px; border-radius: 50%; background: #edf2f8; color: #57616e; font-size: 25px; }
.settings-card { text-align: left; }
.settings-card h2 { text-align: center; }
.setting-line { display: flex; justify-content: space-between; align-items: center; padding: 12px 2px; color: #47505c; font-size: 17px; }
.setting-line input { width: 21px; height: 21px; accent-color: var(--blue); }
.settings-card hr { border: 0; border-top: 1px solid #e5eaf0; margin: 11px 0; }
.danger-link { background: transparent; color: var(--danger); font-weight: 700; padding: 8px 0; }
.finish-star { color: #f4bd35; font-size: 58px; line-height: 1; filter: drop-shadow(0 4px 6px rgba(239,186,40,.22)); }
.finish-stats { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; padding: 13px 0 20px; }
.finish-stats div { background: #f3f7ff; border-radius: 13px; padding: 10px 5px; display: grid; gap: 4px; }
.finish-stats span { color: #667180; font-size: 13px; }
.finish-stats strong { color: var(--blue); font-size: 20px; }
.gameover-icon { width: 57px; height: 57px; margin: 0 auto; display: grid; place-items: center; border-radius: 50%; background: #ffeded; color: var(--danger); font-size: 34px; font-weight: 800; }
.toast { position: fixed; z-index: 80; left: 50%; bottom: calc(var(--safe-bottom) + 7px); transform: translate(-50%, 140%); opacity: 0; background: #263c6a; color: #fff; padding: 10px 15px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px; transition: .22s ease; max-width: calc(100% - 30px); text-align: center; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

body.dark { --bg: #151922; --surface: #202631; --surface-soft: #1b212c; --ink: #e7ebf1; --muted: #aab3c0; --line: #4e5867; --major-line: #a6afba; --blue: #83aaff; --blue-soft: #293d61; --blue-mid: #557cc8; --shadow: 0 14px 35px rgba(0,0,0,.36); background: radial-gradient(circle at 50% -20%, #303945 0, var(--bg) 65%); }
body.dark .new-game-sheet, body.dark .modal-card { background: #242a35; }
body.dark .difficulty-button, body.dark .mode-button { background: #2b3340; color: #e7ecf4; box-shadow: inset 0 1px 0 rgba(255,255,255,.05); }
body.dark .cell.given { color: #edf1f8; }
body.dark .cell.related { background: #283445; }
body.dark .cell.same-number { background: #324a73; }
body.dark .cell.selected { background: #7194d5; color: #10234e; }
body.dark .number-pad button { background: linear-gradient(#2d3542, #252d3a); box-shadow: 0 3px 7px rgba(0,0,0,.25); }
body.dark .tool-button, body.dark .icon-button { color: #e0e5ee; }
body.dark .pencil-switch em { border-color: #242a35; }

@media (min-width: 520px) {
  .app-shell { padding-left: 22px; padding-right: 22px; }
  .controls { padding-top: 25px; }
}
@media (max-height: 720px) {
  .topbar { min-height: 50px; }
  .scoreboard { padding-top: 0; padding-bottom: 6px; }
  .controls { padding-top: 12px; padding-bottom: 8px; }
  .tool-icon { width: 36px; height: 36px; }
  .tool-icon svg { width: 34px; height: 34px; }
  .number-pad { gap: 5px; }
  .number-pad button { border-radius: 12px; }
}
