@font-face {
  font-family: "Xingyi Brush";
  src: url("assets/calligraphy-title.woff") format("woff");
  font-display: swap;
}

:root {
  color-scheme: light;
  --paper: #f5f6f2;
  --ink: #252d2a;
  --muted: #667069;
  --cinnabar: #a43c31;
  --cinnabar-hover: #883027;
  --line: #d5dad2;
  --serif: "Songti SC", "STSong", "Noto Serif CJK SC", serif;
  --header-height: clamp(64px, 6.1vw, 88px);
  --page-inline: clamp(16px, 3.34vw, 48px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; letter-spacing: 0; }
html, body { margin: 0; width: 100%; min-height: 100%; }
html { min-height: 100%; background: var(--paper); }
body {
  background: var(--paper) url("assets/paper.png");
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; opacity: .45; }
button:focus-visible { outline: 2px solid var(--cinnabar); outline-offset: 5px; }
button:active:not(:disabled) { transform: translateY(1px); }
button, a { -webkit-tap-highlight-color: transparent; }
[hidden], .screen[hidden] { display: none !important; }
.screen { position: fixed; inset: var(--header-height) 0 0; z-index: 1; }

.masthead {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  padding: 0 var(--page-inline);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 14px; white-space: nowrap; }
.brand-name { font-family: var(--serif); font-size: 18px; }
.seal {
  width: 34px;
  height: 36px;
  border: 1px solid var(--cinnabar);
  color: var(--cinnabar);
  display: grid;
  place-items: center;
  padding: 2px;
  font: 14px/1.05 var(--serif);
  writing-mode: vertical-rl;
}
.journey { display: flex; align-items: center; gap: clamp(14px, 2.1vw, 30px); margin: 0; padding: 0; list-style: none; }
.journey li { display: flex; align-items: baseline; gap: 7px; color: #777f79; font-size: 12px; white-space: nowrap; }
.journey span { font-size: 10px; font-variant-numeric: tabular-nums; }
.journey [aria-current="step"] { color: var(--cinnabar); }

.splash { inset: 0; display: grid; place-items: center; padding: clamp(88px, 13vh, 120px) 24px calc(78px + var(--safe-bottom)); overflow: auto; isolation: isolate; }
.splash::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: url("assets/ink-person.webp") no-repeat 6% 70%;
  background-size: auto 90%;
  opacity: .085;
  pointer-events: none;
}
.splash-inner { max-width: 100%; text-align: center; }
h1 { font-family: "Xingyi Brush", var(--serif); font-size: 112px; line-height: 1.3; font-weight: 400; margin: 0 0 20px; }
.subtitle { font-family: var(--serif); font-size: 21px; line-height: 1.7; color: var(--muted); margin: 0 0 44px; }
.rule { width: 1px; height: 38px; background: var(--cinnabar); margin: 0 auto 24px; }
.primary {
  min-width: 188px;
  min-height: 54px;
  padding: 14px 28px;
  border: 1px solid var(--cinnabar);
  border-radius: 2px;
  background: var(--cinnabar);
  color: var(--paper);
  font-size: 16px;
  transition: background .2s, border-color .2s;
}
.primary:hover:not(:disabled), .shoot:hover:not(:disabled), #saveBtn:hover:not(:disabled) { background: var(--cinnabar-hover); border-color: var(--cinnabar-hover); }
.splash-colophon { position: absolute; left: var(--page-inline); bottom: calc(28px + var(--safe-bottom)); display: flex; align-items: center; gap: 12px; color: var(--muted); font: 12px/1.5 var(--serif); }
.splash-colophon::before { content: ""; width: 24px; height: 1px; background: #9ca69e; }
.splash-edition { position: absolute; right: var(--page-inline); bottom: calc(28px + var(--safe-bottom)); font: 12px/1.5 var(--serif); color: var(--muted); }

.camera { display: flex; align-items: center; flex-direction: column; justify-content: center; gap: 16px; padding: 16px 32px calc(28px + var(--safe-bottom)); overflow: auto; }
.camera-frame { width: min(100%, 960px, calc((100dvh - var(--header-height) - 170px) * var(--camera-ratio, 1.7778))); aspect-ratio: var(--camera-ratio, 1.7778); max-height: calc(100dvh - var(--header-height) - 170px); min-height: 150px; flex-shrink: 1; background: #222826; position: relative; overflow: hidden; }
.camera-frame::after { content: ""; position: absolute; inset: 12px; border: 1px solid #f5f6f240; pointer-events: none; }
.camera-frame video { width: 100%; height: 100%; object-fit: contain; }
.camera-frame canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.countdown { position: absolute; inset: 0; display: grid; place-items: center; font-family: var(--serif); font-size: 100px; font-variant-numeric: tabular-nums; color: var(--paper); text-shadow: 0 2px 12px #252d2a; background: #252d2a30; pointer-events: none; }
.camera-actions { display: flex; align-items: center; justify-content: center; gap: 14px; flex-shrink: 0; }
.outline, .shoot { min-height: 46px; border: 1px solid #a6afa7; background: transparent; padding: 10px 23px; border-radius: 2px; transition: border-color .2s, background .2s; }
.outline:hover:not(:disabled) { background: #e6eae3; border-color: var(--ink); }
.shoot { width: 132px; background: var(--cinnabar); border-color: var(--cinnabar); color: var(--paper); }
.status { min-height: 24px; max-width: 650px; color: var(--muted); font-size: 13px; line-height: 1.6; text-align: center; }

.loading { display: flex; align-items: center; justify-content: center; text-align: center; flex-direction: column; gap: 28px; padding: 24px 24px 100px; }
.loader { width: 56px; height: 56px; border-radius: 50%; border: 2px solid #d5dad2; border-top-color: var(--ink); border-left-color: var(--ink); animation: spin 1.5s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#loadingText { color: var(--muted); font-family: var(--serif); font-size: 20px; line-height: 1.6; }

.result { display: grid; grid-template-columns: minmax(0, 1fr) 340px; border-top: 1px solid var(--line); }
.particle-wrap { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
.particle-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.result-side { border-left: 1px solid var(--line); padding: 28px 28px 24px; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow-y: auto; background: #edf0e96b; }
.side-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding-bottom: 22px; }
.side-title { margin: 0; font: 24px/1.25 var(--serif); font-weight: 400; }
.side-count { color: var(--muted); font: 11px/1.5 -apple-system, BlinkMacSystemFont, sans-serif; }
.cards { display: flex; flex-direction: column; gap: 8px; }
.char-card { position: relative; display: grid; grid-template-columns: 60px minmax(0, 1fr); align-items: center; gap: 16px; min-height: 84px; border: 1px solid transparent; border-bottom-color: var(--line); border-radius: 2px; padding: 10px 12px; background: transparent; transition: border-color .18s, background .18s; text-align: left; }
.char-card[aria-pressed="true"] { border-color: var(--cinnabar); background: #a43c3108; }
.char-card:hover { border-color: #919c92; background: #e1e6dc66; }
.char-card[aria-pressed="true"]:hover { border-color: var(--cinnabar); }
.glyph { width: 60px; height: 60px; object-fit: contain; opacity: .87; }
.char-details { min-width: 0; }
.char { font-family: var(--serif); font-size: 24px; line-height: 1.2; }
.style { font-size: 12px; color: var(--muted); margin-left: 10px; white-space: nowrap; }
.score { font-size: 11px; font-variant-numeric: tabular-nums; color: var(--muted); margin-top: 7px; }
.char-card[aria-pressed="true"] .score { color: var(--cinnabar); }
.bar { height: 2px; background: #d8ded5; margin-top: 5px; }
.bar i { display: block; height: 100%; background: #89968a; }
.char-card[aria-pressed="true"] .bar i { background: var(--cinnabar); }
.result-status { min-height: 60px; color: var(--muted); font: 14px/1.8 var(--serif); margin: 20px 0; overflow-wrap: anywhere; }
.result-notice { margin: -12px 0 12px; color: var(--cinnabar); font-size: 12px; line-height: 1.6; overflow-wrap: anywhere; }
.result-actions { display: flex; gap: 8px; margin-top: auto; padding-top: 14px; }
.result-actions button { flex: 1 1 0; min-width: 0; display: flex; align-items: center; justify-content: center; gap: 5px; padding-inline: 4px; font-size: 14px; white-space: nowrap; }
.button-icon { width: 17px; height: 17px; flex: 0 0 17px; }
#saveBtn .button-icon { filter: invert(1); }
#saveBtn { background: var(--cinnabar); border-color: var(--cinnabar); color: var(--paper); }
#outlineBtn[aria-pressed="true"] { border-color: var(--cinnabar); color: var(--cinnabar); background: #a43c3108; }
.error { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); width: max-content; max-width: min(90vw, 700px); background: #f6eeeb; border: 1px solid #a43c3160; padding: 13px 20px; border-radius: 3px; color: #86382f; box-shadow: 0 4px 20px #252d2a0c; z-index: 20; text-align: center; font-size: 14px; line-height: 1.5; overflow-wrap: anywhere; }

@media (min-width: 1600px) {
  .splash::before { background-position: 12% 70%; }
  .result { grid-template-columns: minmax(0, 1fr) 380px; }
  .result-side { padding: 36px; }
  .char-card { min-height: 96px; }
}
@media (max-width: 760px) {
  :root { --header-height: 70px; }
  .masthead { padding: 0 max(20px, env(safe-area-inset-left)); gap: 14px; }
  .brand { gap: 9px; }
  .brand-name { font-size: 16px; }
  .seal { width: 28px; height: 32px; font-size: 12px; }
  .journey { gap: 15px; }
  .journey li { font-size: 11px; gap: 0; }
  .journey span { display: none; }
  h1 { font-size: 72px; margin-bottom: 16px; }
  .subtitle { font-size: 18px; margin-bottom: 34px; }
  .splash { padding: 100px 20px 96px; }
  .splash::before { background-size: auto 64%; background-position: 55% 55%; opacity: .065; }
  .rule { height: 28px; margin-bottom: 22px; }
  .splash-colophon { left: 20px; right: 20px; justify-content: center; bottom: 28px; font-size: 11px; }
  .splash-edition { display: none; }
  .camera { padding: 10px max(16px, env(safe-area-inset-left)) max(24px, var(--safe-bottom)) max(16px, env(safe-area-inset-right)); gap: 16px; }
  .camera-frame { width: min(100%, calc((100dvh - var(--header-height) - 190px) * var(--camera-ratio, 1.7778))); max-height: calc(100dvh - var(--header-height) - 190px); min-height: 120px; flex-shrink: 0; }
  .camera-frame::after { inset: 8px; }
  .countdown { font-size: 80px; }
  .camera-actions { gap: 10px; }
  .outline, .shoot { padding: 10px 20px; }
  .status { font-size: 12px; }
  .result { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(160px, 1fr) auto; overflow-y: auto; }
  .result-side { border-left: 0; border-top: 1px solid var(--line); padding: 16px max(16px, env(safe-area-inset-left)) max(16px, var(--safe-bottom)) max(16px, env(safe-area-inset-right)); overflow: visible; }
  .side-heading { padding-bottom: 12px; }
  .side-title { font-size: 19px; }
  .side-count { font-size: 10px; }
  .cards { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .char-card { display: flex; flex-direction: column; gap: 4px; min-height: 124px; padding: 8px 3px; text-align: center; }
  .glyph { width: 38px; height: 38px; }
  .char-details { width: 100%; }
  .char { font-size: 16px; display: block; }
  .style { display: block; font-size: 10px; margin: 3px 0 0; }
  .score { font-size: 10px; margin-top: 5px; }
  .bar { margin: 5px 4px 0; }
  .result-status { min-height: 44px; font-size: 12px; line-height: 1.7; margin: 12px 0; }
  .result-actions { gap: 8px; padding-top: 0; }
  .result-actions button { min-width: 0; padding-inline: 8px; gap: 5px; font-size: 13px; white-space: nowrap; }
  .result-actions .button-icon { width: 15px; height: 15px; flex-basis: 15px; }
}
@media (max-width: 360px) {
  .masthead { padding: 0 16px; }
  .journey { gap: 12px; }
  h1 { font-size: 60px; }
  .subtitle { font-size: 16px; }
  .result-side { padding: 12px; }
  .char-card { min-height: 112px; }
  .glyph { width: 30px; height: 30px; }
  .result-status { margin: 8px 0; }
}
@media (max-height: 640px) and (min-width: 761px) {
  :root { --header-height: 64px; }
  .result-side { padding: 16px 24px; }
  .side-heading { padding-bottom: 12px; }
  .cards { gap: 4px; }
  .char-card { min-height: 62px; grid-template-columns: 46px minmax(0, 1fr); padding: 5px 12px; }
  .glyph { width: 46px; height: 46px; }
  .result-status { margin: 10px 0; }
  .splash { padding: 80px 24px 65px; }
  h1 { font-size: 80px; margin-bottom: 12px; }
  .rule { height: 24px; margin-bottom: 16px; }
  .subtitle { margin-bottom: 24px; }
}

/* Short landscape viewports need the camera controls beside the frame and a
   compact result panel so the primary actions stay in the first viewport. */
@media (orientation: landscape) and (max-height: 640px) {
  :root { --header-height: clamp(52px, 12vh, 68px); }
  .masthead { padding-inline: max(16px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-right)); }
  .splash { padding-block: 68px calc(48px + var(--safe-bottom)); }
  h1 { font-size: clamp(64px, 11vw, 96px); margin-bottom: 10px; }
  .subtitle { font-size: clamp(16px, 2.3vw, 20px); margin-bottom: 20px; }
  .rule { height: 24px; margin-bottom: 14px; }
  .splash-colophon, .splash-edition { bottom: calc(16px + var(--safe-bottom)); }

  .camera {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(132px, 20vw);
    grid-template-rows: minmax(0, 1fr) auto;
    align-content: center;
    column-gap: clamp(12px, 2.5vw, 32px);
    row-gap: 8px;
    padding: 10px max(16px, env(safe-area-inset-left)) max(12px, var(--safe-bottom)) max(16px, env(safe-area-inset-right));
  }
  .camera-frame {
    grid-column: 1;
    grid-row: 1 / span 2;
    width: min(100%, calc((100dvh - var(--header-height) - 56px) * var(--camera-ratio, 1.7778)));
    max-height: calc(100dvh - var(--header-height) - 56px);
    min-height: 0;
  }
  .status { grid-column: 2; grid-row: 1; align-self: end; width: 100%; }
  .camera-actions { grid-column: 2; grid-row: 2; width: 100%; gap: 8px; }
  .outline, .shoot { min-height: 40px; padding: 8px 14px; }
  .shoot { width: 100%; }

  .result { grid-template-columns: minmax(0, 1fr) clamp(250px, 29vw, 340px); }
  .result-side { padding: 14px 18px max(14px, var(--safe-bottom)); }
  .side-heading { padding-bottom: 10px; }
  .side-title { font-size: 21px; }
  .cards { gap: 3px; }
  .char-card { min-height: 58px; grid-template-columns: 42px minmax(0, 1fr); gap: 10px; padding: 5px 8px; }
  .glyph { width: 42px; height: 42px; }
  .char { font-size: 20px; }
  .score { margin-top: 3px; }
  .bar { margin-top: 3px; }
  .result-status { min-height: 36px; margin: 8px 0; font-size: 12px; line-height: 1.55; }
  .result-actions { padding-top: 6px; }
}

/* A phone held sideways has too little vertical room for a two-column result.
   Keep the particle field above a five-column selection strip. */
@media (orientation: landscape) and (max-height: 640px) and (max-width: 900px) {
  .result { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(108px, 1fr) auto; overflow-y: auto; }
  .result-side { padding: 6px max(14px, env(safe-area-inset-left)) max(8px, var(--safe-bottom)) max(14px, env(safe-area-inset-right)); }
  .side-heading { padding-bottom: 4px; }
  .cards { grid-template-columns: repeat(5, minmax(0, 1fr)); display: grid; gap: 4px; }
  .char-card { display: flex; flex-direction: column; min-height: 72px; gap: 2px; padding: 4px 3px; text-align: center; }
  .glyph { width: 28px; height: 28px; }
  .char-details { width: 100%; }
  .char { font-size: 15px; display: block; }
  .style { display: block; margin: 2px 0 0; font-size: 9px; }
  .score { font-size: 9px; margin-top: 2px; }
  .bar { margin-top: 2px; }
  .result-status { min-height: 22px; margin: 4px 0; }
  .result-actions { padding-top: 2px; }
  .result-actions .outline, .result-actions #saveBtn { min-height: 38px; padding-block: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  .loader { animation: none; }
  button { transition: none; }
}
