:root {
  color-scheme: dark;
  --bg: #080c11;
  --panel: #0d131a;
  --panel-raised: #121a23;
  --line: rgba(202, 220, 234, 0.13);
  --line-strong: rgba(202, 220, 234, 0.24);
  --text: #edf5f7;
  --soft: #a7b5bd;
  --dim: #71818b;
  --mint: #63e6be;
  --mint-soft: rgba(99, 230, 190, 0.12);
  --orange: #ff8a4c;
  --orange-soft: rgba(255, 138, 76, 0.12);
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.36);
  --topbar: 66px;
  --rail: 268px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }

body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% -12%, rgba(99, 230, 190, 0.09), transparent 31rem),
    radial-gradient(circle at 84% 6%, rgba(255, 138, 76, 0.07), transparent 28rem),
    var(--bg);
}

button, select { font: inherit; }

button, a { -webkit-tap-highlight-color: transparent; }

a { color: inherit; }

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

.portal {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  grid-template-rows: var(--topbar) minmax(0, 1fr);
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
}

.topbar {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr) auto;
  align-items: center;
  min-width: 0;
  border-bottom: 1px solid var(--line);
  background: rgba(8, 12, 17, 0.86);
  backdrop-filter: blur(18px);
  position: relative;
  z-index: 10;
}

.brand {
  height: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 22px;
  border-right: 1px solid var(--line);
  text-decoration: none;
}

.brand-mark {
  width: 30px;
  height: 30px;
  display: grid;
  align-content: center;
  gap: 4px;
  padding: 6px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: linear-gradient(145deg, rgba(255,255,255,0.07), rgba(255,255,255,0.01));
}

.brand-mark i { display: block; height: 2px; border-radius: 2px; background: var(--mint); }
.brand-mark i:nth-child(2) { width: 72%; background: var(--orange); }
.brand-mark i:nth-child(3) { width: 88%; }
.brand strong { display: block; font-size: 14px; letter-spacing: 0.03em; }
.brand small { display: block; margin-top: 1px; color: var(--dim); font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.14em; text-transform: uppercase; }

.topbar-status {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 22px;
  color: var(--soft);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.live-dot {
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--mint);
  box-shadow: 0 0 0 4px var(--mint-soft), 0 0 18px rgba(99, 230, 190, 0.54);
}

[data-loading="true"] .live-dot { animation: breathe 1.2s ease-in-out infinite; }

@keyframes breathe { 50% { opacity: .35; transform: scale(.82); } }

.topbar-actions { display: flex; align-items: center; gap: 7px; padding-right: 14px; }

.control {
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--soft);
  background: rgba(255,255,255,0.025);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .18s ease, color .18s ease, background .18s ease, transform .18s ease;
}

.control:hover { color: var(--text); border-color: var(--line-strong); background: rgba(255,255,255,0.055); }
.control:active { transform: translateY(1px); }
.control.auto[aria-pressed="true"] { color: var(--mint); border-color: rgba(99, 230, 190, 0.32); background: var(--mint-soft); }
.control.open { color: #09120f; background: var(--mint); border-color: var(--mint); }
.control.open:hover { color: #09120f; background: #7aefca; }
.control:disabled { opacity: .35; cursor: not-allowed; }
.auto-icon { font-size: 9px; }

.rail {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border-right: 1px solid var(--line);
  background: rgba(10, 15, 21, 0.7);
}

.rail-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 14px;
  color: var(--dim);
  font: 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.rail-heading strong {
  min-width: 24px;
  padding: 4px 6px;
  border-radius: 999px;
  color: var(--mint);
  background: var(--mint-soft);
  text-align: center;
}

.diagram-list { overflow: auto; padding: 2px 10px 16px; scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }

.diagram-item {
  width: 100%;
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  padding: 12px 10px;
  border: 1px solid transparent;
  border-radius: 9px;
  color: var(--soft);
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background .18s ease, border-color .18s ease, color .18s ease;
}

.diagram-item + .diagram-item { margin-top: 3px; }
.diagram-item:hover { background: rgba(255,255,255,.035); color: var(--text); }
.diagram-item[aria-current="true"] { color: var(--text); border-color: rgba(99, 230, 190, .2); background: linear-gradient(100deg, var(--mint-soft), rgba(99,230,190,.025)); }
.diagram-item-index { color: var(--dim); font: 10px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
.diagram-item[aria-current="true"] .diagram-item-index { color: var(--mint); }
.diagram-item strong { display: block; overflow: hidden; text-overflow: ellipsis; font-size: 12px; font-weight: 600; white-space: nowrap; }
.diagram-item small { display: block; margin-top: 5px; color: var(--dim); font: 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .08em; text-transform: uppercase; }

.rail-footer {
  padding: 15px 20px 18px;
  border-top: 1px solid var(--line);
  color: var(--dim);
  font-size: 10px;
}

.rail-footer span { display: block; margin-bottom: 6px; text-transform: uppercase; letter-spacing: .1em; }
.rail-footer code { color: var(--soft); font-size: 10px; }

.stage {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 20px 22px 13px;
}

.stage-head {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "copy receipt";
  gap: 22px;
  align-items: end;
}

.stage-copy { grid-area: copy; min-width: 0; }
.eyebrow { display: flex; align-items: center; gap: 8px; color: var(--mint); font: 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .13em; }
.eyebrow span:last-child { color: var(--dim); }
.stage h1 { margin: 5px 0 2px; overflow: hidden; text-overflow: ellipsis; color: var(--text); font-size: clamp(21px, 2.2vw, 31px); line-height: 1.15; letter-spacing: -.035em; white-space: nowrap; }
.stage-copy p { margin: 0; overflow: hidden; text-overflow: ellipsis; color: var(--soft); font-size: 12px; white-space: nowrap; }

.receipt { grid-area: receipt; display: flex; align-items: stretch; gap: 1px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--line); }
.receipt span { min-width: 94px; display: grid; gap: 4px; padding: 9px 11px; background: var(--panel); }
.receipt small { color: var(--dim); font: 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .1em; text-transform: uppercase; }
.receipt strong { max-width: 108px; overflow: hidden; text-overflow: ellipsis; color: var(--soft); font: 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: nowrap; }
#frame-status[data-state="ready"] { color: var(--mint); }
#frame-status[data-state="failed"] { color: var(--orange); }

.mobile-switcher { display: none; }

.viewer-shell {
  min-width: 0;
  min-height: 340px;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background:
    linear-gradient(rgba(255,255,255,.022) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.022) 1px, transparent 1px),
    var(--panel);
  background-size: 28px 28px;
  box-shadow: var(--shadow);
}

.viewer-shell::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.025), inset 0 24px 48px rgba(0,0,0,.12);
}

#diagram-frame { width: 100%; height: 100%; min-height: 340px; display: block; border: 0; background: transparent; opacity: 0; transition: opacity .24s ease; }
.viewer-shell[data-ready="true"] #diagram-frame { opacity: 1; }

.viewer-loading {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 7px;
  background: rgba(13, 19, 26, .86);
  transition: opacity .2s ease, visibility .2s ease;
}

.viewer-shell[data-ready="true"] .viewer-loading { opacity: 0; visibility: hidden; }
.viewer-loading strong { margin-top: 8px; font-size: 12px; letter-spacing: .02em; }
.viewer-loading small { color: var(--dim); font-size: 10px; }
.loader-mark { width: 40px; height: 40px; border: 1px solid var(--line-strong); border-radius: 10px; position: relative; }
.loader-mark::before, .loader-mark::after { content: ""; position: absolute; left: 9px; right: 9px; height: 2px; border-radius: 2px; background: var(--mint); animation: scan 1.1s ease-in-out infinite alternate; }
.loader-mark::before { top: 13px; }
.loader-mark::after { top: 24px; width: 13px; background: var(--orange); animation-delay: -.45s; }
@keyframes scan { from { transform: scaleX(.4); transform-origin: left; } to { transform: scaleX(1); transform-origin: left; } }

.rotation-meter { position: absolute; z-index: 4; left: 0; right: 0; bottom: 0; height: 2px; transform: scaleX(0); transform-origin: left; background: linear-gradient(90deg, var(--mint), var(--orange)); }
.rotation-meter.is-running { animation: rotate-meter var(--rotation-duration, 18s) linear forwards; }
@keyframes rotate-meter { to { transform: scaleX(1); } }

.stage-foot { display: flex; align-items: center; gap: 18px; min-height: 18px; color: var(--dim); font-size: 9px; letter-spacing: .04em; }
.stage-foot a { margin-left: auto; color: var(--soft); text-decoration: none; }
.stage-foot a:hover { color: var(--mint); }

.fatal { grid-column: 1 / -1; grid-row: 2; place-self: center; max-width: 520px; padding: 28px; border: 1px solid rgba(255,138,76,.32); border-radius: 12px; background: var(--orange-soft); text-align: center; }
.fatal strong { color: var(--orange); }
.fatal p { margin: 8px 0 0; color: var(--soft); font-size: 12px; }

@media (max-width: 980px) {
  :root { --rail: 220px; }
  .topbar { grid-template-columns: var(--rail) minmax(0, 1fr) auto; }
  .control.open { display: none; }
  .receipt span:nth-child(2) { display: none; }
  .stage { padding-inline: 16px; }
}

@media (max-width: 760px) {
  body { overflow: auto; }
  .portal { display: block; min-height: 100dvh; padding-top: 58px; }
  .topbar { position: fixed; inset: 0 0 auto; height: 58px; display: flex; justify-content: space-between; }
  .brand { padding: 0 14px; border-right: 0; }
  .brand-mark { width: 28px; height: 28px; }
  .topbar-status, .topbar-actions .ghost { display: none; }
  .topbar-actions { margin-left: auto; padding-right: 10px; }
  .control { height: 34px; }
  .rail { display: none; }
  .stage { min-height: calc(100dvh - 58px); grid-template-rows: auto minmax(430px, 1fr) auto; padding: 14px 10px 12px; }
  .stage-head { grid-template-columns: minmax(0, 1fr); grid-template-areas: "copy" "switcher"; gap: 10px; }
  .stage h1 { font-size: 22px; }
  .stage-copy p { white-space: normal; line-height: 1.45; }
  .mobile-switcher { grid-area: switcher; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 10px; align-items: center; }
  .mobile-switcher label { color: var(--dim); font-size: 10px; }
  .mobile-switcher select { width: 100%; height: 36px; padding: 0 34px 0 11px; border: 1px solid var(--line-strong); border-radius: 7px; color: var(--text); background: var(--panel-raised); }
  .receipt { display: none; }
  .viewer-shell, #diagram-frame { min-height: 430px; }
  .stage-foot span { display: none; }
  .stage-foot a { margin-left: 0; }
}

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