/* ── Reset & Dark Theme Variables ───────────────────────────────────────────── */

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

:root {
  --bg:       #0d1117;
  --surface:  #161b22;
  --surface2: #21262d;
  --border:   #30363d;
  --accent:   #00d2ff;
  --accent-d: rgba(0,210,255,0.14);
  --text:     #e6edf3;
  --muted:    #8b949e;
  --danger:   #f85149;
  --radius:   6px;
  --mono:     "Courier New", Courier, monospace;
}

/* ── Light Theme ────────────────────────────────────────────────────────────── */

[data-theme="light"] {
  --bg:       #f6f8fa;
  --surface:  #ffffff;
  --surface2: #eaeef2;
  --border:   #d0d7de;
  --accent:   #0969da;
  --accent-d: rgba(9,105,218,0.12);
  --text:     #1f2328;
  --muted:    #656d76;
  --danger:   #d1242f;
}

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.4;
}

/* ── App Shell ──────────────────────────────────────────────────────────────── */

#app { display: flex; flex-direction: column; height: 100vh; }

/* ── Header ─────────────────────────────────────────────────────────────────── */

#header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

#logo { display: flex; align-items: center; gap: 8px; margin-right: 2px; }
.logo-sym { font-size: 22px; color: var(--accent); font-family: var(--mono); line-height: 1; }
.logo-txt { font-size: 15px; font-weight: 600; letter-spacing: 0.3px; white-space: nowrap; }

/* ── Button Groups ──────────────────────────────────────────────────────────── */

.grp {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.btn {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s;
}
.btn:hover          { background: var(--surface2); color: var(--text); }
.btn.active         { background: var(--accent-d); color: var(--accent); }
.btn + .btn         { border-left: 1px solid var(--border); }
.btn:disabled       { opacity: 0.35; cursor: not-allowed; }
.btn:disabled:hover { background: transparent; color: var(--muted); }

.btn-solo {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 12px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.btn-solo:hover  { background: var(--surface2); color: var(--text); }
.btn-solo.active { background: var(--accent-d); color: var(--accent); border-color: var(--accent); }

a.btn-solo { text-decoration: none; display: inline-flex; align-items: center; }

.btn-danger         { color: var(--danger); }
.btn-danger:hover   { background: rgba(248,81,73,0.1); color: var(--danger); }
[data-theme="light"] .btn-danger:hover { background: rgba(209,36,47,0.08); }

/* ── Main Area ──────────────────────────────────────────────────────────────── */

#main { display: flex; flex: 1; overflow: hidden; position: relative; }

#canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #161b22;
  transition: background 0.2s;
}
[data-theme="light"] #canvas-container { background: #f6f8fa; }

#canvas { display: block; width: 100%; height: 100%; cursor: crosshair; }

#canvas-hint {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
  background: var(--surface);
  padding: 5px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  white-space: nowrap;
  transition: opacity 0.3s;
  max-width: 90%;
  text-align: center;
}
#canvas-hint.hidden { opacity: 0; }

/* ── Math Panel ─────────────────────────────────────────────────────────────── */

#math-panel {
  width: 0; min-width: 0; overflow: hidden;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: width 0.22s ease, min-width 0.22s ease;
}
#math-panel.open { width: 288px; min-width: 288px; overflow-y: auto; }

.panel-sec { padding: 14px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.panel-sec h3 {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.9px; color: var(--muted); margin-bottom: 10px;
}

.t-num { font-family: var(--mono); font-size: 22px; color: var(--accent); margin-bottom: 8px; }
.t-num span { color: var(--muted); font-size: 14px; }

input[type=range] {
  width: 100%; appearance: none; -webkit-appearance: none;
  height: 4px; border-radius: 2px; background: var(--border);
  outline: none; margin-bottom: 8px; cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--bg); cursor: pointer;
}
input[type=range]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--bg); cursor: pointer;
}

#animate-btn {
  width: 100%; padding: 7px; border: 1px solid var(--border); border-radius: var(--radius);
  background: transparent; color: var(--muted); font-size: 13px; font-family: inherit;
  cursor: pointer; text-align: center; transition: background 0.12s, color 0.12s, border-color 0.12s;
}
#animate-btn:hover  { background: var(--surface2); color: var(--text); }
#animate-btn.active { background: var(--accent-d); color: var(--accent); border-color: var(--accent); }

#formula-display { font-family: var(--mono); font-size: 12px; line-height: 1.7; color: var(--text); word-break: break-word; }
#formula-display .accent  { color: var(--accent); }
#formula-display .muted   { color: var(--muted); }
#formula-display .formula-line { font-size: 12px; }
#formula-display .formula-explicit { font-size: 11px; color: var(--muted); margin-top: 5px; }

#basis-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.basis-item { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.basis-label { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.basis-canvas { border: 1px solid var(--border); border-radius: 3px; display: block; width: 112px; height: 64px; }

#point-info { font-family: var(--mono); font-size: 13px; line-height: 1.8; color: var(--text); }
#point-info .accent { color: var(--accent); }
#point-info .muted  { color: var(--muted); font-size: 11px; }

#level-info { font-family: var(--mono); font-size: 11px; line-height: 1.6; }
.level-row { margin-bottom: 3px; }
.level-label { display: inline-block; width: 80px; color: var(--muted); font-size: 10px; }
.level-pt { margin-right: 4px; }

#math-panel::-webkit-scrollbar { width: 5px; }
#math-panel::-webkit-scrollbar-track { background: var(--surface); }
#math-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Toolbar ─────────────────────────────────────────────────────────────────── */

#toolbar {
  display: flex; align-items: center; gap: 12px;
  padding: 7px 14px; background: var(--surface);
  border-top: 1px solid var(--border); flex-shrink: 0; flex-wrap: wrap;
}

.tbar-group { display: flex; align-items: center; gap: 7px; }
.tbar-label { font-size: 12px; color: var(--muted); white-space: nowrap; }
.tbar-sep { width: 1px; height: 20px; background: var(--border); }

label.toggle {
  display: flex; align-items: center; gap: 5px;
  font-size: 13px; color: var(--muted); cursor: pointer; user-select: none;
}
label.toggle:hover { color: var(--text); }
label.toggle input { accent-color: var(--accent); cursor: pointer; }

#z-slider-wrap input[type=range] { width: 120px; margin-bottom: 0; }

/* ── Misc ────────────────────────────────────────────────────────────────────── */

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

#kbd-help {
  margin-left: auto; font-size: 11px; color: var(--muted); white-space: nowrap;
}
#kbd-help kbd {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 3px; padding: 1px 5px; font-family: var(--mono); font-size: 10px;
}

#cam-hint { font-size: 11px; }

/* Speed control */
#speed-group {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
}
.speed-label {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}
.speed-grp { flex-wrap: nowrap; }
.speed-btn { padding: 3px 6px; font-size: 11px; }

/* ── Font Panel (overlay) ────────────────────────────────────────────────────── */

#font-panel {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: none;
  pointer-events: none;   /* let canvas receive events through transparent area */
  z-index: 10;
}
#font-panel.open { display: flex; }

#font-sidebar {
  width: 296px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  pointer-events: all;    /* sidebar captures its own events */
  box-shadow: 4px 0 24px rgba(0,0,0,0.35);
}

/* ── Sidebar header ── */
.font-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.font-title { font-size: 14px; font-weight: 600; color: var(--text); }
#font-close-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  padding: 3px 9px;
  font-size: 13px;
  line-height: 1.4;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
#font-close-btn:hover { background: var(--surface2); color: var(--text); border-color: var(--muted); }

/* ── Current letter display ── */
.font-current-section {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--surface2);
}
#font-current-display {
  font-size: 64px;
  font-weight: 700;
  color: var(--accent);
  font-family: var(--mono);
  line-height: 1;
  min-width: 52px;
  text-align: center;
  text-shadow: 0 0 18px rgba(0,210,255,0.3);
}
.font-current-info { flex: 1; }
.font-current-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}
#font-current-name { font-size: 15px; font-weight: 600; color: var(--text); margin-top: 2px; }

/* ── Tool buttons row ── */
.font-tools-row {
  display: flex;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.font-tool-btn {
  flex: 1;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 4px;
  cursor: pointer;
  font-size: 11px;
  font-family: inherit;
  font-weight: 500;
  text-align: center;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  white-space: nowrap;
}
.font-tool-btn:hover { background: var(--surface2); color: var(--text); }
.font-tool-accent { color: var(--accent); border-color: rgba(0,210,255,0.3); }
.font-tool-accent:hover { background: var(--accent-d); }
.font-tool-danger { color: var(--danger); border-color: rgba(248,81,73,0.25); }
.font-tool-danger:hover { background: rgba(248,81,73,0.08); color: var(--danger); }
[data-theme="light"] .font-tool-danger:hover { background: rgba(209,36,47,0.07); }

/* ── Section headers ── */
.font-section-header {
  padding: 10px 14px 5px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.1px;
  color: var(--muted);
  flex-shrink: 0;
}
.font-section-header-inner { padding-top: 0; }

/* ── Letter grid ── */
#font-letter-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 5px;
  padding: 4px 14px 14px;
}
.font-letter-btn {
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0;
  cursor: pointer;
  font-size: 14px;
  font-family: var(--mono);
  font-weight: 700;
  text-align: center;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, color 0.1s, border-color 0.1s, box-shadow 0.1s;
}
.font-letter-btn:hover {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--muted);
}
.font-letter-btn.active {
  background: var(--accent-d);
  color: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0,210,255,0.18);
}
.font-letter-btn.modified {
  color: var(--text);
  border-color: rgba(0,210,255,0.45);
  background: rgba(0,210,255,0.05);
}
[data-theme="light"] .font-letter-btn.modified {
  border-color: rgba(9,105,218,0.35);
  background: rgba(9,105,218,0.05);
}
.font-letter-btn.active.modified {
  color: var(--accent);
  background: var(--accent-d);
}

/* ── Preview section ── */
.font-preview-section {
  border-top: 1px solid var(--border);
  padding: 12px 14px 16px;
  flex-shrink: 0;
  margin-top: auto;
}
.font-preview-input-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}
.font-preview-input-row #font-preview-input {
  flex: 1;
  min-width: 0;
  margin-bottom: 0;
}
#font-preview-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  font-family: inherit;
  margin-bottom: 8px;
  outline: none;
  box-sizing: border-box;
  transition: border-color 0.12s;
}
#font-preview-input:focus { border-color: var(--accent); }
#font-expand-btn { flex-shrink: 0; font-size: 15px; padding: 0 8px; }
#font-preview-canvas {
  width: 100%;
  display: block;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.font-tip {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.55;
  margin-top: 10px;
}
.font-tip strong { color: var(--text); font-weight: 600; }

#font-sidebar::-webkit-scrollbar { width: 5px; }
#font-sidebar::-webkit-scrollbar-track { background: var(--surface); }
#font-sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Big preview overlay ── */
#font-big-preview {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.82);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
#font-big-preview-inner {
  width: min(96vw, 1100px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) * 2);
  box-shadow: 0 24px 80px rgba(0,0,0,0.55);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#font-big-preview-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
#font-big-preview-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.12s;
}
#font-big-preview-input:focus { border-color: var(--accent); }
#font-big-preview-close {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 32px; height: 32px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.12s, border-color 0.12s;
}
#font-big-preview-close:hover { color: var(--text); border-color: var(--text); }
#font-big-preview-canvas {
  display: block;
  width: 100%;
}

#back-link {
  color: var(--muted, #888);
  text-decoration: none;
  font-size: 0.8rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border, #444);
  border-radius: 4px;
  white-space: nowrap;
  transition: color 0.2s, border-color 0.2s;
}
#back-link:hover { color: var(--text, #eee); border-color: var(--text, #eee); }
