:root {
  color-scheme: dark;
  --ink: #100d19;
  --ink-deep: #08070d;
  --panel: #181321;
  --panel-raised: #21182d;
  --paper: #f3ead7;
  --paper-dim: #b8ad9a;
  --brass: #d8a84b;
  --brass-light: #f6cf72;
  --orange: #ff6a2a;
  --violet: #a875ff;
  --cyan: #58e8da;
  --danger: #ff5266;
  --line: rgba(216, 168, 75, .34);
  --shadow: rgba(0, 0, 0, .55);
  --radius: 8px;
  font-family: "Courier New", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }

html { min-width: 300px; background: var(--ink-deep); }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--paper);
  background:
    radial-gradient(circle at 12% 8%, rgba(125, 72, 184, .18), transparent 32rem),
    radial-gradient(circle at 90% 46%, rgba(22, 154, 143, .11), transparent 34rem),
    linear-gradient(145deg, #100d19 0%, #0a0910 72%);
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
  opacity: .18;
  background: repeating-linear-gradient(0deg, transparent 0 3px, rgba(255,255,255,.018) 3px 4px);
  mix-blend-mode: screen;
}

button, a { font: inherit; }
button { color: inherit; }
button:focus-visible, a:focus-visible, canvas:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 3px;
}

.lab-atmosphere {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255,255,255,.018) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.018) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: linear-gradient(to bottom, black, transparent 78%);
}

.lab-header {
  position: relative;
  z-index: 5;
  min-height: 70px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: .8rem clamp(1rem, 3vw, 2.5rem);
  border-bottom: 1px solid var(--line);
  background: rgba(8, 7, 13, .78);
  backdrop-filter: blur(12px);
}

.arcade-return {
  width: max-content;
  color: var(--paper-dim);
  text-decoration: none;
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: .72rem;
  border: 1px solid var(--line);
  padding: .55rem .75rem;
  border-radius: 3px;
  transition: color .18s ease, border-color .18s ease, background .18s ease;
}
.arcade-return:hover { color: var(--ink); border-color: var(--brass); background: var(--brass); }

.lab-mark { display: flex; gap: .65rem; align-items: center; letter-spacing: .09em; }
.lab-mark__seal {
  display: grid;
  place-items: center;
  width: 38px;
  aspect-ratio: 1;
  border: 1px solid var(--brass);
  color: var(--brass-light);
  font-size: .65rem;
  transform: rotate(-3deg);
  box-shadow: inset 0 0 0 3px var(--ink), inset 0 0 0 4px var(--line);
}
.lab-mark strong, .lab-mark small { display: block; }
.lab-mark strong { font-size: .75rem; }
.lab-mark small { margin-top: .15rem; color: var(--paper-dim); font-size: .56rem; }

.header-actions { justify-self: end; display: flex; gap: .45rem; }
.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255,255,255,.025);
  cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, color .16s ease;
}
.icon-button:hover { color: var(--brass-light); border-color: var(--brass); transform: translateY(-1px); }

.lab-shell { width: min(1240px, calc(100% - 2rem)); margin: 0 auto; padding: clamp(1.4rem, 4vw, 3.8rem) 0 3rem; }

.cabinet-heading { display: flex; justify-content: space-between; align-items: end; gap: 2rem; margin-bottom: 1.4rem; }
.eyebrow, .panel-label { margin: 0; color: var(--brass); text-transform: uppercase; letter-spacing: .16em; font-size: .66rem; }
.cabinet-heading h1 { margin: .45rem 0 0; font: 800 clamp(2.2rem, 6.5vw, 5.2rem)/.79 Georgia, serif; letter-spacing: -.055em; }
.cabinet-heading h1 span { color: transparent; -webkit-text-stroke: 1px var(--violet); text-shadow: 0 0 28px rgba(168,117,255,.22); }
.cabinet-heading__notice { margin: 0 0 .2rem; color: var(--orange); text-align: right; font-size: .72rem; letter-spacing: .1em; line-height: 1.55; }
.cabinet-heading__notice span { color: var(--paper-dim); font-size: .61rem; }

.control-deck {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  align-items: end;
  gap: 1.2rem;
  padding: .85rem 1rem;
  border: 1px solid var(--line);
  border-bottom: 0;
  background: rgba(25, 19, 35, .88);
  border-radius: var(--radius) var(--radius) 0 0;
}

.segment-group { min-width: 0; display: flex; padding: 0; margin: 0; border: 0; }
.segment-group legend { width: 100%; margin-bottom: .42rem; color: var(--paper-dim); font-size: .61rem; text-transform: uppercase; letter-spacing: .14em; }
.segment {
  flex: 1;
  min-height: 34px;
  padding: .45rem .7rem;
  border: 1px solid var(--line);
  border-right: 0;
  background: rgba(0,0,0,.16);
  color: var(--paper-dim);
  cursor: pointer;
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .045em;
}
.segment:first-of-type { border-radius: 3px 0 0 3px; }
.segment:last-of-type { border-right: 1px solid var(--line); border-radius: 0 3px 3px 0; }
.segment:hover { color: var(--paper); }
.segment.is-active { color: var(--ink); background: var(--brass-light); border-color: var(--brass-light); font-weight: 800; }

.round-id { padding: 0 .3rem .15rem; text-align: right; }
.round-id span { display: block; color: var(--paper-dim); font-size: .56rem; text-transform: uppercase; letter-spacing: .13em; }
.round-id strong { font-size: .72rem; color: var(--cyan); }

.briefing-panel {
  display: grid;
  grid-template-columns: 1fr minmax(210px, .72fr) 1fr;
  gap: 1px;
  padding: 1px;
  background: var(--line);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}
.swatch-card, .similarity-instrument { min-width: 0; background: rgba(16, 13, 25, .94); }
.swatch-card { display: flex; align-items: center; gap: .9rem; padding: 1rem; }
.swatch {
  position: relative;
  flex: 0 0 68px;
  height: 68px;
  border: 4px solid rgba(255,255,255,.08);
  border-radius: 50%;
  background: #393240;
  box-shadow: 0 0 0 1px var(--line), 0 8px 22px rgba(0,0,0,.34), inset 0 -12px 18px rgba(0,0,0,.25);
  transition: background-color .65s ease, box-shadow .65s ease;
}
.swatch::after { content: ""; position: absolute; inset: 9px; border-radius: 50%; border: 1px solid rgba(255,255,255,.13); }
.swatch-glint { position: absolute; width: 18px; height: 10px; border-radius: 50%; top: 11px; left: 13px; background: rgba(255,255,255,.38); transform: rotate(-24deg); filter: blur(.5px); }
.swatch.is-empty { background: repeating-linear-gradient(135deg, #2e2934 0 6px, #26212c 6px 12px) !important; }
.swatch-copy { min-width: 0; }
.swatch-copy h2 { margin: .28rem 0 .3rem; font: 700 clamp(.9rem, 1.8vw, 1.15rem)/1.1 Georgia, serif; display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.swatch-copy p { margin: 0; color: var(--paper-dim); font-size: .65rem; line-height: 1.4; }

.similarity-instrument { display: flex; align-items: center; justify-content: center; gap: .8rem; padding: .8rem; }
.grade-orb {
  flex: 0 0 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border: 1px solid var(--violet);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(168,117,255,.18), transparent 68%);
  box-shadow: 0 0 18px rgba(168,117,255,.12);
}
.grade-orb span { font: 800 1.3rem Georgia, serif; color: var(--violet); }
.similarity-copy { flex: 1; max-width: 180px; }
.similarity-copy strong { display: block; margin: .25rem 0 .45rem; font-size: .74rem; }
.similarity-copy small { display: block; margin-top: .38rem; color: var(--paper-dim); font-size: .58rem; }
.meter { position: relative; height: 7px; overflow: hidden; border: 1px solid var(--line); background: #09070d; }
.meter > span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--danger), var(--orange), var(--cyan)); transition: width .55s cubic-bezier(.2,.8,.2,1); }
.threshold-mark { position: absolute; top: -2px; bottom: -2px; left: 74%; width: 1px; background: white; box-shadow: 0 0 5px white; }

.game-layout { display: grid; grid-template-columns: minmax(0, 1.75fr) minmax(260px, .75fr); border: 1px solid var(--line); background: var(--panel); box-shadow: 0 26px 60px var(--shadow); }
.playfield-frame { position: relative; min-height: 520px; overflow: hidden; background: #090812; border-right: 1px solid var(--line); }
.playfield-status {
  position: absolute;
  z-index: 3;
  top: .75rem;
  left: .8rem;
  right: .8rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
  color: var(--paper-dim);
  font-size: .62rem;
  text-transform: uppercase;
  letter-spacing: .08em;
}
.status-lamp { width: 7px; height: 7px; border-radius: 50%; background: var(--cyan); box-shadow: 0 0 10px var(--cyan); }
#potion-canvas { display: block; width: 100%; height: 520px; touch-action: none; cursor: ew-resize; }
#potion-canvas.is-dropping { cursor: wait; }
.canvas-vignette { position: absolute; inset: 0; pointer-events: none; box-shadow: inset 0 0 80px rgba(0,0,0,.58); }
.keyboard-hint { position: absolute; bottom: .65rem; left: .8rem; z-index: 2; color: rgba(243,234,215,.38); font-size: .54rem; letter-spacing: .12em; pointer-events: none; }
.salamander-egg {
  --salamander-heat: 0;
  position: absolute;
  z-index: 4;
  width: clamp(104px, 15vw, 142px);
  opacity: 0;
  pointer-events: none;
  transform: translate(-44%, 24%) scale(.62) rotate(7deg);
  transform-origin: 30% 80%;
  transition: opacity .28s ease, transform .48s cubic-bezier(.18,.86,.2,1.28), filter .2s ease;
  filter: drop-shadow(0 0 calc(5px + var(--salamander-heat) * 11px) rgba(252,199,50,.72));
}
.salamander-egg img { display: block; width: 100%; height: auto; image-rendering: auto; }
.salamander-egg.is-awake {
  opacity: 1;
  transform: translate(-44%, 0) scale(1) rotate(-2deg);
}
.salamander-egg.is-awake img { animation: salamander-flicker .78s steps(2,end) infinite; }
.salamander-egg.is-helping { transform: translate(-50%, -8%) scale(1.06) rotate(-5deg); }
.salamander-plaque {
  width: max-content;
  max-width: 176px;
  margin: -.05rem 0 0 34%;
  padding: .32rem .42rem;
  border: 1px solid rgba(252,199,50,.7);
  background: rgba(18,16,18,.93);
  box-shadow: 3px 3px 0 rgba(0,0,0,.4);
  color: #fcc732;
  font: 700 .45rem/1.35 "Courier New", monospace;
  letter-spacing: .07em;
  text-transform: uppercase;
}
.salamander-plaque strong, .salamander-plaque span { display: block; }
.salamander-plaque span { margin-top: .16rem; color: rgba(252,199,50,.64); font-size: .38rem; white-space: nowrap; }

.lab-console { min-width: 0; display: flex; flex-direction: column; background: linear-gradient(160deg, rgba(34,25,46,.95), rgba(16,13,25,.98)); }
.console-section, .readout-grid, .console-actions, .field-log { padding: 1rem; border-bottom: 1px solid var(--line); }
.console-heading { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.console-heading > span:last-child { color: var(--paper-dim); font-size: .56rem; }
.ingredient-queue { display: grid; grid-template-columns: repeat(3, 1fr); gap: .55rem; margin-top: .85rem; min-height: 78px; }
.queue-item {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 78px;
  padding: .45rem .2rem .35rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
  color: inherit;
  background: rgba(0,0,0,.18);
  font: inherit;
  transition: border-color .14s ease, background-color .14s ease, transform .14s ease, box-shadow .14s ease;
}
.queue-item:not(:disabled):hover { transform: translateY(-2px); border-color: rgba(88,232,218,.62); background: rgba(88,232,218,.055); }
.queue-item:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
.queue-item:disabled { cursor: wait; opacity: .55; }
.queue-pebble { width: 38px; height: 38px; border-radius: 50%; border: 3px solid rgba(255,255,255,.12); box-shadow: inset -6px -8px 10px rgba(0,0,0,.26), 0 5px 12px rgba(0,0,0,.32); }
.queue-item small { width: 100%; margin-top: .32rem; overflow: hidden; color: var(--paper-dim); text-align: center; text-overflow: ellipsis; white-space: nowrap; font-size: .5rem; }
.queue-item.is-current { border-color: var(--brass-light); background: rgba(216,168,75,.1); box-shadow: inset 0 0 0 1px rgba(216,168,75,.18), 0 0 15px rgba(216,168,75,.08); }
.queue-item.is-current::before { content: "SELECTED"; position: absolute; top: 3px; left: 4px; color: var(--brass-light); font-size: .4rem; letter-spacing: .07em; }

.readout-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); padding: 1px; }
.readout { padding: .8rem; background: var(--ink); }
.readout span { display: block; color: var(--paper-dim); font-size: .54rem; text-transform: uppercase; letter-spacing: .1em; }
.readout strong { display: block; margin-top: .3rem; color: var(--cyan); font-size: 1rem; }

.console-actions { display: grid; gap: .6rem; }
.lab-button {
  min-height: 42px;
  padding: .65rem .8rem;
  border-radius: 3px;
  border: 1px solid var(--line);
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: .07em;
  font-size: .68rem;
  font-weight: 800;
  transition: transform .15s ease, filter .15s ease, opacity .15s ease;
}
.lab-button:not(:disabled):hover { transform: translateY(-1px); filter: brightness(1.1); }
.lab-button:disabled { cursor: not-allowed; opacity: .38; }
.lab-button--primary { color: #17101f; border-color: var(--brass-light); background: var(--brass-light); }
.lab-button--secondary { color: var(--paper); background: rgba(255,255,255,.035); }
.field-log { margin-top: auto; border-bottom: 0; min-height: 104px; }
.field-log p { margin: .55rem 0 0; color: var(--paper-dim); font: italic .72rem/1.5 Georgia, serif; }

.onboarding { display: flex; align-items: center; gap: .8rem; margin-top: 1rem; padding: .75rem 1rem; border: 1px dashed var(--violet); background: rgba(168,117,255,.08); }
.onboarding[hidden] { display: none; }
.onboarding > span { color: var(--violet); font-size: 1.3rem; }
.onboarding p { flex: 1; margin: 0; color: var(--paper-dim); font-size: .68rem; line-height: 1.5; }
.onboarding strong { color: var(--paper); }
.onboarding button { padding: .45rem .65rem; border: 1px solid var(--violet); background: transparent; cursor: pointer; color: var(--paper); font-size: .61rem; }

dialog { color: var(--paper); border: 1px solid var(--brass); background: #17111f; box-shadow: 0 28px 90px #000; }
dialog::backdrop { background: rgba(5,4,8,.78); backdrop-filter: blur(5px); }
.result-dialog { width: min(520px, calc(100% - 2rem)); padding: clamp(1.4rem, 4vw, 2.5rem); text-align: center; overflow: hidden; }
.result-grade { position: relative; z-index: 1; margin: .7rem auto .35rem; font: 900 clamp(4.5rem, 20vw, 7rem)/1 Georgia, serif; color: var(--brass-light); text-shadow: 4px 4px 0 var(--orange), 0 0 35px rgba(246,207,114,.35); }
.result-dialog h2 { position: relative; z-index: 1; margin: 0; font: 700 1.35rem/1.2 Georgia, serif; }
.result-dialog > p:not(.eyebrow) { color: var(--paper-dim); line-height: 1.55; font-size: .76rem; }
.result-metrics { display: grid; grid-template-columns: repeat(3,1fr); gap: 1px; margin: 1.2rem 0; background: var(--line); }
.result-metrics div { padding: .7rem; background: var(--ink); }
.result-metrics dt { color: var(--paper-dim); text-transform: uppercase; letter-spacing: .1em; font-size: .55rem; }
.result-metrics dd { margin: .25rem 0 0; color: var(--cyan); font-weight: 800; }
.result-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.result-burst { position: absolute; width: 260px; height: 260px; top: -90px; left: calc(50% - 130px); border-radius: 50%; background: repeating-conic-gradient(from 0deg, rgba(216,168,75,.16) 0 4deg, transparent 4deg 12deg); animation: result-spin 16s linear infinite; }

.records-dialog { width: min(660px, calc(100% - 2rem)); padding: 1.4rem; }
.records-head { display: flex; justify-content: space-between; align-items: center; }
.records-head h2 { margin: .3rem 0 0; font: 700 1.5rem Georgia, serif; }
.records-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 1px; margin-top: 1.2rem; background: var(--line); }
.record { min-height: 92px; padding: .8rem; background: var(--ink); }
.record span { color: var(--paper-dim); font-size: .55rem; text-transform: uppercase; letter-spacing: .09em; }
.record strong { display: block; margin-top: .45rem; color: var(--cyan); font-size: 1.1rem; }

.lab-footer { display: flex; justify-content: space-between; gap: 1rem; padding: 1.5rem clamp(1rem,3vw,2.5rem) 2.5rem; color: rgba(184,173,154,.55); font-size: .56rem; letter-spacing: .1em; text-transform: uppercase; }

@keyframes result-spin { to { transform: rotate(360deg); } }
@keyframes salamander-flicker {
  0%, 100% { opacity: .92; filter: brightness(.92); }
  28% { opacity: 1; filter: brightness(1.18); }
  54% { opacity: .84; filter: brightness(.82); }
  77% { opacity: .97; filter: brightness(1.08); }
}
@keyframes lamp-alert { 50% { opacity: .25; } }
.status-lamp.is-alert { background: var(--danger); box-shadow: 0 0 10px var(--danger); animation: lamp-alert .4s steps(2) 3; }
@keyframes potion-impact {
  0%, 100% { transform: translate(0); }
  30% { transform: translate(-2px, 1px); }
  65% { transform: translate(2px, -1px); }
}
.playfield-frame.is-perfect { animation: potion-impact .18s ease-out; }

@media (max-width: 900px) {
  .lab-mark { justify-self: center; }
  .briefing-panel { grid-template-columns: 1fr 1fr; }
  .similarity-instrument { grid-column: 1 / -1; grid-row: 2; }
  .game-layout { grid-template-columns: 1fr; }
  .playfield-frame { min-height: 460px; border-right: 0; border-bottom: 1px solid var(--line); }
  #potion-canvas { height: 460px; }
  .lab-console { display: grid; grid-template-columns: 1.2fr 1fr; }
  .console-section, .readout-grid, .console-actions, .field-log { margin: 0; border-right: 1px solid var(--line); }
  .field-log { border-bottom: 1px solid var(--line); }
}

@media (max-width: 640px) {
  .lab-header { min-height: 58px; grid-template-columns: 1fr auto; padding: .65rem .75rem; }
  .lab-mark { display: none; }
  .arcade-return { font-size: .62rem; }
  .icon-button { width: 40px; height: 40px; }
  .arcade-return { min-height: 40px; display: inline-flex; align-items: center; }
  .lab-shell { width: min(100% - 1rem, 1240px); padding-top: 1.6rem; }
  .cabinet-heading { align-items: start; }
  .cabinet-heading h1 { font-size: clamp(2.15rem, 15vw, 3.5rem); }
  .cabinet-heading__notice { display: none; }
  .control-deck { grid-template-columns: 1fr; gap: .8rem; padding: .75rem; }
  .round-id { display: none; }
  .segment { padding: .5rem .28rem; font-size: .6rem; }
  .briefing-panel { display: grid; grid-template-columns: 1fr 1fr; }
  .swatch-card { align-items: flex-start; padding: .72rem; gap: .55rem; }
  .swatch { flex-basis: 46px; height: 46px; border-width: 3px; }
  .swatch-glint { width: 12px; height: 7px; top: 7px; left: 8px; }
  .swatch-copy h2 { white-space: normal; font-size: .78rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  .swatch-copy p { font-size: .55rem; }
  .similarity-instrument { justify-content: stretch; padding: .7rem 1rem; }
  .similarity-copy { max-width: none; }
  .playfield-frame { min-height: 410px; }
  #potion-canvas { height: 410px; }
  .keyboard-hint { display: none; }
  .lab-console { display: flex; }
  .console-section, .readout-grid, .console-actions, .field-log { border-right: 0; }
  .console-actions { grid-template-columns: 1fr 1fr; }
  .onboarding { align-items: flex-start; flex-wrap: wrap; }
  .onboarding p { min-width: calc(100% - 3rem); }
  .onboarding button { margin-left: 2.1rem; }
  .records-grid { grid-template-columns: 1fr 1fr; }
  .lab-footer { flex-direction: column; }
}

@media (max-width: 390px) {
  .cabinet-heading h1 { font-size: 2.45rem; }
  .swatch-card { display: block; }
  .swatch { width: 46px; margin-bottom: .55rem; }
  .playfield-frame { min-height: 380px; }
  #potion-canvas { height: 380px; }
  .result-actions { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
  body::before { display: none; }
}
