:root {
  --bg: #f4f3ee;
  --panel: #ffffff;
  --ink: #1a1a1a;
  --muted: #7a7670;
  --accent: #2a6df5;
  --accent-soft: #dde7ff;
  --good: #2ea043;
  --border: #d8d3c8;
  --line: #ddd6c5;
  --paper: #ffffff;
  --shadow: 0 2px 6px rgba(0,0,0,0.06), 0 12px 32px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 2;
}
.topbar h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}
.tabs {
  display: flex;
  gap: 4px;
  margin-left: 8px;
}
.tab {
  background: transparent;
  border: 1px solid transparent;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
}
.tab.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent-soft);
}
.actions {
  margin-left: auto;
  display: flex;
  gap: 10px;
  align-items: center;
}
.name-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.name-field input {
  width: 160px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  color: var(--ink);
  background: #fff;
}

button {
  font: inherit;
  background: #fff;
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--ink);
}
button:hover { background: #faf8f3; }
button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
button.primary:hover { background: #1f5edc; }

main {
  flex: 1;
  position: relative;
  overflow: hidden;
}
.mode {
  position: absolute;
  inset: 0;
  display: none;
}
.mode.active { display: flex; }

/* Draw mode */
#drawMode {
  display: none;
  flex-direction: row;
  gap: 16px;
  padding: 16px;
}
#drawMode.active { display: flex; }

.canvas-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.prompt {
  position: relative;
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--panel);
  padding: 8px 12px;
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.font-toggle {
  width: 24px;
  height: 24px;
  padding: 0;
  font-size: 12px;
  line-height: 1;
  border-radius: 6px;
  color: var(--muted);
}
.font-toggle.open { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.font-picker {
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  width: 720px;
  max-width: 92vw;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 12px;
  z-index: 20;
}
.font-picker-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}
.font-picker-head b { color: var(--ink); font-size: 15px; }
.fp-head-right { display: flex; align-items: center; gap: 14px; }
.font-picker.drop { outline: 2px dashed var(--accent); outline-offset: 3px; }

.linkish {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
}
.linkish:hover { background: none; text-decoration: underline; }

.fp-cell.uploaded { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent-soft); }
.fp-cell.uploaded .fp-name { color: var(--accent); }
.font-picker-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  max-height: 56vh;
  overflow-y: auto;
}
.fp-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
}
.fp-cell:hover { border-color: var(--accent); background: var(--accent-soft); }
.fp-ch {
  font-size: 30px;
  line-height: 1.05;
  height: 38px;
  display: grid;
  place-items: center;
  color: var(--ink);
}
.fp-name {
  font-size: 8px;
  color: var(--muted);
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.prompt button {
  font-size: 22px;
  width: 38px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 999px;
  line-height: 1;
}
.char-display {
  display: flex;
  align-items: baseline;
  gap: 12px;
  min-width: 220px;
  justify-content: center;
}
.big-char {
  font-size: 36px;
  font-weight: 600;
  font-family: ui-serif, "Iowan Old Style", "Georgia", serif;
  line-height: 1;
}
#charDescription {
  color: var(--muted);
  font-size: 13px;
}
.progress {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.canvas-wrapper {
  position: relative;
  width: 600px;
  height: 600px;
  background: var(--paper);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: crosshair;
}
.canvas-wrapper canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
#ledgerCanvas { z-index: 0; }
#ghostCanvas  { z-index: 1; opacity: 0.18; pointer-events: none; }
#drawCanvas   { z-index: 2; pointer-events: none; }
#shapeCanvas  { z-index: 3; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 18px;
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  justify-content: center;
}
.tool-group {
  display: flex;
  gap: 4px;
}
.tool {
  width: 36px;
  height: 36px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 16px;
}
.tool.active,
.toolbar button.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.toolbar button:disabled { opacity: 0.4; cursor: default; }
.toolbar button:disabled:hover { background: #fff; }
.slider {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.slider input[type="range"] { width: 140px; }
.slider select {
  font: inherit;
  font-size: 13px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
.checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

/* Sidebar */
.grid-column {
  width: 320px;
  flex-shrink: 0;
  background: var(--panel);
  border-radius: 12px;
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.grid-column h3 {
  margin: 0 0 10px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.grid-column .muted { color: var(--muted); font-weight: 400; font-size: 12px; }
.glyph-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  overflow-y: auto;
  padding-right: 4px;
  flex: 1 1 0;
  min-height: 0; /* allow the flex item to shrink so it actually scrolls */
}
.glyph-cell {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.glyph-cell .label {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 9px;
  color: var(--muted);
  font-family: ui-serif, serif;
  pointer-events: none;
}
.glyph-cell canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.glyph-cell.drawn { border-color: var(--good); box-shadow: inset 0 0 0 1px var(--good); }
.glyph-cell.current { outline: 2px solid var(--accent); outline-offset: -2px; }
.glyph-cell:hover { border-color: var(--accent); }
.glyph-cell.pair { border-color: var(--accent); }
.glyph-cell.pair .label { font-size: 10px; letter-spacing: 0.02em; }

/* Ligature pairs panel */
.pairs-panel {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.pairs-head { font-size: 13px; font-weight: 600; margin-bottom: 8px; }
.add-pair { display: flex; gap: 6px; margin-bottom: 8px; }
.add-pair input {
  flex: 1;
  min-width: 0;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: #fff;
  color: var(--ink);
}
.add-pair button { padding: 5px 12px; }
.pairs-list { display: flex; flex-wrap: wrap; gap: 6px; }
.pairs-list .muted { color: var(--muted); font-size: 12px; }
.pair-chip {
  display: inline-flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}
.pair-go {
  border: none;
  background: none;
  padding: 4px 8px;
  font-size: 13px;
  cursor: pointer;
  color: var(--ink);
}
.pair-go:hover { background: var(--accent-soft); color: var(--accent); }
.pair-x {
  border: none;
  border-left: 1px solid var(--border);
  background: none;
  padding: 4px 8px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}
.pair-x:hover { background: #fdeaea; color: #c0392b; }
.hint {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
  color: var(--muted);
  font-size: 12px;
}
.hint p { margin: 4px 0; }
kbd {
  background: #f0ebe0;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}

/* Test mode */
#testMode {
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}
#testMode.active { display: flex; }
.test-controls {
  display: flex;
  gap: 18px;
  align-items: center;
  background: var(--panel);
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.test-controls label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.test-controls button.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
#testInput {
  width: 100%;
  height: 100px;
  resize: vertical;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: 14px ui-monospace, "SF Mono", Menlo, monospace;
  background: #fff;
  color: var(--ink);
}
.test-preview {
  flex: 1;
  background: #fff;
  border-radius: 12px;
  padding: 24px 28px;
  overflow: auto;
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.25;
  color: var(--ink);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 99;
  animation: toastIn 0.2s ease-out;
}
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 6px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}
