:root {
  --void: #1c1914;
  --floor: #2a261f;
  --panel: #312c24;
  --raise: #3c362c;
  --line: #5a5348;
  --ink: #e6dfd2;
  --mute: #9a9184;
  --hazard: #c4a035;
  --go: #6a9a4a;
  --stop: #b43b32;
  --sans: "Segoe UI", system-ui, sans-serif;
  --mono: "Cascadia Mono", "Consolas", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--void); color: var(--ink); font-family: var(--sans); }
html, body { overflow-x: clip; }
::selection { background: var(--hazard); color: #141210; }
textarea { caret-color: var(--hazard); }

.plant {
  height: 100%;
  display: grid;
  grid-template-rows: 48px minmax(0, 1fr);
}

.mast {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 0.9rem;
  background: var(--panel);
  border-bottom: 3px solid var(--hazard);
}
.mast .mark {
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 0.92rem;
}
.mast .mark small { display: block; font-size: 0.62rem; font-weight: 600; color: var(--mute); letter-spacing: 0.12em; }
.agent-banner {
  margin-left: 1rem;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--hazard);
  color: var(--hazard);
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.agent-banner:not([hidden]) { display: inline-block; }
body.agent-play .shifts,
body.agent-play .board button,
body.agent-play .board textarea { pointer-events: none; }
.mast nav { margin-left: auto; display: flex; gap: 0.85rem; }
.mast a { color: var(--ink); text-decoration: none; font-size: 0.88rem; }
.mast a:hover { text-decoration: underline; }

.work {
  display: grid;
  grid-template-columns: minmax(240px, 28fr) minmax(0, 72fr);
  min-height: 0;
}
.board { order: 0; }
.stage-col { order: 1; }

.stage-col {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

#factory-stage {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  width: 100%;
  height: auto;
  background: var(--floor);
}
#factory-stage canvas { display: block; width: 100%; height: 100%; }

.foreman-talk {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  column-gap: 0.7rem;
  row-gap: 0.25rem;
  width: auto;
  margin: 0;
  padding: 0.4rem 0.85rem 0.5rem 1rem;
  background: #161410;
  border: 0;
  border-bottom: 2px solid var(--hazard);
  color: var(--ink);
  cursor: default;
}
.foreman-talk::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--hazard);
}
.foreman-talk::after {
  content: "";
  position: absolute;
  left: 1.15rem;
  bottom: -9px;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 9px solid var(--hazard);
  pointer-events: none;
}
.foreman-talk[hidden] { display: none; }
.foreman-who {
  flex: 1 0 100%;
  margin: 0;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--hazard);
  font-weight: 700;
}
#foreman-line {
  flex: 1 0 100%;
  min-width: 0;
  min-height: 0;
  margin: 0;
  font-size: 0.88rem;
  line-height: 1.3;
  white-space: nowrap;
}
.foreman-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.2rem;
  flex: 0 0 auto;
}
.foreman-replies[hidden] { display: none; }
.foreman-replies button {
  min-height: 32px;
  padding: 0.25rem 0.7rem;
  border: 1px solid var(--line);
  background: var(--raise);
  color: var(--ink);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.foreman-replies button:hover { border-color: var(--hazard); }
.foreman-replies button.primary {
  background: var(--go);
  border-color: #4d7a32;
  color: #111;
  font-weight: 700;
}

@media (max-width: 900px) {
  .foreman-talk { width: 100%; }
}

.board {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.shifts {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.55rem 0.75rem 0.45rem;
  background: var(--void);
}
.phase-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin: 0 0 0.35rem;
}
.phase-nav button {
  flex: 1 1 auto;
  min-height: 28px;
  padding: 0.2rem 0.35rem;
  border: 1px solid var(--line);
  background: var(--raise);
  color: var(--mute);
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
}
.phase-nav button[aria-current="true"] {
  border-color: var(--hazard);
  color: var(--hazard);
}
.phase-nav button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.shift-buttons button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.shifts .bay {
  margin: 0;
  padding: 0;
  color: var(--mute);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.shifts .night-grow {
  margin: 0.2rem 0 0;
  color: var(--hazard);
  font-size: 0.68rem;
  letter-spacing: 0.04em;
}
.shifts .night-count {
  margin: 0.15rem 0 0;
  color: var(--hazard);
  font-family: var(--mono);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
}
.shifts .night-now {
  margin: 0.2rem 0 0;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 700;
}
.shifts .night-next {
  margin: 0.15rem 0 0;
  color: var(--mute);
  font-size: 0.8rem;
}
.shifts .shift-buttons {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}
.shifts button {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  border: 1px solid var(--line);
  background: var(--raise);
  color: var(--ink);
  padding: 0.35rem 0.3rem;
  cursor: pointer;
  font: inherit;
  font-size: 0.68rem;
}
.shifts .t {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.shifts button[aria-current="true"] {
  border-color: var(--hazard);
  background: #4a4028;
}
.shifts button.covered {
  border-color: var(--ok, #2ecc71);
}
.shifts .mark {
  margin-left: auto;
  color: var(--hazard);
  font-size: 0.72rem;
}

.goal {
  padding: 0.55rem 0.9rem 0.45rem;
  border-bottom: 1px solid var(--line);
}
.goal .chapter {
  margin: 0 0 0.2rem;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 600;
}
.goal .concept {
  margin: 0 0 0.35rem;
  color: var(--hazard);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.goal p { margin: 0; color: var(--mute); font-size: 0.88rem; line-height: 1.4; }
.goal p[hidden], .goal .concept[hidden], .goal .do-this[hidden] { display: none; }
.hands-help {
  margin: 0 0.9rem 0.5rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line);
  background: var(--raise);
  color: var(--ink);
  font-size: 0.88rem;
  line-height: 1.4;
}
.board.is-hands textarea { display: none; }
.board.is-hands #run-btn { display: none; }
.board.is-hands .do-this,
.board.is-hands .concept { display: none; }
.goal .do-this {
  margin-top: 0.55rem;
  padding: 0.45rem 0.55rem;
  color: var(--ink);
  background: var(--raise);
  border-left: 3px solid var(--hazard);
  font-size: 0.9rem;
  line-height: 1.35;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.9rem;
  padding: 0.4rem 0.9rem;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--mute);
  font-variant-numeric: tabular-nums;
}
.stats > span { white-space: nowrap; }
.stats b { color: var(--ink); font-weight: 600; }
.stats .clock b { color: var(--hazard); }
#line-progress:empty { display: none; }
#factory-console[hidden] { display: none; }

.editor {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0.5rem 0.7rem;
}
.editor textarea {
  flex: 1;
  min-height: 6rem;
  width: 100%;
  resize: none;
  background: #1a1814;
  color: var(--ink);
  border: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.45;
  padding: 0.6rem;
}
.editor.is-callout textarea {
  border-color: var(--hazard);
  outline: 2px solid var(--hazard);
  outline-offset: 2px;
  animation: factory-callout 1.7s ease-in-out infinite;
}
@keyframes factory-callout {
  0%, 100% { box-shadow: 0 0 4px 2px rgba(196, 160, 53, 0.2); }
  50% { box-shadow: 0 0 16px 6px rgba(232, 196, 90, 0.55); }
}
@media (prefers-reduced-motion: reduce) {
  .editor.is-callout textarea { animation: none; box-shadow: 0 0 10px 4px rgba(196, 160, 53, 0.4); }
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.45rem 0 0.3rem;
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}
.actions button {
  min-height: 36px;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--line);
  background: var(--raise);
  color: var(--ink);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
}
.actions button.primary { background: var(--go); border-color: #4d7a32; color: #111; }
.actions button:focus-visible { outline: 2px solid var(--hazard); outline-offset: 2px; }

#factory-status {
  min-height: 1.3rem;
  font-size: 0.85rem;
  color: var(--mute);
  padding: 0.15rem 0.25rem;
}
#factory-status[data-kind="good"] { color: var(--go); }
#factory-status[data-kind="bad"] {
  color: #e07068;
  animation: status-hit 0.5s ease-out;
}
@keyframes status-hit {
  0% { color: #ffe8e0; background: rgba(180, 60, 50, 0.45); }
  100% { color: #e07068; background: transparent; }
}
@media (prefers-reduced-motion: reduce) {
  #factory-status[data-kind="bad"] { animation: none; background: rgba(180, 60, 50, 0.2); }
}

#factory-console {
  flex: 0 0 auto;
  height: 5.5rem;
  overflow: auto;
  margin: 0 0.7rem 0.7rem;
  padding: 0.45rem 0.55rem;
  background: #141210;
  border: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 0.75rem;
  white-space: pre-wrap;
  color: #c8c0b2;
}

.board.is-hands .editor { flex: 0 0 auto; }
.board.is-hands #factory-console,
.board.is-hands .hands-help { display: none; }

@media (max-width: 768px) {
  .work { grid-template-columns: 1fr; grid-template-rows: minmax(220px, 50vh) minmax(0, 1fr); }
  .stage-col { order: 0; }
  .board { order: 1; border-right: 0; border-top: 1px solid var(--line); }
  .mast nav .hide-sm { display: none; }
  .editor textarea { min-height: 4.5rem; }
  #factory-console { height: 4rem; }
  .foreman-talk { padding: 0.35rem 0.7rem 0.45rem 0.85rem; }
  #foreman-line { white-space: normal; }
}

.briefing {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: rgba(14, 12, 10, 0.72);
}
.briefing[hidden] { display: none; }
.briefing-card {
  max-width: 32rem;
  padding: 1.35rem 1.4rem 1.2rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 4px solid var(--hazard);
  color: var(--ink);
}
.briefing-kicker {
  margin: 0 0 0.35rem;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--hazard);
  font-weight: 700;
}
.briefing-step {
  margin: 0 0 0.45rem;
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  color: var(--mute);
}
.tut-keys {
  position: absolute;
  left: 0.7rem;
  bottom: 0.7rem;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  max-width: min(36rem, 92%);
  pointer-events: none;
}
.tut-keys[hidden] { display: none; }
.tut-coach {
  flex: 1 1 100%;
  margin: 0 0 0.15rem;
  padding: 0.45rem 0.55rem;
  background: rgba(22, 20, 16, 0.92);
  border: 1px solid var(--hazard);
  color: var(--ink);
  font-size: 0.88rem;
  line-height: 1.35;
  font-weight: 600;
}
.tut-keys span {
  padding: 0.28rem 0.5rem;
  background: rgba(22, 20, 16, 0.88);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
}
.tut-keys kbd {
  font-family: var(--mono);
  color: var(--hazard);
}
.briefing-card h2 {
  margin: 0 0 0.75rem;
  font-size: 1.35rem;
  font-weight: 750;
}
.briefing-card p {
  margin: 0 0 0.7rem;
  color: var(--mute);
  line-height: 1.45;
  font-size: 0.95rem;
}
.briefing-card p:last-of-type { color: var(--ink); }
.briefing-card .primary {
  margin-top: 0.4rem;
  min-height: 40px;
  padding: 0.45rem 1rem;
  border: 1px solid #4d7a32;
  background: var(--go);
  color: #111;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
