@font-face {
  font-family: "Ma Shan Zheng";
  src: url("../assets/fonts/ma-shan-zheng-subset.woff2") format("woff2");
  font-display: swap;
}

:root {
  --paper: #efe6d2;
  --paper-light: #f7f1e3;
  --ink: #1c1a17;
  --ink-2: rgba(28, 26, 23, 0.72);
  --ink-3: rgba(28, 26, 23, 0.45);
  --ink-line: rgba(28, 26, 23, 0.16);
  --cinnabar: #b5332a;
  --cinnabar-dark: #8f241d;
  --font-brush: "Ma Shan Zheng", "Kaiti SC", "STKaiti", "KaiTi", "BiauKai", "楷体", serif;
  --font-text: "Iowan Old Style", "Palatino Linotype", Palatino, "Songti SC", "STSong", "Noto Serif CJK SC", "Source Han Serif SC", Georgia, serif;
  --hud-h: 56px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --ease-ink: cubic-bezier(0.2, 0.7, 0.2, 1);
}

html.hc {
  --paper: #fbf8f0;
  --paper-light: #ffffff;
  --ink: #000;
  --ink-2: #000;
  --ink-3: #222;
  --ink-line: rgba(0, 0, 0, 0.5);
  --cinnabar: #c0180c;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background-color: var(--paper);
  /* Fine paper grain without an image download. */
  background-image:
    radial-gradient(ellipse at 50% 40%, rgba(255, 252, 244, 0.6), rgba(255, 252, 244, 0) 60%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .45 0 0 0 0 .37 0 0 0 0 .25 0 0 0 .09 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  color: var(--ink);
  font-family: var(--font-text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--cinnabar);
  outline-offset: 3px;
  border-radius: 4px;
}

[hidden] { display: none !important; }

/* ---------- Layout ---------- */

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + 10px) calc(var(--safe-b) + 10px) calc(var(--safe-l) + 10px);
}

#app.state-playing, #app.state-paused, #app.state-over { touch-action: none; }
#app.state-playing[data-steer="free"] #stage { cursor: crosshair; }

#stage {
  position: relative;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
}

#board {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

/* ---------- HUD ---------- */

#hud {
  flex: 0 0 var(--hud-h);
  width: 100%;
  display: grid;
  grid-template-columns: auto auto 1fr auto auto;
  align-items: center;
  gap: 4px 14px;
  padding: 0 4px;
  transition: opacity 0.4s;
}

#app.state-home #hud { visibility: hidden; opacity: 0; }

.hud-cell { display: flex; flex-direction: column; line-height: 1; min-width: 0; }
.hud-label { font-size: 11px; letter-spacing: 0.12em; color: var(--ink-3); text-transform: uppercase; }
.hud-value {
  font-family: var(--font-brush);
  font-size: 30px;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  transition: transform 0.18s var(--ease-ink);
}
.hud-best .hud-value { font-size: 22px; color: var(--ink-2); padding-top: 4px; }
.hud-value.bump { transform: scale(1.22); }

.hud-combo {
  justify-self: center;
  align-items: center;
  gap: 4px;
  opacity: 0;
  transform: translateY(4px) scale(0.9);
  transition: opacity 0.2s, transform 0.25s var(--ease-ink);
}
.hud-combo.on { opacity: 1; transform: none; }
.combo-text { font-family: var(--font-brush); font-size: 22px; color: var(--cinnabar); white-space: nowrap; }
.combo-bar { display: block; width: 56px; height: 3px; background: var(--ink-line); border-radius: 2px; overflow: hidden; }
.combo-bar i { display: block; height: 100%; width: 100%; background: var(--cinnabar); transform-origin: left; }

.hud-extra { align-items: flex-end; gap: 4px; }

.incense { display: flex; align-items: center; gap: 6px; }
.incense-stick { position: relative; width: 64px; height: 4px; background: var(--ink-line); border-radius: 2px; }
.incense-stick i { position: absolute; left: 0; top: 0; bottom: 0; background: #8b6a45; border-radius: 2px; width: 100%; }
.incense-stick .ember {
  position: absolute; top: 50%; width: 6px; height: 6px; margin: -3px 0 0 -3px; border-radius: 50%;
  background: radial-gradient(circle, #ffb36b, var(--cinnabar) 60%, rgba(181, 51, 42, 0) 70%);
  box-shadow: 0 -6px 8px rgba(120, 110, 100, 0.25);
  left: 100%;
}
.incense-time { font-family: var(--font-brush); font-size: 20px; font-variant-numeric: tabular-nums; min-width: 2ch; text-align: right; }
.incense.warn .incense-time { color: var(--cinnabar); }

.debug-chip {
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--ink-2);
  box-shadow: inset 0 0 0 1px var(--ink-line);
  white-space: nowrap;
}

.feibai-chip {
  display: flex; align-items: center; gap: 6px;
  font-family: var(--font-brush); font-size: 16px; color: var(--cinnabar);
}
.feibai-chip i { display: block; width: 40px; height: 3px; background: var(--cinnabar); border-radius: 2px; transform-origin: left; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 50%;
  transition: background-color 0.2s, transform 0.15s;
}
.icon-btn:hover { background: rgba(28, 26, 23, 0.06); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="false"] .ico-on, .icon-btn[aria-pressed="true"] .ico-off { display: none; }
.lang-btn span { font-family: var(--font-text); font-weight: 600; font-size: 15px; letter-spacing: 0.04em; }

/* ---------- Board overlays ---------- */

.tutorial {
  position: absolute;
  left: 50%;
  bottom: 8%;
  transform: translateX(-50%);
  width: max-content;
  max-width: 92%;
  padding: 8px 18px;
  text-align: center;
  pointer-events: none;
  animation: fade-in 0.6s var(--ease-ink) both;
}
.tutorial p {
  margin: 0.2em 0;
  font-family: var(--font-brush);
  font-size: clamp(17px, 4.4vmin, 24px);
  color: var(--ink-2);
  text-shadow: 0 0 6px var(--paper), 0 0 12px var(--paper);
}
.tutorial p:nth-child(2) { animation: fade-in 0.6s 0.35s var(--ease-ink) both; }
.tutorial p:nth-child(3) { animation: fade-in 0.6s 0.7s var(--ease-ink) both; }
.tutorial.out { animation: fade-out 0.5s both; }

.banner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-family: var(--font-brush);
  font-size: clamp(44px, 14vmin, 96px);
  color: var(--ink);
  opacity: 0;
  letter-spacing: 0.1em;
  text-shadow: 0 0 18px var(--paper), 0 0 36px var(--paper);
}
.banner.show { animation: banner 1.8s var(--ease-ink) both; }

/* ---------- D-pad ---------- */

.dpad {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 48px);
  grid-template-areas: ". up ." "left . right" ". down .";
  gap: 2px;
  margin-top: 6px;
}
.dpad button {
  border-radius: 14px;
  display: grid; place-items: center;
  background: rgba(28, 26, 23, 0.05);
  border: 1px solid var(--ink-line);
}
.dpad button:active { background: rgba(28, 26, 23, 0.15); }
.dpad svg { width: 26px; height: 26px; fill: none; stroke: var(--ink); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.dpad [data-dir="up"] { grid-area: up; }
.dpad [data-dir="down"] { grid-area: down; }
.dpad [data-dir="left"] { grid-area: left; }
.dpad [data-dir="right"] { grid-area: right; }

/* ---------- Screens ---------- */

.screen {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-t) + 16px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  overflow-y: auto;
}

.screen-panel {
  background: rgba(239, 230, 210, 0.72);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  animation: fade-in 0.25s both;
}
.screen-panel .panel { animation: ink-open 0.38s var(--ease-ink) both; }

.panel {
  position: relative;
  width: min(100%, 380px);
  padding: 28px 24px 22px;
  text-align: center;
  background: var(--paper-light);
  border-radius: 6px 10px 7px 12px;
  box-shadow:
    0 0 0 1px var(--ink-line),
    0 18px 40px -18px rgba(60, 45, 25, 0.45),
    inset 0 0 40px rgba(160, 130, 90, 0.08);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}
.panel h2 {
  margin: 0 0 4px;
  font-family: var(--font-brush);
  font-weight: 400;
  font-size: 32px;
  letter-spacing: 0.08em;
}
.brush-title.big { font-size: clamp(56px, 16vmin, 76px); line-height: 1; margin-top: -4px; }

/* Buttons */

.btn {
  min-height: 50px;
  padding: 10px 20px;
  font-family: var(--font-brush);
  font-size: 22px;
  letter-spacing: 0.08em;
  border-radius: 5px 9px 6px 10px;
  transition: transform 0.12s, box-shadow 0.2s, background-color 0.2s;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  color: #fbf4e6;
  background: var(--cinnabar);
  box-shadow: inset 0 0 0 2px rgba(255, 230, 210, 0.25), inset 0 -10px 18px rgba(80, 10, 5, 0.25), 0 6px 14px -8px rgba(143, 36, 29, 0.8);
}
.btn-primary:hover { background: #c03b31; }

.btn-ink {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--ink-2);
}
.btn-ink:hover { background: rgba(28, 26, 23, 0.05); }

.link-btn {
  font-size: 15px;
  padding: 10px 6px;
  color: var(--ink-2);
  letter-spacing: 0.06em;
  text-decoration: underline;
  text-decoration-color: var(--ink-line);
  text-underline-offset: 5px;
}
.link-btn:hover { color: var(--ink); text-decoration-color: var(--ink-2); }
.link-btn.danger { color: var(--cinnabar-dark); font-size: 13px; }

.row-links, .home-links {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  color: var(--ink-3);
}

/* Home */

.screen-home {
  grid-template-rows: auto 1fr auto;
  place-items: stretch;
  justify-items: center;
  background:
    linear-gradient(to bottom, rgba(239, 230, 210, 0.92) 0%, rgba(239, 230, 210, 0) 26%, rgba(239, 230, 210, 0) 64%, rgba(239, 230, 210, 0.94) 80%),
    radial-gradient(ellipse 70% 42% at 50% 50%, rgba(239, 230, 210, 0) 0%, rgba(239, 230, 210, 0.15) 60%, rgba(239, 230, 210, 0.6) 100%);
  animation: fade-in 0.6s both;
}
.home-top { grid-row: 1; width: min(100%, 560px); text-align: center; }
.home-bottom {
  grid-row: 3;
  width: min(100%, 360px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: center;
}
.home-tools { display: flex; justify-content: flex-end; gap: 2px; margin: -8px -8px 0 0; }

.logo { display: inline-flex; align-items: center; gap: 14px; margin-top: 0; }
.logo h1 {
  margin: 0;
  font-family: var(--font-brush);
  font-weight: 400;
  font-size: clamp(46px, 13vmin, 84px);
  letter-spacing: 0.06em;
  line-height: 1;
  text-shadow: 0 0 20px var(--paper), 0 0 2px var(--paper);
}
html[lang="en"] .logo h1 { font-family: var(--font-text); font-style: italic; font-weight: 600; letter-spacing: 0.01em; }
.logo-seal {
  display: inline-grid;
  place-items: center;
  width: clamp(36px, 8vmin, 50px);
  height: clamp(36px, 8vmin, 50px);
  padding-top: 1px;
  font-family: var(--font-brush);
  font-size: clamp(14px, 3.2vmin, 20px);
  line-height: 1.05;
  color: #fbf1e3;
  background: var(--cinnabar);
  border-radius: 3px 5px 3px 6px;
  box-shadow: inset 0 0 0 2px var(--cinnabar), inset 0 0 0 3.5px rgba(251, 241, 227, 0.85);
  transform: rotate(-6deg);
}
.tagline {
  margin: 12px 0 2px;
  font-family: var(--font-brush);
  font-size: clamp(18px, 4.6vmin, 26px);
  color: var(--ink-2);
  letter-spacing: 0.08em;
  text-shadow: 0 0 12px var(--paper);
}
html[lang="en"] .tagline { font-family: var(--font-text); font-style: italic; letter-spacing: 0; font-size: clamp(15px, 3.6vmin, 19px); }
.desc { margin: 0; font-size: 13px; color: var(--ink-3); letter-spacing: 0.06em; }

.btn-mode { display: flex; flex-direction: column; align-items: center; gap: 0; padding: 8px 18px; }
.btn-mode .mode-name { font-size: 26px; line-height: 1.1; }
.btn-mode .mode-sub { font-family: var(--font-text); font-size: 12.5px; letter-spacing: 0.04em; opacity: 0.85; }
.btn-mode .mode-sub b { font-family: var(--font-brush); font-weight: 400; font-size: 16px; }
.btn-ink.btn-mode { background: rgba(247, 241, 227, 0.75); }
.games-played { margin: -6px 0 0; font-size: 12px; color: var(--ink-3); min-height: 1em; }

/* Game over */

.over-reason { margin: -6px 0 0; color: var(--ink-3); font-size: 14px; letter-spacing: 0.06em; }
.over-score { position: relative; display: flex; flex-direction: column; align-items: center; margin-top: 2px; }
.over-label { font-size: 12px; letter-spacing: 0.14em; color: var(--ink-3); }
.over-value { font-family: var(--font-brush); font-size: 64px; line-height: 1; font-variant-numeric: tabular-nums; }
.new-best {
  position: absolute;
  right: 12%;
  top: 14px;
  padding: 4px 6px;
  font-family: var(--font-brush);
  font-size: 15px;
  color: #fbf1e3;
  background: var(--cinnabar);
  border-radius: 3px;
  transform: rotate(10deg);
  animation: stamp 0.45s 0.25s cubic-bezier(0.3, 1.6, 0.5, 1) both;
}
html[lang="en"] .new-best { font-family: var(--font-text); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.over-stats { display: flex; justify-content: center; gap: 36px; margin: 0; }
.over-stats div { display: flex; flex-direction: column; align-items: center; }
.over-stats dt { font-size: 12px; letter-spacing: 0.12em; color: var(--ink-3); }
.over-stats dd { margin: 0; font-family: var(--font-brush); font-size: 28px; }
.over-percentile { margin: -2px 0 2px; font-size: 13px; color: var(--ink-2); min-height: 1em; }
.btn-again { min-height: 58px; font-size: 26px; }
.kbd-hint { margin: -4px 0 0; font-size: 11.5px; color: var(--ink-3); letter-spacing: 0.04em; }
@media (hover: none) { .kbd-hint { display: none; } }
.screen-over.locked button { pointer-events: none; }

/* Settings */

.settings { display: flex; flex-direction: column; text-align: left; }
.toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 48px;
  padding: 0 2px;
  border-bottom: 1px solid var(--ink-line);
  font-size: 16px;
}
.toggle b {
  position: relative;
  width: 44px; height: 24px;
  border-radius: 12px;
  background: rgba(28, 26, 23, 0.15);
  transition: background-color 0.2s;
}
.toggle b::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--paper-light);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s var(--ease-ink);
}
.toggle[aria-pressed="true"] b { background: var(--ink); }
.toggle[aria-pressed="true"] b::after { transform: translateX(20px); }
.seg { display: inline-flex; border-radius: 6px; box-shadow: inset 0 0 0 1px var(--ink-line); overflow: hidden; }
.seg button { padding: 6px 12px; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper-light); }

/* How to */

.howto { margin: 0; padding: 0 0 0 1.1em; text-align: left; display: flex; flex-direction: column; gap: 8px; font-size: 15px; line-height: 1.55; }
.controls { margin: 0; font-size: 13px; color: var(--ink-3); line-height: 1.5; }

/* Achievements */

.ach-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 10px; text-align: left; }
.ach-list li { display: flex; gap: 10px; align-items: center; }
.ach-seal {
  flex: 0 0 auto;
  display: grid; place-items: center;
  width: 38px; height: 38px;
  font-family: var(--font-brush); font-size: 22px;
  color: #fbf1e3; background: var(--cinnabar);
  border-radius: 3px 5px 3px 6px;
  box-shadow: inset 0 0 0 2px var(--cinnabar), inset 0 0 0 3.5px rgba(251, 241, 227, 0.8);
  transform: rotate(-4deg);
}
.ach-list li.locked .ach-seal { background: transparent; color: var(--ink-3); box-shadow: inset 0 0 0 1.5px var(--ink-line); }
.ach-name { display: block; font-family: var(--font-brush); font-size: 18px; line-height: 1.1; }
.ach-desc { display: block; font-size: 12px; color: var(--ink-3); line-height: 1.35; }

/* Toast */

.toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  top: calc(var(--safe-t) + 64px);
  transform: translate(-50%, -12px);
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px 8px 10px;
  background: var(--paper-light);
  border-radius: 6px;
  box-shadow: 0 0 0 1px var(--ink-line), 0 10px 26px -12px rgba(60, 45, 25, 0.5);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.35s var(--ease-ink);
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast .ach-seal { width: 30px; height: 30px; font-size: 17px; }
.toast strong { font-family: var(--font-brush); font-weight: 400; font-size: 18px; }

.noscript { position: fixed; inset: auto 0 40% 0; text-align: center; font-size: 16px; }

/* ---------- Animations ---------- */

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes fade-out { from { opacity: 1; } to { opacity: 0; } }
@keyframes ink-open {
  from { opacity: 0; transform: scale(0.94); filter: blur(4px); }
  to { opacity: 1; transform: none; filter: none; }
}
@keyframes banner {
  0% { opacity: 0; transform: scale(1.25); filter: blur(8px); }
  22% { opacity: 0.9; transform: scale(1); filter: blur(0); }
  70% { opacity: 0.85; }
  100% { opacity: 0; transform: scale(0.98); filter: blur(3px); }
}
@keyframes stamp {
  from { opacity: 0; transform: rotate(10deg) scale(2.2); }
  to { opacity: 1; transform: rotate(10deg) scale(1); }
}

html.rm *, html.rm *::before, html.rm *::after {
  animation-duration: 0.01ms !important;
  animation-delay: 0ms !important;
  transition-duration: 0.01ms !important;
}
html.rm .banner.show { animation: fade-in-out-rm 1.6s both !important; }
@keyframes fade-in-out-rm { 0%, 100% { opacity: 0; } 15%, 75% { opacity: 0.85; } }

/* ---------- Responsive ---------- */

@media (min-width: 720px) {
  :root { --hud-h: 64px; }
  .hud-value { font-size: 34px; }
}

/* Phone landscape: HUD becomes a side column so the board keeps its height. */
@media (orientation: landscape) and (max-height: 520px) {
  #app { flex-direction: row; padding-top: calc(var(--safe-t) + 8px); }
  #hud {
    flex: 0 0 auto;
    width: 112px;
    height: 100%;
    grid-template-columns: 1fr;
    grid-auto-rows: min-content;
    align-content: center;
    gap: 14px;
  }
  .hud-combo { justify-self: start; align-items: flex-start; }
  .hud-extra { align-items: flex-start; }
  #btn-pause { order: -1; }
  .dpad { align-self: center; margin: 0 0 0 8px; }
  .screen-home { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; align-items: center; gap: 20px; background: rgba(239, 230, 210, 0.8); }
  #hud { width: 112px !important; }
  .home-top, .home-bottom { grid-row: 1; }
  .home-bottom { grid-column: 2; }
  .logo { margin-top: 0; }
}

@media (max-height: 640px) and (orientation: portrait) {
  .desc { display: none; }
  .logo { margin-top: 8px; }
}
