:root {
  --bg: #f7f3ea;
  --ink: #111827;
  --muted: #667085;
  --line: rgba(17, 24, 39, .14);
  --card: #fffaf2;
  --accent: #2563eb;
  --danger: #b42318;
  --success: #067647;
  --shadow: 0 24px 70px rgba(17, 24, 39, .16);
  --page-x: max(14px, env(safe-area-inset-left));
  --page-r: max(14px, env(safe-area-inset-right));
  --page-top: max(14px, env(safe-area-inset-top));
  --page-bottom: max(14px, env(safe-area-inset-bottom));
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html {
  width: 100%;
  height: 100%;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
  width: 100%;
  min-width: 320px;
  height: 100%;
  min-height: 100dvh;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 8%, rgba(37, 99, 235, .18), transparent 32rem),
    radial-gradient(circle at 84% 12%, rgba(239, 68, 68, .12), transparent 28rem),
    var(--bg);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}
button, input { font: inherit; }
button {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button:disabled { cursor: default; }

.app-shell {
  width: 100%;
  height: 100dvh;
  min-height: 0;
  padding: var(--page-top) var(--page-r) var(--page-bottom) var(--page-x);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: clamp(12px, 2.2dvh, 24px);
  overflow: hidden;
}

.app-heading {
  width: min(1240px, 100%);
  margin: 0 auto;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(10px, 2.5vw, 22px);
}
.heading-copy { min-width: 0; flex: 1 1 auto; }
.kicker {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: clamp(.62rem, 1.2vw, .76rem);
  font-weight: 850;
  letter-spacing: .12em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
h1 {
  margin: 0;
  font-size: clamp(1.18rem, 3.1vw, 2.5rem);
  line-height: 1.02;
  letter-spacing: -.045em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.72);
  box-shadow: 0 12px 34px rgba(17,24,39,.11);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}
.back-header-button,
.restart-header-button,
.edit-header-button,
.edit-close-button,
.fixture-button {
  min-height: 38px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: #111827;
  color: white;
  font-weight: 850;
  cursor: pointer;
}
.back-header-button { background: rgba(17,24,39,.08); color: var(--ink); }
.back-header-button:disabled,
.back-header-button[aria-disabled="true"] { opacity: .42; }
.restart-header-button { min-width: 70px; }

.stage {
  width: min(1240px, 100%);
  min-height: 0;
  margin: 0 auto;
  display: grid;
  place-items: center;
  padding-block: clamp(8px, 2dvh, 24px);
  overflow: hidden;
}
.stage-frame,
.track,
.slide {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  place-items: center;
}
.card {
  width: min(820px, 100%);
  height: min(720px, 100%);
  min-height: 0;
  background: color-mix(in srgb, var(--card) 92%, white);
  border: 1px solid var(--line);
  border-radius: clamp(24px, 4.8vw, 38px);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  overflow: hidden;
  contain: layout paint;
}
.card-toolbar {
  min-height: 62px;
  display: grid;
  grid-template-columns: minmax(64px, auto) minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.42);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.position {
  font-size: .8rem;
  font-weight: 850;
  color: var(--muted);
  letter-spacing: .04em;
  white-space: nowrap;
}
.current-card-id {
  min-width: 0;
  justify-self: center;
  max-width: 100%;
  padding: 7px 10px;
  border-radius: 999px;
  background: rgba(17,24,39,.06);
  color: var(--ink);
  font-size: .72rem;
  font-weight: 950;
  letter-spacing: .04em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.view-toggle {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.65);
}
.view-button {
  width: 42px;
  height: 34px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.view-button svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.view-button.active { background: var(--ink); color: white; }

.card-heading {
  padding: clamp(16px, 2.8vw, 30px) clamp(22px, 4.2vw, 52px) clamp(8px, 1.2vw, 16px);
  display: grid;
  gap: 8px;
  text-align: center;
}
.eyebrow {
  min-height: 1.05rem;
  margin: 0;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: .12em;
  font-size: clamp(.68rem, 1.3vw, .8rem);
  font-weight: 900;
}
.card-heading h2 {
  margin: 0;
  font-size: clamp(1.8rem, 4.5vw, 3.35rem);
  line-height: 1.02;
  letter-spacing: -.055em;
}
.face {
  min-height: 0;
  height: 100%;
  display: none;
  overflow: hidden;
  padding: clamp(12px, 2.4vw, 28px) clamp(22px, 4.2vw, 52px);
}
.face.active { display: grid; }
.graphic-face { place-items: center; }
.visual-card {
  width: min(100%, 520px);
  min-height: 0;
  display: grid;
  place-items: center;
  align-content: center;
  gap: clamp(12px, 2dvh, 22px);
}
.visual-mark {
  width: min(42dvh, 42vw, 260px);
  aspect-ratio: 1;
  border-radius: clamp(28px, 5vw, 52px);
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 35% 20%, rgba(37,99,235,.16), transparent 42%),
    rgba(17,24,39,.045);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.72), 0 18px 44px rgba(17,24,39,.10);
}
.visual-symbol {
  width: 68%;
  height: 68%;
  stroke: var(--ink);
  fill: none;
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.visual-symbol path,
.visual-symbol circle,
.visual-symbol ellipse { vector-effect: non-scaling-stroke; }
.visual-label {
  margin: 0;
  max-width: 28rem;
  color: #374151;
  font-size: clamp(1.05rem, 2.2vw, 1.45rem);
  font-weight: 850;
  line-height: 1.2;
  text-align: center;
  overflow-wrap: anywhere;
}
.text-face { place-items: stretch; }
.text-copy {
  min-height: 0;
  display: grid;
  align-items: center;
  justify-items: center;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.body {
  margin: 0;
  color: #374151;
  font-size: clamp(1.05rem, 1.9vw, 1.32rem);
  line-height: 1.48;
  max-width: 48rem;
  text-align: left;
}
.branch-footer {
  min-height: 78px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
  padding: 10px 14px 12px;
  border-top: 1px solid var(--line);
  background: rgba(255,255,255,.48);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.branch-exit {
  min-width: 0;
  min-height: 52px;
  border-radius: 18px;
  padding: 8px 10px;
  background: rgba(17,24,39,.05);
  display: grid;
  align-content: center;
  gap: 2px;
}
.branch-exit:last-child { text-align: right; }
.branch-meta {
  color: var(--muted);
  font-size: .64rem;
  font-weight: 950;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.branch-target,
.branch-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.branch-target {
  color: var(--ink);
  font-size: .92rem;
  font-weight: 950;
}
.branch-label {
  color: var(--muted);
  font-size: .76rem;
  font-weight: 750;
}
.branch-exit.disabled { opacity: .45; }

.edit-screen[hidden] { display: none !important; }
.app-shell.editing { grid-template-rows: minmax(0, 1fr); }
.app-shell.editing .app-heading,
.app-shell.editing .stage { display: none; }
.edit-screen {
  width: min(920px, 100%);
  min-height: 0;
  margin: 0 auto;
  display: grid;
  place-items: center;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.edit-panel {
  width: min(100%, 760px);
  max-height: 100%;
  overflow: auto;
  padding: clamp(18px, 4vw, 34px);
  border: 1px solid var(--line);
  border-radius: clamp(24px, 5vw, 38px);
  background: rgba(255, 250, 242, .94);
  box-shadow: var(--shadow);
}
.edit-panel-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 14px;
}
.edit-panel h2 {
  margin: 0;
  font-size: clamp(1.7rem, 5vw, 3rem);
  line-height: 1.02;
  letter-spacing: -.045em;
}
.edit-close-button { background: rgba(17,24,39,.08); color: var(--ink); }
.edit-intro {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.45;
  font-weight: 650;
}
.edit-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.file-loader {
  min-height: 104px;
  border: 1px dashed rgba(17,24,39,.28);
  border-radius: 22px;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--ink);
  background: rgba(255,255,255,.48);
  font-weight: 850;
  cursor: pointer;
}
.file-loader input {
  width: 1px;
  height: 1px;
  opacity: 0;
  position: absolute;
  pointer-events: none;
}
.fixture-buttons { display: grid; gap: 10px; }
.fixture-button {
  width: 100%;
  background: rgba(37, 99, 235, .1);
  color: var(--ink);
  border: 1px solid var(--line);
}
.schema-preview {
  margin: 0;
  max-height: 260px;
  overflow: auto;
  padding: 14px;
  border-radius: 18px;
  background: rgba(17,24,39,.92);
  color: #f8fafc;
  font-size: .78rem;
  line-height: 1.45;
  white-space: pre-wrap;
  user-select: text;
}
.load-status {
  min-height: 1.3em;
  margin: 14px 0 0;
  font-weight: 800;
  color: var(--muted);
}
.load-status.error { color: var(--danger); }
.load-status.success { color: var(--success); }

@media (orientation: landscape) and (min-width: 820px) {
  .app-shell { gap: clamp(10px, 1.8dvh, 20px); }
  .stage { padding-block: clamp(6px, 1.2dvh, 18px); }
  .card {
    width: min(1120px, 100%);
    height: min(590px, 100%);
  }
  .card-heading { padding-block: 18px 8px; }
  .card-heading h2 { font-size: clamp(2rem, 4vw, 3.25rem); }
  .visual-card { width: min(100%, 660px); }
  .visual-mark { width: min(30dvh, 22vw, 210px); }
  .body { max-width: 58rem; }
}

@media (max-width: 680px) {
  .app-shell { gap: 10px; padding-inline: 10px; }
  .app-heading { align-items: flex-start; gap: 8px; }
  .header-controls { gap: 5px; padding: 5px; }
  .back-header-button,
  .restart-header-button,
  .edit-header-button { min-height: 36px; padding-inline: 10px; font-size: .88rem; }
  .stage { padding-block: 8px; }
  .card-toolbar { min-height: 56px; grid-template-columns: auto minmax(0, 1fr) auto; padding: 8px 10px; }
  .view-button { width: 38px; height: 32px; }
  .card-heading { padding: 16px 18px 6px; }
  .card-heading h2 { font-size: clamp(1.55rem, 7.2vw, 2.35rem); }
  .face { padding: 12px 18px; }
  .visual-mark { width: min(38dvh, 58vw, 230px); }
  .visual-label { font-size: 1rem; }
  .body { font-size: .98rem; line-height: 1.42; }
  .branch-footer { min-height: 70px; gap: 6px; padding: 8px 10px 10px; }
  .branch-exit { min-height: 46px; padding: 7px 8px; border-radius: 15px; }
  .branch-target { font-size: .82rem; }
  .branch-label { font-size: .7rem; }
  .edit-grid { grid-template-columns: 1fr; }
  .edit-panel-header { align-items: center; }
}

@media (max-height: 560px) and (orientation: landscape) {
  .app-shell { gap: 8px; }
  .app-heading { min-height: 44px; }
  .kicker { display: none; }
  h1 { font-size: clamp(1rem, 2.4vw, 1.55rem); }
  .stage { padding-block: 4px; }
  .card { height: 100%; }
  .card-toolbar { min-height: 48px; padding-block: 6px; }
  .card-heading { padding: 10px 22px 4px; gap: 4px; }
  .eyebrow { font-size: .62rem; }
  .card-heading h2 { font-size: clamp(1.28rem, 3.5vw, 2.1rem); }
  .face { padding: 8px 22px; }
  .visual-card { gap: 8px; }
  .visual-mark { width: min(27dvh, 18vw, 150px); }
  .visual-label { font-size: .95rem; }
  .body { font-size: .95rem; line-height: 1.36; }
  .branch-footer { min-height: 64px; padding-block: 7px; }
}
