:root {
  --bg-root: #0b0c10;
  --bg-panel: #12141a;
  --bg-elevated: #191c24;
  --bg-hover: #222631;
  --border-subtle: #252936;
  --border-strong: #343a4c;
  --text-primary: #f3f4f6;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --accent: #d4ff33;
  --accent-dim: rgba(212, 255, 51, 0.14);
  --node-anchor: #38bdf8;
  --node-handle: #f59e0b;
  --danger: #ef4444;
  --font-ui: -apple-system, BlinkMacSystemFont, "Inter", "SF Pro Text", "Noto Sans TC", sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Fira Code", monospace;
}

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

html, body {
  height: 100%;
  background: var(--bg-root);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.4;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

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

/* Topbar */
.topbar {
  height: 52px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

.brand-name {
  font-weight: 700;
  letter-spacing: 0.08em;
  font-size: 14px;
  color: var(--text-primary);
}

.brand-tag {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
  border: 1px solid var(--border-subtle);
  padding: 2px 6px;
  border-radius: 4px;
  letter-spacing: 0.06em;
}

.view-switcher {
  display: flex;
  background: var(--bg-root);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 3px;
  gap: 4px;
}

.tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  color: var(--text-secondary);
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  background: var(--bg-elevated);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--border-strong);
}

.export-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  margin-right: 6px;
}

.btn {
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-primary {
  background: var(--accent);
  color: #0b0c10;
}

.btn-primary:hover {
  filter: brightness(1.08);
  transform: translateY(-0.5px);
}

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.btn-secondary:hover {
  background: var(--bg-hover);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 11px;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.14);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.35);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.25);
}

.btn-danger:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.btn-text {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
}

.btn-text:hover {
  color: var(--accent);
}

/* Workspace Container */
.workspace-container {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* Sidebar */
.sidebar {
  width: 290px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  padding: 16px;
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.sidebar-header h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--text-secondary);
  font-weight: 600;
}

.presets-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 18px;
}

.preset-chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
}

.preset-chip:hover {
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.preset-chip.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.sliders-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.slider-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.slider-label-row label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary);
}

.zh-sub {
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 400;
  margin-left: 4px;
}

.param-value {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--accent);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--bg-hover);
  border-radius: 2px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-primary);
  border: 2px solid var(--bg-panel);
  box-shadow: 0 0 0 1px var(--border-strong);
  cursor: pointer;
  transition: transform 0.1s ease, background 0.1s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
  background: var(--accent);
  transform: scale(1.12);
}

.metrics-info {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid var(--border-subtle);
}

.metrics-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.metrics-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
}

.metrics-grid span:nth-child(even) {
  color: var(--text-primary);
  text-align: right;
}

/* Main Stage */
.main-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-root);
}

.workspace-view {
  display: none;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}

.workspace-view.active {
  display: flex;
}

/* Specimen Toolbar */
.toolbar {
  height: 48px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  padding: 0 18px;
  gap: 18px;
  flex-shrink: 0;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar-group label {
  font-size: 11px;
  color: var(--text-secondary);
}

.toolbar-group input[type="range"] {
  width: 110px;
}

.toolbar-val {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-primary);
  min-width: 36px;
}

.toolbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border-subtle);
}

.segmented-control {
  display: flex;
  background: var(--bg-root);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 2px;
}

.seg-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
}

.seg-btn.active {
  background: var(--bg-elevated);
  color: var(--accent);
}

.checkbox-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
  cursor: pointer;
  margin-left: auto;
}

.checkbox-pill input {
  accent-color: var(--accent);
}

.specimen-editor-bar {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(18, 20, 26, 0.6);
}

#specimen-input {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 10px 14px;
  resize: vertical;
  outline: none;
}

#specimen-input:focus {
  border-color: var(--accent);
}

.specimen-canvas-wrap {
  flex: 1;
  overflow: auto;
  padding: 32px;
  background-image: radial-gradient(rgba(255, 255, 255, 0.04) 1px, transparent 1px);
  background-size: 24px 24px;
}

#specimen-svg {
  display: block;
  min-width: 100%;
}

/* Glyphs View */
.glyphs-layout {
  display: flex;
  flex: 1;
  min-height: 0;
}

.glyph-grid-panel {
  width: 340px;
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  flex-shrink: 0;
}

.glyph-filter-bar {
  display: flex;
  gap: 4px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-subtle);
  flex-wrap: wrap;
}

.filter-chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  border-radius: 5px;
  padding: 4px 8px;
  font-size: 11px;
  cursor: pointer;
}

.filter-chip.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.glyph-grid {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  align-content: start;
}

.glyph-cell {
  aspect-ratio: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 6px;
  cursor: pointer;
  position: relative;
  transition: all 0.12s ease;
}

.glyph-cell:hover {
  border-color: var(--border-strong);
  background: var(--bg-hover);
}

.glyph-cell.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.glyph-cell svg {
  width: 100%;
  height: 75%;
}

.glyph-cell-label {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
}

.glyph-edited-dot {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--node-anchor);
}

/* Node Editor Panel */
.node-editor-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-root);
}

.editor-toolbar {
  height: 52px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
}

.editor-char-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.char-badge {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
}

.char-meta {
  display: flex;
  flex-direction: column;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
}

.edited-tag {
  font-size: 10px;
  background: rgba(56, 189, 248, 0.15);
  color: var(--node-anchor);
  border: 1px solid rgba(56, 189, 248, 0.35);
  padding: 2px 8px;
  border-radius: 99px;
}

.hidden {
  display: none !important;
}

.editor-actions {
  display: flex;
  gap: 8px;
}

.editor-canvas-wrap {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px);
  background-size: 32px 32px;
}

#editor-svg {
  width: 100%;
  height: 100%;
  max-width: 820px;
  max-height: 820px;
  user-select: none;
}

.editor-hint {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(18, 20, 26, 0.9);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font-size: 11px;
  padding: 6px 14px;
  border-radius: 99px;
  pointer-events: none;
  white-space: nowrap;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--bg-elevated);
  border: 1px solid var(--accent);
  color: var(--text-primary);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  z-index: 999;
}

/* ==========================================================================
   Responsive & touch adjustments (iPad / iPhone)
   ========================================================================== */

/* Use the dynamic viewport so Safari's toolbars never cover the bottom edge */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
  .app-shell { height: 100dvh; }
}

/* Dragging nodes must not scroll or zoom the page on touch screens */
#editor-svg {
  touch-action: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.tab-btn,
.btn,
.seg-btn,
.preset-chip,
.filter-chip,
.checkbox-pill {
  white-space: nowrap;
}

/* Finger-sized controls on touch devices */
@media (pointer: coarse) {
  input[type="range"] { height: 6px; }
  input[type="range"]::-webkit-slider-thumb { width: 24px; height: 24px; }
  .preset-chip { padding: 9px 8px; }
  .filter-chip { padding: 7px 11px; }
  .seg-btn { padding: 7px 12px; }
  .btn-sm { padding: 8px 12px; }
  .btn-text { padding: 6px 4px; font-size: 12px; }
}

.checkbox-pill-follow { margin-left: 0; }

.back-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  margin-right: 2px;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 15px;
  text-decoration: none;
}
.back-link:hover { color: var(--accent); border-color: var(--accent); }

/* Font name / designer fields */
.font-identity {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary);
}
.field input {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 13px;
  padding: 7px 10px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }
@media (pointer: coarse) {
  /* 16px stops iOS Safari from zooming the page when a field is focused */
  .field input, #specimen-input { font-size: 16px; }
}

/* --- iPad landscape and small laptops --------------------------------- */
@media (max-width: 1200px) {
  .brand-tag { display: none; }
  .sidebar { width: 260px; }
  .glyph-grid-panel { width: 280px; }
  .glyph-grid { grid-template-columns: repeat(3, 1fr); }
  .toolbar {
    height: auto;
    min-height: 48px;
    flex-wrap: wrap;
    padding: 8px 18px;
    gap: 10px 18px;
  }
  .editor-toolbar {
    height: auto;
    min-height: 52px;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 18px;
  }
  .editor-hint {
    white-space: normal;
    text-align: center;
    width: max-content;
    max-width: calc(100% - 28px);
    border-radius: 12px;
  }
}

/* --- iPad portrait: parameters move above the stage -------------------- */
@media (max-width: 960px) {
  .status-badge { display: none; }
  .topbar { padding: 0 12px; gap: 10px; }
  .tab-btn { padding: 6px 10px; }

  .workspace-container { flex-direction: column; }
  .main-stage { min-height: 0; }

  .sidebar {
    width: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-subtle);
    padding: 12px 16px 14px;
    overflow: visible;
  }
  .sidebar-header { margin-bottom: 8px; }
  .presets-bar { grid-template-columns: repeat(4, 1fr); margin-bottom: 12px; }
  .font-identity { grid-template-columns: 1fr 1fr; gap: 12px 20px; margin-bottom: 12px; }
  .sliders-list {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px 20px;
  }
  .metrics-info { display: none; }

  .toolbar { padding: 8px 16px; }
  .toolbar-divider { display: none; }
  .specimen-editor-bar { padding: 10px 16px; }
  .specimen-canvas-wrap { padding: 20px 16px; }

  /* Glyphs view: grid becomes a swipeable strip above the editor */
  .glyphs-layout { flex-direction: column; }
  .glyph-grid-panel {
    width: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-subtle);
  }
  .glyph-filter-bar { padding: 8px 12px; }
  .glyph-grid {
    flex: none;
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 10px 12px;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }
  .glyph-cell {
    flex: 0 0 76px;
    height: 76px;
    scroll-snap-align: start;
  }
  .node-editor-panel { min-height: 0; }
  .editor-canvas-wrap { min-height: 0; }
  #editor-svg { max-width: 100%; max-height: 100%; }
  .editor-toolbar { padding: 8px 16px; }
}

/* --- iPhone and narrow split view ------------------------------------- */
@media (max-width: 640px) {
  .topbar {
    height: auto;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 8px;
  }
  .view-switcher { order: 3; width: 100%; }
  .tab-btn { flex: 1; justify-content: center; }
  .export-actions .btn { padding: 7px 10px; }

  .workspace-container { overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .sidebar { flex-shrink: 0; }
  .presets-bar { grid-template-columns: repeat(2, 1fr); }
  .sliders-list { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
  .zh-sub { display: none; }

  .main-stage { flex: none; min-height: 78vh; }
  .toolbar-group input[type="range"] { width: 90px; }
  .checkbox-pill { margin-left: 0; }
  .editor-actions { width: 100%; }
  .editor-actions .btn { flex: 1; }
  .glyph-cell { flex-basis: 64px; height: 64px; }
  .toast { left: 12px; right: 12px; bottom: 12px; text-align: center; }
}
