/* Write your strokes — calm write mode + settings drawer */

:root {
  --paper: #f7f1e3;
  --paper-edge: #e8dcc8;
  --ink: #1a1a1a;
  --ink-soft: #3a3a3a;
  --grid: #e8a0a0;
  --grid-strong: #d87878;
  --accent: #c45c5c;
  --btn-bg: #fffdf8;
  --btn-border: #d4c4a8;
  --btn-active: #c45c5c;
  --btn-active-text: #fffdf8;
  --shadow: 0 4px 18px rgba(60, 40, 20, 0.12);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --danger: #a33;
}

/* [hidden] must beat class display:flex/grid (e.g. .sheet-actions) */
[hidden] {
  display: none !important;
}


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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: linear-gradient(165deg, #faf6ee 0%, #efe4d2 100%);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang HK", "Hiragino Sans CNS",
    "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body {
  padding: 14px 14px calc(18px + var(--safe-bottom));
}

.page {
  max-width: 480px;
  margin: 0 auto;
}

/* Header */
.header {
  margin-bottom: 12px;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header h1 {
  margin: 0;
  font-size: clamp(1.15rem, 4.5vw, 1.45rem);
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1.3;
}

.subtitle {
  margin: 4px 0 0;
  font-size: 0.88rem;
  color: var(--ink-soft);
}

.header-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.btn-settings {
  appearance: none;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink-soft);
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 40px;
  flex-shrink: 0;
}

.btn-settings:hover,
.btn-settings[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

/* Stage — large focus */
.stage-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 14px;
}

.stage {
  position: relative;
  width: min(92vw, 400px);
  aspect-ratio: 1 / 1;
  background: var(--paper);
  border: 2px solid var(--paper-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* Shared surface: grid + writer occupy the SAME box so 米字格 aligns with glyph */
.stage-surface {
  position: absolute;
  inset: 4%;
  width: auto;
  height: auto;
  z-index: 0;
}

.grid-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  overflow: visible;
}

.grid-svg.hidden { display: none; }

.grid-svg .grid-line,
.cell-grid .grid-line {
  stroke: var(--grid);
  stroke-width: 0.7;
  fill: none;
  vector-effect: non-scaling-stroke;
}

.grid-svg .grid-border,
.cell-grid .grid-border {
  stroke: var(--grid-strong);
  stroke-width: 1.4;
  fill: none;
  vector-effect: non-scaling-stroke;
}

.cell-grid .grid-line { stroke-width: 1; }
.cell-grid .grid-border { stroke-width: 1.6; }

.writer-target {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.writer-target svg {
  width: 100% !important;
  height: 100% !important;
  display: block;
  overflow: visible;
}

.stroke-num-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.now-playing {
  margin: 12px 0 0;
  font-size: 0.95rem;
  color: var(--ink-soft);
}

.stage-hint {
  margin-top: 6px;
}

/* Write bar — replay + discreet speed */
.write-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px 14px;
  margin-bottom: 16px;
}

.speed-btns.discreet {
  display: flex;
  gap: 4px;
}

.speed-btns.discreet .btn.speed {
  min-height: 36px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  box-shadow: none;
}

.speed-btns.discreet .btn.speed:hover {
  border-color: var(--btn-border);
  background: var(--btn-bg);
}

.speed-btns.discreet .btn.speed.active {
  background: transparent;
  border-color: var(--accent);
  color: var(--accent);
}

/* Buttons */
.btn {
  appearance: none;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, color 0.15s, border-color 0.15s, transform 0.1s;
  min-height: 44px;
}

.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn.primary {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
  padding-left: 28px;
  padding-right: 28px;
}

.btn.primary:hover:not(:disabled) {
  background: var(--ink-soft);
  border-color: var(--ink-soft);
}

.btn.sm {
  min-height: 40px;
  padding: 8px 14px;
  font-size: 0.88rem;
}

.btn.active {
  background: var(--btn-active);
  color: var(--btn-active-text);
  border-color: var(--btn-active);
}

.section-label {
  display: block;
  margin: 0 0 8px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.hint {
  margin: 8px 0 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--danger);
  line-height: 1.4;
}

.hint.info { color: var(--ink-soft); }

/* Sheet in write mode */
.sheet-panel {
  margin-bottom: 18px;
}

.sheet-panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.sheet-panel-head .section-label { margin: 0; }

.sheet-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.sheet-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(68px, 1fr));
  gap: 8px;
  padding: 10px;
  background: var(--paper);
  border: 2px solid var(--paper-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.sheet-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border: 1.5px solid var(--grid-strong);
  background: #faf6ee;
  border-radius: 4px;
  cursor: pointer;
  padding: 0;
  margin: 0;
  overflow: hidden;
  font-family: "Noto Serif TC", "Songti TC", "STSong", serif;
  font-size: clamp(1.5rem, 6vw, 2rem);
  color: var(--ink);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.15s, border-color 0.15s;
}

.sheet-cell:hover,
.sheet-cell:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(196, 92, 92, 0.35);
  outline: none;
}

.sheet-cell.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
  background: #fff8f0;
}

.sheet-cell .cell-grid {
  position: absolute;
  inset: 4%;
  width: 92%;
  height: 92%;
  pointer-events: none;
  z-index: 0;
}

.sheet-cell .cell-char {
  position: relative;
  z-index: 1;
  font-weight: 500;
}

.sheet-cell.missing {
  color: #b09090;
  opacity: 0.7;
}

.sheet-cell.missing::after {
  content: "?";
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.65rem;
  color: var(--danger);
  font-family: sans-serif;
}

/* Settings panel */
.settings {
  margin-bottom: 16px;
}

.settings-card {
  background: rgba(255, 253, 248, 0.92);
  border: 1.5px solid var(--paper-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 14px 14px;
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.settings-head h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.settings-block {
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--paper-edge);
}

.settings-block:last-of-type {
  border-bottom: none;
  margin-bottom: 8px;
  padding-bottom: 0;
}

.char-picker-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-start;
}

.char-btn {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 1.4rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s, transform 0.1s;
}

.char-btn:hover { border-color: var(--accent); }
.char-btn:active { transform: scale(0.96); }
.char-btn.active {
  background: var(--btn-active);
  color: var(--btn-active-text);
  border-color: var(--btn-active);
}

.input-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: stretch;
}

.input-row input[type="text"] {
  flex: 1 1 140px;
  min-height: 40px;
  padding: 8px 12px;
  border: 1.5px solid var(--btn-border);
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--ink);
  font-size: 1rem;
  font-family: inherit;
}

.input-row input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.grid-btns {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.btn.grid-style {
  padding: 8px 14px;
  min-height: 40px;
}

.toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
  min-height: 36px;
}

.toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.settings-help {
  margin: 0;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

.footer {
  text-align: center;
  padding-top: 4px;
}

.footer p {
  margin: 0;
  font-size: 0.72rem;
  color: #a09078;
}

@media (max-width: 420px) {
  body { padding: 10px 10px calc(14px + var(--safe-bottom)); }
  .stage { width: min(94vw, 360px); }
  .sheet-grid { grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); gap: 6px; }
}

@media (min-width: 600px) {
  body { padding: 28px 20px 36px; }
  .page { max-width: 520px; }
  .stage { width: 420px; }
}

/* Brush technique layer (variable width ink) */
.brush-layer-svg {
  position: absolute;
  inset: 5%;
  width: 90%;
  height: 90%;
  z-index: 2;
  pointer-events: none;
  overflow: visible;
}

.brush-outline-path {
  fill: #d4c8b0;
  stroke: none;
  opacity: 0.55;
}

.brush-ink-path {
  stroke: none;
}
.brush-reveal-path {
  fill: none;
}

/* Hanzi Writer draws the real glyph reveal; brush layer only used in lesson
   when we pass filled paths. Hide duplicate outline from brush on write stage. */
#stage .brush-layer-svg {
  display: none; /* write mode uses Hanzi Writer glyph reveal only */
}
.writer-target svg {
  opacity: 1;
  pointer-events: none;
}

.stroke-num-svg {
  z-index: 3;
}

.stroke-tip {
  line-height: 1.45;
  max-width: 36rem;
  margin: 8px 0 0;
  font-size: 0.88rem;
  color: var(--accent);
  text-align: center;
  min-height: 1.3em;
  line-height: 1.4;
  font-weight: 600;
}

/* Lesson mode */
.lesson {
  margin-bottom: 16px;
}

.lesson-card {
  background: rgba(255, 253, 248, 0.92);
  border: 1.5px solid var(--paper-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 14px 14px;
}

.lesson-intro {
  margin: 0 0 14px;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

.lesson-types {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.lesson-type-btn {
  appearance: none;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  border-radius: 12px;
  min-height: 52px;
  font-size: 1.15rem;
  font-weight: 700;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
  padding: 8px 4px;
}

.lesson-type-btn small {
  display: block;
  font-size: 0.65rem;
  font-weight: 500;
  color: var(--ink-soft);
  margin-top: 2px;
}

.lesson-type-btn.active {
  background: var(--btn-active);
  color: var(--btn-active-text);
  border-color: var(--btn-active);
}

.lesson-type-btn.active small {
  color: rgba(255, 253, 248, 0.85);
}

.lesson-detail h3 {
  margin: 0 0 6px;
  font-size: 1.35rem;
}

.lesson-yong {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.lesson-explain {
  margin: 0 0 14px;
  font-size: 0.95rem;
  line-height: 1.55;
  color: var(--ink);
}

.lesson-demo-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 10px;
}

.lesson-stage {
  width: min(56vw, 220px) !important;
  flex-shrink: 0;
}

.lesson-examples {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-width: 120px;
}

.lesson-ex-btn {
  appearance: none;
  border: 1.5px solid var(--btn-border);
  background: var(--btn-bg);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 1.4rem;
  font-weight: 600;
  font-family: "Noto Serif TC", "Songti TC", serif;
  cursor: pointer;
  text-align: left;
  color: var(--ink);
}

.lesson-ex-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(196, 92, 92, 0.3);
}

.lesson-ex-btn span {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  font-family: "Noto Sans TC", sans-serif;
  color: var(--ink-soft);
  margin-top: 2px;
}

@media (max-width: 420px) {
  .lesson-types {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }
  .lesson-type-btn {
    min-height: 48px;
    font-size: 1.05rem;
  }
}

.lesson-stage {
  position: relative;
}
.lesson-stage .stage-surface {
  inset: 4%;
}
.lesson-writer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}
.lesson-writer svg {
  width: 100% !important;
  height: 100% !important;
}
#lessonBrushSvg {
  display: none;
}


/* Language / charset switch */
.lang-switch, .charset-switch, .preset-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.lang-switch .btn, .charset-switch .btn, .preset-tabs .btn {
  min-height: 34px;
  padding: 5px 12px;
  font-size: 0.85rem;
}
.preset-group-label {
  margin: 10px 0 6px;
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 600;
}
.char-picker-row.wrap {
  flex-wrap: wrap;
}
#btnOpenLesson[hidden] { display: none !important; }
.subtitle .lang-tag {
  color: var(--accent);
  font-weight: 600;
}
