:root {
  --ink: #17201d;
  --muted: #66706c;
  --paper: #f4f1ea;
  --surface: #fffefa;
  --line: #d9d7cf;
  --accent: #d45b2c;
  --dark: #163d33;
  --gold: #b87912;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    linear-gradient(rgba(23, 32, 29, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(23, 32, 29, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 24px 24px;
  font-family: "Noto Sans KR", sans-serif;
}

button, input { font: inherit; }
button { cursor: pointer; }

.topbar {
  min-height: 94px;
  padding: 20px clamp(20px, 4vw, 64px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  color: white;
  background: var(--dark);
  border-bottom: 5px solid var(--accent);
}

.eyebrow, .section-label {
  margin: 0 0 3px;
  font-family: "DM Sans", sans-serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0;
}

.eyebrow { color: #f4b28f; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(24px, 3vw, 34px); letter-spacing: 0; }
h2 { margin-bottom: 0; font-size: 20px; }

.header-actions, .step-controls, .template-controls, .tabs { display: flex; align-items: center; gap: 8px; }
.button, .step-controls button, .tab {
  border: 1px solid transparent;
  border-radius: 6px;
  font-weight: 700;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}
.button:active, .step-controls button:active, .tab:active { transform: translateY(1px); }
.button { padding: 10px 16px; }
.button.primary { color: var(--dark); background: #f8ba51; }
.button.ghost { color: white; background: transparent; border-color: rgba(255,255,255,.35); }
.button.dark { color: white; background: var(--dark); }

main { padding: 26px clamp(16px, 4vw, 64px) 48px; }
.control-band {
  max-width: 1320px;
  margin: 0 auto 18px;
  padding: 18px 20px;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 22px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 5px solid var(--accent);
}
.base-control { display: flex; align-items: end; gap: 28px; }
.section-label { color: var(--accent); }
.price-input-wrap { display: flex; align-items: baseline; gap: 6px; }
.price-input-wrap input {
  width: 220px;
  padding: 2px 0;
  border: 0;
  border-bottom: 2px solid var(--ink);
  outline: none;
  color: var(--ink);
  background: transparent;
  font-family: "DM Sans", sans-serif;
  font-size: 32px;
  font-weight: 700;
}
.price-input-wrap span { font-weight: 700; }
.step-controls button { padding: 9px 11px; color: var(--ink); background: #efede6; border-color: #ddd9cf; }
.step-controls button:hover { background: #e4e0d6; }
.suffix-toggle { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: 13px; font-weight: 600; cursor: pointer; }
.suffix-toggle input { position: absolute; opacity: 0; }
.suffix-toggle span { width: 36px; height: 20px; padding: 2px; background: #b8bdb9; border-radius: 10px; }
.suffix-toggle span::after { content: ""; display: block; width: 16px; height: 16px; border-radius: 50%; background: white; transition: transform 160ms ease; }
.suffix-toggle input:checked + span { background: var(--accent); }
.suffix-toggle input:checked + span::after { transform: translateX(16px); }

.workspace { max-width: 1320px; margin: auto; display: grid; grid-template-columns: minmax(320px, 420px) minmax(0, 1fr); gap: 18px; }
.formula-panel, .preview-area { background: var(--surface); border: 1px solid var(--line); }
.formula-panel { padding: 20px; }
.panel-heading { display: flex; justify-content: space-between; align-items: center; }
.count { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; color: white; background: var(--accent); font-family: "DM Sans", sans-serif; font-weight: 700; }
.hint { margin: 8px 0 16px; color: var(--muted); font-size: 12px; }
.hint code { color: var(--dark); }
.formula-list { display: grid; gap: 7px; max-height: 636px; overflow-y: auto; padding-right: 4px; }
.formula-row { display: grid; grid-template-columns: minmax(86px, 1fr) 112px 96px; align-items: center; gap: 8px; }
.formula-row label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 700; }
.formula-row input { min-width: 0; width: 100%; padding: 7px 8px; border: 1px solid var(--line); border-radius: 4px; background: white; font-family: "DM Sans", sans-serif; font-size: 12px; }
.formula-row input.invalid { border-color: #c62f2f; background: #fff3f3; }
.formula-value { text-align: right; color: var(--gold); font-family: "DM Sans", sans-serif; font-size: 12px; font-weight: 700; }

.preview-area { min-width: 0; display: flex; flex-direction: column; }
.preview-toolbar { min-height: 62px; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); }
.preview-toolbar p { margin: 0; color: var(--muted); font-size: 12px; }
.tab { padding: 8px 18px; color: var(--muted); background: transparent; border-color: var(--line); }
.tab.active { color: white; background: var(--dark); border-color: var(--dark); }
.canvas-stage { min-height: 680px; padding: 28px; display: grid; place-items: center; overflow: auto; background: #dddcd5; }
#previewCanvas { display: block; max-width: 100%; max-height: 624px; width: auto; height: auto; background: white; box-shadow: 0 16px 40px rgba(34, 40, 37, .18); touch-action: none; }
.template-controls { min-height: 62px; padding: 12px 16px; justify-content: flex-end; border-top: 1px solid var(--line); }
.template-controls label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; font-weight: 700; }
.template-controls output { min-width: 34px; }
.template-controls input[type="color"] { width: 34px; height: 30px; padding: 2px; border: 1px solid var(--line); border-radius: 4px; background: white; }
.history-banner { max-width: 1320px; margin: 18px auto 0; color: white; background: var(--dark); border-bottom: 5px solid var(--accent); }
.history-summary { min-height: 92px; padding: 18px 22px; display: flex; align-items: center; justify-content: space-between; gap: 24px; }
.history-summary h2 { font-size: 19px; }
.history-metrics { display: flex; align-items: center; gap: clamp(18px, 4vw, 48px); }
.history-metrics div { display: grid; gap: 3px; }
.history-metrics span { color: #a9c1ba; font-size: 11px; font-weight: 700; }
.history-metrics strong { font-family: "DM Sans", sans-serif; font-size: 16px; }
.history-metrics strong.positive { color: #f8ba51; }
.history-metrics strong.negative { color: #ff9e81; }
.chart-wrap { position: relative; height: 210px; padding: 0 14px 12px; }
#historyChart { display: block; width: 100%; height: 100%; }
#historyEmpty { position: absolute; inset: 0; display: grid; place-items: center; color: #a9c1ba; font-size: 13px; }
#historyEmpty[hidden] { display: none; }
.toast { position: fixed; right: 24px; bottom: 24px; padding: 11px 15px; color: white; background: var(--dark); border-radius: 5px; box-shadow: 0 8px 24px rgba(0,0,0,.2); opacity: 0; transform: translateY(8px); pointer-events: none; transition: 180ms ease; }
.toast.visible { opacity: 1; transform: translateY(0); }

@media (max-width: 860px) {
  .topbar { align-items: flex-start; }
  .workspace { grid-template-columns: 1fr; }
  .formula-list { max-height: 340px; }
  .canvas-stage { min-height: 520px; }
}

@media (max-width: 620px) {
  .topbar, .control-band, .base-control, .preview-toolbar { align-items: stretch; flex-direction: column; }
  .header-actions .button { flex: 1; }
  .base-control { gap: 14px; }
  .step-controls { display: grid; grid-template-columns: repeat(2, 1fr); }
  .step-controls button { min-height: 42px; }
  .price-input-wrap input { width: min(76vw, 260px); }
  .control-band { align-items: flex-start; }
  .formula-row { grid-template-columns: minmax(78px, 1fr) 100px 86px; }
  .canvas-stage { min-height: 420px; padding: 14px; }
  .template-controls { flex-wrap: wrap; justify-content: flex-start; }
  .template-controls .button { width: 100%; }
  .history-summary { align-items: stretch; flex-direction: column; }
  .history-metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 20px; }
  .history-metrics div:last-child { grid-column: 1 / -1; }
  .history-metrics strong { font-size: 13px; }
  .history-metrics strong { white-space: nowrap; }
  .chart-wrap { height: 180px; }
}