:root {
  --paper: #f6f5f1;
  --card: #ffffff;
  --ink: #191919;
  --muted: #77746e;
  --line: #d9d7d0;
  --soft-line: #ebe9e3;
  --gold: #b9954e;
  --radius: 4px;
  --shadow: 0 24px 60px rgba(28, 27, 24, .08);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", sans-serif;
  color: var(--ink);
  background: var(--paper);
  font-synthesis: none;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--paper); }
body { margin: 0; min-height: 100%; background: var(--paper); overflow-x: hidden; }
button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, label:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.app-shell { min-height: 100dvh; }
.topbar {
  height: 72px; padding: 0 clamp(18px, 4vw, 64px); display: flex; align-items: center;
  justify-content: space-between; border-bottom: 1px solid rgba(25,25,25,.08);
  background: rgba(246,245,241,.92); backdrop-filter: blur(14px); position: sticky; top: 0; z-index: 30;
}

.brand { border: 0; background: transparent; padding: 0; display: inline-flex; gap: 11px; align-items: center; cursor: pointer; font-weight: 760; letter-spacing: -.035em; font-size: 21px; }
.brand-mark { width: 25px; height: 25px; display: grid; grid-template-columns: 1fr 1fr; gap: 2px; transform: rotate(2deg); }
.brand-mark i { display: block; background: var(--ink); }
.brand-mark i:nth-child(1) { border-radius: 1px 4px 1px 1px; }
.brand-mark i:nth-child(2) { border-radius: 4px 1px 1px 1px; transform: translateY(-1px); }
.brand-mark i:nth-child(3) { border-radius: 1px 1px 1px 4px; transform: translateX(-1px); }
.brand-mark i:nth-child(4) { border-radius: 1px 1px 4px 1px; }
.top-actions { display: flex; align-items: center; gap: 7px; }
.icon-button, .toolbar-button {
  border: 1px solid transparent; background: transparent; min-width: 42px; height: 42px;
  display: inline-flex; align-items: center; justify-content: center; border-radius: 999px; cursor: pointer;
}
.icon-button:hover, .toolbar-button:hover { background: rgba(0,0,0,.045); }
.icon-button svg, .toolbar-button svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-button[aria-pressed="false"] { opacity: .4; }

.screen { width: min(1180px, calc(100% - 36px)); margin: 0 auto; }
.welcome-screen { padding: clamp(46px, 7vw, 98px) 0 90px; display: grid; grid-template-columns: minmax(290px, .82fr) minmax(430px, 1.18fr); gap: clamp(36px, 8vw, 120px); align-items: start; }
.intro-copy { position: sticky; top: 140px; padding-top: 26px; }
.eyebrow { margin: 0 0 18px; color: #77746e; font-size: 11px; letter-spacing: .2em; font-weight: 760; }
.intro-copy h1 { margin: 0; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(44px, 6vw, 78px); font-weight: 400; line-height: 1.06; letter-spacing: -.055em; }
.intro-copy h1 em { color: #7f7767; font-style: italic; }
.intro-copy > p:last-child { color: var(--muted); line-height: 1.85; max-width: 440px; margin: 30px 0 0; font-size: 15px; }

.setup-card { background: var(--card); border: 1px solid rgba(25,25,25,.08); box-shadow: var(--shadow); padding: clamp(24px, 4vw, 44px); margin-bottom: 24px; }
.step-heading { display: flex; gap: 18px; align-items: flex-start; margin-bottom: 28px; }
.step-index { font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--gold); padding-top: 7px; }
.step-heading h2 { margin: 0 0 7px; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(24px, 3vw, 32px); font-weight: 400; letter-spacing: -.03em; }
.step-heading p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.6; }

.upload-zone {
  min-height: 290px; border: 1px dashed #aaa69d; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; gap: 8px; cursor: pointer;
  background-color: #fbfbf9; background-image: radial-gradient(#dedbd4 .65px, transparent .65px);
  background-size: 12px 12px; transition: border .2s, transform .2s, background-color .2s;
}
.upload-zone:hover, .upload-zone.dragover { border-color: var(--ink); background-color: #fff; transform: translateY(-2px); }
.upload-icon { width: 70px; height: 70px; border-radius: 50%; display: grid; place-items: center; background: var(--ink); color: #fff; margin-bottom: 9px; }
.upload-icon svg { width: 42px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.upload-zone strong { font-size: 15px; font-weight: 680; }
.upload-zone small { font-size: 12px; color: var(--muted); }
.sample-row { margin-top: 17px; display: flex; align-items: center; justify-content: center; gap: 6px; color: var(--muted); font-size: 12px; }
.text-button { padding: 4px 1px; border: 0; border-bottom: 1px solid var(--ink); background: transparent; cursor: pointer; font-weight: 650; }

.recent-section { margin-top: 26px; padding-top: 22px; border-top: 1px solid var(--soft-line); }
.recent-section > p { margin: 0 0 10px; font-size: 11px; letter-spacing: .12em; color: var(--muted); }
.recent-list { display: flex; gap: 10px; overflow: auto; padding: 2px; }
.recent-thumb { padding: 0; border: 1px solid var(--soft-line); background: #fff; width: 88px; aspect-ratio: 4/3; cursor: pointer; flex: 0 0 auto; overflow: hidden; }
.recent-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.config-card { animation: reveal .45s ease both; }
@keyframes reveal { from { opacity: 0; transform: translateY(18px); } }
.art-preview-wrap { background: #f1f0ec; padding: 22px 22px 15px; position: relative; box-shadow: inset 0 0 0 1px rgba(0,0,0,.05); }
.art-preview-wrap canvas { display: block; width: 100%; max-height: 320px; object-fit: contain; background: #ddd; box-shadow: 0 13px 35px rgba(0,0,0,.13); }
.art-label { display: flex; justify-content: space-between; gap: 20px; margin-top: 13px; font: 650 9px/1.3 ui-monospace, monospace; letter-spacing: .12em; color: #5f5d58; }
.art-label small { font-size: inherit; }
.change-image { position: absolute; top: 30px; right: 30px; background: rgba(255,255,255,.91); border: 0; padding: 8px 12px; font-size: 11px; cursor: pointer; box-shadow: 0 3px 15px rgba(0,0,0,.12); }

.difficulty-picker { border: 0; padding: 0; margin: 28px 0 22px; display: grid; gap: 9px; }
.difficulty-picker legend { font-size: 11px; font-weight: 760; letter-spacing: .12em; color: var(--muted); margin-bottom: 11px; }
.difficulty-card { border: 1px solid var(--soft-line); padding: 15px 16px; display: grid; grid-template-columns: 50px 1fr 18px; align-items: center; gap: 15px; cursor: pointer; transition: .18s ease; position: relative; }
.difficulty-card:hover { border-color: #a5a197; }
.difficulty-card.selected { border-color: var(--ink); box-shadow: inset 3px 0 0 var(--ink); }
.difficulty-card input { position: absolute; opacity: 0; pointer-events: none; }
.difficulty-card > span:nth-of-type(2) { display: flex; flex-direction: column; gap: 4px; }
.difficulty-card strong { font-size: 14px; }
.difficulty-card small { color: var(--muted); font-size: 11px; }
.difficulty-card > i { width: 15px; height: 15px; border: 1px solid #aaa59b; border-radius: 50%; position: relative; }
.difficulty-card.selected > i::after { content: ""; position: absolute; inset: 3px; background: var(--ink); border-radius: 50%; }
.difficulty-grid { width: 48px; height: 38px; border: 1px solid #aaa69d; background-color: #fafafa; }
.grid-5 { background-image: linear-gradient(90deg, transparent 19%, #d3d0c8 20%, transparent 21%, transparent 39%, #d3d0c8 40%, transparent 41%, transparent 59%, #d3d0c8 60%, transparent 61%, transparent 79%, #d3d0c8 80%, transparent 81%), linear-gradient(transparent 19%, #d3d0c8 20%, transparent 21%, transparent 39%, #d3d0c8 40%, transparent 41%, transparent 59%, #d3d0c8 60%, transparent 61%, transparent 79%, #d3d0c8 80%, transparent 81%); }
.grid-10 { background-image: linear-gradient(90deg, transparent 9%, #d3d0c8 10%, transparent 11%, transparent 19%, #d3d0c8 20%, transparent 21%, transparent 29%, #d3d0c8 30%, transparent 31%, transparent 39%, #d3d0c8 40%, transparent 41%, transparent 49%, #d3d0c8 50%, transparent 51%, transparent 59%, #d3d0c8 60%, transparent 61%, transparent 69%, #d3d0c8 70%, transparent 71%, transparent 79%, #d3d0c8 80%, transparent 81%, transparent 89%, #d3d0c8 90%, transparent 91%), linear-gradient(transparent 9%, #d3d0c8 10%, transparent 11%, transparent 19%, #d3d0c8 20%, transparent 21%, transparent 29%, #d3d0c8 30%, transparent 31%, transparent 39%, #d3d0c8 40%, transparent 41%, transparent 49%, #d3d0c8 50%, transparent 51%, transparent 59%, #d3d0c8 60%, transparent 61%, transparent 69%, #d3d0c8 70%, transparent 71%, transparent 79%, #d3d0c8 80%, transparent 81%, transparent 89%, #d3d0c8 90%, transparent 91%); }
.grid-20 { background-image: linear-gradient(90deg, rgba(0,0,0,.12) 1px, transparent 1px), linear-gradient(rgba(0,0,0,.12) 1px, transparent 1px); background-size: 5px 5px; }

.primary-button, .secondary-button { min-height: 52px; border: 1px solid var(--ink); padding: 0 20px; display: inline-flex; gap: 12px; align-items: center; justify-content: center; cursor: pointer; font-weight: 700; font-size: 13px; letter-spacing: .02em; }
.primary-button { background: var(--ink); color: #fff; }
.primary-button:hover { background: #393939; }
.secondary-button { background: #fff; color: var(--ink); }
.primary-button svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.config-card > .primary-button { width: 100%; justify-content: space-between; padding: 0 24px; }

.game-screen { width: 100%; max-width: none; margin: 0; padding: 0 clamp(12px, 2.5vw, 36px) 20px; }
.game-toolbar { height: 78px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 20px; }
.toolbar-button { border-radius: 2px; min-width: auto; gap: 8px; padding: 0 10px; font-size: 12px; }
.toolbar-actions { display: flex; justify-content: flex-end; align-items: center; gap: 5px; }
.hint-button { border: 1px solid var(--line); padding: 0 13px; background: #fff; }
.hint-button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.stat-group { display: flex; align-items: center; justify-content: center; gap: clamp(20px, 5vw, 72px); }
.stat { text-align: center; min-width: 58px; }
.stat small { display: block; color: var(--muted); font-size: 9px; letter-spacing: .14em; margin-bottom: 4px; }
.stat strong { font: 650 18px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .03em; }
.progress-stat strong { color: #7c6437; }

.stage-wrap { width: 100%; height: clamp(560px, calc(100dvh - 190px), 850px); min-height: 520px; position: relative; background: #eceae4; border: 1px solid #dfdcd4; overflow: hidden; touch-action: none; }
#gameCanvas { width: 100%; height: 100%; display: block; touch-action: none; }
.tray-label { position: absolute; left: 16px; bottom: 12px; display: flex; flex-direction: column; gap: 3px; pointer-events: none; color: rgba(25,25,25,.55); }
.tray-label span { font: 720 9px/1 ui-monospace, monospace; letter-spacing: .15em; }
.tray-label small { font-size: 10px; }
.tray-pager { position: absolute; right: 13px; bottom: 11px; display: flex; gap: 5px; }
.tray-pager button { width: 39px; height: 34px; border: 1px solid rgba(0,0,0,.18); background: rgba(255,255,255,.86); backdrop-filter: blur(8px); cursor: pointer; }
.tray-pager button:disabled { opacity: .28; cursor: default; }
.game-tip { text-align: center; margin: 10px 0 0; color: var(--muted); font-size: 11px; }

.overlay { position: fixed; inset: 0; z-index: 90; background: rgba(244,243,239,.88); backdrop-filter: blur(14px); display: grid; place-items: center; padding: 20px; }
.processing-card { width: min(430px, 100%); text-align: center; }
.processing-card h2 { font-family: Georgia, "Noto Serif TC", serif; font-weight: 400; font-size: 27px; margin: 0 0 28px; }
.processing-card > p:last-child { color: var(--muted); font: 650 12px/1 ui-monospace, monospace; }
.progress-track { height: 2px; background: #d7d4cc; overflow: hidden; }
.progress-track i { display: block; width: 0%; height: 100%; background: var(--ink); transition: width .18s ease; }
.slice-animation { width: 86px; height: 86px; margin: 0 auto 28px; display: grid; grid-template-columns: 1fr 1fr; gap: 3px; transform: rotate(3deg); }
.slice-animation span { background: #fff; border: 1px solid #bdb9af; box-shadow: 2px 4px 7px rgba(0,0,0,.1); animation: slice 1.8s ease-in-out infinite; }
.slice-animation span:nth-child(2) { animation-delay: -.35s; }
.slice-animation span:nth-child(3) { animation-delay: -.7s; }
.slice-animation span:nth-child(4) { animation-delay: -1.05s; }
@keyframes slice { 50% { transform: translate(4px,-5px) rotate(3deg); box-shadow: 6px 10px 13px rgba(0,0,0,.14); } }

.result-card, .help-card { position: relative; background: #fff; border: 1px solid rgba(0,0,0,.09); box-shadow: var(--shadow); }
.result-card { width: min(670px, 100%); max-height: calc(100dvh - 30px); overflow: auto; padding: clamp(26px, 5vw, 48px); text-align: center; }
.result-card h2, .help-card h2 { margin: 0 0 24px; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(28px, 5vw, 42px); font-weight: 400; letter-spacing: -.035em; }
.modal-close { position: absolute; right: 13px; top: 10px; width: 40px; height: 40px; border: 0; background: transparent; font-size: 28px; color: #777; cursor: pointer; }
.result-art { padding: 15px; background: #f2f1ed; box-shadow: inset 0 0 0 1px rgba(0,0,0,.04); }
.result-art canvas { display: block; width: 100%; max-height: 350px; object-fit: contain; box-shadow: 0 13px 30px rgba(0,0,0,.14); background: #fff; }
.result-stats { display: grid; grid-template-columns: 1fr 1fr; margin: 24px 0 18px; border-top: 1px solid var(--soft-line); border-bottom: 1px solid var(--soft-line); }
.result-stats > div { padding: 18px; }
.result-stats > div + div { border-left: 1px solid var(--soft-line); }
.result-stats small { color: var(--muted); font-size: 10px; display: block; margin-bottom: 6px; }
.result-stats strong { font: 650 24px/1 ui-monospace, monospace; }
.new-record { color: #8b6b2e; font-size: 10px; letter-spacing: .12em; font-weight: 740; }
.result-actions { display: grid; grid-template-columns: 1fr 1.25fr; gap: 10px; margin-top: 20px; }

.help-card { width: min(560px, 100%); padding: clamp(28px, 5vw, 48px); }
.help-card ol { list-style: none; margin: 0 0 30px; padding: 0; }
.help-card li { display: grid; grid-template-columns: 38px 1fr; gap: 14px; padding: 16px 0; border-top: 1px solid var(--soft-line); }
.help-card li:last-child { border-bottom: 1px solid var(--soft-line); }
.help-card li > span { color: var(--gold); font: 650 10px/1.5 ui-monospace, monospace; }
.help-card strong { font-size: 14px; }
.help-card li p { margin: 5px 0 0; color: var(--muted); font-size: 12px; line-height: 1.65; }
.help-card > .primary-button { width: 100%; }

.toast { position: fixed; left: 50%; bottom: max(26px, env(safe-area-inset-bottom)); z-index: 120; transform: translateX(-50%); background: #1d1d1d; color: #fff; padding: 11px 17px; box-shadow: 0 8px 28px rgba(0,0,0,.2); font-size: 12px; max-width: calc(100% - 30px); text-align: center; }
.celebration-canvas { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 100; pointer-events: none; }

@media (max-width: 860px) {
  .welcome-screen { grid-template-columns: 1fr; max-width: 640px; gap: 42px; }
  .intro-copy { position: static; padding-top: 0; }
  .intro-copy h1 { font-size: clamp(43px, 12vw, 70px); }
  .topbar { height: 64px; }
  .game-toolbar { height: 72px; grid-template-columns: auto 1fr auto; gap: 7px; }
  .toolbar-button span { display: none; }
  .stat-group { gap: clamp(12px, 4vw, 30px); }
  .stat strong { font-size: 16px; }
  .stat small { font-size: 8px; }
  .hint-button { border: 0; padding: 0 8px; }
  .game-screen { padding: 0 8px 12px; }
  .stage-wrap { height: calc(100dvh - 156px); min-height: 510px; }
  .game-tip { display: none; }
}

@media (max-width: 540px) {
  .screen { width: min(100% - 24px, 500px); }
  .welcome-screen { padding: 36px 0 60px; }
  .setup-card { padding: 22px 18px; }
  .upload-zone { min-height: 240px; }
  .step-heading { gap: 12px; }
  .art-preview-wrap { padding: 12px 12px 11px; }
  .change-image { top: 18px; right: 18px; }
  .difficulty-card { grid-template-columns: 42px 1fr 16px; padding: 13px 12px; gap: 11px; }
  .difficulty-grid { width: 40px; height: 32px; }
  .result-actions { grid-template-columns: 1fr; }
  .result-card { padding: 28px 18px; }
  .result-stats > div { padding: 15px 8px; }
  .result-stats strong { font-size: 20px; }
  .stat-group { gap: 8px; }
  .progress-stat { display: none; }
  .stat { min-width: 50px; }
}

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