/* MiŠKo 3 emulator. Light and dark schemes follow the system setting. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c2024;
  --muted: #5d6670;
  --border: #d3d8de;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --focus: #1f6feb;
  --error-bg: #fde8e8;
  --error-text: #8a1111;
  --error-border: #e0a3a3;
  --info-bg: #e8f0fd;
  --info-border: #a9c2ea;
  --warn: #9a6200;
  --changed: #fff3bf;
  --terminal-bg: #12161a;
  --terminal-text: #d6e2ea;

  /* The board drawing keeps the real board's colours in both schemes. */
  --pcb: #b3221d;
  --pcb-edge: #7c1410;
  --silkscreen: #f6f2ee;
  --metal: #c8ccd1;
  --cap: #2a2d31;
  --cap-pressed: #5b6168;
  --module-pcb: #1d2733;

  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c;
    --surface: #1e2227;
    --text: #e3e7eb;
    --muted: #9aa4ae;
    --border: #353b42;
    --accent: #4d8eea;
    --accent-text: #0b1220;
    --focus: #6aa5ff;
    --error-bg: #3a1717;
    --error-text: #ffb4b4;
    --error-border: #7a2c2c;
    --info-bg: #17263b;
    --info-border: #2f4c75;
    --warn: #f0b44c;
    --changed: #4a3f12;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
}

h1 { font-size: 1.15rem; margin: 0; white-space: nowrap; }
h3 { font-size: 0.95rem; margin: 0 0 0.4rem; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.mono { font-family: var(--mono); font-size: 0.85rem; }
.muted { color: var(--muted); }

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

/* --- Buttons ---------------------------------------------------------------------- */

button {
  font: inherit;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
#run:not(:disabled) { background: var(--accent); color: var(--accent-text); border-color: var(--accent); min-width: 5.5em; }
button.link {
  border: none; background: none; padding: 0 0.2rem;
  color: var(--accent); text-decoration: underline; font-size: 0.85rem;
}

/* --- Header ----------------------------------------------------------------------- */

.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem 1.5rem;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.toolbar { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.status { display: flex; flex-wrap: wrap; gap: 0.3rem 1.5rem; margin: 0; }
.status div { display: flex; gap: 0.4rem; align-items: baseline; }
.status dt { color: var(--muted); font-size: 0.85rem; }
.status dd { margin: 0; font-variant-numeric: tabular-nums; }
#firmware-name { font-weight: 600; overflow-wrap: anywhere; }

/* --- Banners ---------------------------------------------------------------------- */

.fault-banner, .notice {
  margin: 0.6rem 1rem 0;
  padding: 0.5rem 0.8rem;
  border-radius: 6px;
  border: 1px solid;
}
.fault-banner {
  background: var(--error-bg); color: var(--error-text); border-color: var(--error-border);
  border-left-width: 6px;
}
.fault-banner strong { font-size: 1.05rem; margin-right: 0.4rem; }
.fault-banner button { margin-left: 0.5rem; padding: 0.15rem 0.6rem; }
.notice { background: var(--info-bg); border-color: var(--info-border); }
.notice[data-kind="error"] { background: var(--error-bg); color: var(--error-text); border-color: var(--error-border); }
[hidden] { display: none !important; }

/* --- Layout: board and terminal side by side, diagnostics below ------------------ */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 2.2fr) minmax(18rem, 1fr);
  gap: 1rem;
  padding: 1rem;
}
.diagnostics-panel { grid-column: 1 / -1; }
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 0.6rem; }
}

.board-panel, .terminal-panel, .diagnostics-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem;
  min-width: 0;
}

.key-legend { margin: 0.6rem 0 0; color: var(--muted); font-size: 0.85rem; }
kbd {
  font-family: var(--mono); font-size: 0.8rem;
  padding: 0 0.3rem; border: 1px solid var(--border); border-radius: 4px; background: var(--bg);
}

/* --- Board drawing (SVG in mm) ------------------------------------------------- */

/* Below a certain width the buttons would be too small to touch: the drawing
   then keeps that width and its panel scrolls sideways. */
.board-panel { overflow-x: auto; }
.board { position: relative; width: 100%; min-width: 30rem; }
.board svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.board text { font-family: system-ui, sans-serif; user-select: none; -webkit-user-select: none; pointer-events: none; }

.pcb { fill: var(--pcb); stroke: var(--pcb-edge); stroke-width: 0.3; }
.hole { fill: var(--surface); stroke: #d9c27a; stroke-width: 0.5; }
.silkscreen { fill: var(--silkscreen); font-weight: 700; }
.silkscreen.hint { font-weight: 400; opacity: 0.85; }

.lcd-frame { fill: #3b3f44; }
.lcd-glass { fill: #0c0f12; }
/* The LCD box is placed and rotated by lcd-view.js; the canvas fills it. */
.lcd-box { position: absolute; pointer-events: none; }
canvas.lcd {
  display: block; width: 100%; height: 100%;
  image-rendering: pixelated;   /* crisp when enlarged */
  pointer-events: none;
}
canvas.lcd.touchable { pointer-events: auto; cursor: crosshair; touch-action: none; }
.lcd-marker {
  position: absolute; width: 14px; height: 14px; margin: -7px 0 0 -7px; box-sizing: border-box;
  border: 2px solid #fff; border-radius: 50%; box-shadow: 0 0 0 1.5px #d6249f, 0 0 4px #000;
  pointer-events: none;
}
.lcd-bar { display: flex; flex-wrap: wrap; gap: 0.2rem 0.8rem; align-items: baseline; margin: 0.4rem 0 0; font-size: 0.85rem; }
.lcd-status { color: var(--muted); }
.lcd-dialog { max-width: 96vw; padding: 0.8rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: inherit; }
.lcd-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.lcd-dialog button { margin-top: 0.6rem; }
/* Fits both the width and the height of the window; --ratio is width / height. */
.lcd-large { position: relative; aspect-ratio: var(--ratio, 1.333);
  width: min(90vw, calc(75vh * var(--ratio, 1.333))); background: #0c0f12; }

.led-body { fill: #e9e4d8; stroke: #9b9686; stroke-width: 0.15; }
.glow { pointer-events: none; }

.control { cursor: pointer; touch-action: none; -webkit-touch-callout: none; outline: none; }
.tact-body { fill: var(--metal); stroke: #7d838a; stroke-width: 0.2; }
.cap { fill: var(--cap); transition: fill 60ms; }
.cap-glyph { fill: #ffffff; font-weight: 700; }
.stick-base { fill: #3a3f45; stroke: #1b1e21; stroke-width: 0.3; }
.stick-cap { stroke: #111; stroke-width: 0.4; }
/* Joystick: the cap follows the deflection and springs back when released. */
.joystick { cursor: grab; }
.joystick.dragging { cursor: grabbing; }
.stick-gate { fill: none; stroke: #23272c; stroke-width: 0.4; stroke-dasharray: 0.6 0.5; }
.stick-cap-group { transition: transform 140ms cubic-bezier(0.2, 1.4, 0.4, 1); }
.joystick.dragging .stick-cap-group { transition: none; }
.stick-dimple { fill: #1f2226; stroke: #3c4148; stroke-width: 0.3; pointer-events: none; }
.joystick.pressed .stick-cap { fill: var(--cap-pressed); }
.joystick.pressed .stick-base { stroke: #6aa5ff; stroke-width: 0.5; }
.joystick:focus-visible .stick-base { stroke: var(--focus); stroke-width: 0.7; }
.readout { font-variant-numeric: tabular-nums; }
.push-ring { fill: var(--metal); stroke: #7d838a; stroke-width: 0.2; }
.button.push.pressed .push-ring { stroke: #6aa5ff; stroke-width: 0.5; }
.control.push:focus-visible .push-ring { stroke: var(--focus); stroke-width: 0.6; }
/* PWM-dimmed LEDs: between two partial levels the change is smoothed. */
.led.fading .led-lit, .led.fading .glow { transition: opacity 90ms linear; }
.button.pressed .cap { fill: var(--cap-pressed); }
.button.pressed .tact-body, .button.pressed .stick-base { stroke: #6aa5ff; stroke-width: 0.5; }
.reset .cap { fill: #b03030; }
.reset:active .cap { fill: #e05050; }
.reset[aria-disabled="true"] { cursor: default; opacity: 0.6; }
/* SVG groups take no CSS outline in every browser: mark the focus on the switch body. */
.control:focus-visible .tact-body, .control:focus-visible .stick-base { stroke: var(--focus); stroke-width: 0.7; }

.module-pcb { fill: var(--module-pcb); stroke: #0c1117; stroke-width: 0.3; }
.cable { fill: none; stroke: #8a8f96; stroke-width: 1.6; stroke-linecap: round; }

/* --- Terminal ---------------------------------------------------------------------- */

.terminal-panel { display: flex; flex-direction: column; min-height: 14rem; }
.tabs { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.4rem; }
[role="tablist"] { display: flex; gap: 0.2rem; }
[role="tab"] { border-radius: 6px 6px 0 0; padding: 0.25rem 0.7rem; font-size: 0.9rem; }
[role="tab"][aria-selected="true"] { border-color: var(--accent); font-weight: 600; }
.tab-actions { display: flex; gap: 0.4rem; }
[role="tab"].unread::after {
  content: ""; display: inline-block; width: 0.45rem; height: 0.45rem; margin-left: 0.35rem;
  border-radius: 50%; background: var(--accent); vertical-align: middle;
}
.uart-panel { display: flex; flex-direction: column; flex: 1 1 auto; gap: 0.4rem; min-height: 0; }
.uart-screen { cursor: text; }
.uart-screen .line { min-height: 1.4em; }
.uart-screen .cursor { outline: 1px solid var(--terminal-text); outline-offset: -1px; }
.uart-screen:focus .cursor, .uart-screen.focused .cursor {
  outline: none; background: var(--terminal-text); color: var(--terminal-bg);
}
.uart-screen:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.uart-input { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.uart-input input[type="text"] {
  flex: 1 1 10rem; min-width: 0; font: inherit; font-family: var(--mono); font-size: 0.85rem;
  padding: 0.3rem 0.5rem; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.uart-input select {
  font: inherit; font-size: 0.85rem; padding: 0.3rem; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.uart-input .check { display: inline-flex; align-items: center; gap: 0.2rem; font-size: 0.85rem; white-space: nowrap; }
.small { font-size: 0.8rem; margin: 0; }

/* ANSI colours (SGR 30-37, 90-97 and the backgrounds) on the dark terminal. */
.uart-screen .bold { font-weight: 700; }
.uart-screen .underline { text-decoration: underline; }
.fg-0 { color: #4d5560; } .fg-1 { color: #ff6b6b; } .fg-2 { color: #5fd37a; } .fg-3 { color: #f0c64a; }
.fg-4 { color: #6aa5ff; } .fg-5 { color: #d58cff; } .fg-6 { color: #4fd6d6; } .fg-7 { color: #d6e2ea; }
.fg-8 { color: #7d8792; } .fg-9 { color: #ff9b9b; } .fg-10 { color: #8ef0a5; } .fg-11 { color: #ffe27a; }
.fg-12 { color: #a3c8ff; } .fg-13 { color: #e8b5ff; } .fg-14 { color: #8af0f0; } .fg-15 { color: #ffffff; }
.bg-0 { background: #000000; } .bg-1 { background: #a12626; } .bg-2 { background: #227a3a; } .bg-3 { background: #8a6d10; }
.bg-4 { background: #1f4f9c; } .bg-5 { background: #7a2f9a; } .bg-6 { background: #1d7a7a; } .bg-7 { background: #b8c2cc; }
.bg-8 { background: #4d5560; } .bg-9 { background: #d94545; } .bg-10 { background: #3aa65a; } .bg-11 { background: #c9a227; }
.bg-12 { background: #3d7ad9; } .bg-13 { background: #a64fd1; } .bg-14 { background: #2ea6a6; } .bg-15 { background: #e8eef3; }
.fg-default-fg { color: var(--terminal-text); } .bg-default-bg { background: var(--terminal-bg); }
.fg-default-bg { color: var(--terminal-bg); } .bg-default-fg { background: var(--terminal-text); }

.terminal {
  flex: 1 1 auto;
  margin: 0;
  min-height: 12rem;
  max-height: 60vh;
  overflow: auto;
  padding: 0.5rem;
  border-radius: 6px;
  background: var(--terminal-bg);
  color: var(--terminal-text);
  font-family: var(--mono);
  font-size: 0.82rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* --- Diagnostics ----------------------------------------------------------------- */

.diagnostics-panel summary { cursor: pointer; font-weight: 600; }
#diag-counts { font-weight: 400; color: var(--muted); font-size: 0.9rem; }
#diag-counts.has-issues { color: var(--warn); }
#diag-counts.has-faults { color: var(--error-text); font-weight: 700; }

.diag-body {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
  gap: 1rem 1.5rem;
  margin-top: 0.8rem;
}
.diag-body .wide { grid-column: 1 / -1; }

dl.kv { margin: 0 0 0.6rem; display: grid; grid-template-columns: max-content 1fr; gap: 0.15rem 0.8rem; }
dl.kv div { display: contents; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; font-variant-numeric: tabular-nums; }

dl.registers {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
  gap: 0.1rem 0.8rem;
}
.register { display: flex; justify-content: space-between; gap: 0.5rem; padding: 0 0.25rem; border-radius: 3px; }
.register dt { color: var(--muted); }
.register dd { margin: 0; }
.register.changed { background: var(--changed); }

.fault {
  border: 1px solid var(--error-border); border-left-width: 6px; border-radius: 6px;
  background: var(--error-bg); color: var(--error-text);
  padding: 0.4rem 0.7rem; margin-bottom: 0.5rem;
}
.fault p { margin: 0.15rem 0; }
.fault-title { font-weight: 700; }
.fault .muted { color: inherit; opacity: 0.8; font-weight: 400; }
.empty { color: var(--muted); margin: 0; }

.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
th, td { text-align: left; padding: 0.2rem 0.6rem 0.2rem 0; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
td.number { text-align: right; font-variant-numeric: tabular-nums; }
td:last-child, th:last-child { padding-right: 0; }

.kind { display: inline-block; padding: 0 0.4rem; border-radius: 4px; font-size: 0.8rem; font-weight: 600; border: 1px solid; }
.kind-unmapped { color: var(--error-text); background: var(--error-bg); border-color: var(--error-border); }
.kind-unimplemented { color: var(--warn); border-color: currentColor; }
.kind-clock-disabled { color: var(--accent); border-color: currentColor; }
.kind-read-only { color: var(--muted); border-color: currentColor; }

/* --- Footer and drop target ------------------------------------------------------- */

.footer { padding: 0 1rem 1rem; color: var(--muted); font-size: 0.8rem; display: flex; flex-wrap: wrap; gap: 1rem; }
.mock-badge { color: var(--warn); font-weight: 600; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  border: 4px dashed var(--accent);
  font-size: 1.3rem; font-weight: 600;
  pointer-events: none;
}

/* --- DAC read-out ------------------------------------------------------------------ */
.dac-header .dac-body { fill: #1b1b1b; stroke: #555; stroke-width: 0.15; }
.dac-header .dac-pad { fill: #d9c27a; }
.dac-header .dac-track { fill: rgba(0, 0, 0, 0.45); }
.dac-header .dac-level { fill: #ffd60a; }
.dac-panel { margin: 0.6rem 0 0; }
.dac-panel h3 { margin: 0 0 0.3rem; font-size: 0.95rem; }
.dac-list { list-style: none; margin: 0; padding: 0; }
.dac-row { display: flex; align-items: center; gap: 0.5rem; margin: 0.15rem 0; font-size: 0.9rem; }
.dac-swatch { width: 0.7rem; height: 0.7rem; border-radius: 2px; flex: none; }
.dac-text { font-family: var(--mono); font-size: 0.85rem; min-width: 14em; }
.dac-bar { flex: 1 1 4rem; max-width: 12rem; height: 0.6rem; background: var(--border); border-radius: 3px; overflow: hidden; }
.dac-fill { display: block; height: 100%; }
.dac-scope { display: block; width: 100%; max-width: 32rem; height: 90px; margin-top: 0.3rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 3px; }
@media (max-width: 480px) { .dac-text { min-width: 0; } }
