:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --paper: #fffdf8;
  --ink: #1d1b18;
  --muted: #6d675e;
  --line: #e2ddd3;
  --accent: #d2562b;
  --accent-ink: #ffffff;
  --guide: #3c7fd1;
  --done: #2f8f5b;
  --danger: #b3261e;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171614;
    --surface: #211f1c;
    --ink: #f2efe9;
    --muted: #a39d93;
    --line: #37332e;
    --accent: #ef7a4f;
    --accent-ink: #1d1b18;
    --done: #5cc38b;
    --danger: #f28b82;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, input, textarea, .button {
  font: inherit;
  color: inherit;
}

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary, .button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
button.link {
  min-height: 0;
  padding: 4px 0;
  border: 0;
  background: none;
  color: var(--muted);
  text-decoration: underline;
}
button.link.danger { color: var(--danger); }
button.danger { color: var(--danger); }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.small { font-size: 13px; }

.muted { color: var(--muted); }

/* Header */

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 24px;
  padding: 14px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand {
  font-weight: 800;
  font-size: 22px;
  letter-spacing: -0.02em;
}
.font-switch select {
  max-width: 220px;
  min-height: 40px;
  padding: 0 32px 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
}
.progress {
  margin-left: auto;
  display: grid;
  gap: 4px;
  min-width: 160px;
  font-size: 14px;
}
.bar {
  height: 6px;
  border-radius: 3px;
  background: var(--line);
  overflow: hidden;
}
#progress-bar {
  height: 100%;
  width: 0;
  background: var(--done);
  transition: width 0.2s;
}

/* Layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 520px) minmax(0, 1fr);
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px;
}
@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

/* Drawing stage */

.stage-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
}
.glyph-char {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 32px;
  font-family: Georgia, serif;
}
.glyph-label { font-weight: 600; }

.pad-wrap {
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 0 0 1px var(--line), 0 6px 24px rgb(0 0 0 / 0.06);
}
#pad {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}
.toolbar #next { margin-left: auto; }
.toolbar #prev { width: 44px; padding: 0; }

.pens {
  display: flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
}
.pens button {
  width: 38px;
  min-height: 38px;
  padding: 0;
  border: 0;
}
.pens button i {
  display: block;
  width: var(--w);
  height: var(--w);
  border-radius: 50%;
  background: var(--ink);
}
.pens button[aria-checked="true"] { background: var(--line); }

.hint {
  margin: 12px 0 0;
  font-size: 13px;
}

/* Font card */

.font-card {
  display: grid;
  gap: 10px;
  margin-bottom: 20px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.font-card p { margin: 0; }
.field {
  display: grid;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.field input {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px; /* 16px+ stops iOS zooming into the field */
}
.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Glyph grid */

.side h3 {
  margin: 18px 0 8px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.side h3:first-child { margin-top: 0; }
.cells {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 6px;
}
.cell {
  position: relative;
  aspect-ratio: 1;
  min-height: 0;
  padding: 0;
  border-radius: 8px;
  background: var(--paper);
  color: #b9b2a6;
  font-family: Georgia, serif;
  font-size: 20px;
  overflow: hidden;
}
.cell canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.cell.done { border-color: var(--done); }
.cell.done span { visibility: hidden; }
.cell[aria-current="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}
#reset { margin-top: 18px; }

/* Preview */

.preview {
  max-width: 1200px;
  margin: 0 auto 48px;
  padding: 0 16px;
}
.preview-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 24px;
}
.preview h2 { margin: 0; font-size: 20px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
#status.error { color: var(--danger); }
#sample {
  width: 100%;
  min-height: 140px;
  padding: 16px 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  color: #1d1b18;
  font-size: 44px;
  line-height: 1.3;
  resize: vertical;
}

.image-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}
.image-tools[hidden] { display: none; }
.image-tools p { flex-basis: 100%; margin: 0; }
.image-style {
  display: flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
}
.image-style button {
  min-height: 38px;
  padding: 0 12px;
  border: 0;
  font-size: 14px;
}
.image-style button[aria-checked="true"] { background: var(--line); }
