:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #f8f5ee;
  --text: #1d1d1f;
  --muted: #6d6a64;
  --line: #e2dccf;
  --accent: #1e3482;
  --accent-soft: #e4e9f7;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --ok: #1d6b3a;
  --pad-bg: #fffefb;
  --pad-ink: #1e3482;
  --guide: #cdd6ea;
  --guide-strong: #8193c4;
  --guide-label: #9aa3b8;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 4px 16px rgba(40, 30, 10, .06);
  --radius: 14px;
  --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);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17171a;
    --surface: #222226;
    --surface-2: #1c1c20;
    --text: #f1f1f3;
    --muted: #a1a1a8;
    --line: #34343a;
    --accent: #8ea4ff;
    --accent-soft: #2a3050;
    --accent-text: #0f1530;
    --danger: #ff8a80;
    --ok: #7fd19b;
    --pad-bg: #1f1f23;
    --pad-ink: #c9d4ff;
    --guide: #3a4058;
    --guide-strong: #6878ad;
    --guide-label: #6d7590;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
h1, h2, h3 { font-family: ui-serif, "New York", Georgia, serif; font-weight: 600; margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
#app { min-height: 100%; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: calc(10px + var(--safe-t)) calc(16px + var(--safe-r)) 10px calc(16px + var(--safe-l));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 22px; }
.topbar .title { display: flex; flex-direction: column; min-width: 0; }
.topbar .title small { color: var(--muted); font-size: 13px; }
.topbar .title h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  cursor: pointer; user-select: none; -webkit-user-select: none; white-space: nowrap;
  font-weight: 500; text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.soft { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.icon-btn { min-width: 44px; padding: 0 10px; }
.btn svg { width: 20px; height: 20px; flex: none; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

main { padding: 20px calc(16px + var(--safe-r)) calc(40px + var(--safe-b)) calc(16px + var(--safe-l)); max-width: 1200px; margin: 0 auto; }

/* ---------- Library ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column;
}
.card .preview { height: 110px; background: var(--pad-bg); border-bottom: 1px solid var(--line); position: relative; }
.card .preview canvas { width: 100%; height: 100%; display: block; }
.card .preview .placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 14px; }
.card .body { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.card h2 { font-size: 20px; }
.meta { color: var(--muted); font-size: 14px; }
.progress { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); border-radius: 3px; }

.intro {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; box-shadow: var(--shadow); max-width: 720px; margin: 10px auto 24px;
}
.intro ol { margin: 10px 0 0; padding-left: 22px; }
.intro li { margin: 6px 0; }
.banner {
  display: flex; gap: 10px; align-items: center; background: var(--accent-soft); color: var(--text);
  border-radius: 12px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px;
}

/* ---------- Glyph grid ---------- */
.section-title { display: flex; align-items: baseline; gap: 10px; margin: 26px 0 10px; }
.section-title h3 { font-size: 18px; }
.glyph-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 10px; }
.cell {
  position: relative; aspect-ratio: 1; border-radius: 12px; background: var(--surface);
  border: 1px solid var(--line); cursor: pointer; overflow: hidden; padding: 0;
}
.cell canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.cell .lbl { position: absolute; left: 7px; top: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.cell .cnt {
  position: absolute; right: 6px; bottom: 6px; font-size: 11px; font-weight: 700; color: var(--accent);
  background: var(--accent-soft); border-radius: 8px; padding: 1px 6px;
}
.cell.empty .lbl { font-size: 26px; left: 0; right: 0; top: 50%; transform: translateY(-50%); text-align: center; color: var(--guide-strong); font-weight: 400; font-family: ui-serif, Georgia, serif; }
.cell.auto { border-style: dashed; }
.cell.auto .cnt { background: transparent; color: var(--muted); }

/* ---------- Draw view ---------- */
.draw-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 18px; align-items: start; }
@media (max-width: 900px), (orientation: portrait) { .draw-layout { grid-template-columns: 1fr; } }
.pad-wrap {
  background: var(--pad-bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; position: relative; margin: 0 auto;
  --pad-scale: 1;
  width: calc(100% * var(--pad-scale)); min-width: 120px;
  max-width: calc((100vh - 260px) * 1600 / 1420 * var(--pad-scale));
  container-type: inline-size;
}
.pad {
  display: block; width: 100%; aspect-ratio: 1600 / 1420; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: crosshair;
}
.pad-big-char {
  position: absolute; right: 2.5cqw; top: 1cqw; font: 600 clamp(14px, 4cqw, 28px) ui-serif, Georgia, serif; color: var(--guide-strong);
  pointer-events: none;
}
.pad-tools { display: flex; gap: 8px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.pad-size { display: flex; gap: 8px; align-items: center; margin-left: 6px; }
.pad-size input { width: 130px; }
.pad-size #padSizeVal { white-space: nowrap; min-width: 3.2em; }
.variants { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.variant {
  width: 64px; height: 64px; border-radius: 10px; border: 2px solid var(--line); background: var(--pad-bg);
  position: relative; padding: 0; cursor: pointer;
}
.variant canvas { width: 100%; height: 100%; display: block; }
.variant.active { border-color: var(--accent); }
.variant .num { position: absolute; left: 4px; top: 1px; font-size: 11px; color: var(--muted); }
.variant.add { display: grid; place-items: center; font-size: 26px; color: var(--accent); border-style: dashed; }
.side-panel { display: flex; flex-direction: column; gap: 14px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.panel h3 { font-size: 16px; margin-bottom: 8px; }
.sample { width: 100%; height: 90px; display: block; background: var(--pad-bg); border-radius: 10px; }
.char-strip { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 10px; scrollbar-width: none; }
.char-strip button {
  min-width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  font: 500 17px ui-serif, Georgia, serif; flex: none; position: relative; padding: 0 6px;
}
.char-strip button.done::after { content: ""; position: absolute; right: 4px; top: 4px; width: 6px; height: 6px; border-radius: 3px; background: var(--ok); }
.char-strip button.cur { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 4px; padding: 8px 0; }
.field + .field { border-top: 1px solid var(--line); }
.field .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.field label { font-size: 14px; font-weight: 500; }
.field .val { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.field .hint { font-size: 12px; color: var(--muted); }
input[type=range] { width: 100%; height: 32px; accent-color: var(--accent); margin: 0; }
select, input[type=text], input[type=number], textarea {
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); padding: 9px 11px; min-height: 42px;
}
select { -webkit-appearance: none; appearance: none; padding-right: 30px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
input[type=color] { width: 44px; height: 36px; border: 1px solid var(--line); border-radius: 10px; padding: 2px; background: var(--surface-2); }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; position: absolute; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 15px; background: var(--line); transition: background .15s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 12px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.swatches { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.swatch { width: 32px; height: 32px; border-radius: 16px; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.swatch.active { box-shadow: 0 0 0 2px var(--accent); }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 7px 10px; border-radius: 8px; font-size: 14px; cursor: pointer; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); color: var(--accent); font-weight: 600; }

details.group { border-top: 1px solid var(--line); }
details.group:first-of-type { border-top: 0; }
details.group > summary {
  list-style: none; cursor: pointer; padding: 12px 0; font-weight: 600; display: flex; justify-content: space-between; align-items: center;
}
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::after { content: "›"; transform: rotate(90deg); color: var(--muted); font-size: 20px; transition: transform .15s; }
details.group:not([open]) > summary::after { transform: rotate(0deg); }

/* ---------- Write view ---------- */
.write {
  display: grid; grid-template-columns: 370px minmax(0, 1fr); height: calc(100vh - 66px - var(--safe-t)); height: calc(100dvh - 66px - var(--safe-t));
}
.write aside {
  overflow-y: auto; -webkit-overflow-scrolling: touch; border-right: 1px solid var(--line); background: var(--surface);
  padding: 14px 16px calc(30px + var(--safe-b)) calc(16px + var(--safe-l));
}
.write textarea { width: 100%; min-height: 180px; resize: vertical; line-height: 1.45; font-size: 16px; }
.write .pages { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 20px 20px calc(40px + var(--safe-b)); display: flex; flex-direction: column; align-items: center; gap: 22px; }
.page-box { width: 100%; max-width: 820px; }
.page-box canvas { width: 100%; display: block; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.12), 0 0 0 1px rgba(0,0,0,.04); border-radius: 2px; }
.page-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; color: var(--muted); font-size: 13px; }
.warn { background: #fff4d6; color: #6b4b00; border-radius: 10px; padding: 8px 12px; font-size: 13px; margin: 8px 0; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .warn { background: #3a3220; color: #f3d98a; } }
.export-box { display: flex; flex-direction: column; gap: 8px; margin: 12px 0 6px; }
.export-box .btn { width: 100%; }
.only-portrait { display: none; }
.scrim { display: none; }
@media (orientation: portrait), (max-width: 900px) {
  .write { grid-template-columns: 1fr; height: auto; }
  .write aside { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(420px, 92vw); transform: translateX(-102%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(0,0,0,.2); padding-top: calc(16px + var(--safe-t)); }
  .write.open aside { transform: none; }
  .write .pages { overflow: visible; }
  .only-portrait { display: inline-flex; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 25; display: none; }
  .write.open .scrim { display: block; }
}

/* ---------- Export view ---------- */
.export-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 18px; }
.font-test {
  min-height: 160px; padding: 16px; border-radius: 12px; background: var(--pad-bg); border: 1px solid var(--line);
  font-size: 44px; line-height: 1.35; white-space: pre-wrap; word-break: break-word; color: var(--pad-ink);
  font-feature-settings: "calt" 1, "liga" 1;
}
.steps { margin: 6px 0 0; padding-left: 20px; }
.steps li { margin: 6px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chip { font: 15px ui-serif, Georgia, serif; border: 1px solid var(--line); border-radius: 8px; padding: 2px 8px; background: var(--surface-2); }
.stat { display: flex; gap: 18px; flex-wrap: wrap; margin: 6px 0 2px; }
.stat b { font-size: 22px; display: block; font-family: ui-serif, Georgia, serif; }
.stat span { font-size: 12px; color: var(--muted); }

/* ---------- Sheet / dialog ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.32); z-index: 50; display: grid; place-items: center; padding: 16px; animation: fade .15s; }
.sheet {
  background: var(--surface); border-radius: 18px; width: min(520px, 100%); max-height: calc(100dvh - 40px);
  overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); padding: 20px;
}
.sheet h2 { font-size: 22px; margin-bottom: 12px; }
.sheet .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.sheet .menu { display: flex; flex-direction: column; gap: 6px; }
.sheet .menu .btn { justify-content: flex-start; }
@keyframes fade { from { opacity: 0; } }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #1d1d1f; color: #fff; padding: 11px 18px; border-radius: 12px; font-size: 15px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 100; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mt { margin-top: 12px; }
