/* =================================================================================================
   Stimulus Generator – the look of the Thomson apps (Eye Dynamix / clinic portal / LogMAR Scorer):
   navigation rail with the Thomson logo, top bar, dark blue title band, near-white cards.
   Three colour sets: White (light), Black (dark) and Blue.
   The stimulus fills the main area; Full screen gives it the whole screen with fading buttons.
   On a phone the options become a bottom sheet.
   ================================================================================================= */
:root, html[data-theme="light"] {
  --canvas: #eef2f6; --surface: #ffffff; --surface-alt: #f6f8fa; --input: #ffffff;
  --ink: #1b2733; --muted: #5b6b7b; --border: #d5dde5; --grid: #e8edf2;
  --primary: #095595; --primary-hover: #0e67b2; --primary-soft: #dce9f6; --on-primary: #fff;
  --nav: #0c2e52; --nav-fore: #ffffff; --nav-muted: #a5bdd6; --nav-selected: #095595;
  --header: #ffffff; --header-fore: #1b2733; --header-muted: #5b6b7b;
  --danger: #b42318; --danger-soft: #fdeceb; --success: #16803d; --success-soft: #e1f5e8;
  --warning: #b06a00; --warning-soft: #fff3dc; --band: #1b4884; --select: #dce9f6;
  --cell: #ffffff; --cell-ink: #111820; --cross: #d12a1f; --near: #c0261b;
  color-scheme: light;
}
html[data-theme="dark"] {
  --canvas: #17191c; --surface: #212428; --surface-alt: #2a2e33; --input: #1c1f23;
  --ink: #e8eaed; --muted: #9aa3ad; --border: #3a3f46; --grid: #30343a;
  --primary: #3d8bda; --primary-hover: #569ee6; --primary-soft: #1e3a56; --on-primary: #fff;
  --nav: #111316; --nav-fore: #e8eaed; --nav-muted: #828c96; --nav-selected: #284668;
  --header: #1c1f23; --header-fore: #e8eaed; --header-muted: #9aa3ad;
  --danger: #ef6155; --danger-soft: #46201e; --success: #57c878; --success-soft: #1c3c28;
  --warning: #f0b446; --warning-soft: #463719; --band: #1b3a63; --select: #284668;
  --cell: #f4f5f7; --cell-ink: #111820; --cross: #e0362b; --near: #ff7a70;
  color-scheme: dark;
}
html[data-theme="blue"] {
  --canvas: #bfcddb; --surface: #fbfcfe; --surface-alt: #ecf2f8; --input: #f4f8fc;
  --ink: #102136; --muted: #364e69; --border: #96aeca; --grid: #c9d6e4;
  --primary: #1f4e79; --primary-hover: #2a6194; --primary-soft: #b0c7e0; --on-primary: #fff;
  --nav: #1f4e79; --nav-fore: #ffffff; --nav-muted: #bed2e6; --nav-selected: #5b80a8;
  --header: #99b4d1; --header-fore: #102136; --header-muted: #364e69;
  --danger: #aa1e14; --danger-soft: #f6dedc; --success: #147337; --success-soft: #d6eede;
  --warning: #a05f00; --warning-soft: #f6e9cd; --band: #1b4884; --select: #b0c7e0;
  --cell: #ffffff; --cell-ink: #102136; --cross: #c8231a; --near: #b3170e;
  color-scheme: light;
}


* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--canvas); color: var(--ink); font: 14px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
.hidden { display: none !important; }
[hidden] { display: none !important; }
svg.i { width: 18px; height: 18px; flex: none; }
.muted { color: var(--muted); font-size: 12.5px; margin: 0 0 4px; }

.app { display: flex; height: 100vh; height: 100dvh; }

/* ---------- Navigation rail ---------- */
.rail { width: 208px; flex: none; background: var(--nav); color: var(--nav-fore); display: flex; flex-direction: column; padding: 0 0 10px; overflow-y: auto; }
.rail .logo { display: flex; align-items: center; gap: 10px; min-height: 74px; padding: 12px 16px; font-size: 15px; font-weight: 600; line-height: 1.15; }
.rail .logo img { width: 36px; height: 36px; flex: none; }
.rail .logo .sub { display: block; font-size: 11.5px; font-weight: 400; color: var(--nav-muted); margin-top: 2px; }
.rail .item { position: relative; display: flex; align-items: center; gap: 14px; height: 42px; margin: 2px 8px; padding: 0 12px; border: 0; border-radius: 8px; background: transparent; color: var(--nav-muted); font-weight: 600; font-size: 13.5px; cursor: pointer; text-align: left; text-decoration: none; }
.rail .item:hover { background: rgba(255,255,255,.11); color: var(--nav-fore); }
.rail .item.on { background: var(--nav-selected); color: var(--nav-fore); }
.rail .item.on::before { content: ""; position: absolute; left: 0; top: 10px; bottom: 10px; width: 3px; border-radius: 2px; background: var(--nav-fore); }
.rail .grow { flex: 1; }
.rail .thomson { display: block; margin: 10px 8px 0; background: #fff; border-radius: 8px; padding: 10px 12px; }
.rail .thomson img { display: block; width: 100%; }

/* ---------- Top bar ---------- */
.body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { position: relative; display: flex; align-items: center; gap: 10px; height: 60px; flex: none; padding: 0 22px; background: var(--header); color: var(--header-fore); border-bottom: 1px solid var(--border); }
.topbar .mlogo { display: none; width: 30px; height: 30px; }
.topbar .ttl b { display: block; font-size: 15px; }
.topbar .ttl small { display: block; font-size: 12px; color: var(--header-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 52vw; }
.spacer { flex: 1; }
.chip { display: inline-flex; align-items: center; gap: 8px; height: 28px; padding: 0 12px; border-radius: 14px; background: var(--surface-alt); color: var(--ink); font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.chip.warn { background: var(--warning-soft); } .chip.warn .dot { background: var(--warning); }
.iconbtn { width: 38px; height: 38px; display: grid; place-items: center; border: 0; border-radius: 8px; background: transparent; color: inherit; }
.iconbtn:hover { background: var(--surface-alt); }
.iconbtn svg.i { width: 20px; height: 20px; }
.monly { display: none; }
.menu { position: absolute; right: 14px; top: 54px; z-index: 60; background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18); padding: 6px; min-width: 210px; }
.menu button, .menu a { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border: 0; border-radius: 6px; background: transparent; cursor: pointer; text-align: left; text-decoration: none; font-size: 14px; }
.menu button:hover, .menu a:hover { background: var(--surface-alt); }
.menu button.on { font-weight: 700; color: var(--primary); }
.menu .sep { height: 1px; background: var(--border); margin: 6px 4px; }
.menu .note { padding: 4px 10px 2px; font-size: 11.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--border); }

/* ---------- Main area and title band ---------- */
main { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 18px 22px 20px; gap: 14px; }
.band { flex: none; display: flex; align-items: center; gap: 8px; background: var(--band); color: #fff; border-radius: 4px; padding: 12px 14px 11px 20px; }
.band h1 { font-size: 21px; margin: 0; font-weight: 600; line-height: 1.2; }
.band p { margin: 3px 0 0; font-size: 12.5px; opacity: .85; }
.band .grow { flex: 1; min-width: 0; }
.bandtools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.bandbtn { display: inline-flex; align-items: center; gap: 7px; padding: 7px 12px; border: 1px solid rgba(255,255,255,.45); border-radius: 6px; background: transparent; color: #fff; font-size: 13px; font-weight: 600; white-space: nowrap; }
.bandbtn:hover { background: rgba(255,255,255,.15); }
.bandbtn:disabled { opacity: .4; cursor: default; background: transparent; }
.bandbtn.solid { background: #fff; color: var(--band); border-color: #fff; }
.bandbtn.solid:hover { background: #e8eef6; }
.bandchk { display: inline-flex; align-items: center; gap: 7px; padding: 6px 10px; border: 1px solid rgba(255,255,255,.45); border-radius: 6px; color: #fff; font-size: 13px; font-weight: 600; cursor: pointer; user-select: none; white-space: nowrap; }
.bandchk input { width: 16px; height: 16px; margin: 0; accent-color: #f5a623; cursor: pointer; }
.bandchk:has(input:checked) { background: rgba(245,166,35,.22); border-color: #f5a623; }
.band .help { flex: none; width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent; color: #fff; font-size: 16px; font-weight: 600; opacity: .8; }
.band .help:hover { background: rgba(255,255,255,.15); opacity: 1; }

.content { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 14px; }
.chartcol { min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; align-items: center; padding-bottom: 4px; }
.side { min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; padding-right: 2px; }
.sheethead { display: none; }

/* ---------- Cards and controls ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; }
.card h2 { position: relative; font-size: 15px; margin: 0 0 12px; padding-left: 12px; color: var(--primary); font-weight: 600; }
.card h2::before { content: ""; position: absolute; left: 0; top: 2px; bottom: 2px; width: 3px; border-radius: 2px; background: var(--primary); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 8px 14px; font-weight: 600; font-size: 13px; }
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.flat { border-color: transparent; background: transparent; }
.btn.flat:hover { background: var(--surface-alt); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }
.buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }
select, input[type=text] { width: 100%; background: var(--input); border: 1px solid var(--border); border-radius: 4px; padding: 7px 9px; min-height: 36px; }
select:focus, input:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.row { display: grid; grid-template-columns: 60px 1fr; align-items: center; gap: 8px; margin: 6px 0; }
.row > span { font-size: 13px; color: var(--muted); font-weight: 600; }
.switch { display: flex; align-items: center; gap: 10px; padding: 7px 0; cursor: pointer; user-select: none; }
.switch input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); flex: none; }
.switch small { color: var(--muted); }
.tag { display: inline-block; padding: 2px 9px; border-radius: 10px; font-size: 11.5px; font-weight: 600; background: var(--surface-alt); color: var(--muted); white-space: nowrap; }
.tag.good { background: var(--success-soft); color: var(--success); }


/* ---------- Dialog and toast ---------- */
dialog { border: 1px solid var(--border); border-radius: 10px; padding: 0; background: var(--surface); color: var(--ink); width: min(460px, calc(100vw - 28px)); box-shadow: 0 18px 50px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(10,20,35,.45); }
dialog .dh { background: var(--band); color: #fff; padding: 13px 18px; font-size: 17px; font-weight: 600; }
dialog .db { padding: 16px 18px; max-height: 64vh; overflow-y: auto; }
dialog .db p { margin: 0 0 10px; }
dialog .db h3 { font-size: 14px; color: var(--primary); margin: 14px 0 6px; }
dialog .db ul { margin: 0 0 8px; padding-left: 20px; } dialog .db li { margin: 4px 0; }
dialog .df { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; }
dialog .err { color: var(--danger); font-size: 12.5px; min-height: 18px; margin-top: 6px; }
dialog .radio { display: flex; gap: 8px; align-items: center; padding: 6px 0; }
.ic { display: inline-grid; place-items: center; width: 26px; height: 26px; border: 1px solid var(--border); border-radius: 5px; vertical-align: middle; color: var(--primary); background: var(--surface-alt); }
.ic svg.i { width: 18px; height: 18px; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--success); color: #fff; padding: 9px 18px; border-radius: 6px; z-index: 90; font-weight: 600; box-shadow: 0 6px 18px rgba(0,0,0,.2); white-space: nowrap; }
.backdrop { position: fixed; inset: 0; background: rgba(10,20,35,.45); z-index: 70; }



/* ---------- Layout ---------- */
.content { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 14px; }
.stagecol { min-height: 0; display: flex; }
.side { min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; padding-right: 2px; }
.sheethead { display: none; }
.lbl { font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 12px 0 6px; }
.card h2 + .lbl { margin-top: 0; }
.off { opacity: .35; pointer-events: none; }
.note { margin-top: 10px; padding: 8px 10px; border-radius: 6px; background: var(--warning-soft); font-size: 12.5px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.note .btn { padding: 5px 10px; }

/* the dot / grating choice in the band */
.segs { display: inline-flex; border: 1px solid rgba(255,255,255,.45); border-radius: 6px; overflow: hidden; }
.segs button { border: 0; background: transparent; color: #fff; padding: 7px 14px; font-weight: 600; font-size: 13px; }
.segs button + button { border-left: 1px solid rgba(255,255,255,.3); }
.segs button.on { background: rgba(255,255,255,.22); }
.segs button:hover:not(.on) { background: rgba(255,255,255,.1); }
.bandbtn svg.i { width: 16px; height: 16px; }

/* only the card of the stimulus in use */
body[data-stim="dot"] .kind-grating, body[data-stim="grating"] .kind-dot { display: none; }

/* ---------- The stimulus ---------- */
.stage { position: relative; flex: 1; min-height: 240px; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); background: #fff; touch-action: manipulation; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.stagechips { position: absolute; left: 10px; bottom: 10px; display: flex; gap: 6px; pointer-events: none; }
.pchip { background: rgba(20,30,45,.72); color: #fff; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 10px; font-variant-numeric: tabular-nums; white-space: pre; }
body.paused .stagechips::after { content: "Paused"; background: #f5a623; color: #1b1b1b; font-size: 12px; font-weight: 700; padding: 3px 10px; border-radius: 10px; }
.presentbar { display: none; position: absolute; top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right)); gap: 6px; z-index: 5; transition: opacity .4s; }
.pbtn { width: 44px; height: 44px; display: grid; place-items: center; border: 1px solid rgba(255,255,255,.35); border-radius: 10px; background: rgba(20,30,45,.72); color: #fff; }
.pbtn:hover { background: rgba(20,30,45,.9); }
.pbtn svg.i { width: 20px; height: 20px; }

/* full screen */
body.present .stage { position: fixed; inset: 0; z-index: 60; border: 0; border-radius: 0; }
body.present .presentbar { display: flex; }
body.present .stagechips { display: none; }
body.present.idle .presentbar { opacity: 0; pointer-events: none; }
body.present.idle { cursor: none; }
body.present .side { display: none; }
body.present.popts .side { display: flex; position: fixed; z-index: 65; top: 68px; right: 14px; width: 340px; max-height: calc(100vh - 82px); background: var(--canvas); padding: 10px; border-radius: 10px; box-shadow: 0 12px 40px rgba(0,0,0,.35); }

/* ---------- Colour swatches ---------- */
.swatches { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.sw { aspect-ratio: 1; border-radius: 8px; border: 1px solid rgba(0,0,0,.25); background: var(--c); padding: 0; min-height: 30px; position: relative; }
.sw.on { outline: 3px solid var(--primary); outline-offset: 2px; }
.sw.on::after { content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 50%; border: solid #fff; border-width: 0 3px 3px 0; transform: translate(-50%, -62%) rotate(45deg); }
.sw.light.on::after { border-color: #111; }
.swname { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); }

/* ---------- Two- or three-way choices in the cards ---------- */
.segs2 { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.segs2 button { flex: 1; border: 0; background: var(--surface); padding: 8px 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.segs2 button + button { border-left: 1px solid var(--border); }
.segs2 button.on { background: var(--primary); color: var(--on-primary); }

/* ---------- Sliders with a number box ---------- */
.slider { margin-top: 14px; }
.shead { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.shead label { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.num { display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: 6px; background: var(--input); overflow: hidden; }
.num input { width: 58px; border: 0; background: transparent; text-align: right; padding: 5px 2px; font-weight: 600; font-variant-numeric: tabular-nums; -moz-appearance: textfield; }
.num input::-webkit-inner-spin-button, .num input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.num input:focus { outline: none; }
.num .unit { font-size: 12px; color: var(--muted); padding: 0 6px 0 2px; min-width: 8px; }
.num .step { width: 30px; height: 30px; border: 0; background: var(--surface-alt); font-size: 16px; font-weight: 600; color: var(--primary); touch-action: manipulation; }
.num .step:first-child { border-right: 1px solid var(--border); } .num .step:last-child { border-left: 1px solid var(--border); }
.slider input[type=range] { width: 100%; margin: 10px 0 2px; accent-color: var(--primary); height: 22px; }
.sends { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }

/* =================================================================================================
   Tablet: narrower rail
   ================================================================================================= */
@media (max-width: 1100px) {
  .rail { width: 64px; }
  .rail .logo { padding: 14px; justify-content: center; }
  .rail .logo > span { display: none; }
  .rail .item { font-size: 0; gap: 0; justify-content: center; padding: 0; }
  .rail .item svg.i { width: 22px; height: 22px; }
  .rail .thomson { padding: 6px; }
  .content { grid-template-columns: minmax(0, 1fr) 310px; }
}

/* =================================================================================================
   Phone (and a phone on its side)
   ================================================================================================= */
@media (max-width: 760px), (max-height: 520px) {
  .rail { display: none; }
  .topbar { height: calc(54px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 6px 0 12px; gap: 6px; background: var(--nav); color: var(--nav-fore); border-bottom: 0; }
  .topbar .mlogo { display: block; }
  .topbar .ttl small { color: var(--nav-muted); }
  .iconbtn:hover { background: rgba(255,255,255,.12); }
  .monly { display: grid; }
  main { padding: 0; gap: 0; }
  .band { border-radius: 0; padding: 8px 6px 8px 10px; }
  .band .grow { display: none; }
  .bandtools { flex: 1; justify-content: flex-start; flex-wrap: nowrap; }
  .bandbtn { padding: 7px 10px; }
  .content { display: flex; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); }
  .stagecol { flex: 1; }
  .stage { min-height: 0; }

  .side { position: fixed; left: 0; right: 0; bottom: 0; z-index: 80; max-height: 78vh; max-height: 78dvh; padding: 0 12px calc(16px + env(safe-area-inset-bottom)); background: var(--canvas); border-radius: 16px 16px 0 0; box-shadow: 0 -10px 40px rgba(0,0,0,.25); transform: translateY(105%); transition: transform .22s ease; gap: 10px; }
  body.sheet .side { transform: none; }
  body.present.sheet .side { display: flex; }
  .sheethead { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 1; background: var(--canvas); padding: 12px 2px 6px; font-size: 16px; }
  .sheethead::before { content: ""; position: absolute; left: 50%; top: 5px; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: var(--border); }
  .backdrop { z-index: 75; }
  .swatches { gap: 8px; }
  .sw { aspect-ratio: auto; height: 36px; min-height: 0; }
  .toast { bottom: calc(24px + env(safe-area-inset-bottom)); }
  .menu { top: calc(50px + env(safe-area-inset-top)); right: 8px; }
}
@media (max-width: 400px) {
  .bandbtn span { display: none; }
  .bandbtn { padding: 7px 9px; }
}
@media (max-height: 520px) {
  .topbar { height: calc(44px + env(safe-area-inset-top)); }
  .topbar .ttl small { display: none; }
  .band { padding: 5px 8px; }
  .side { max-height: 92vh; max-height: 92dvh; }
}
