:root {
  --bg: #f3f2ef;
  --panel: #ffffff;
  --line: #e1dfd9;
  --text: #1f1f1d;
  --muted: #8a877f;
  --accent: #2f62e8;
  --accent-soft: #e6ecfd;
  --danger: #c83a3a;
  --radius: 6px;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", sans-serif;
  font-size: 13px;
  color: var(--text);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  display: grid;
  grid-template-columns: 290px 1fr 260px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "top top top" "left center right";
  overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }
select { border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 6px; background: #fff; max-width: 100%; }
#right select { width: 100%; }

button {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: #c4c1b8; }
button.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button:disabled { opacity: .4; cursor: default; }

input[type="text"], input[type="number"], input[type="search"] {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px 8px;
  background: #fff;
  width: 100%;
  min-width: 0;
}
input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; min-width: 2.5em; display: inline-block; }
label { display: block; margin: 8px 0 3px; color: #55534d; }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; white-space: nowrap; }
label.inline input[type="range"] { width: 90px; }

/* ---------- top bar ---------- */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
#topbar .brand { font-weight: 650; letter-spacing: -.01em; }
#topbar .group { display: flex; align-items: center; gap: 8px; }
#topbar .spacer { flex: 1; }

/* ---------- glyph list ---------- */
#left {
  grid-area: left;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}
#search { margin: 10px; width: auto; }
#glyphList { overflow-y: auto; padding: 0 10px 20px; flex: 1; }
#glyphList h4 {
  margin: 14px 0 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(50px, 1fr)); gap: 4px; }
.cell {
  position: relative;
  aspect-ratio: 1 / 1.15;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
}
.cell:hover { border-color: #bdb9ae; }
.cell.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.cell canvas { position: absolute; inset: 0 0 14px 0; width: 100%; height: calc(100% - 14px); }
.cell .ph {
  position: absolute; inset: 0 0 14px 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; color: #d3d0c8; pointer-events: none;
}
.cell.drawn .ph { display: none; }
.cell .nm {
  position: absolute; left: 0; right: 0; bottom: 0; height: 14px;
  font-size: 9px; line-height: 14px; text-align: center; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 2px;
  border-top: 1px solid #f1efea;
}
.cell .badge {
  position: absolute; top: 2px; right: 2px;
  font-size: 9px; line-height: 13px; padding: 0 4px; border-radius: 7px;
  background: var(--accent); color: #fff;
}

/* ---------- center ---------- */
#center {
  grid-area: center;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
#glyphbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: #faf9f7;
  overflow-x: auto;
}
#glyphTitle { font-weight: 600; margin-right: 6px; white-space: nowrap; }
#glyphTitle .ch { font-size: 18px; margin-right: 6px; }
#variantTabs { display: flex; gap: 4px; }
#variantTabs button { padding: 4px 9px; }
#variantTabs button.empty { color: var(--muted); }
#variantTabs .tabx { margin-left: 6px; padding: 0 3px; border-radius: 3px; color: var(--muted); }
#variantTabs .tabx:hover { background: #f3d6d6; color: var(--danger); }

#stage { position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none; }
#stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#cOver { cursor: none; }
#stageInfo { position: absolute; left: 10px; bottom: 8px; font-size: 11px; pointer-events: none; }

#preview {
  border-top: 1px solid var(--line);
  background: var(--panel);
  height: 150px;
  display: flex; flex-direction: column;
}
.previewbar { display: flex; align-items: center; gap: 12px; padding: 6px 10px; }
.previewbar input[type="text"] { max-width: 420px; }
#previewCanvasWrap { position: relative; flex: 1; min-height: 0; }
#cPreview { position: absolute; inset: 0; width: 100%; height: 100%; cursor: pointer; }

/* ---------- right panel ---------- */
#right {
  grid-area: right;
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 14px 30px;
}
#right section { padding: 10px 0 14px; border-bottom: 1px solid var(--line); }
#right h3 { margin: 4px 0 6px; font-size: 12px; font-weight: 650; }
#right h3 .muted { font-weight: 400; }
#right .row { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
#right .row input[type="number"] { width: 80px; flex: none; }
#right .row input[type="range"] { flex: 1; }
#right input[type="range"] { width: 100%; }
#right label.inline { margin-top: 6px; display: flex; }
.metrics { display: grid; grid-template-columns: 1fr 90px; gap: 4px 8px; align-items: center; }
.metrics label { margin: 0; }
.spacing { display: grid; grid-template-columns: 1fr 48px 48px 48px; gap: 4px; align-items: center; }
.spacing label { margin: 0; }
.spacing input { padding: 4px 5px; }
.spacing .muted { font-size: 10px; text-align: center; }
#right .row #lsb, #right .row #rsb { width: 62px; }
.hint { font-size: 11px; line-height: 1.45; margin: 6px 0 0; }
.ligform { display: flex; gap: 4px; margin-top: 6px; }
.ligform input { flex: 1; }
.help p { line-height: 1.9; margin: 0; color: #55534d; }
.credits { font-size: 11px; line-height: 1.5; margin: 14px 0 4px; }
.credits a { color: inherit; }

#welcome {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(31, 31, 29, .35); padding: 16px;
}
#welcome[hidden] { display: none; }
.welcome-card {
  background: var(--panel); border-radius: 10px; max-width: 420px;
  padding: 20px 22px 18px; box-shadow: 0 12px 40px rgba(0, 0, 0, .18);
  font-size: 14px; line-height: 1.5;
}
.welcome-card h2 { margin: 0 0 10px; font-size: 17px; font-weight: 650; }
.welcome-card p { margin: 0 0 10px; }
.welcome-actions { display: flex; justify-content: flex-end; margin-top: 14px; }

/* Phones: the three-column layout doesn't fit, so show a notice instead */
#smallScreen { display: none; }
@media (max-width: 760px) {
  #smallScreen {
    position: fixed; inset: 0; z-index: 200;
    display: flex; align-items: center; justify-content: center;
    background: var(--bg); padding: 16px;
  }
  #welcome { display: none; }
}

kbd {
  font: 11px ui-monospace, Menlo, monospace;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px;
  padding: 0 4px; background: #faf9f7;
}
