/* MECHANICA 2201 — a study at night: dark wood, warm light, brass and ivory. */

:root {
  --bg: #14100c;
  --panel: #201913;
  --panel-2: #2a2118;
  --panel-3: #332818;
  --line: #453626;
  --brass: #c9a24b;
  --brass-hi: #ecd28f;
  --brass-lo: #6f5720;
  --ivory: #efe6d2;
  --text: #d8cdb6;
  --dim: #9b8f77;
  --accent: #86a695;
  --warn: #d09a63;
  --danger: #c46a52;
  --type-font: "Courier New", "Courier", "Nimbus Mono PS", ui-monospace, monospace;
  --ui-font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background:
    radial-gradient(120% 90% at 50% 0%, #241c12 0%, var(--bg) 62%),
    var(--bg);
  color: var(--text);
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body { min-height: 100dvh; }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

/* ------------------------------------------------------------------ topbar */
#topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 16px 8px;
  background: linear-gradient(180deg, #2a2116, #1c1610);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  z-index: 5;
}

.brand { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.brand-mark {
  font-family: var(--ui-font);
  font-weight: 700;
  letter-spacing: 5px;
  font-size: 15px;
  color: var(--brass-hi);
  text-shadow: 0 1px 0 #000, 0 0 18px rgba(201, 162, 75, 0.25);
}
.brand-model {
  font-family: var(--type-font);
  font-weight: 700;
  font-size: 13px;
  color: var(--brass);
  letter-spacing: 2px;
  border: 1px solid var(--brass-lo);
  border-radius: 4px;
  padding: 0 5px;
}
.brand-sub {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 2.2px;
  color: var(--dim);
  margin-left: 6px;
}

.readout { display: flex; gap: 14px; margin-left: auto; }
.readout .ro { display: flex; flex-direction: column; align-items: center; min-width: 46px; }
.readout .ro b {
  font-family: var(--type-font);
  font-size: 17px;
  color: var(--ivory);
  letter-spacing: 1px;
}
.readout .ro i {
  font-style: normal;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.6px;
  color: var(--dim);
}

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

button.tool, .rail-btn, .tray-key {
  font-family: var(--ui-font);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--text);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--line);
  border-bottom-color: #120d09;
  border-radius: 7px;
  padding: 7px 11px;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.12s ease, border-color 0.12s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button.tool:hover, .rail-btn:hover, .tray-key:hover { border-color: var(--brass-lo); color: var(--ivory); }
button.tool:active, .rail-btn:active, .tray-key:active { transform: translateY(1px); background: var(--panel-3); }
.rail-btn.active, .tray-key.active, button.tool.active { border-color: var(--brass); color: var(--brass-hi); box-shadow: inset 0 0 0 1px rgba(201, 162, 75, 0.25); }
.rail-btn.disabled { opacity: 0.35; pointer-events: none; }
.rail-btn.danger { border-color: #6f3a2c; color: #f0b9a4; }
button.tool.off { color: var(--dim); }

/* ------------------------------------------------------------------- ruler */
#ruler-wrap {
  padding: 4px 18px 0;
  background: linear-gradient(180deg, #1b1510, #16110d);
  border-bottom: 1px solid #2c2318;
}
#ruler-wrap svg { display: block; width: 100%; height: 40px; }

/* ----------------------------------------------------------------- modebar */
#modebar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 18px 7px;
  font-size: 11px;
  background: #191410;
  border-bottom: 1px solid #271f16;
}
.mode-chip {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1.8px;
  padding: 3px 9px;
  border-radius: 20px;
  border: 1px solid var(--accent);
  color: var(--accent);
  white-space: nowrap;
}
.mode-chip.align { border-color: var(--brass); color: var(--brass-hi); }
.mode-chip.busy { border-color: #5f7d8a; color: #9fc3cf; }
.mode-chip.warn { border-color: var(--warn); color: var(--warn); }
.hint { color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stats { margin-left: auto; color: #6f6451; font-size: 10px; letter-spacing: 0.6px; white-space: nowrap; }

/* ------------------------------------------------------------------- stage */
#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  justify-content: center;
  background:
    radial-gradient(90% 70% at 50% 8%, rgba(120, 90, 50, 0.12), transparent 70%),
    #0f0c09;
}
#scene { width: 100%; height: 100%; display: block; touch-action: none; }

#scene [data-part] { cursor: pointer; }
#scene [data-part]:hover { filter: brightness(1.14); }
#scene [data-part="knob"]:hover { cursor: ns-resize; }
#scene [data-part="carriage"]:hover, #scene [data-part="paper"]:hover { cursor: ew-resize; }
#scene text.ink { font-family: var(--type-font); paint-order: stroke; }

/* -------------------------------------------------------------- paper panel */
#paper-panel { display: none; }
#paper-panel.open {
  display: block;
  border-top: 1px solid var(--line);
  background: #16110d;
  max-height: 26dvh;
  overflow: auto;
}
.pp-head {
  display: flex;
  justify-content: space-between;
  padding: 5px 14px;
  font-size: 10px;
  letter-spacing: 1.5px;
  color: var(--dim);
  text-transform: uppercase;
}
.pp-body {
  margin: 0;
  padding: 4px 14px 14px;
  font-family: var(--type-font);
  font-size: clamp(12px, 3.4vw, 16px);
  line-height: 1.45;
  color: #3b362c;
  background: linear-gradient(180deg, #f4eddc, #e7ddc6);
  min-height: 84px;
  white-space: pre-wrap;
}

/* -------------------------------------------------------------------- rail */
#rail {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  background: linear-gradient(180deg, #1e1811, #15100c);
  border-top: 1px solid var(--line);
}
.rail-btn.wide { min-width: 132px; }

#keytray { display: none; }
#keytray.open {
  display: block;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom, 0px));
  background: #15100c;
  border-top: 1px solid #2a2118;
}
.tray-row { display: flex; gap: 4px; margin-bottom: 5px; }
.tray-key {
  flex: 1 1 0;
  min-width: 0;
  min-height: 44px;
  padding: 6px 2px;
  font-size: 13px;
  font-family: var(--type-font);
  font-weight: 700;
}
.tray-row.tray-foot .tray-key { font-family: var(--ui-font); font-size: 9.5px; letter-spacing: 1px; }
.tray-key.tray-fn { flex: 1.4 1 0; }

/* ------------------------------------------------------------------- toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 24px);
  max-width: min(640px, 92vw);
  padding: 10px 16px;
  background: rgba(28, 21, 13, 0.96);
  border: 1px solid var(--brass-lo);
  border-radius: 24px;
  color: var(--ivory);
  font-size: 12.5px;
  letter-spacing: 0.2px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 40;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------------------------------------------------------- overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(8, 6, 4, 0.78);
  backdrop-filter: blur(3px);
  z-index: 50;
}
.overlay.open { display: flex; }
.ov-card {
  max-width: 780px;
  width: 100%;
  max-height: 86dvh;
  overflow: auto;
  background: linear-gradient(180deg, #241d15, #1a140e);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
.ov-card.wide { max-width: 900px; }
.ov-card h2 {
  margin: 0 0 12px;
  font-size: 15px;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--brass-hi);
  font-weight: 600;
}
.ov-card h3 {
  margin: 0 0 6px;
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--accent);
}
.ov-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; }
.ov-card ul { margin: 0; padding-left: 16px; }
.ov-card li { font-size: 12.5px; line-height: 1.65; color: var(--text); margin-bottom: 5px; }
.ov-card li b { color: var(--ivory); }
.ov-foot { font-size: 11px; color: var(--dim); margin: 16px 0 0; }
.ov-close, .pg-nav button {
  margin-top: 16px;
  font-family: var(--ui-font);
  font-size: 11px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
  color: #241d13;
  background: linear-gradient(180deg, var(--brass-hi), var(--brass));
  border: 1px solid var(--brass-lo);
  border-radius: 7px;
  padding: 9px 16px;
  cursor: pointer;
}
.pg-nav { display: flex; align-items: center; gap: 12px; }
.pg-nav button { margin-top: 14px; }
.pg-nav span { margin-left: auto; font-size: 11px; color: var(--dim); letter-spacing: 1.4px; }
#pages-view { min-height: 200px; }
.pg-paper {
  position: relative;
  background: linear-gradient(180deg, #f6efdf, #e9dfc8);
  border-radius: 3px;
  padding: 22px 26px 16px;
  color: #262119;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.5);
}
.pg-tear {
  position: absolute;
  top: -1px; left: 0; right: 0; height: 8px;
  background: repeating-linear-gradient(-45deg, #e9dfc8 0 5px, #cfc2a4 5px 7px);
  border-radius: 3px 3px 0 0;
  opacity: 0.9;
}
.pg-text {
  margin: 0;
  font-family: var(--type-font);
  font-size: clamp(11px, 1.5vw, 14px);
  line-height: 1.5;
  white-space: pre-wrap;
  min-height: 180px;
}
.pg-meta { margin-top: 12px; font-size: 10.5px; letter-spacing: 1px; color: #6f6552; text-transform: uppercase; }
.pg-empty { padding: 40px 10px; text-align: center; color: var(--dim); font-size: 13px; }

/* ------------------------------------------------------------------ mobile */
@media (max-width: 860px) {
  .brand-sub { display: none; }
  .brand-mark { letter-spacing: 3px; font-size: 13px; }
  #topbar { flex-wrap: wrap; gap: 10px; padding: 8px 10px; }
  .readout { gap: 8px; }
  .readout .ro { min-width: 38px; }
  #ruler-wrap { padding: 2px 8px 0; }
  #modebar { flex-wrap: wrap; gap: 6px; padding: 6px 10px; }
  .hint { white-space: normal; flex-basis: 100%; order: 3; line-height: 1.4; }
  .stats { display: none; }
  #stage { min-height: 30dvh; }
  .rail-btn { font-size: 9.5px; padding: 10px 8px; flex: 1 1 30%; min-width: 92px; min-height: 42px; }
  .rail-btn.wide { min-width: 120px; }
  .overlay { padding: 8px; }
  .ov-card { padding: 16px; max-height: 92dvh; }
}

@media (max-width: 480px) {
  .rail-btn { flex: 1 1 44%; }
  .readout .ro i { font-size: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
}
