:root {
  color-scheme: dark;
  --bg: #0d131a;
  --surface: #151e27;
  --surface-2: #1b2732;
  --line: #2b3944;
  --closed: #334654;
  --closed-top: #4b6170;
  --open: #e6ebe8;
  --ink: #f5f7f4;
  --muted: #9eabb2;
  --teal: #36d1b1;
  --yellow: #f2bd4c;
  --red: #f56b6b;
  --blue: #4e9dff;
  --green: #36ac72;
  --violet: #a874ff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  overscroll-behavior: none;
}

body {
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

button { font: inherit; }
button:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }

.app-shell {
  min-height: 100svh;
  background:
    radial-gradient(circle at 50% -15%, rgba(54, 209, 177, .09), transparent 32rem),
    linear-gradient(rgba(255,255,255,.014) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.014) 1px, transparent 1px),
    var(--bg);
  background-size: auto, 36px 36px, 36px 36px, auto;
}

.topbar {
  height: calc(62px + var(--safe-top));
  padding: var(--safe-top) max(18px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(255,255,255,.07);
  background: rgba(13, 19, 26, .88);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 18px; }
.brand-mark { position: relative; width: 24px; height: 24px; border-radius: 50%; background: var(--teal); display: block; }
.brand-mark::before, .brand-mark::after, .brand-mark i::before, .brand-mark i::after {
  content: ""; position: absolute; inset: 10px -5px; height: 4px; background: var(--teal); border-radius: 2px;
}
.brand-mark::after { transform: rotate(45deg); }
.brand-mark i::before { transform: rotate(90deg); }
.brand-mark i::after { transform: rotate(135deg); }
.brand-mark i { position: absolute; width: 6px; height: 6px; left: 6px; top: 5px; border-radius: 50%; background: #d9fff7; z-index: 2; }

.header-actions { display: flex; gap: 8px; }
.player-button {
  min-width: 62px; max-width: 112px; height: 42px; padding: 0 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  border: 1px solid var(--line); border-radius: 7px; background: var(--surface); color: var(--teal); font-size: 12px; font-weight: 800; cursor: pointer;
}
.icon-button {
  width: 42px; height: 42px; padding: 0; border: 1px solid var(--line); border-radius: 7px;
  background: var(--surface); color: var(--ink); font-size: 22px; font-weight: 700; cursor: pointer;
}
.icon-button[aria-pressed="false"] { color: #65737d; }

main { padding: 18px max(14px, env(safe-area-inset-right)) 28px max(14px, env(safe-area-inset-left)); }
.game { width: min(100%, 720px); margin: 0 auto; }

.site-footer {
  padding: 4px max(18px, env(safe-area-inset-right)) calc(22px + var(--safe-bottom)) max(18px, env(safe-area-inset-left));
  text-align: center;
}
.site-footer a {
  color: var(--muted); font-size: 12px; line-height: 1.8; text-decoration: none;
}
.site-footer a:hover { color: var(--ink); text-decoration: underline; }
.site-footer a:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }

.difficulty {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px;
  width: min(100%, 420px); margin: 0 auto 16px; border: 1px solid var(--line); border-radius: 8px; background: #111820;
}
.difficulty button {
  height: 40px; border: 0; border-radius: 5px; background: transparent; color: var(--muted); font-weight: 700; cursor: pointer;
}
.difficulty button[aria-pressed="true"] { color: #09231e; background: var(--teal); }

.play-limit {
  width: fit-content; min-height: 30px; margin: -8px auto 10px; padding: 0 10px;
  display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px;
}
.play-limit strong { color: var(--yellow); font: 800 15px/1 ui-monospace, "SFMono-Regular", Menlo, monospace; }
.game.is-resting .play-limit { opacity: .55; }

.scorebar {
  height: 76px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 0 12px; border: 1px solid var(--line); border-bottom: 0; border-radius: 8px 8px 0 0; background: var(--surface);
}
.metric { display: flex; flex-direction: column; gap: 2px; }
.metric-right { text-align: right; }
.metric-label { color: var(--muted); font-size: 12px; }
.metric strong { color: var(--red); font: 800 28px/1 ui-monospace, "SFMono-Regular", Menlo, monospace; }
.metric-right strong { color: var(--teal); }
.metric-right strong { font-size: 24px; white-space: nowrap; }
.round-controls { display: flex; align-items: center; gap: 4px; margin-inline: 8px; }
.status-button {
  width: 38px; height: 42px; padding: 0; border: 1px solid #435460; border-radius: 7px;
  background: #18232d; color: var(--ink); font-size: 20px; font-weight: 800; cursor: pointer;
}
.status-button:disabled { color: #53616a; border-color: #2d3a44; cursor: default; }
.status-button.active { color: #10211e; background: var(--teal); border-color: var(--teal); }
.face-button {
  width: 48px; height: 48px; margin: auto; border: 1px solid #536572; border-radius: 7px;
  background: var(--surface-2); color: var(--yellow); cursor: pointer; box-shadow: inset 0 2px rgba(255,255,255,.06);
}
#faceIcon { position: relative; display: block; font-size: 22px; line-height: 1; }
#faceIcon::before, #faceIcon::after { content: ""; position: absolute; top: 4px; width: 4px; height: 4px; border-radius: 50%; background: #111820; }
#faceIcon::before { left: 13px; } #faceIcon::after { right: 13px; }

.board-wrap {
  position: relative; width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 0 0 8px 8px;
  background: #0a1016; overflow: hidden;
}
.board {
  --cols: 9; display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: 3px; width: 100%; margin: auto;
  touch-action: manipulation;
}
.game.is-paused .board, .game.is-reviewing .board, .game.is-resting .board { pointer-events: none; }
.cell {
  position: relative; min-width: 0; padding: 0; aspect-ratio: 1; border: 1px solid #556976; border-radius: min(6px, 14%);
  background: linear-gradient(180deg, var(--closed-top), var(--closed) 20%); color: #172029; cursor: pointer;
  font-weight: 900; font-size: clamp(11px, calc(64vw / var(--cols)), 30px); line-height: 1;
  display: grid; place-items: center; overflow: hidden; transform: translateZ(0);
  transition: transform 90ms ease, background 120ms ease;
}
.cell::before { content: ""; position: absolute; inset: 2px 3px auto; height: 2px; border-radius: 3px; background: rgba(255,255,255,.12); }
.cell.pressed { transform: scale(.91); background: #273a47; }
.cell.open { border-color: #cbd3cf; background: var(--open); }
.cell.open::before { display: none; }
.cell.flagged::after { content: ""; position: absolute; width: 46%; height: 42%; top: 21%; left: 28%; background: var(--red); clip-path: polygon(0 0, 100% 34%, 0 68%); }
.cell.flagged::before { display: block; width: 8%; height: 55%; left: 27%; top: 20%; background: #f5f7f4; box-shadow: 10px 14px 0 -2px #f5f7f4; transform: none; }
.cell.mine { background: #f3dddd; border-color: var(--red); }
.cell.mine::after, .result-symbol.lose::after {
  content: ""; width: 48%; aspect-ratio: 1; border-radius: 50%; background: var(--red);
  box-shadow: 0 -.36em 0 -.27em var(--red), 0 .36em 0 -.27em var(--red), .36em 0 0 -.27em var(--red), -.36em 0 0 -.27em var(--red), .25em .25em 0 -.27em var(--red), -.25em -.25em 0 -.27em var(--red), .25em -.25em 0 -.27em var(--red), -.25em .25em 0 -.27em var(--red);
}
.cell.exploded { animation: explode .48s ease both; }
.cell.wrong::after { content: "×"; position: absolute; color: var(--red); font-size: 1.4em; }
.cell.n1 { color: var(--blue); } .cell.n2 { color: var(--green); } .cell.n3 { color: var(--red); }
.cell.n4 { color: var(--violet); } .cell.n5, .cell.n6, .cell.n7, .cell.n8 { color: #93452f; }
.cell.just-opened { animation: reveal .22s ease both; }

.result, .pause-overlay, .rest-overlay {
  position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; background: rgba(9, 15, 21, .88); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.result[hidden], .pause-overlay[hidden], .rest-overlay[hidden] { display: none; }
.result-symbol { width: 62px; height: 62px; display: grid; place-items: center; border-radius: 50%; background: var(--teal); color: #08231d; font-size: 34px; font-weight: 900; }
.result-symbol.lose { position: relative; background: #3a2427; color: transparent; }
.result h2 { margin: 18px 0 6px; font-size: 30px; letter-spacing: 0; }
.result p { margin: 0 0 22px; color: var(--muted); }
.result button, .pause-overlay button, .primary-button {
  min-height: 48px; padding: 0 24px; border: 0; border-radius: 7px; background: var(--teal); color: #08231d; font-weight: 800; cursor: pointer;
}
.result-actions { display: flex; gap: 10px; }
.result .secondary-button { background: #26343f; color: var(--ink); box-shadow: inset 0 0 0 1px #4a5b67; }
.pause-symbol { width: 62px; height: 62px; display: grid; place-items: center; border: 1px solid #4e625f; border-radius: 50%; color: var(--teal); font-size: 26px; font-weight: 900; }
.pause-overlay h2 { margin: 18px 0 6px; font-size: 28px; }
.pause-overlay p { margin: 0 0 22px; color: var(--muted); }
.rest-overlay { z-index: 7; text-align: center; background: rgba(9, 15, 21, .96); }
.rest-symbol {
  width: 62px; height: 62px; display: grid; place-items: center; border: 1px solid #c89d45;
  border-radius: 50%; background: #2b2519; color: var(--yellow); font-size: 25px; font-weight: 900;
}
.rest-overlay h2 { margin: 18px 0 6px; font-size: 28px; }
.rest-overlay p { max-width: 320px; margin: 0 0 16px; color: var(--muted); line-height: 1.6; }
.rest-overlay strong { color: var(--yellow); font: 800 22px/1.2 ui-monospace, "SFMono-Regular", Menlo, monospace; }
.game.is-resting .difficulty, .game.is-resting .scorebar, .game.is-resting .control-dock { pointer-events: none; }

.review-dock {
  margin-top: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.review-dock[hidden] { display: none; }
.review-meta { display: flex; flex-direction: column; gap: 3px; min-width: 84px; }
.review-meta strong { font-size: 13px; }
.review-meta span { color: var(--muted); font-size: 11px; }
.review-controls { display: flex; align-items: center; gap: 6px; }
.review-controls button {
  min-width: 40px; height: 40px; padding: 0 10px; border: 1px solid #42525e; border-radius: 6px;
  background: var(--surface-2); color: var(--ink); font-weight: 800; cursor: pointer;
}
.review-controls button:disabled { color: #58656e; }
.review-controls .review-exit { color: var(--teal); }
.game.is-reviewing .control-dock { display: none; }

.control-dock { margin-top: 16px; }
.mode-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.mode-switch button {
  height: 50px; display: flex; align-items: center; justify-content: center; gap: 9px; border: 0; border-radius: 5px;
  background: transparent; color: var(--muted); font-weight: 800; cursor: pointer;
}
.mode-switch button[aria-pressed="true"] { background: var(--surface-2); color: var(--ink); box-shadow: inset 0 0 0 1px #40515d; }
.mode-switch button[data-mode="reveal"][aria-pressed="true"] .mode-icon { background: var(--teal); }
.mode-switch button[data-mode="flag"][aria-pressed="true"] .mode-icon { background: var(--red); }
.mode-icon { width: 17px; height: 17px; display: inline-block; background: #6e7d86; }
.reveal-icon { border-radius: 4px; box-shadow: inset 0 0 0 4px #1b2732; }
.flag-icon { clip-path: polygon(8% 0, 100% 28%, 25% 56%, 25% 100%, 8% 100%); }
#touchHint { margin: 9px 0 0; color: var(--muted); text-align: center; font-size: 12px; }
.best-line { display: flex; justify-content: center; gap: 9px; margin-top: 18px; color: var(--muted); font-size: 13px; }
.best-line strong { color: var(--yellow); }
.leaderboard { width: min(100%, 520px); margin: 18px auto 0; padding-top: 16px; border-top: 1px solid var(--line); }
.leaderboard-head { min-height: 28px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.leaderboard-head h2 { margin: 0; font-size: 15px; letter-spacing: 0; }
.leaderboard-head span { color: var(--muted); font-size: 11px; white-space: nowrap; }
.leaderboard-head span[data-state="ready"] { color: var(--teal); }
.leaderboard-head span[data-state="error"] { color: var(--yellow); }
.leaderboard-list { margin: 8px 0 0; padding: 0; list-style: none; }
.leaderboard-list li {
  min-height: 34px; display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 8px;
  border-bottom: 1px solid rgba(255,255,255,.055); color: var(--muted); font-size: 12px;
}
.leaderboard-list li:last-child { border-bottom: 0; }
.leaderboard-list li > span:nth-child(2) { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
.leaderboard-list .rank { width: 22px; height: 22px; display: grid; place-items: center; border: 1px solid #43535e; border-radius: 50%; color: var(--ink); font-weight: 800; }
.leaderboard-list strong { color: var(--teal); font: 800 14px/1 ui-monospace, "SFMono-Regular", Menlo, monospace; }

dialog {
  width: min(calc(100% - 28px), 520px); padding: 0; border: 1px solid #42525e; border-radius: 8px;
  background: var(--surface); color: var(--ink); box-shadow: 0 30px 100px rgba(0,0,0,.55);
}
dialog::backdrop { background: rgba(5, 9, 13, .78); backdrop-filter: blur(4px); }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; padding: 24px 24px 18px; border-bottom: 1px solid var(--line); }
.eyebrow { color: var(--teal); font-size: 12px; font-weight: 800; }
.dialog-head h2 { margin: 4px 0 0; font-size: 26px; }
.close-button { flex: 0 0 auto; }
.rules { padding: 8px 24px; }
.rule { display: grid; grid-template-columns: 40px 1fr; gap: 14px; padding: 16px 0; border-bottom: 1px solid var(--line); }
.rule:last-child { border-bottom: 0; }
.rule-number { width: 34px; height: 34px; display: grid; place-items: center; border: 1px solid #3a786c; border-radius: 50%; color: var(--teal); font-weight: 900; }
.rule strong { font-size: 16px; }
.rule p { margin: 4px 0 0; color: var(--muted); font-size: 13px; line-height: 1.6; }
.primary-button { width: calc(100% - 48px); margin: 8px 24px 24px; }
.player-form { padding: 22px 24px 24px; }
.player-form label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 12px; font-weight: 700; }
.player-form select, .player-form input {
  width: 100%; height: 46px; padding: 0 12px; border: 1px solid #435460; border-radius: 6px;
  background: #111820; color: var(--ink); font: inherit; outline: none;
}
.player-form select:focus, .player-form input:focus { border-color: var(--teal); box-shadow: 0 0 0 2px rgba(54, 209, 177, .16); }
.form-separator { height: 34px; display: flex; align-items: center; gap: 10px; color: #64727b; font-size: 11px; }
.form-separator::before, .form-separator::after { content: ""; height: 1px; flex: 1; background: var(--line); }
.form-error { margin: 10px 0 0; color: var(--red); font-size: 12px; line-height: 1.5; }
.player-submit { width: 100%; margin: 18px 0 0; }
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); z-index: 10; transform: translate(-50%, 20px); padding: 10px 16px; border-radius: 6px; background: #f4f6f3; color: #101820; font-weight: 700; font-size: 13px; opacity: 0; pointer-events: none; transition: .2s ease; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@keyframes reveal { from { transform: scale(.72); opacity: .4; } to { transform: scale(1); opacity: 1; } }
@keyframes explode { 35% { transform: scale(1.18); } 100% { transform: scale(1); } }

@media (min-width: 740px) {
  main { padding-top: 28px; }
  .game { width: min(78vw, 760px); }
  .scorebar { height: 88px; padding-inline: 28px; }
  .metric-label { font-size: 13px; }
  .metric strong { font-size: 34px; }
  .metric-right strong { font-size: 30px; }
  .board-wrap { padding: 14px; }
  .board { gap: 4px; }
  .control-dock { width: min(100%, 520px); margin: 18px auto 0; }
}

@media (max-width: 390px) {
  .brand { font-size: 16px; }
  .header-actions { gap: 5px; }
  .player-button { min-width: 54px; max-width: 78px; padding-inline: 8px; }
  .scorebar { grid-template-columns: minmax(56px, 1fr) auto minmax(72px, 1fr); padding-inline: 8px; }
  .round-controls { gap: 3px; margin-inline: 4px; }
  .status-button { width: 32px; height: 38px; font-size: 17px; }
  .face-button { width: 42px; height: 42px; }
  .metric strong { font-size: 24px; }
  .metric-right strong { font-size: 14px; }
}

@media (max-width: 520px) {
  .review-dock { flex-direction: column; align-items: stretch; }
  .review-meta { flex-direction: row; align-items: center; justify-content: space-between; }
  .review-controls { display: grid; grid-template-columns: repeat(5, 1fr); }
  .review-controls button { min-width: 0; }
  .review-controls .review-exit { grid-column: 1 / -1; }
}

@media (orientation: landscape) and (max-height: 900px) {
  .topbar { height: calc(50px + var(--safe-top)); }
  main { padding-top: 8px; padding-bottom: 10px; }
  .site-footer { padding-bottom: calc(10px + var(--safe-bottom)); }
  .game { width: min(78vh, calc(100vw - 330px), 700px); }
  .difficulty { position: fixed; left: max(14px, env(safe-area-inset-left)); top: calc(72px + var(--safe-top)); width: 128px; grid-template-columns: 1fr; z-index: 4; }
  .play-limit { position: fixed; left: max(14px, env(safe-area-inset-left)); top: calc(226px + var(--safe-top)); width: 128px; justify-content: center; margin: 0; }
  .scorebar { height: 56px; }
  .metric strong { font-size: 22px; }
  .face-button { width: 38px; height: 38px; }
  .status-button { width: 32px; height: 36px; font-size: 17px; }
  .round-controls { gap: 3px; margin-inline: 4px; }
  .control-dock { position: fixed; right: max(14px, env(safe-area-inset-right)); top: calc(72px + var(--safe-top)); width: 128px; margin: 0; }
  .mode-switch { grid-template-columns: 1fr; }
  .mode-switch button { height: 46px; }
  #touchHint, .best-line, .leaderboard { display: none; }
  .board-wrap { padding: 8px; }
  .review-dock { position: fixed; right: max(14px, env(safe-area-inset-right)); top: calc(72px + var(--safe-top)); width: 180px; flex-direction: column; align-items: stretch; margin: 0; z-index: 6; }
  .review-controls { display: grid; grid-template-columns: repeat(5, 1fr); gap: 3px; }
  .review-controls button { min-width: 0; padding: 0; }
  .review-controls .review-exit { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
