:root {
  color-scheme: light dark;
  --panel: #e7eaee;
  --face: #f9fafb;
  --surface: #ffffff;
  --well: #eef1f4;
  --ink: #1a2027;
  --muted: #5a6470;
  --line: #cdd3da;
  --line-strong: #aeb7c1;
  --live: #1d7f49;
  --live-ink: #ffffff;
  --live-soft: #dcefe3;
  --lamp: #22c35e;
  --lamp-off: #9aa3ad;
  --warn: #9a6200;
  --warn-soft: #fbf0d9;
  --danger: #b23a3a;
  --focus: #2f7de1;
  --term-bg: #fafbfc;
  --term-fg: #24292f;
  --term-strong: #0b0d12;
  --term-muted: #5a6470;
  --watermark: url("/static/img/hglynx-mark-on-light.svg");
  --splash-bg: #f4f1eaf2;
  --on-dark: none;
  --on-light: block;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace;
}

/* Dark tokens twice: for the system setting and for the explicit choice. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --panel: #1a1f25;
    --face: #232930;
    --surface: #1f252b;
    --well: #151a1f;
    --ink: #dfe5eb;
    --muted: #8f9aa6;
    --line: #2d343c;
    --line-strong: #414b56;
    --live: #2a9259;
    --live-soft: #173326;
    --lamp: #3be37a;
    --lamp-off: #4d5763;
    --warn: #e5ab4a;
    --warn-soft: #33290f;
    --danger: #f0827a;
    --focus: #5b9cf0;
    --splash-bg: #0b0d12f2;
    --on-dark: block;
    --on-light: none;
    --term-bg: #0f1419;
    --term-fg: #d5dce4;
    --term-strong: #f4f7fa;
    --term-muted: #9aa6b2;
    --watermark: url("/static/img/hglynx-mark-on-dark.svg");
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --panel: #1a1f25;
  --face: #232930;
  --surface: #1f252b;
  --well: #151a1f;
  --ink: #dfe5eb;
  --muted: #8f9aa6;
  --line: #2d343c;
  --line-strong: #414b56;
  --live: #2a9259;
  --live-soft: #173326;
  --lamp: #3be37a;
  --lamp-off: #4d5763;
  --warn: #e5ab4a;
  --warn-soft: #33290f;
  --danger: #f0827a;
  --focus: #5b9cf0;
  --splash-bg: #0b0d12f2;
  --on-dark: block;
  --on-light: none;
  --term-bg: #0f1419;
  --term-fg: #d5dce4;
  --term-strong: #f4f7fa;
  --term-muted: #9aa6b2;
  --watermark: url("/static/img/hglynx-mark-on-dark.svg");
}
:root[data-theme="light"] { color-scheme: light; }

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--panel);
  color: var(--ink);
  font: 13px/1.4 var(--ui);
}

body.unsupported .toolbar .group:not(.end),
body.unsupported #term-size,
body.unsupported #btn-share,
body[data-mode="helper"] .host-only,
body[data-mode="host"] .helper-only { display: none !important; }

/* Toolbar: groups read like the bays of an instrument front panel. */
/* Each group draws its divider on the left and sits 1px left of its slot, so a
   divider that lands at the start of a wrapped row is clipped away. */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 0;
  padding: 7px 10px 7px 0;
  overflow: hidden;
  border-bottom: 1px solid var(--line);
}
.group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  margin-left: -1px;
  padding: 0 9px 0 10px;
  border-left: 1px solid var(--line);
}
.group.end { margin-left: auto; border-left-color: transparent; padding-right: 0; }
.joined { display: inline-flex; }
.joined > * { border-radius: 0; }
.joined > :first-child { border-radius: 6px 0 0 6px; }
.joined > :last-child { border-radius: 0 6px 6px 0; }
.joined > * + * { margin-left: -1px; }

.btn, .ctl {
  height: 30px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--face);
  color: var(--ink);
  font: inherit;
  white-space: nowrap;
}
.btn { padding: 0 10px; cursor: pointer; }
.btn:hover:not(:disabled), .ctl:hover:not(:disabled) { border-color: var(--muted); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled, .ctl:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { height: 24px; padding: 0 8px; font-size: 12px; }
.ctl.small { height: 22px; padding-left: 6px; font-size: 12px; }
.ctl { padding: 0 8px; }
select.ctl {
  appearance: none;
  padding-right: 24px;
  background-image:
    linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 13px) 54%, calc(100% - 9px) 54%;
  background-size: 4px 4px;
  background-repeat: no-repeat;
  cursor: pointer;
}
select.ctl option { background: var(--surface); color: var(--ink); }
.num { width: 98px; font-variant-numeric: tabular-nums; }
.num.ms { width: 68px; }

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

/* Signal lamps: lit while the line (or port) is active. */
.lamp::before, .btn.port::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  background: var(--lamp-off);
  vertical-align: 0;
}
.lamp[aria-pressed="true"]::before, .btn.port.live::before {
  background: var(--lamp);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--lamp) 25%, transparent), 0 0 7px var(--lamp);
}
.lamp:disabled::before { box-shadow: none; }
.btn.port.live { font-variant-numeric: tabular-nums; }

.size {
  padding: 0 6px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  cursor: help;
}
.btn.share.live, .btn.go {
  background: var(--live);
  border-color: var(--live);
  color: var(--live-ink);
  font-weight: 600;
}
.btn.share.live { font-variant-numeric: tabular-nums; letter-spacing: .01em; }
.btn.danger { color: var(--danger); }

/* Helper session strip. */
.strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 16px;
  padding: 6px 12px;
  background: var(--live-soft);
  border-bottom: 1px solid var(--line);
  font-weight: 500;
}
body[data-session="ended"] .strip, body[data-session="failed"] .strip { background: var(--warn-soft); }
#sas-banner { font-size: 14px; }

/* Padding sits on .stage: FitAddon ignores the padding of the terminal's parent. */
.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 6px 4px 4px 10px;
  background: var(--term-bg);
}
#terminal { position: relative; z-index: 1; height: 100%; }
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--watermark) center / min(42%, 42vh) no-repeat;
  opacity: .07;
  pointer-events: none;
}
body[data-mode="helper"] .stage { padding: 6px; }
body[data-mode="helper"] #terminal { display: grid; place-items: center; }
body[data-mode="helper"] .btn.port::before { display: none; }
#terminal .xterm { height: 100%; }
body[data-mode="helper"] #terminal .xterm { height: auto; }
#terminal .xterm-viewport { background-color: transparent !important; }

.notice {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: color-mix(in srgb, var(--term-bg) 88%, transparent);
  color: var(--term-fg);
  text-align: center;
}
.notice p { margin: 0 0 8px; max-width: 46ch; }
.notice-title { font-size: 18px; font-weight: 600; color: var(--term-strong); }
.notice .hint { color: var(--term-muted); }
.emojis {
  margin: 6px 0 14px;
  font-size: 38px;
  line-height: 1.2;
  letter-spacing: .18em;
  text-align: center;
}
.notice .emojis:empty { display: none; }

/* Status line. */
#status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 14px;
  min-height: 28px;
  padding: 4px 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
#status .conn { color: var(--ink); }
#status .conn.live::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--lamp);
}
#status .event { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1 1 200px; }
#status .warn { color: var(--warn); font-weight: 600; }
#status .credit { display: flex; align-items: center; gap: 4px; margin-left: auto; white-space: nowrap; }
#status .credit select { margin-right: 10px; color: var(--muted); }
#status a, .linkbtn { color: inherit; text-decoration: none; }
#status a:hover, .linkbtn:hover { color: var(--ink); text-decoration: underline; }
.linkbtn { padding: 0; border: 0; background: none; font: inherit; cursor: pointer; }

/* Dialogs. */
dialog {
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 18px 50px rgba(8, 12, 16, .28);
}
dialog::backdrop { background: rgba(10, 14, 18, .45); }
dialog.attention { border-top: 4px solid var(--warn); }
dialog#dlg-about { width: min(540px, calc(100vw - 32px)); }
.dlg { padding: 20px 22px 18px; }
.dlg h2 { margin: 0 0 6px; font-size: 18px; font-weight: 650; }
.dlg .lead { margin: 0 0 8px; color: var(--muted); }
.dlg b { color: var(--ink); font-family: var(--mono); }
.about-logo { margin: 0 0 4px; }
.about-logo img { display: var(--on-light); width: 242px; max-width: 100%; height: auto; }
.about-logo img.logo-on-dark { display: var(--on-dark); }
.about h3 { margin: 14px 0 4px; font-size: 13px; font-weight: 650; }
.about ol { margin: 0; padding-left: 20px; }
.about li { margin: 2px 0; }
.about .lead { margin-top: 12px; }
.about-links { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px; margin-top: 14px; }
.about-links a { display: inline-flex; align-items: center; gap: 8px; color: var(--focus); }
.about-links img { display: block; }
.code {
  display: block;
  margin: 12px 0 10px;
  padding: 14px 10px;
  border-radius: 8px;
  background: var(--well);
  font: 600 30px/1.2 var(--mono);
  letter-spacing: .08em;
  text-align: center;
  user-select: all;
}
.code.placeholder { font: 14px/1.6 var(--ui); letter-spacing: 0; color: var(--muted); user-select: none; }
.row { display: flex; flex-wrap: wrap; gap: 6px; }
.lockout, .code-warning {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--warn-soft);
  color: var(--ink);
}
.lockout p { margin: 0 0 8px; }
fieldset { margin: 16px 0 6px; padding: 0; border: 0; }
legend { padding: 0; margin-bottom: 4px; font-weight: 600; }
fieldset label, .opt { display: flex; align-items: baseline; gap: 8px; padding: 3px 0; cursor: pointer; }
.opt { flex-wrap: wrap; margin-top: 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.opt .hint { flex-basis: 100%; padding-left: 22px; color: var(--muted); font-size: 12px; }
input[type="checkbox"] { accent-color: var(--live); width: 14px; height: 14px; margin: 0; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 18px; }
.grow { flex: 1; }
.code-input {
  width: 100%;
  height: 46px;
  margin: 6px 0 4px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--well);
  color: var(--ink);
  font: 600 22px var(--mono);
  letter-spacing: .08em;
  text-transform: uppercase;
}
.code-input::placeholder { color: var(--muted); opacity: .5; }
.error { margin: 6px 0 0; color: var(--danger); }

@media (max-width: 640px) {
  .toolbar { padding: 6px 8px 6px 0; }
  .group { padding: 0 6px 0 8px; }
  .group.end { margin-left: 0; }
  .code { font-size: 24px; }
}

/* Splash: shown on load, fades out on its own, or on a click or key. */
.splash {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--splash-bg);
  cursor: pointer;
  animation: splash-out .35s ease 1s forwards;
}
.splash.leaving { animation: splash-out .35s ease forwards; }
.splash img { display: var(--on-light); width: min(46vh, 34vw); height: auto; animation: splash-in .45s ease-out; }
.splash img.logo-on-dark { display: var(--on-dark); }
@keyframes splash-in {
  from { opacity: 0; transform: scale(.96); }
  to { opacity: 1; transform: none; }
}
@keyframes splash-out {
  to { opacity: 0; visibility: hidden; }
}
@media (prefers-reduced-motion: reduce) {
  .splash { display: none; }
}
