*, *::before, *::after {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  background: #fff;
}

body {
  margin: 0;
  overflow: hidden;
  touch-action: none;
  font-family: system-ui, sans-serif;
}

/* ── Screens ─────────────────────────────────────────── */
#screen-writing {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}
#screen-writing[hidden] { display: none; }

/* ── Scribble warning banner (iOS only, shown once) ─────────────────────── */
#scribble-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 1rem;
  background: #fff8e1;
  border-bottom: 1px solid #ffe082;
  font-size: 0.85rem;
  color: #5d4037;
}

#scribble-banner[hidden] { display: none; }

#scribble-dismiss {
  flex: none;
  padding: 0.25rem 0.75rem;
  border: 1px solid #ffe082;
  border-radius: 5px;
  background: #fff;
  font-size: 0.8rem;
  cursor: pointer;
  color: #5d4037;
  white-space: nowrap;
}

/* ── Prompt zone (top ~25%) ───────────────────────────────────────────────── */
#prompt-zone {
  flex: 0 0 25%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 1rem 1.5rem 0.5rem;
  border-bottom: 1px solid #e0e0e0;
  gap: 0.5rem;
}

#prompt-text {
  font-size: clamp(1.2rem, 4vw, 2.2rem);
  font-weight: 400;
  letter-spacing: 0.08em;
  color: #222;
  text-align: center;
  line-height: 1.4;
  word-break: break-all;
  position: relative;
  align-self: stretch;
}

/* Baseline and cap-height sizing guides.
   Positions are set at runtime via --guide-cap / --guide-baseline CSS custom
   properties computed by calibrateGuideLines() in app.js.
   The fallback values (0.3em / 1.0em) approximate line-height:1.4 with a
   system-font cap-height ratio of ~0.7 and will be overwritten on load. */
#prompt-text::before,
#prompt-text::after {
  content: "";
  position: absolute;
  left: 0; right: 0;
  height: 0;
  border-top: 1px solid #aaa;
  z-index: -1;
  pointer-events: none;
}
#prompt-text::before { top: var(--guide-cap,      0.3em); }  /* cap-height */
#prompt-text::after  { top: var(--guide-baseline, 1.0em); }  /* baseline   */

#prompt-progress {
  font-size: 0.8rem;
  color: #999;
}

#prompt-hint {
  font-size: 0.72rem;
  color: #bbb;
  letter-spacing: 0.04em;
  text-align: center;
  min-height: 1.2em;
  line-height: 1.2;
}

/* ── Writing canvas (fills remaining space) ──────────────────────────────── */
#canvas-zone {
  flex: 1 1 0;
  position: relative;
  overflow: hidden;
  background: #fff;
}

#writing-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

/* ── Undo button (floating, upper-right of canvas) ──────────────────────── */
#undo-btn {
  position: absolute;
  top: 0.6rem;
  right: 0.8rem;
  padding: 0.35rem 0.9rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.85);
  font-size: 0.9rem;
  cursor: pointer;
  color: #333;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
#undo-btn:active  { background: #eee; }
#undo-btn:disabled { opacity: 0.3; cursor: default; }

/* ── Controls bar (bottom) ───────────────────────────────────────────────── */
#controls-bar {
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  background: #f8f8f8;
  border-top: 1px solid #e0e0e0;
  gap: 0.5rem;
}

#controls-bar button {
  padding: 0.4rem 1.2rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 0.95rem;
  cursor: pointer;
  color: #333;
}

#controls-bar button:active { background: #eee; }
#controls-bar button:disabled { opacity: 0.4; cursor: default; }

#done-btn {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}
#done-btn:active { background: #333; }

#status-msg {
  flex: 1;
  text-align: center;
  font-size: 0.8rem;
  color: #888;
}

/* ── Overlay (job polling, completion, errors) ───────────────────────────── */
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.94);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.2rem;
  z-index: 100;
}

#overlay[hidden] { display: none; }

#overlay-message {
  font-size: 1.2rem;
  color: #222;
  text-align: center;
  padding: 0 2rem;
  line-height: 1.5;
}

#overlay-action {
  padding: 0.6rem 2rem;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: #1a1a1a;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
}
#overlay-action[hidden] { display: none; }

#overlay-input {
  font-size: 1.2rem;
  padding: 0.4rem 0.8rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  text-align: center;
  width: 14rem;
}
#overlay-input[hidden] { display: none; }

#overlay-handedness {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
  color: #555;
}
#overlay-handedness[hidden] { display: none; }
#overlay-handedness button {
  padding: 0.3rem 0.9rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 0.9rem;
  cursor: pointer;
}
#overlay-handedness button.hand-selected {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}

/* ── Right-handed layout: mirror controls ────────────────────────────────── */
/* Clear moves to the right (obscured by writing hand), Done and Undo to the left */
body.right-handed #controls-bar { flex-direction: row-reverse; }
body.right-handed #undo-btn     { right: auto; left: 0.8rem; }

/* ── Icon buttons (review top bar) ──────────────────── */
.icon-btn {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #555;
}
.icon-btn:active { background: #eee; }

/* ── Back button (writing screen) ────────────────────── */
#back-btn {
  align-self: flex-start;
  padding: 0.3rem 0.7rem;
  border: none;
  background: none;
  font-size: 0.85rem;
  color: #888;
  cursor: pointer;
}
#back-btn:active { color: #333; }

/* ── Popover (FAQ / Settings) ────────────────────────── */
#popover {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
#popover[hidden] { display: none; }

#popover-card {
  background: #fff;
  border-radius: 12px;
  width: min(92vw, 420px);
  max-height: 80dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
}

#popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.2rem 0.75rem;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
}

#popover-title {
  font-size: 1rem;
  font-weight: 600;
  color: #1a1a1a;
}

#popover-close {
  border: none;
  background: none;
  font-size: 1rem;
  color: #888;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
}
#popover-close:active { color: #333; }

#faq-content,
#settings-content {
  padding: 1rem 1.2rem 1.2rem;
  overflow-y: auto;
}
#faq-content[hidden],
#settings-content[hidden] { display: none; }

#faq-content dl { margin: 0; }
#faq-content dt {
  font-weight: 600;
  font-size: 0.9rem;
  color: #1a1a1a;
  margin-top: 1rem;
}
#faq-content dt:first-child { margin-top: 0; }
#faq-content dd {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: #444;
  line-height: 1.5;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid #f0f0f0;
  font-size: 0.9rem;
  color: #333;
}
.settings-row:last-child { border-bottom: none; }
.settings-row label { flex-shrink: 0; }

#settings-font-name {
  flex: 1;
  min-width: 0;
  font-size: 0.9rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  text-align: right;
}

.settings-hand-btns {
  display: flex;
  gap: 0.4rem;
}
.settings-hand-btns button {
  padding: 0.3rem 0.8rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 0.85rem;
  cursor: pointer;
}
.settings-hand-btns button.hand-selected {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}
.settings-weight-btns {
  display: flex;
  gap: 0.3rem;
}
.settings-weight-btns button {
  padding: 0.3rem 0.6rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 0.82rem;
  cursor: pointer;
}
.settings-weight-btns button.weight-selected {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}

/* ── Review screen ───────────────────────────────────────────────────────── */

#screen-review {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}
#screen-review[hidden] { display: none; }

#review-top-bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.55rem 1rem;
  border-bottom: 1px solid #e0e0e0;
}

#review-font-name  { font-size: 0.95rem; font-weight: 600; flex: 1; }
#review-built-badge {
  font-size: 0.68rem; padding: 0.18rem 0.5rem;
  background: #f0f4ff; border: 1px solid #c0c8f0;
  border-radius: 10px; color: #3355cc; flex: none;
}

#review-body {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

#review-sidebar {
  flex: none;
  width: 120px;
  border-right: 1px solid #e0e0e0;
  background: #fafafa;
  overflow-y: auto;
  padding: 0.5rem 0;
}

/* Handedness flip — sidebar moves to right for right-handed users */
body.right-handed #review-body    { flex-direction: row-reverse; }
body.right-handed #review-sidebar {
  border-right: none;
  border-left: 1px solid #e0e0e0;
}

#review-grid {
  flex: 1;
  overflow-y: auto;
  min-width: 0;
}

/* Sidebar items */
.review-sb-item {
  display: block;
  font-size: 0.72rem;
  padding: 0.4rem 0.75rem;
  color: #888;
  text-decoration: none;
  border-left: 2px solid transparent;
  line-height: 1.3;
}
.review-sb-item.active {
  color: #1a1a1a;
  font-weight: 600;
  border-left-color: #1a1a1a;
  background: #fff;
}
.review-sb-item.locked  { color: #bbb; cursor: pointer; pointer-events: auto; }
.review-sb-count        { font-size: 0.6rem; color: #aaa; display: block; }

/* Sidebar group headers (Latin / Symbols / Scripts) */
.review-sb-group-header {
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #bbb;
  padding: 0.6rem 0.75rem 0.2rem;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
}
.review-sb-group-header::after {
  content: "▾";
  font-size: 1rem;
  margin-left: auto;
  transition: transform 0.15s;
}
.review-sb-group.collapsed .review-sb-group-header::after { transform: rotate(-90deg); }
.review-sb-group.collapsed .review-sb-item { display: none; }
.review-sb-group + .review-sb-group {
  border-top: 1px solid #ebebeb;
  margin-top: 0.25rem;
  padding-top: 0.25rem;
}
body.right-handed .review-sb-group-header { text-align: right; }
body.right-handed .review-sb-group-header::after { margin-left: 0; margin-right: auto; }

body.right-handed .review-sb-item {
  border-left: none;
  border-right: 2px solid transparent;
  text-align: right;
}
body.right-handed .review-sb-item.active { border-right-color: #1a1a1a; }

/* Character sections */
.review-section         { padding: 0.75rem 1rem 0.6rem; border-bottom: 1px solid #f0f0f0; }
.review-section-heading {
  font-size: 0.68rem; font-weight: 600; color: #aaa;
  text-transform: uppercase; letter-spacing: 0.06em;
  margin-bottom: 0.5rem;
}
.review-section.locked .review-section-heading { color: #ddd; }
.review-subsection-heading {
  font-size: 0.75rem;
  font-weight: 600;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0.8rem 0 0.3rem;
}
.review-locked-msg      { font-size: 0.75rem; color: #ccc; font-style: italic; }

.review-chars-row       { display: flex; flex-wrap: wrap; gap: 10px; }
.review-char-group      { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.review-custom-cell-row {
  display: flex; flex-direction: row; flex-wrap: nowrap; align-items: center; gap: 3px; padding: 4px;
  background: #f8f8f8; border-radius: 6px; border: 1px solid #e8e8e8;
}
.review-char-variants   {
  display: flex; flex-direction: row; flex-wrap: nowrap; align-items: center; gap: 3px; padding: 4px;
  background: #f8f8f8; border-radius: 6px;
  border: 1px solid #e8e8e8;
}
.review-glyph-cell {
  width: 56px; height: 56px;
  background: #fff; border: 1px solid #e0e0e0; border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; overflow: hidden;
}
.review-glyph-cell svg  { width: 100%; height: 100%; }
.review-glyph-cell.shimmer {
  background: #f0f0f0;
  animation: review-shimmer 1.2s ease-in-out infinite;
}
@keyframes review-shimmer {
  0%, 100% { background: #f0f0f0; }
  50%       { background: #e4e4e4; }
}
.review-char-label      { font-size: 0.6rem; color: #bbb; }

/* Kern pair cell: single double-wide box showing both glyphs composed with kern offset */
.review-kern-cell {
  width: 112px; height: 56px;
  background: #fff; border: 1px solid #e0e0e0; border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.review-kern-cell svg  { width: 100%; height: 100%; }
.review-kern-cell.shimmer {
  background: #f0f0f0;
  animation: review-shimmer 1.2s ease-in-out infinite;
}
.review-kern-value { font-size: 0.6rem; color: #aaa; text-align: center; display: block; }

#review-export-btn {
  flex: none;
  padding: 0.3rem 0.9rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #1a1a1a;
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
}
#review-export-btn:disabled { opacity: 0.4; cursor: default; }

.review-add-tile {
  width: 56px; height: 56px;
  background: #f8f8f8;
  border: 1.5px dashed #ccc;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; color: #bbb;
  cursor: pointer;
  flex-shrink: 0;
}
.review-add-tile:active { background: #eee; color: #555; }

.review-add-tile.queued {
  border-style: solid;
  border-color: #888;
  color: #444;
  font-size: 0.85rem;
  font-weight: 600;
}

.review-new-glyph-tile {
  width: 56px; height: 56px;
  background: #1a1a1a;
  border: none;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; color: #fff;
  cursor: pointer;
  flex-shrink: 0;
  align-self: flex-start;
}
.review-new-glyph-tile:active { background: #333; }

.review-empty-hint {
  font-size: 0.72rem;
  color: #ccc;
  font-style: italic;
  margin-bottom: 0.4rem;
}

/* Download strip */
#review-download-strip {
  flex: none;
  display: flex;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid #e0e0e0;
  background: #fafafa;
}
#review-download-strip a {
  flex: 1; padding: 0.45rem;
  border: 1px solid #c0c8f0; border-radius: 6px;
  background: #f0f4ff; font-size: 0.78rem;
  color: #1a1a1a; text-align: center; text-decoration: none;
}

/* ── Glyph detail popover ────────────────────────────────────────────────── */
#glyph-popover {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}
#glyph-popover[hidden] { display: none; }

#glyph-popover-close {
  position: fixed;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #fff;
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem 0.5rem;
  z-index: 301;
}

#glyph-popover-layout {
  display: flex;
  flex-direction: row;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  width: min(90vw, 900px);
  height: min(80vh, 700px);
}

#glyph-popover-canvas {
  flex: 1 1 0;
  min-width: 0;
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}
#glyph-popover-canvas svg {
  width: 100%;
  height: 100%;
}

#glyph-popover-ui {
  width: 200px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 1.5rem 1rem 1rem;
  border-left: 1px solid #e0e0e0;
}

#glyph-popover-char {
  font-size: clamp(3rem, 8vw, 5rem);
  font-weight: 400;
  text-align: center;
  line-height: 1.4;
  position: relative;
  color: #1a1a1a;
  margin-bottom: 1rem;
}
/* Baseline and cap-height guide lines — same custom properties as #prompt-text */
#glyph-popover-char::before,
#glyph-popover-char::after {
  content: "";
  position: absolute;
  left: 0; right: 0;
  height: 0;
  border-top: 1px solid #aaa;
  pointer-events: none;
}
#glyph-popover-char::before { top: var(--guide-cap,      0.3em); }  /* cap-height */
#glyph-popover-char::after  { top: var(--guide-baseline, 1.0em); }  /* baseline   */

#glyph-popover-hint {
  font-size: 0.72rem;
  color: #bbb;
  letter-spacing: 0.04em;
  text-align: center;
  min-height: 1.2em;
  line-height: 1.2;
  margin-bottom: 0.25rem;
}

#glyph-popover-tools {
  flex: 1;
}

#glyph-popover-delete {
  width: 100%;
  padding: 0.7rem;
  background: #d32f2f;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
}
#glyph-popover-delete:active { background: #b71c1c; }

/* Right-handed: flip so UI column is on the left, canvas on the right */
body.right-handed #glyph-popover-layout { flex-direction: row-reverse; }
body.right-handed #glyph-popover-ui {
  border-left: none;
  border-right: 1px solid #e0e0e0;
}

/* ── Language picker modal ───────────────────────────────────────────────── */
#lang-picker {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 300;
}
#lang-picker[hidden] { display: none; }
#lang-picker-card {
  background: #fff;
  border-radius: 12px;
  width: min(360px, 92vw);
  max-height: 70vh;
  overflow: hidden;
  display: flex; flex-direction: column;
}
#lang-picker-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #eee;
  font-weight: 600;
  font-size: 0.9rem;
}
#lang-picker-list {
  overflow-y: auto;
  padding: 0.5rem 0;
}
.lang-picker-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.65rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
}
.lang-picker-item:active { background: #f5f5f5; }
.lang-picker-progress {
  font-size: 0.75rem;
  color: #999;
}

/* ── Glyph popover: "Write 5×" button ───────────────────────────────────── */
#glyph-popover-tools {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.glyph-write-more-btn {
  width: 100%;
  padding: 0.55rem 0.9rem;
  background: #1a1a1a;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
}
.glyph-write-more-btn:active { background: #333; }

/* ── Auth screen ─────────────────────────────────────────────── */
#screen-auth[hidden] { display: none; }
#screen-auth {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  background: #f5f5f5;
}
#auth-card {
  background: #fff;
  border-radius: 12px;
  padding: 2rem;
  width: min(360px, 90vw);
  box-shadow: 0 2px 16px rgba(0,0,0,.10);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
#auth-card h1 { margin: 0; font-size: 1.4rem; }
#auth-card p  { margin: 0; color: #555; font-size: 0.9rem; }
#auth-email {
  padding: 0.6rem 0.8rem;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 1rem;
  width: 100%;
  box-sizing: border-box;
}
#auth-submit {
  padding: 0.65rem 1rem;
  background: #1a1a1a;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
}
#auth-submit:disabled { opacity: 0.5; cursor: default; }
