@font-face {
  font-family: 'B612 Mono';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/b612-mono-400.woff2') format('woff2');
}

@font-face {
  font-family: 'B612 Mono';
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url('../fonts/b612-mono-700.woff2') format('woff2');
}

@font-face {
  font-family: 'Big Shoulders Stencil';
  font-style: normal;
  font-weight: 800;
  font-display: swap;
  src: url('../fonts/big-shoulders-stencil-800.woff2') format('woff2');
}

@font-face {
  font-family: 'Nanum Pen Script';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/nanum-pen-script-400.woff2') format('woff2');
}

:root {
  --desk: #081626;
  --paper: #0f3e78;
  --paper-deep: #0a2c57;
  --line: #eaf2ff;
  --line-soft: rgba(234, 242, 255, 0.5);
  --line-faint: rgba(234, 242, 255, 0.18);
  --pencil: #ff8a65;
  --accent: #ffd65a;
  --metal-hi: #dfe5ec;
  --metal-lo: #6f7a86;
  --noise: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.16'/%3E%3C/svg%3E");
  --font-body: 'B612 Mono', ui-monospace, 'Cascadia Mono', monospace;
  --font-stencil: 'Big Shoulders Stencil', 'Arial Narrow', sans-serif;
  --font-pencil: 'Nanum Pen Script', 'Segoe Print', cursive;
  color-scheme: dark;
}

[data-theme="light"] {
  --desk: #cbc4b1;
  --paper: #f5f3eb;
  --paper-deep: #e3dfd0;
  --line: #1b3a6b;
  --line-soft: rgba(27, 58, 107, 0.5);
  --line-faint: rgba(27, 58, 107, 0.16);
  --pencil: #cf3b24;
  --accent: #b86f0d;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding:
    max(clamp(6px, 2.4vw, 32px), env(safe-area-inset-top))
    max(clamp(6px, 2.4vw, 32px), env(safe-area-inset-right))
    max(clamp(6px, 2.4vw, 32px), env(safe-area-inset-bottom))
    max(clamp(6px, 2.4vw, 32px), env(safe-area-inset-left));
  color: var(--line);
  font-family: var(--font-body);
  background:
    radial-gradient(ellipse at 30% 20%, rgba(255, 255, 255, 0.06), transparent 60%),
    var(--noise),
    var(--desk);
  transition: background-color 0.5s ease, color 0.5s ease;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }

button { touch-action: manipulation; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Sheet frame ---------- */
.drafting {
  position: relative;
  width: min(1180px, 100%);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "sheet head"
    "sheet block";
  background: var(--noise), linear-gradient(135deg, var(--paper), var(--paper-deep));
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(0, 0, 0, 0.2);
  animation: unroll 0.9s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.drafting::before {
  content: '';
  position: absolute;
  inset: 12px;
  border: 2px solid var(--line);
  outline: 1px solid var(--line-soft);
  outline-offset: 4px;
  pointer-events: none;
}

/* ---------- Masthead ---------- */
.masthead {
  grid-area: head;
  margin: 12px 12px 0 0;
  padding: 14px 12px;
  border-left: 2px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 10px;
  position: relative;
  z-index: 1;
}

.brand { display: grid; gap: 8px; }

.brand-name {
  margin: 0;
  font-family: var(--font-stencil);
  font-weight: 800;
  font-size: clamp(46px, 5vw, 64px);
  line-height: 0.85;
  letter-spacing: 0.06em;
}

.brand-sub { font-size: 10px; letter-spacing: 0.22em; }

.tools { display: flex; gap: 6px; }

.tool {
  min-width: 40px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line-soft);
  background: none;
  font-size: 11px;
  letter-spacing: 0.14em;
  cursor: pointer;
}

.tool:hover { background: var(--line-faint); }

/* ---------- Plot ---------- */
.sheet {
  grid-area: sheet;
  overflow: clip;
  padding: clamp(40px, 4.6vw, 60px) clamp(28px, 4vw, 54px) clamp(28px, 4vw, 54px);
  display: grid;
  place-items: center;
}

.plot-area {
  --pen-x: 100;
  --pen-y: -3;
  position: relative;
  width: min(100%, calc(100dvh - 190px));
  min-width: 240px;
  aspect-ratio: 1;
}

.plot { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; color: var(--line); }

.corners path { fill: none; stroke: var(--line-soft); stroke-width: 0.35; }

.outline { fill: none; stroke: var(--line); stroke-width: 0.5; stroke-linejoin: round; }

.hatch-fill { fill: url(#hatch); clip-path: inset(0 100% 0 0); transition: clip-path 0.9s steps(18, end); }
.hatch-fill.is-filled { clip-path: inset(0 0 0 0); }
.hatch-fill.is-instant { transition: none; }

.dimension { animation: fade-in 0.5s ease both; }
.dimension line { stroke: var(--line-soft); stroke-width: 0.22; }
.dimension text { fill: var(--line); font-family: var(--font-body); }

.ink-draw { fill: none; stroke-dasharray: 1; stroke-dashoffset: 1; animation: ink 0.35s ease-out forwards; }

.cross-mark { stroke: var(--line); stroke-width: 0.45; stroke-linecap: round; }
.hit-ring { stroke: var(--accent); stroke-width: 0.55; animation-duration: 0.6s; }
.leader { stroke: var(--pencil); stroke-width: 0.3; }
.centre-mark { stroke: var(--accent); stroke-width: 0.3; animation: fade-in 0.4s ease 0.3s both; }
.centre-link { stroke: var(--accent); stroke-width: 0.22; stroke-dasharray: 0.8 0.6; animation: fade-in 0.4s ease 0.3s both; }

.pencil-note { fill: var(--pencil); font-family: var(--font-pencil); animation: fade-in 0.4s ease both; }
.pencil-note.is-good { fill: var(--accent); }

.mark-group.is-old { opacity: 0.4; }

/* ---------- Gantry ---------- */
.rail {
  position: absolute;
  left: -5%;
  right: -5%;
  top: calc(var(--pen-y) * 1%);
  height: 0;
  transition: top 0.62s cubic-bezier(0.65, 0, 0.3, 1);
  pointer-events: none;
  z-index: 2;
}

.rail::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -30px;
  height: 7px;
  border-radius: 4px;
  background: linear-gradient(180deg, var(--metal-hi), var(--metal-lo));
  box-shadow: 0 6px 10px rgba(0, 0, 0, 0.35);
  opacity: 0.9;
}

.carriage {
  position: absolute;
  /* The rail is 110% of the plot wide and starts 5% before it. */
  left: calc((5 + var(--pen-x)) * 1% / 1.1);
  top: 0;
  width: 26px;
  height: 36px;
  transform: translate(-50%, -100%);
  transition: left 0.62s cubic-bezier(0.65, 0, 0.3, 1);
}

.carriage-body {
  position: absolute;
  inset: 0 0 12px;
  border-radius: 4px 4px 2px 2px;
  background: linear-gradient(90deg, #2c3238, #4c555e 50%, #2c3238);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.45);
  transition: transform 0.12s ease;
}

.carriage-body::after {
  content: '';
  position: absolute;
  left: 5px;
  right: 5px;
  top: 5px;
  height: 5px;
  border-radius: 2px;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}

.pen-tip {
  position: absolute;
  left: 50%;
  bottom: 4px;
  width: 8px;
  height: 12px;
  transform: translateX(-50%);
  clip-path: polygon(0 0, 100% 0, 60% 100%, 40% 100%);
  background: linear-gradient(180deg, #e8e8e8, #1b1b1b);
  transition: transform 0.12s ease;
}

.is-pen-down .pen-tip { transform: translate(-50%, 4px); }
.is-pen-down .carriage-body { transform: translateY(2px); }
.is-tracing .rail, .is-tracing .carriage { transition: none; }

/* ---------- Approval stamp ---------- */
.stamp {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 3;
  padding: 10px 26px 8px;
  border: 5px double var(--accent);
  color: var(--accent);
  font: 800 clamp(34px, 7vw, 72px)/0.9 var(--font-stencil);
  letter-spacing: 0.12em;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%) rotate(-12deg) scale(2.2);
}

.stamp small { display: block; margin-top: 6px; font: 700 12px var(--font-body); letter-spacing: 0.3em; }

.stamp.is-stamped { animation: stamp 1.7s cubic-bezier(0.3, 1.6, 0.5, 1) forwards; }

/* ---------- Title block ---------- */
.title-block {
  grid-area: block;
  margin: 0 12px 12px 0;
  border-left: 2px solid var(--line);
  display: grid;
  grid-auto-rows: min-content;
  font-size: 11px;
  letter-spacing: 0.06em;
  position: relative;
  z-index: 1;
}

.details { display: contents; }
.details .specs { order: -1; }
.details-close, .details:not([open]) .mode-switch { display: none; }
.details[open] {
  display: grid;
  width: min(480px, calc(100% - 16px));
  max-height: calc(100dvh - 16px);
  overflow-y: auto;
  padding: 12px;
  border: 2px solid var(--line);
  color: var(--line);
  background: var(--noise), linear-gradient(135deg, var(--paper), var(--paper-deep));
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
  font: 11px var(--font-body);
  letter-spacing: 0.06em;
}
.details::backdrop { background: rgba(4, 12, 24, 0.7); }
.details[open] .mode-switch { order: -2; margin-bottom: 10px; }
.details.is-round-complete .mode-switch { display: none; }
.details[open] .specs { order: 0; }
.details[open] .details-close { display: grid; padding: 12px 0 0; }
.details-close .plot-button { font-size: 22px; letter-spacing: 0.14em; text-indent: 0.14em; }
.details[open] .notes { min-height: 0; }

.mode-switch { display: flex; width: max-content; border: 1px solid var(--line-soft); }
.mode-switch button {
  min-height: 28px;
  padding: 0 9px;
  border: 0;
  background: transparent;
  font-size: 10px;
  cursor: pointer;
}
.mode-switch button[aria-pressed="true"] { background: var(--accent); color: var(--paper); }
.mode-switch button:disabled { cursor: default; }
.timer { display: block; padding: 8px 12px; border-bottom: 1px solid var(--line); color: var(--accent); text-align: right; font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }
.timer.is-urgent { color: var(--pencil); }
.timer[hidden] { display: none; }

.cell {
  padding: 8px 12px;
  display: grid;
  gap: 3px;
  min-width: 0;
}

.cell-label { font-size: 8px; letter-spacing: 0.24em; opacity: 0.7; }
.cell-value { font-size: 14px; font-weight: 700; overflow-wrap: anywhere; }
.cell-value.is-bumped { animation: bump 0.45s cubic-bezier(0.3, 1.8, 0.5, 1); }

.specs { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); }
.specs .cell:nth-child(odd) { border-right: 1px solid var(--line); }
.specs .cell:nth-child(-n + 2) { border-bottom: 1px solid var(--line); }

.coords {
  margin: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.coord { display: grid; gap: 6px; min-width: 0; }

.coord label { font-size: 8px; letter-spacing: 0.2em; opacity: 0.7; }

.stepper { display: grid; grid-template-columns: 36px minmax(0, 1fr) 36px; border: 1.5px solid var(--line); }

.stepper button,
.stepper input {
  min-width: 0;
  height: 46px;
  border: 0;
  background: transparent;
  color: var(--line);
  font-weight: 700;
  font-size: 20px;
  text-align: center;
}

.stepper button { cursor: pointer; font-size: 20px; user-select: none; -webkit-user-select: none; }
.stepper button:hover, .stepper button.is-held { background: var(--line-soft); color: var(--paper); }
.stepper input { border-inline: 1.5px solid var(--line); appearance: textfield; -moz-appearance: textfield; }
.stepper input::-webkit-inner-spin-button,
.stepper input::-webkit-outer-spin-button { appearance: none; margin: 0; }
.stepper input:focus { outline: none; background: var(--line-faint); }

.slider { display: none; }

@media (pointer: coarse) {
  .coords { grid-template-columns: 1fr; gap: 6px; padding: 10px 12px; }
  .coord { grid-template-columns: 1fr auto; align-items: center; row-gap: 0; }
  .coord label { font-size: 10px; }
  .stepper { grid-template-columns: 72px; }
  .stepper button { display: none; }
  .stepper input { height: 30px; border-inline: 0; font-size: 19px; }

  .slider {
    display: block;
    grid-column: 1 / -1;
    width: 100%;
    height: 34px;
    margin: 0;
    background: transparent;
    appearance: none;
    -webkit-appearance: none;
    cursor: pointer;
  }

  .slider:focus-visible { outline-offset: 0; }

  .slider::-webkit-slider-runnable-track { height: 2px; background: var(--line); }
  .slider::-moz-range-track { height: 2px; background: var(--line); }

  .slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 30px;
    height: 30px;
    margin-top: -14px;
    border: 2px solid var(--accent);
    border-radius: 2px;
    background: var(--paper);
    box-shadow: 0 0 0 4px var(--line-faint), 0 3px 6px rgba(0, 0, 0, 0.35);
  }

  .slider::-moz-range-thumb {
    width: 26px;
    height: 26px;
    border: 2px solid var(--accent);
    border-radius: 2px;
    background: var(--paper);
    box-shadow: 0 0 0 4px var(--line-faint), 0 3px 6px rgba(0, 0, 0, 0.35);
  }

  .slider:active::-webkit-slider-thumb { background: var(--accent); }
  .slider:active::-moz-range-thumb { background: var(--accent); }
}

.plot-button {
  grid-column: 1 / -1;
  padding: 12px;
  border: 2px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font: 800 28px/1 var(--font-stencil);
  letter-spacing: 0.4em;
  text-indent: 0.4em;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  isolation: isolate;
  transition: color 0.2s ease;
}

.plot-button::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--accent);
  transform: translateX(-101%);
  transition: transform 0.3s cubic-bezier(0.6, 0, 0.3, 1);
  z-index: -1;
}

.plot-button:hover:not(:disabled),
.plot-button:focus-visible { color: var(--paper); }
.plot-button:hover:not(:disabled)::before,
.plot-button:focus-visible::before { transform: none; }
.plot-button:disabled { opacity: 0.45; cursor: progress; }

.notes { min-height: 72px; border-bottom: 1px solid var(--line); }
.notes p { margin: 0; font: 23px/1 var(--font-pencil); color: var(--pencil); }

.revisions ol { margin: 0; padding: 0; list-style: none; display: grid; gap: 2px; }
.revisions li { display: grid; grid-template-columns: 22px 1fr auto; gap: 8px; animation: fade-in 0.4s ease both; }
.revisions li b { color: var(--pencil); }
.revisions li.is-hit b { color: var(--accent); }
.revisions li:empty { display: none; }

/* ---------- Help dialog ---------- */
.help {
  width: min(520px, calc(100% - 24px));
  max-height: calc(100dvh - 16px);
  overflow-y: auto;
  padding: 28px 26px 22px;
  border: 2px solid var(--line);
  outline: 1px solid var(--line-soft);
  outline-offset: -8px;
  color: var(--line);
  background: var(--noise), linear-gradient(135deg, var(--paper), var(--paper-deep));
  font-size: 13px;
  line-height: 1.6;
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.6);
}

.help[open] { animation: unroll 0.5s cubic-bezier(0.2, 0.8, 0.2, 1) both; }

.help::backdrop { background: rgba(4, 12, 24, 0.6); backdrop-filter: blur(3px); }

.help h2 { margin: 0 0 10px; font: 800 38px/1 var(--font-stencil); letter-spacing: 0.1em; }

.help ol { margin: 0 0 12px; padding-left: 20px; display: grid; gap: 6px; }

.help b { color: var(--accent); }

.help-note { margin: 0 0 16px; font: 22px/1 var(--font-pencil); color: var(--pencil); }

.help form { display: grid; }

.help.confirm { width: min(440px, calc(100% - 24px)); }
.help.confirm p { margin: 0 0 12px; }
.confirm .mode-switch { margin-bottom: 14px; }

.confirm-actions { grid-template-columns: 1fr 1fr; gap: 10px; }
.confirm-actions .plot-button { grid-column: auto; font-size: 22px; letter-spacing: 0.2em; text-indent: 0.2em; }

.secondary-button {
  padding: 12px 8px;
  border: 1px solid var(--line-soft);
  background: none;
  font-size: 11px;
  letter-spacing: 0.16em;
  cursor: pointer;
}

.secondary-button:hover { background: var(--line-faint); }

.noscript { padding: 20px; font-size: 14px; }

/* ---------- Motion ---------- */
@keyframes unroll {
  from { clip-path: inset(0 0 100% 0); transform: translateY(-12px); }
  to { clip-path: inset(0 0 0 0); transform: none; }
}

@keyframes ink { to { stroke-dashoffset: 0; } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes bump { from { transform: scale(1.3); color: var(--accent); } }

@keyframes stamp {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(-12deg) scale(2.2); }
  18% { opacity: 0.95; transform: translate(-50%, -50%) rotate(-12deg) scale(1); }
  80% { opacity: 0.95; transform: translate(-50%, -50%) rotate(-12deg) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) rotate(-12deg) scale(1); }
}

/* ---------- Small screens ---------- */
@media (max-width: 860px) and (orientation: portrait), (max-width: 560px) {
  body { height: 100dvh; min-height: 0; overflow: hidden; padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left)); }
  .drafting {
    height: 100%;
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "head" "sheet" "block";
  }

  .masthead {
    margin: 12px 12px 0;
    padding: 8px 10px;
    border-left: 0;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
  }

  .brand { gap: 4px; }
  .brand-name { font-size: 34px; }
  .brand-sub { font-size: 8px; letter-spacing: 0.14em; }
  .tool { height: 32px; }
  .details-trigger { display: block; }

  .sheet { min-height: 0; padding: 50px 22px 12px; }
  .plot-area { width: 100%; height: 100%; min-width: 0; aspect-ratio: auto; }

  .title-block { margin: 0 12px 12px; border-left: 0; border-top: 1px solid var(--line); }
}

@media (max-width: 860px) and (orientation: portrait), (max-width: 560px), (orientation: landscape) and (max-height: 520px) {
  .specs { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .specs .cell { padding: 6px; border-bottom: 0 !important; }
  .specs .cell:nth-child(odd) { border-right: 0; }
  .specs .cell:not(:last-child) { border-right: 1px solid var(--line); }
  .specs .cell-value { font-size: 11px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  body { height: 100dvh; min-height: 0; overflow: hidden; padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left)); }
  .drafting { height: 100%; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "sheet head" "sheet block"; }
  .masthead { padding: 8px 10px; grid-template-columns: minmax(0, 1fr) auto; align-items: end; }
  .brand-name { font-size: 32px; }
  .brand-sub { display: none; }
  .details-trigger { display: block; }
  .sheet { min-height: 0; padding: 36px 18px 12px; }
  .plot-area { width: 100%; height: 100%; min-width: 0; aspect-ratio: auto; }
  .title-block { min-height: 0; }
  .coords { padding: 8px 12px; gap: 8px; }
  .stepper button, .stepper input { height: 38px; }
  .plot-button { padding: 8px; font-size: 22px; }
}

@media (pointer: coarse) and (orientation: landscape) and (max-height: 520px) {
  .coords { grid-template-columns: 1fr 1fr; gap: 6px 14px; }
  .slider { height: 34px; }
}

@media (pointer: coarse) and (orientation: portrait) {
  .plot-button { padding: 8px; font-size: 26px; }
  .specs .cell { padding: 4px 6px; gap: 1px; }
}

@media (max-height: 620px) and (orientation: portrait) {
  .brand-sub { display: none; }
  .sheet { padding-top: 36px; }
  .coords { grid-template-columns: 1fr 1fr; gap: 6px; padding: 6px; }
  .coord { display: grid; }
  .slider { display: none; }
  .stepper { grid-template-columns: 28px minmax(0, 1fr) 28px; }
  .stepper button { display: block; }
  .stepper input, .stepper button { height: 34px; }
  .plot-button { padding: 6px; }
}

@media (max-width: 380px) {
  .masthead { gap: 4px; }
  .tools { gap: 2px; }
  .tools .tool { min-width: 30px; padding: 0 4px; font-size: 10px; letter-spacing: 0; }
  .brand-name { font-size: 30px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .masthead { gap: 4px; }
  .tools { gap: 2px; }
  .tools .tool { min-width: 30px; padding: 0 4px; font-size: 10px; letter-spacing: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
