:root {
  --ink: #181818;
  --muted: #686863;
  --line: #c7c6bf;
  --line-dark: #8f8e87;
  --paper: #f1f0ec;
  --panel: #f5f4ef;
  --ground: #e7e6e0;
  --accent: #ff4f1f;
  --control-width: 344px;
  --canvas-ratio: 1.6;
}

html[data-theme="dark"] {
  --ink: #f0efe9;
  --muted: #aaa9a3;
  --line: #383834;
  --line-dark: #5b5a55;
  --panel: #1d1d1b;
  --ground: #11110f;
}

* { box-sizing: border-box; border-radius: 0 !important; }
html, body { margin: 0; min-height: 100%; }

body {
  overflow: hidden;
  color: var(--ink);
  background: var(--ground);
  font-family: "Courier New", Courier, monospace;
}

button, input, textarea, select { font: inherit; }
button, select, input[type="range"], input[type="color"], .file-label { cursor: pointer; }

.topbar {
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--ink);
  background: #f2f1ec;
}

.brand { display: flex; align-items: center; gap: 10px; font-size: 12px; font-weight: 700; letter-spacing: -.02em; text-transform: uppercase; }
.brand-mark { display: grid; place-items: center; width: 28px; height: 28px; color: #fff; background: var(--ink); font-size: 15px; }
.top-actions { display: flex; gap: 6px; }
.theme-button { min-width: 82px; }

.button, .icon-button {
  min-height: 34px;
  padding: 0 13px;
  border: 1px solid var(--ink);
  color: var(--ink);
  background: #f7f6f1;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}
.button:hover, .icon-button:hover { color: #fff; background: var(--ink); }
.button:active, .icon-button:active { background: #353535; }
.button.dark { color: #fff; background: var(--ink); }
.button.dark:hover { color: var(--ink); background: #f7f6f1; }
.button.accent { color: #fff; border-color: var(--accent); background: var(--accent); }
.button.accent:hover { color: var(--accent); background: #fff; }
.button.ghost { border-color: var(--line-dark); background: transparent; }
.icon-button { width: 38px; padding: 0; font-size: 18px; }
.grow { flex: 1; }

.workspace { height: calc(100vh - 58px); display: grid; grid-template-columns: var(--control-width) 1fr; }

.controls {
  overflow-y: auto;
  border-right: 1px solid var(--ink);
  background: #ecebe5;
  scrollbar-width: thin;
  scrollbar-color: var(--line-dark) transparent;
}

.control-section { padding: 18px 16px 20px; border-bottom: 1px solid var(--line-dark); background: var(--panel); }
.control-section:nth-child(even) { background: #eeede7; }
.section-kicker { margin-bottom: 15px; color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: .08em; }
.control-section > label:not(.file-label):not(.check-row) { display: block; margin-bottom: 7px; font-size: 11px; font-weight: 700; text-transform: uppercase; }

textarea, select, input[type="number"] {
  width: 100%;
  border: 1px solid var(--line-dark);
  color: var(--ink);
  background: #fbfaf6;
  outline: none;
}
textarea { min-height: 96px; padding: 10px; resize: vertical; font: 14px/1.45 "Courier New", Courier, monospace; }
select { height: 36px; padding: 0 9px; font-size: 11px; }
textarea:focus, select:focus, input[type="number"]:focus { border-color: var(--accent); outline: 1px solid var(--accent); }
.split-row { display: flex; gap: 6px; margin-top: 8px; }

#fontUpload { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.file-label { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; padding: 10px; border: 1px solid var(--line-dark); background: transparent; font-size: 10px; }
.file-label:hover { background: #fff; }
.file-label span:first-child { font-weight: 700; text-transform: uppercase; }
.file-label span:last-child { color: var(--muted); font-size: 8px; }
.font-status { margin-top: 7px; color: var(--muted); font-size: 9px; line-height: 1.4; }

.range-head { display: flex; justify-content: space-between; align-items: baseline; margin: 13px 0 6px; }
.range-head:first-of-type { margin-top: 0; }
.range-head label { font-size: 10px; font-weight: 700; text-transform: uppercase; }
.range-head output { color: var(--muted); font-size: 9px; }
input[type="range"] { width: 100%; height: 16px; margin: 0; accent-color: var(--accent); }

.mode-toggle {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding: 8px 9px;
  border: 1px solid var(--ink);
  color: var(--ink);
  background: #f7f6f1;
  text-align: left;
}
.mode-toggle:hover { background: #fff; }
.mode-copy { display: flex; flex-direction: column; gap: 3px; }
.mode-copy strong { font-size: 11px; text-transform: uppercase; }
.mode-copy small { color: var(--muted); font-size: 8px; text-transform: uppercase; }
.mode-state { min-width: 39px; padding: 5px 6px; border: 1px solid var(--line-dark); background: #deddd7; font-size: 9px; font-weight: 700; text-align: center; }
.mode-toggle[aria-pressed="true"] { color: #fff; border-color: var(--ink); background: var(--ink); }
.mode-toggle[aria-pressed="true"] .mode-copy small { color: #c9c9c5; }
.mode-toggle[aria-pressed="true"] .mode-state { color: #fff; border-color: var(--accent); background: var(--accent); }

.ratio-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.ratio-preset {
  min-height: 88px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 8px 5px 7px;
  border: 1px solid var(--line-dark);
  color: var(--ink);
  background: #efeee8;
}
.ratio-preset:hover { background: #fff; }
.ratio-preset.active { color: #fff; border-color: var(--ink); background: var(--ink); }
.ratio-name { align-self: flex-start; font-size: 9px; font-weight: 700; }
.ratio-shape {
  display: block;
  width: min(38px, calc(38px * var(--shape-ratio)));
  height: min(38px, calc(38px / var(--shape-ratio)));
  max-width: 43px;
  max-height: 38px;
  aspect-ratio: var(--shape-ratio);
  border: 1px solid currentColor;
}
.ratio-size { color: inherit; font-size: 7px; letter-spacing: -.05em; opacity: .7; }

.dimension-row { display: grid; grid-template-columns: 1fr auto 1fr auto; align-items: end; gap: 6px; margin-top: 10px; }
.dimension-row label { color: var(--muted); font-size: 8px; font-weight: 700; }
.dimension-row input { height: 34px; margin-top: 3px; padding: 0 8px; color: var(--ink); font-size: 11px; }
.dimension-row > span { padding-bottom: 9px; color: var(--muted); font-size: 10px; }
.dimension-row .unit { padding-bottom: 10px; font-size: 8px; }
.canvas-divider { height: 1px; margin: 15px 0; background: var(--line); }

.swatch-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.swatch-row label { display: flex; align-items: center; justify-content: space-between; padding: 7px 8px; border: 1px solid var(--line-dark); background: transparent; font-size: 9px; font-weight: 700; text-transform: uppercase; }
input[type="color"] { width: 25px; height: 25px; padding: 0; border: 1px solid var(--line-dark); background: none; }
.check-row { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 10px; font-weight: 700; text-transform: uppercase; }
.check-row input { width: 14px; height: 14px; accent-color: var(--accent); }

.hint { padding: 16px; border-bottom: 1px solid var(--line-dark); color: var(--muted); background: #deddd7; font-size: 9px; line-height: 1.5; }
.hint span { margin-right: 5px; color: var(--accent); font-weight: 800; }

.stage-area { min-width: 0; display: flex; flex-direction: column; padding: 14px 18px 12px; background: var(--ground); }
.stage-meta, .stage-footer { display: flex; justify-content: space-between; color: var(--muted); font-size: 9px; line-height: 1.2; letter-spacing: .02em; text-transform: uppercase; }
.stage-meta { padding-bottom: 10px; }
.stage-footer { padding-top: 9px; }
.canvas-frame { flex: 1; min-height: 0; display: grid; place-items: center; overflow: hidden; border: 1px solid var(--line); background: #dcdad3; }

.artboard { position: relative; max-width: 100%; max-height: 100%; aspect-ratio: var(--canvas-ratio); overflow: hidden; outline: 1px solid var(--ink); background: var(--paper); user-select: none; touch-action: none; }
.artboard.grain::after {
  content: ""; position: absolute; inset: 0; z-index: 9999; pointer-events: none; opacity: .11; mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 180 180' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.42'/%3E%3C/svg%3E");
}

.glyph { position: absolute; display: block; line-height: .78; transform-origin: center; transform-style: preserve-3d; backface-visibility: visible; color: var(--glyph-ink, #161616); cursor: grab; white-space: pre; will-change: left, top, transform; }
.artboard.mode-transition .glyph { transition: left .28s cubic-bezier(.2,.8,.2,1), top .28s cubic-bezier(.2,.8,.2,1); }
.glyph:active { cursor: grabbing; }
.glyph.selected { outline: 1px solid var(--accent); outline-offset: 4px; z-index: 9000 !important; }
.show-bounds .glyph:not(.selected) { outline: 1px solid rgba(255,79,31,.38); }

.toast { position: fixed; right: 18px; bottom: 18px; z-index: 10000; padding: 10px 12px; border: 1px solid var(--ink); color: #fff; background: var(--ink); font-size: 10px; opacity: 0; transform: translateY(7px); pointer-events: none; transition: .15s ease; }
.toast.show { opacity: 1; transform: translateY(0); }

html[data-theme="dark"] .topbar { background: #151513; }
html[data-theme="dark"] .brand-mark { color: #11110f; background: var(--ink); }
html[data-theme="dark"] .controls { background: #171715; }
html[data-theme="dark"] .control-section:nth-child(even) { background: #242421; }
html[data-theme="dark"] .button,
html[data-theme="dark"] .icon-button { color: var(--ink); background: #242421; }
html[data-theme="dark"] .button:hover,
html[data-theme="dark"] .icon-button:hover { color: #11110f; background: var(--ink); }
html[data-theme="dark"] .button:active,
html[data-theme="dark"] .icon-button:active { color: #11110f; background: #c9c8c1; }
html[data-theme="dark"] .button.dark { color: #11110f; background: var(--ink); }
html[data-theme="dark"] .button.dark:hover { color: var(--ink); background: #242421; }
html[data-theme="dark"] .button.accent { color: #fff; border-color: var(--accent); background: var(--accent); }
html[data-theme="dark"] .button.accent:hover { color: var(--accent); background: #11110f; }
html[data-theme="dark"] textarea,
html[data-theme="dark"] select,
html[data-theme="dark"] input[type="number"] { color: var(--ink); background: #151513; }
html[data-theme="dark"] .file-label:hover { background: #2a2a27; }
html[data-theme="dark"] .mode-toggle { color: var(--ink); background: #242421; }
html[data-theme="dark"] .mode-toggle:hover { background: #2a2a27; }
html[data-theme="dark"] .mode-state { background: #353531; }
html[data-theme="dark"] .mode-toggle[aria-pressed="true"] { color: #11110f; background: var(--ink); }
html[data-theme="dark"] .mode-toggle[aria-pressed="true"] .mode-copy small { color: #5d5c57; }
html[data-theme="dark"] .mode-toggle[aria-pressed="true"] .mode-state { color: #fff; background: var(--accent); }
html[data-theme="dark"] .ratio-preset { color: var(--ink); background: #242421; }
html[data-theme="dark"] .ratio-preset:hover { background: #2a2a27; }
html[data-theme="dark"] .ratio-preset.active { color: #11110f; background: var(--ink); }
html[data-theme="dark"] .hint { background: #242421; }
html[data-theme="dark"] .canvas-frame { background: #20201e; }
html[data-theme="dark"] .toast { color: #11110f; background: var(--ink); }

@media (max-width: 820px) {
  body { overflow: auto; }
  .workspace { height: auto; min-height: calc(100vh - 58px); grid-template-columns: 1fr; }
  .controls { display: grid; grid-template-columns: 1fr 1fr; overflow: visible; border-right: 0; border-top: 1px solid var(--ink); }
  .control-section { border-right: 1px solid var(--line-dark); }
  .intro { grid-column: 1 / -1; }
  .hint { display: none; }
  .stage-area { min-height: 66vw; order: -1; }
}

@media (max-width: 540px) {
  .topbar { padding: 0 9px; }
  .brand span:last-child { display: none; }
  .top-actions { gap: 3px; }
  .top-actions .button { padding-inline: 8px; }
  .theme-button { min-width: 74px; }
  .workspace { display: flex; flex-direction: column; }
  .stage-area { order: 0; min-height: 78vw; padding: 10px; }
  .controls { order: 1; display: block; }
  .stage-footer span:first-child { display: none; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition-duration: .01ms !important; } }
