:root {
  --bg: #1d2024;
  --panel: #c9ccd0;          /* machine sheet metal */
  --panel-dark: #9ea3a9;
  --panel-edge: #7d838a;
  --ink: #15181b;
  --lcd-bg: #f2f4f2;
  --lcd-ink: #101418;
  --lcd-head: #2d5d8f;
  --lcd-head-ink: #ffffff;
  --lcd-hi: #f5d742;
  --lcd-dim: #6a7179;
  --lcd-alarm: #d02020;
  --key: #e9eaec;
  --key-ink: #1b1e21;
  --key-fn: #3a6ea5;
  --key-edit: #f0d26a;
  --green: #2fae4f;
  --red: #d83a2e;
  --amber: #f0a020;
  --lamp-off: #4a4f55;
  --view-bg: #2a3036;
  --side: #23272c;
  --side-ink: #e6e8ea;
  --side-dim: #9aa2ab;
  --accent: #5fa8ff;
  font-family: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--side-ink); }
body { overflow: hidden; }
button { font-family: inherit; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: minmax(0, 1fr) 820px; grid-template-rows: minmax(0, 1fr); height: 100vh; }
.left { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 6px 12px; background: var(--side); border-bottom: 1px solid #000; }
.topbar > div:not(.grow) { min-width: 0; }
.topbar h1 { font-size: 15px; margin: 0; font-weight: 600; white-space: nowrap; }
.topbar .sub { color: var(--side-dim); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar label { white-space: nowrap; }
.topbar .grow { flex: 1; }
.topbar a, .topbar button { color: var(--side-ink); background: #343a41; border: 1px solid #4a525b; border-radius: 4px; padding: 4px 10px; font-size: 12px; text-decoration: none; cursor: pointer; }
.topbar button:hover, .topbar a:hover { background: #414852; }
.view { position: relative; background: var(--view-bg); min-height: 0; min-width: 0; overflow: hidden; }
.view canvas { display: block; width: 100%; height: 100%; }
.view .hud { position: absolute; left: 10px; top: 10px; font: 12px/1.4 Consolas, monospace; background: rgba(0,0,0,.55); padding: 6px 9px; border-radius: 4px; pointer-events: none; white-space: pre; }
.view .viewbtns { position: absolute; right: 10px; top: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; max-width: 60%; }
.view .viewbtns button { background: rgba(0,0,0,.55); color: #fff; border: 1px solid #555; border-radius: 4px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.view .viewbtns button.on { background: #2d5d8f; }
.view .crash { position: absolute; left: 50%; top: 14px; transform: translateX(-50%); background: var(--red); color: #fff; padding: 6px 14px; border-radius: 4px; font-weight: 600; display: none; }
.view .crash.show { display: block; }

/* lessons drawer */
.lessons { background: var(--side); border-top: 1px solid #000; height: 36vh; min-height: 0; overflow: auto; font-size: 13px; }
.lessons.collapsed { height: auto; }
.lessons header { display: flex; align-items: center; gap: 8px; padding: 6px 12px; position: sticky; top: 0; background: var(--side); border-bottom: 1px solid #333a42; }
.lessons header h2 { font-size: 13px; margin: 0; }
.lessons header select, .lessons header button { background: #343a41; color: var(--side-ink); border: 1px solid #4a525b; border-radius: 4px; padding: 3px 6px; font-size: 12px; }
.lessons .body { padding: 8px 14px 12px; }
.lessons.collapsed .body { display: none; }
.lessons .intro { color: var(--side-dim); margin: 0 0 8px; }
.lessons ol { margin: 0; padding-left: 20px; }
.lessons li { margin: 4px 0; color: var(--side-dim); }
.lessons li.current { color: #fff; }
.lessons li.done { color: #7fcf8f; }
.lessons li.done::marker { content: "✓ "; }
.lessons li .hint { display: block; color: #c9b56a; font-size: 12px; margin-top: 2px; }
.lessons .tools { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.lessons .tools button { background: #343a41; color: var(--side-ink); border: 1px solid #4a525b; border-radius: 4px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.lessons .measure { margin-top: 8px; font: 12px Consolas, monospace; color: #bfe3ff; }
.lessons kbd { background: #444b53; border-radius: 3px; padding: 0 4px; font: 11px Consolas, monospace; color: #fff; }

/* ---------- control cabinet (Lynx 220LM style) ---------- */
.cabinet { background: #8fb4dc; color: var(--ink); overflow-y: auto; scrollbar-gutter: stable; min-height: 0; padding: 12px; border-left: 2px solid #000; }
.nc { background: #55595f; border-radius: 4px; padding: 10px; box-shadow: inset 0 0 0 3px #3a3d42, 0 2px 6px rgba(0,0,0,.4); display: grid; grid-template-columns: minmax(0, 1fr) 268px; gap: 10px; }
.nc .screenside { display: flex; flex-direction: column; min-width: 0; }
.nc .brand { align-self: flex-start; background: #f2c200; color: #c0281e; font: italic 700 11px/1 Arial, sans-serif; letter-spacing: .04em; padding: 3px 26px 3px 8px; margin: 0 0 6px; }
.lcd { background: var(--lcd-bg); color: var(--lcd-ink); height: 330px; border: 8px solid #222; border-radius: 2px; font: 12.5px/1.25 Consolas, "Courier New", monospace; overflow: hidden; display: flex; flex-direction: column; }
.lcd.off { background: #0b0d0e; }
.lcd.off > * { visibility: hidden; }
.lcd .title { background: var(--lcd-head); color: var(--lcd-head-ink); display: flex; justify-content: space-between; padding: 1px 6px; font-weight: 700; }
.lcd .content { flex: 1; padding: 3px 6px; overflow: hidden; white-space: pre; position: relative; }
.lcd .big { font-size: 26px; line-height: 1.15; font-weight: 700; }
.lcd .label { color: var(--lcd-dim); }
.lcd .hi { background: var(--lcd-hi); }
.lcd .cur { background: #1b3b66; color: #fff; }
.lcd .alm { color: var(--lcd-alarm); font-weight: 700; }
.lcd .note { color: #2d5d8f; }
.lcd .input { border-top: 1px solid #9aa; padding: 1px 6px; display: flex; justify-content: space-between; }
.lcd .input .buf::after { content: "_"; animation: blink 1s steps(1) infinite; }
.lcd .status { background: #dfe3e6; padding: 1px 6px; display: flex; gap: 10px; font-size: 12px; }
.lcd .status .alm { animation: blink 1s steps(1) infinite; }
.lcd .softkeys { display: grid; grid-template-columns: 18px repeat(5, 1fr) 18px; gap: 2px; padding: 2px; background: #c7ccd1; }
.lcd .softkeys div { background: #e9edf0; border: 1px solid #8f969d; text-align: center; font-size: 11px; height: 18px; line-height: 16px; overflow: hidden; }
.lcd canvas { width: 100%; height: 100%; }
@keyframes blink { 50% { opacity: 0; } }

.skrow { display: grid; grid-template-columns: repeat(7, 1fr); gap: 10px; margin: 8px 6px 0; background: #45484d; padding: 5px 8px; border-radius: 2px; }
.skrow button { height: 20px; background: #e6e7e9; border: 1px solid #222; border-radius: 2px; cursor: pointer; font-size: 10px; }
.skrow button:active { background: #b5babf; }

.keypad { align-self: start; background: #6c7076; border-radius: 4px; padding: 8px; display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; }
.keypad button { height: 36px; background: #efece6; color: #1b1e21; border: 1px solid #8e8a82; border-radius: 4px; box-shadow: 0 2px 0 #9a958c; font: 600 15px Arial, sans-serif; cursor: pointer; position: relative; padding: 0; line-height: 1; }
.keypad button sub { position: absolute; right: 4px; bottom: 3px; font-size: 9px; color: #444; }
.keypad button.fn { background: #e2d8c4; font-size: 8.5px; font-weight: 600; line-height: 1.25; box-shadow: 0 2px 0 #a39a87; }
.keypad button.arrow { font-size: 15px; }
.keypad button.shift.on { outline: 3px solid var(--amber); }
.keypad button:active { transform: translateY(2px); box-shadow: none; }
.keypad .gap { visibility: hidden; }

/* operator panel */
.op { margin-top: 12px; background: #4a4e54; border: 3px solid #2e3135; border-radius: 4px; padding: 8px; color: #f1f2f3;
  display: grid; gap: 6px; grid-template-columns: 140px 160px minmax(0, 1fr) 132px;
  grid-template-areas: "pwr rdy top lmp" "est rap main cool" "mode mpg main door" "mode mpg main tool" "cyc quill keys rs" "x x x x"; }
.op .sec { min-width: 0; border: 1px solid #8a8f96; border-radius: 3px; padding: 5px; display: flex; flex-wrap: wrap; gap: 6px; align-content: flex-start; justify-content: center; }
.op .a-pwr { grid-area: pwr; } .op .a-rdy { grid-area: rdy; } .op .a-top { grid-area: top; gap: 3px; flex-wrap: nowrap; justify-content: space-between; } .op .a-lmp { grid-area: lmp; }
.op .a-est { grid-area: est; align-items: center; align-content: center; } .op .a-rap { grid-area: rap; flex-direction: column; align-items: center; }
.op .a-mode { grid-area: mode; } .op .a-mpg { grid-area: mpg; align-items: center; align-content: center; } .op .a-main { grid-area: main; flex-direction: column; flex-wrap: nowrap; }
.op .a-cool { grid-area: cool; flex-direction: column; align-items: center; } .op .a-door { grid-area: door; } .op .a-tool { grid-area: tool; flex-direction: column; align-items: center; }
.op .a-cyc { grid-area: cyc; } .op .a-quill { grid-area: quill; } .op .a-keys { grid-area: keys; justify-content: space-around; align-items: flex-end; } .op .a-rs { grid-area: rs; flex-direction: column; align-items: center; }
.op .extras { grid-area: x; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 11px; background: #3b3e43; padding: 5px 8px; border-radius: 3px; }
.op .extras button { background: #5a5f66; color: #fff; border: 1px solid #777; border-radius: 3px; padding: 3px 8px; font-size: 11px; cursor: pointer; }
.op .extras button.on { background: #b8860b; }
.op .lbl { font: 600 8px/1.15 Arial, sans-serif; text-align: center; color: #f4f5f6; letter-spacing: .02em; margin-bottom: 2px; }
.op .row { display: flex; gap: 6px; justify-content: center; }
.op .btn { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; min-width: 0; }
.op button.pb { overflow: hidden; width: 40px; height: 36px; border-radius: 3px; border: 2px solid #1f2124; background: #c9ccd0; color: #222; font: 700 8px/1.1 Arial, sans-serif; cursor: pointer; position: relative; padding: 0; box-shadow: inset 0 0 0 2px #a2a6ab; }
.op button.pb .ic { display: block; font-size: 9px; line-height: 1.05; letter-spacing: -.02em; white-space: nowrap; }
.op button.pb .ic small { display: block; font-size: 7px; opacity: .8; }
.op button.pb sub { font-size: 7px; }
.op button.pb .led { position: absolute; left: 3px; top: 3px; width: 6px; height: 4px; border-radius: 1px; background: #6b6f74; }
.op button.pb.on { background: #f3f4e8; }
.op button.pb.on .led { background: #3dff6b; box-shadow: 0 0 5px #3dff6b; }
.op button.pb.green { background: #1f9e3f; color: #fff; box-shadow: inset 0 0 0 2px #157a2f; }
.op button.pb.red { background: #d2312a; color: #fff; box-shadow: inset 0 0 0 2px #a5221c; }
.op button.pb.blue { background: #2f6fd6; color: #fff; box-shadow: inset 0 0 0 2px #2154a8; }
.op button.pb.green.on, .op button.pb.red.on, .op button.pb.blue.on { filter: brightness(1.25); }
.op button.pb.green.on .led, .op button.pb.red.on .led, .op button.pb.blue.on .led { background: #fff; box-shadow: 0 0 6px #fff; }
.op button.pb.big { width: 46px; height: 44px; }
.op button.pb:active { transform: translateY(1px); }
.op .estop { width: 84px; height: 84px; border-radius: 50%; background: radial-gradient(circle at 42% 38%, #ff5c4d, #b8180f 70%); border: 9px solid #f0c419; color: #fde; font-size: 26px; cursor: pointer; box-shadow: 0 3px 0 #222; }
.op .estop.pressed { transform: scale(.94); box-shadow: inset 0 0 12px #000; filter: brightness(.85); }
.op .grid3 { display: grid; grid-template-columns: repeat(3, 40px); gap: 4px 4px; }
.op .lamps { display: flex; flex-wrap: wrap; gap: 6px 10px; font: 600 8.5px Arial, sans-serif; justify-content: center; }
.op .lamps span { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.op .lamps i { width: 16px; height: 10px; border-radius: 2px; background: #2c2f33; border: 1px solid #777; }
.op .lamps span.on i { background: #ffd23f; box-shadow: 0 0 6px #ffd23f; }
.op .lamps span[data-l="alm"].on i { background: #ff4a3d; box-shadow: 0 0 6px #ff4a3d; }
.op .lamps span[data-l="contact"].on i { background: #3dff6b; box-shadow: 0 0 6px #3dff6b; }
.op .extras .lamps span { flex-direction: row; }
.op .ovr { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-start; justify-content: space-around; width: 100%; }
.op .knobwrap { width: 104px; flex: none; }
.op .knob { position: relative; width: 100px; height: 100px; margin: 0 auto; cursor: ew-resize; touch-action: none; user-select: none; }
.op .knob .tk { position: absolute; transform: translate(-50%, -50%); font: 600 8.5px Arial, sans-serif; color: #eee; }
.op .knob .tk.hundred { background: #eee; color: #111; border-radius: 6px; padding: 0 2px; }
.op .knob .cap { position: absolute; left: 25%; top: 25%; width: 50%; height: 50%; border-radius: 50%; background: radial-gradient(circle at 40% 35%, #555, #111); box-shadow: 0 2px 3px #000; }
.op .knob .ptr { position: absolute; left: 44%; top: 6%; width: 12%; height: 45%; background: #e9e2c8; border-radius: 2px; }
.op .jog, .op .spc { flex: none; }
.op .jog .jgrid, .op .spc .sgrid { display: grid; grid-template-columns: repeat(3, 40px); gap: 4px; }
.op .homelamps { display: flex; flex-direction: column; font: 600 8px Arial, sans-serif; gap: 1px; }
.op .homelamps span { display: flex; gap: 3px; align-items: center; }
.op .homelamps i { width: 7px; height: 7px; border-radius: 50%; background: #2c2f33; border: 1px solid #888; }
.op .homelamps span.on i { background: #3dff6b; box-shadow: 0 0 4px #3dff6b; }
.op .mpg { display: flex; flex-direction: column; align-items: center; }
.op .mpg .dial { width: 136px; height: 136px; border-radius: 50%; background: repeating-conic-gradient(#ddd 0 1deg, transparent 1deg 3.6deg); padding: 10px; }
.op .mpg .wheel { width: 100%; height: 100%; border-radius: 50%; background: radial-gradient(circle at 45% 40%, #f4f4f4, #b9bcc0 70%, #8d9095); box-shadow: 0 3px 6px #000; position: relative; cursor: grab; touch-action: none; }
.op .mpg .hub { position: absolute; inset: 26%; border-radius: 50%; background: radial-gradient(circle at 40% 35%, #fff, #c5c8cc); box-shadow: inset 0 0 0 2px #9a9ea3; }
.op .mpg .knob-pin { position: absolute; width: 16px; height: 16px; border-radius: 50%; background: #777; left: 10%; top: 58%; box-shadow: 0 1px 2px #000; }
.op .mpg .pm { display: flex; justify-content: space-between; width: 130px; font-weight: 700; }
.op .keysw-wrap { display: flex; flex-direction: column; align-items: center; }
.op .kpos { display: flex; justify-content: space-between; width: 72px; font: 600 8px Arial, sans-serif; }
.op .keysw { width: 40px; height: 40px; border-radius: 50%; background: #111; border: 3px solid #666; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.op .keysw .plug { width: 26px; height: 26px; border-radius: 50%; background: radial-gradient(circle, #ddd, #888); transform: rotate(-35deg); transition: transform .15s; display: flex; align-items: center; justify-content: center; }
.op .keysw.right .plug { transform: rotate(35deg); }
.op .keysw .bit { width: 5px; height: 20px; background: #333; border-radius: 2px; }
.op .pedalbtn { width: 26px; height: 26px; border-radius: 50%; background: #222; border: 2px solid #555; }
.op .seg { font: 700 22px/1 "Courier New", monospace; color: #ff3b2f; background: #1a0a09; padding: 3px 8px; border-radius: 2px; min-width: 52px; min-height: 28px; text-align: center; letter-spacing: 2px; text-shadow: 0 0 6px #ff3b2f; }
.op .db25 { width: 80px; height: 18px; background: linear-gradient(#ddd, #aaa); border-radius: 3px 3px 9px 9px; border: 1px solid #555; }
.op.off button.pb .led, .op.off .lamps i, .op.off .homelamps i { background: #2c2f33 !important; box-shadow: none !important; }
.op.light { box-shadow: 0 0 0 3px #fff6c0; }

/* modal editor */
dialog.pcedit { width: min(900px, 92vw); height: min(80vh, 760px); background: #1f2327; color: #eee; border: 1px solid #555; border-radius: 6px; padding: 0; }
dialog.pcedit form { display: grid; grid-template-rows: auto 1fr auto; height: 100%; }
dialog.pcedit header, dialog.pcedit footer { padding: 8px 12px; display: flex; gap: 8px; align-items: center; }
dialog.pcedit textarea { width: 100%; height: 100%; resize: none; background: #111; color: #e4e4e4; border: 0; padding: 10px; font: 13px/1.4 Consolas, monospace; }
dialog.pcedit button { background: #343a41; color: #eee; border: 1px solid #4a525b; border-radius: 4px; padding: 5px 12px; cursor: pointer; }
dialog.pcedit .err { color: #ff8a80; font-size: 12px; }

/* picker page */
.picker { max-width: 980px; margin: 0 auto; padding: 40px 20px; overflow: auto; height: 100vh; }
.picker h1 { font-size: 26px; margin: 0 0 6px; }
.picker p { color: var(--side-dim); }
.picker .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 18px; margin-top: 24px; }
.picker a.card { display: block; background: var(--side); border: 1px solid #3a424b; border-radius: 8px; padding: 18px; color: inherit; text-decoration: none; }
.picker a.card:hover { border-color: var(--accent); }
.picker .card h2 { margin: 0 0 4px; font-size: 18px; }
.picker .card .ctl { color: var(--accent); font-size: 13px; }
.picker .card ul { color: var(--side-dim); font-size: 13px; padding-left: 18px; }
.picker .warn { margin-top: 24px; font-size: 13px; color: #d9c27a; }

@media (max-width: 1100px) {
  body { overflow: auto; }
  .app { grid-template-columns: 1fr; height: auto; }
  .app { grid-template-rows: auto auto; }
  .left { grid-template-rows: auto 55vh auto; }
  .cabinet { border-left: 0; }
}

/* lesson guide */
.lessons kbd { cursor: pointer; border-bottom: 1px dotted #9cf; }
.lessons kbd:hover { background: #5a6a7d; }
.lessons .showme { background: #2d5d8f; color: #fff; border: 1px solid #5fa8ff; border-radius: 4px; padding: 1px 8px; font-size: 11px; cursor: pointer; margin-left: 6px; }
.lessons .showme:hover { background: #3a74b0; }
.guide-flash { outline: 4px solid #ffd400 !important; outline-offset: 2px; box-shadow: 0 0 14px 6px rgba(255, 212, 0, .85) !important; animation: guidepulse .55s ease-in-out; position: relative; z-index: 5; }
@keyframes guidepulse { 0% { transform: scale(1); } 40% { transform: scale(1.12); } 100% { transform: scale(1); } }
