/* Contract v20: factory page; only the final entry/warp rules affect the lab. */
body.factory {
  --fc-line: var(--line, rgba(137, 217, 239, .18));
  --fc-muted: var(--muted, #80959e);
  display: grid;
  grid-template-columns: minmax(0, 280px) minmax(0, 1fr) minmax(0, 300px);
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 18px;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 0 34px max(18px, env(safe-area-inset-bottom));
  background: #050c12;
  color: #d6e3e9;
  font-family: Inter, 'Helvetica Neue', 'Noto Sans JP', Arial, sans-serif;
  font-size: 12px;
  line-height: 1.7;
  letter-spacing: .04em;
  color-scheme: dark;
  overflow: hidden;
  overflow-wrap: anywhere;
  isolation: isolate;
}
body.factory, body.factory * { box-sizing: border-box; min-width: 0; }
body.factory [hidden] { display: none !important; }
body.factory #factory-stage { position: fixed; inset: 0; z-index: -2; background: #050c12; overflow: hidden; }
body.factory #factory-stage canvas { display: block; width: 100%; height: 100%; }
body.factory > .vignette { z-index: -1; pointer-events: none; }

body.factory .factory-top {
  position: relative;
  inset: auto;
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
  gap: 4px 20px;
  height: auto;
  min-height: 88px;
  padding: 12px 0;
  border-bottom: 1px solid var(--fc-line);
  background: #09101755;
}
body.factory .factory-top .brand {
  color: #edfaff;
  font-size: 23px;
  font-weight: 500;
  letter-spacing: 4px;
  text-decoration: none;
}
body.factory #fc-plant { font-size: 14px; font-weight: 500; }
body.factory #fc-shift { color: #7cbfd4; font-size: 11px; }
body.factory #fc-status { margin-left: auto; color: #9ddded; font-variant-numeric: tabular-nums; }
body.factory #fc-note { flex-basis: 100%; color: #e5aa63; font-size: 11px; margin: 0; }

body.factory .panel {
  position: relative;
  inset: auto;
  align-self: start;
  min-height: 0;
  width: 100%;
  max-height: 100%;
  margin: 0;
  padding: 16px;
  background: linear-gradient(125deg, #0d1c26c9, #0b141dcc);
  border: 1px solid var(--fc-line);
  border-radius: 3px;
  backdrop-filter: blur(14px);
  overflow: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: #365361 #091925;
  overscroll-behavior: contain;
}
body.factory #factory-processes { grid-column: 1; grid-row: 2; }
body.factory #factory-detail { grid-column: 3; grid-row: 2; }
body.factory h2, body.factory #fc-detail-title { margin: 0 0 12px; font-size: 14px; font-weight: 500; }
body.factory h3 { margin: 18px 0 8px; color: #9ddded; font-size: 12px; font-weight: 500; }
body.factory #fc-processes, body.factory #fc-equipment, body.factory #fc-detail-stats {
  list-style: none;
  margin: 0;
  padding: 0;
}
body.factory .fc-process, body.factory .fc-equip {
  --fc-state: #658795;
  position: relative;
  display: grid;
  gap: 3px 8px;
  margin: 0 0 8px;
  padding: 10px 10px 13px 14px;
  border: 1px solid var(--fc-line);
  background: #0f1f2bb3;
  cursor: pointer;
}
body.factory .fc-process { grid-template-columns: 24px minmax(0, 1fr); }
body.factory .fc-equip { grid-template-columns: minmax(0, 1fr) minmax(0, auto); }
body.factory :is(.fc-process, .fc-equip)[data-state="running"] { --fc-state: #69d9ff; }
body.factory :is(.fc-process, .fc-equip)[data-state="setup"] { --fc-state: #e5aa63; }
body.factory :is(.fc-process, .fc-equip)[data-state="idle"] { --fc-state: #658795; }
body.factory :is(.fc-process, .fc-equip)[data-state="down"] { --fc-state: #ff6b6b; }
body.factory :is(.fc-process, .fc-equip)::before {
  content: '';
  position: absolute;
  top: 9px;
  bottom: 9px;
  left: 0;
  width: 2px;
  background: var(--fc-state);
}
/* --util is supplied on each row as a unitless value in [0, 1]. */
body.factory :is(.fc-process, .fc-equip)::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: calc(clamp(0, var(--util, 0), 1) * 100%);
  height: 2px;
  background: var(--fc-state);
  opacity: .65;
  pointer-events: none;
}
body.factory :is(.fc-process, .fc-equip)[data-state="running"]::before { animation: fc-running 2s ease-in-out infinite; }
body.factory :is(.fc-process, .fc-equip):hover { background: #193342; }
body.factory :is(.fc-process, .fc-equip)[aria-selected="true"] {
  border-color: #69d9ff;
  background: linear-gradient(90deg, #69d9ff20, #0f1f2bcc);
}
body.factory .fc-step { grid-column: 1; grid-row: 1; color: var(--fc-state); font-family: ui-monospace, monospace; }
body.factory .fc-name { grid-column: 2; font-size: 13px; font-weight: 500; }
body.factory :is(.fc-summary, .fc-metric, .fc-demo) { grid-column: 2; }
body.factory .fc-summary { color: var(--fc-muted); font-size: 11px; }
body.factory .fc-metric { color: #9ddded; font-size: 11px; font-variant-numeric: tabular-nums; }
body.factory .fc-demo { justify-self: start; margin-top: 5px; }
body.factory #fc-detail-meta { color: var(--fc-muted); margin: 0 0 12px; font-size: 11px; }
body.factory #fc-detail-stats > li {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 12px;
  padding: 7px 0;
  border-bottom: 1px solid var(--fc-line);
  font-variant-numeric: tabular-nums;
}
body.factory #fc-detail-stats > li > :first-child { color: var(--fc-muted); }
body.factory #fc-detail-stats > li > :last-child { color: #d6e3e9; }
body.factory .fc-detail-alert { margin: 12px 0; padding: 8px 10px; border-left: 2px solid #ff6b6b; color: #ffb0b0; background: #ff6b6b0d; }
body.factory .fc-detail-alert:empty { display: none; }
body.factory :is(#fc-detail-focus, #fc-detail-demo) { margin: 10px 4px 0 0; }
body.factory .fc-equip-name { font-weight: 500; }
body.factory .fc-equip-state { color: var(--fc-state); font-size: 11px; }
body.factory .fc-equip-util { grid-column: 1 / -1; color: #9ddded; font-size: 11px; font-variant-numeric: tabular-nums; }

body.factory button, body.factory #fc-portal-newtab {
  max-width: 100%;
  padding: 6px 9px;
  border: 1px solid var(--fc-line);
  border-radius: 0;
  box-shadow: none;
  background: #0f1f2b;
  color: #9ddded;
  font: inherit;
  letter-spacing: .04em;
  white-space: normal;
  overflow-wrap: anywhere;
  text-decoration: none;
  cursor: pointer;
}
body.factory button:hover:not(:disabled), body.factory #fc-portal-newtab:hover { background: #193342; border-color: #8ce4fa; }
body.factory button[aria-pressed="true"] { color: #69d9ff; border-color: #69d9ff; background: #16313e; }
body.factory button:disabled { opacity: .45; cursor: not-allowed; }
body.factory :focus-visible { outline: 2px solid #8ce4fa; outline-offset: 2px; }
body.factory #factory-views {
  position: relative;
  grid-column: 1 / -1;
  grid-row: 3;
  justify-self: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 100%;
  padding: 8px;
  border: 1px solid var(--fc-line);
  background: #091925ed;
  z-index: 2;
}
body.factory #toast { z-index: 5; bottom: 110px; max-width: calc(100% - 28px); overflow-wrap: anywhere; }

body.factory dialog#factory-portal {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(1280px, 96vw);
  max-width: 96vw;
  height: min(820px, calc(100vh - 150px));
  height: min(820px, calc(100dvh - 150px));
  max-height: 96dvh;
  padding: 0;
  border: 1px solid var(--fc-line);
  border-radius: 3px;
  background: #091925;
  color: #d6e3e9;
  overflow: hidden;
  z-index: 30;
}
body.factory #factory-portal[open] { display: flex; flex-direction: column; }
body.factory #factory-portal::backdrop { background: #010a13b3; }
body.factory #factory-portal .picker-top {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  max-height: 45%;
  padding: 6px 10px;
  border-bottom: 1px solid var(--fc-line);
  color: var(--fc-muted);
  font: 11px/1.7 ui-monospace, monospace;
  letter-spacing: .04em;
  overflow: auto;
  overflow-x: hidden;
}
body.factory #fc-portal-title { flex: 1 1 160px; }
body.factory #factory-portal .picker-top button { width: auto; height: auto; min-height: 28px; font-size: 12px; }
body.factory #factory-portal #fc-portal-close { font-size: 20px; line-height: 1; min-width: 32px; }
body.factory #fc-portal-frame { display: block; flex: 1; min-height: 0; width: 100%; border: 0; background: #000; }

@media (max-width: 1100px) {
  body.factory { grid-template-columns: minmax(0, 245px) minmax(0, 1fr) minmax(0, 260px); gap: 12px; padding-inline: 20px; }
  body.factory .panel { padding: 12px; }
}
@media (max-width: 900px) {
  body.factory {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) auto;
    gap: 8px;
    padding-inline: 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
  body.factory .factory-top { min-height: 68px; padding: 8px 0; gap: 2px 12px; }
  body.factory .factory-top .brand { font-size: 20px; }
  body.factory #fc-plant { font-size: 12px; }
  body.factory #fc-status { margin-left: 0; font-size: 11px; }
  body.factory #factory-processes { grid-column: 1; grid-row: 3; }
  body.factory #factory-detail { grid-column: 1; grid-row: 4; }
  body.factory .panel { height: 100%; padding: 10px 12px; background: linear-gradient(125deg, #0d1c26ed, #0b141dee); }
  body.factory #factory-views { grid-row: 5; width: 100%; gap: 4px; padding: 6px; }
  body.factory #factory-views button { flex: 1 1 72px; padding: 6px; font-size: 11px; }
}
/* Without WebGL, let the document scroll as a centered reading surface. */
body.factory.no-3d {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: auto;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-y: auto;
  overflow-x: hidden;
}
body.factory.no-3d > .vignette { display: none; }
body.factory.no-3d .factory-top { align-self: stretch; }
body.factory.no-3d #factory-stage {
  position: relative;
  inset: auto;
  order: 1;
  z-index: auto;
  width: min(880px, 100%);
  padding: 18px;
  border: 1px solid var(--fc-line);
  color: var(--fc-muted);
}
body.factory.no-3d .panel { order: 2; align-self: center; width: min(880px, 100%); height: auto; max-height: 70vh; max-height: 70dvh; flex: none; }
body.factory.no-3d #factory-views { order: 3; position: sticky; bottom: 0; flex: none; width: min(880px, 100%); }

/* The lab's shared header styling supplies the button's remaining appearance. */
header #factory-open { min-width: 0; max-width: 100%; overflow-wrap: anywhere; }
#factory-warp {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  place-items: center;
  overflow: hidden;
  isolation: isolate;
  background: radial-gradient(ellipse at center, #14394ae6, #050c12fa 72%);
  color: #bcefff;
  pointer-events: auto;
}
body.warping #factory-warp { display: grid; animation: fc-warp-fade .6s ease-out both; }
#factory-warp::before, #factory-warp::after { content: ''; position: absolute; inset: -20%; pointer-events: none; z-index: -1; }
#factory-warp::before {
  background-image: linear-gradient(#69d9ff30 1px, transparent 1px), linear-gradient(90deg, #69d9ff30 1px, transparent 1px);
  background-size: 64px 64px;
  transform-origin: 50% 50%;
}
#factory-warp::after { background: repeating-linear-gradient(0deg, transparent 0 7px, #69d9ff12 7px 8px); }
body.warping #factory-warp::before { animation: fc-warp-grid .6s cubic-bezier(.2, .6, .3, 1) both; }
body.warping #factory-warp::after { animation: fc-warp-scan .6s ease-out both; }
#factory-warp .fw-title {
  max-width: 90%;
  padding: 16px;
  text-align: center;
  font: 500 clamp(14px, 2.4vw, 28px)/1.8 Inter, 'Helvetica Neue', sans-serif;
  letter-spacing: .2em;
  overflow-wrap: anywhere;
}
body.warping #factory-warp .fw-title { animation: fc-warp-title .6s ease-out both; }
@keyframes fc-running { 0%, 100% { opacity: .45; } 50% { opacity: 1; } }
@keyframes fc-warp-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes fc-warp-grid {
  from { transform: perspective(500px) rotateX(58deg) scale(.65); opacity: .15; }
  to { transform: perspective(500px) rotateX(58deg) scale(1.65); opacity: .7; }
}
@keyframes fc-warp-scan { from { transform: translateY(-24px); opacity: 0; } to { transform: translateY(24px); opacity: .7; } }
@keyframes fc-warp-title { from { transform: scale(.94); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  body.factory :is(.fc-process, .fc-equip)[data-state="running"]::before { animation: none; }
  body.warping #factory-warp { animation: fc-warp-fade .2s linear both; }
  body.warping #factory-warp::before, body.warping #factory-warp::after,
  body.warping #factory-warp .fw-title { animation: none; transform: none; }
  #factory-warp::before, #factory-warp::after { opacity: .2; }
  body.factory #toast { transition: none; }
}
