@font-face {
  font-family: "MOE Edu Kai";
  src: url("fonts/edukai-5.1_20251208.ttf") format("truetype");
  font-display: swap;
}

:root {
  --color-page-background: oklch(96% 0.018 92);
  --color-surface: oklch(100% 0.004 95);
  --color-text: oklch(22% 0.025 250);
  --color-text-muted: oklch(48% 0.018 245);
  --color-border: oklch(88% 0.018 92);
  --color-accent: oklch(49% 0.105 205);
  --color-accent-soft:
    color-mix(in oklch, var(--color-accent) 12%, var(--color-surface));
  --color-trace-text: oklch(62% 0.18 24);
  --color-guide-line: oklch(78% 0.012 92);
  --color-status-warning: oklch(43% 0.105 72);
  --font-display: "MOE Edu Kai", serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Noto Sans TC",
    "Microsoft JhengHei", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  color: var(--color-text);
  background:
    radial-gradient(
      circle at top left,
      color-mix(in oklch, var(--color-accent) 12%, transparent),
      transparent 34rem
    ),
    var(--color-page-background);
  font-family: var(--font-body);
  line-height: 1.5;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 400px minmax(0, 1fr);
  min-height: 100vh;
}

.workspace-panel {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  padding: 16px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 88%,
      var(--color-page-background)
    );
  border-right: 1px solid var(--color-border);
}

.brand-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 18px;
  padding: 16px;
  border: 1px solid var(--color-border);
  border-radius: 18px;
  background: var(--color-surface);
}

.eyebrow {
  margin: 0;
  color: var(--color-accent);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  font-family: var(--font-display);
  letter-spacing: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.15;
}

h2 {
  font-size: 20px;
  line-height: 1.25;
}

p {
  margin: 0;
  color: var(--color-text-muted);
  text-wrap: pretty;
}

.panel-card {
  display: grid;
  gap: 18px;
  margin-top: 14px;
  padding: 14px;
  border: 1px solid var(--color-border);
  border-radius: 16px;
  background: var(--color-surface);
}

.panel-card a {
  color: var(--color-accent);
  font-weight: 800;
  text-decoration-thickness: 0.08em;
  text-underline-offset: 3px;
}

.about-card {
  gap: 12px;
}

.about-card > p {
  color: var(--color-text-muted);
  font-size: 12px;
  line-height: 1.55;
}

.about-card a {
  font-weight: 700;
}

.section-heading {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 12px;
  align-items: start;
}

.step-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  background: var(--color-accent-soft);
  color: var(--color-accent);
  font-family: var(--font-mono);
  font-weight: 800;
}

.section-heading p {
  margin-top: 3px;
  font-size: 12px;
}

.field,
fieldset,
.switch {
  display: grid;
  gap: 9px;
  min-width: 0;
  margin: 0;
}

fieldset {
  padding: 0;
  border: 0;
}

legend,
.field > span,
.switch > span {
  color: var(--color-text);
  font-size: 14px;
  font-weight: 800;
}

textarea,
input[type="number"] {
  width: 100%;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 94%,
      var(--color-page-background)
    );
  color: var(--color-text);
}

textarea {
  min-height: 136px;
  padding: 13px 14px;
  resize: vertical;
  line-height: 1.65;
}

input[type="number"] {
  min-height: 42px;
  padding: 8px 11px;
}

textarea:focus,
input[type="number"]:focus {
  outline:
    3px solid color-mix(in oklch, var(--color-accent) 18%, transparent);
  border-color: var(--color-accent);
}

.segmented {
  display: grid;
  gap: 8px;
}

.segmented.grid-modes,
.segmented.ink-modes {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.segmented label {
  position: relative;
  min-width: 0;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: grid;
  place-items: center;
  min-height: 42px;
  padding: 8px 6px;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 88%,
      var(--color-page-background)
    );
  color: var(--color-text);
  font-size: 14px;
  font-weight: 800;
  text-align: center;
  transition: background 0.16s ease, border-color 0.16s ease, color 0.16s ease;
}

.segmented input:checked + span {
  border-color: var(--color-accent);
  background: var(--color-accent);
  color: var(--color-surface);
}

.segmented input:focus-visible + span {
  outline:
    3px solid color-mix(
      in oklch,
      var(--color-accent) 55%,
      var(--color-surface)
    );
  outline-offset: 2px;
}

.switch-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.switch {
  grid-template-columns: 44px 1fr;
  align-items: center;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 90%,
      var(--color-page-background)
    );
}

.switch input {
  width: 22px;
  height: 22px;
  accent-color: var(--color-accent);
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  align-items: end;
}

.actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.primary-button {
  min-height: 46px;
  border: 1px solid var(--color-accent);
  border-radius: 12px;
  background: var(--color-accent);
  color: var(--color-surface);
  font-weight: 900;
  transition: transform 0.06s ease, filter 0.16s ease, border-color 0.16s ease;
}

.primary-button:active {
  transform: translateY(1px);
}

.primary-button:hover {
  filter: brightness(0.97);
}

.status {
  min-height: 76px;
  padding: 12px 13px;
  border-radius: 12px;
  background:
    color-mix(
      in oklch,
      var(--color-status-warning) 11%,
      var(--color-surface)
    );
  color: var(--color-status-warning);
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-line;
}

.preview-stage {
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-width: 0;
  padding: 28px;
  overflow: hidden;
}

.sheet-frame {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: block;
  padding-bottom: 18px;
}

.sheet-fit {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  margin: 0 auto;
}

.sheet {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 12mm 13mm;
  background: #fff;
  border:
    1px solid color-mix(
      in oklch,
      var(--color-border) 72%,
      var(--color-text)
    );
  box-shadow:
    0 22px 48px color-mix(
      in oklch,
      var(--color-text) 18%,
      transparent
    );
}

.grid {
  --grid-column-count: 6;
  --grid-row-count: 11;
  display: grid;
  direction: rtl;
  grid-auto-flow: column;
  grid-template-rows: repeat(var(--grid-row-count), auto);
  grid-auto-columns: minmax(0, 1fr);
  grid-template-columns: repeat(var(--grid-column-count), minmax(0, 1fr));
  gap: 0;
  align-items: start;
  border-top: 1.2px solid var(--color-text);
  border-right: 1.2px solid var(--color-text);
}

.cell {
  display: grid;
  grid-template-columns: 1fr 34px;
  min-width: 0;
  direction: ltr;
  border-bottom: 1.2px solid var(--color-text);
  border-left: 1.2px solid var(--color-text);
  break-inside: avoid;
}

.cell.polyphonic {
  cursor: pointer;
}

.cell.polyphonic .zhuyin {
  position: relative;
}

.cell.polyphonic .zhuyin::before {
  content: "選音";
  position: absolute;
  top: 3px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  padding: 1px 4px;
  border: 1px solid color-mix(in oklch, var(--color-accent) 44%, transparent);
  border-radius: 4px;
  background: var(--color-accent-soft);
  color: var(--color-accent);
  font-size: 10px;
  font-weight: 900;
  line-height: 1.25;
  white-space: nowrap;
  pointer-events: none;
}

.cell.polyphonic:focus-visible {
  outline: 3px solid color-mix(in oklch, var(--color-accent) 45%, transparent);
  outline-offset: -3px;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  aspect-ratio: 1 / 1;
  container-type: inline-size;
  border-right: 1.2px solid var(--color-text);
  overflow: hidden;
}

.guide {
  position: absolute;
  pointer-events: none;
  z-index: 0;
  display: none;
  border-color: var(--color-guide-line);
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}

.center-v {
  top: 0;
  bottom: 0;
  left: 50%;
  border-left: 1px dashed var(--color-guide-line);
}

.center-h {
  top: 50%;
  left: 0;
  right: 0;
  border-top: 1px dashed var(--color-guide-line);
}

.diagonal-a,
.diagonal-b {
  top: 50%;
  left: -21%;
  width: 142%;
  border-top: 1px dashed var(--color-guide-line);
}

.diagonal-a {
  transform: rotate(45deg);
}

.diagonal-b {
  transform: rotate(-45deg);
}

.grid.tian .center-v,
.grid.tian .center-h,
.grid.mi .guide,
.grid.nine .center-v,
.grid.nine .center-h {
  display: block;
}

.grid.nine .center-v {
  left: 28%;
}

.grid.nine .center-v::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 44.5cqw;
  border-left: 1px dashed var(--color-guide-line);
}

.grid.nine .center-h {
  top: 20%;
}

.grid.nine .center-h::before {
  content: "";
  position: absolute;
  top: 59.8cqw;
  left: 0;
  right: 0;
  border-top: 1px dashed var(--color-guide-line);
}

.char {
  position: relative;
  z-index: 1;
  color: var(--color-trace-text);
  font-family: var(--font-display);
  font-size: calc(100cqi - 4px);
  line-height: 1;
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}

.grid.read .char {
  color: var(--color-text);
}

.grid.gray .char {
  color: #d0d0d0;
}

.grid.outline .char {
  color: transparent;
  -webkit-text-stroke:
    1.2px color-mix(
      in oklch,
      var(--color-text) 38%,
      var(--color-surface)
    );
}

@supports not (-webkit-text-stroke: 1px black) {
  .grid.outline .char {
    color:
      color-mix(in oklch, var(--color-text) 28%, var(--color-surface));
  }
}

.grid.hide .char {
  visibility: hidden;
}

.zhuyin {
  display: grid;
  align-items: center;
  min-width: 0;
  color: var(--color-text);
}

.zhuyin-syllable {
  display: grid;
  grid-template-columns: 9px 1fr 9px;
  align-items: center;
  position: relative;
  width: 100%;
  font-family: var(--font-display);
  font-size: 16px;
  line-height: 1;
}

.zhuyin-body {
  grid-column: 2;
  justify-self: center;
  writing-mode: vertical-rl;
  text-orientation: upright;
  text-align: center;
}

.zhuyin-light-tone {
  font-size: 0.72em;
  line-height: 0.7;
}

.zhuyin-tone {
  position: absolute;
  right: 0;
  bottom: 9px;
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-size: 1em;
}

.zhuyin-syllable rp {
  display: none;
}

.grid.no-zhuyin .cell {
  grid-template-columns: 1fr 0;
}

.grid.no-zhuyin .zhuyin {
  display: none;
}

.reading-dialog {
  width: min(92vw, 380px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: transparent;
  color: var(--color-text);
}

.reading-dialog::backdrop {
  background: color-mix(in oklch, var(--color-text) 34%, transparent);
}

.reading-dialog__panel {
  display: grid;
  gap: 16px;
  padding: 18px;
  border: 1px solid var(--color-border);
  border-radius: 16px;
  background: var(--color-surface);
  box-shadow:
    0 24px 56px color-mix(
      in oklch,
      var(--color-text) 22%,
      transparent
    );
}

.reading-dialog__header {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 90%,
      var(--color-page-background)
    );
  color: var(--color-text);
  font-size: 24px;
  line-height: 1;
}

.reading-choices {
  display: grid;
  gap: 8px;
}

.reading-choice {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background:
    color-mix(
      in oklch,
      var(--color-surface) 92%,
      var(--color-page-background)
    );
  color: var(--color-text);
  font-weight: 900;
  text-align: left;
}

.reading-choice[aria-pressed="true"] {
  border-color: var(--color-accent);
  background: var(--color-accent);
  color: var(--color-surface);
}

.reading-choice__index {
  color: inherit;
  font-family: var(--font-mono);
  font-size: 12px;
  opacity: 0.76;
}

@media (max-width: 1120px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .workspace-panel {
    position: static;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--color-border);
  }

  .preview-stage {
    height: auto;
    padding: 20px;
    overflow: auto;
  }

  .sheet-frame {
    min-height: min(78vh, 760px);
  }
}

@media (max-width: 760px) {
  .workspace-panel {
    padding: 16px;
  }

  .control-grid,
  .actions {
    grid-template-columns: 1fr;
  }

  .sheet-frame {
    min-height: min(78vh, 760px);
  }
}

@page {
  size: A4 portrait;
  margin: 0;
}

@media print {
  body {
    background: var(--color-surface);
  }

  .workspace-panel {
    display: none;
  }

  .cell.polyphonic .zhuyin::before,
  .reading-dialog {
    display: none;
  }

  .app-shell,
  .preview-stage {
    display: block;
    height: auto;
    min-height: 0;
    padding: 0;
    overflow: visible;
  }

  .sheet-frame {
    display: block;
    padding: 0;
    overflow: visible;
  }

  .sheet-fit {
    display: block;
  }

  .sheet {
    width: 210mm;
    height: 297mm;
    min-height: 297mm;
    margin: 0;
    border: 0;
    box-shadow: none;
    break-inside: avoid;
  }

  .sheet + .sheet {
    break-before: page;
  }
}
