:root {
  color-scheme: light;
  --paper: #f1efe8;
  --panel: #f8f6ef;
  --stage: #171b22;
  --stage-grid: rgba(255,255,255,.045);
  --ink: #17202b;
  --muted: #68717b;
  --quiet: #8c949d;
  --line: #d4d0c5;
  --control: rgba(250,248,241,.95);
  --control-hover: #e8e4d9;
  --accent: #bd6a38;
  --gold: #e9b44c;
  --cyan: #54b7ac;
  --red: #bf5060;
  --shadow: rgba(20,25,32,.18);
  --header: 66px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #111419;
  --panel: #171b22;
  --stage: #0d1015;
  --stage-grid: rgba(255,255,255,.05);
  --ink: #edf0ec;
  --muted: #a6adb4;
  --quiet: #7e8790;
  --line: #303741;
  --control: rgba(27,32,40,.94);
  --control-hover: #28303a;
  --accent: #e38a52;
  --gold: #f1bd55;
  --cyan: #63c8bd;
  --red: #e16d79;
  --shadow: rgba(0,0,0,.38);
}

* { box-sizing: border-box; }
html, body { width: 100%; min-width: 0; margin: 0; overflow: hidden; }
body { min-height: 100vh; min-height: 100svh; color: var(--ink); background: var(--paper); font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, select, input { color: inherit; font: inherit; }
button, select, input[type="number"] { border: 1px solid var(--line); border-radius: 7px; background: var(--panel); }
button { min-height: 32px; padding: 0 10px; cursor: pointer; }
button:hover, button:focus-visible, select:hover, select:focus-visible, input:focus-visible { border-color: var(--accent); outline: 0; }
button:disabled { opacity: .35; cursor: default; }
canvas { display: block; }

.topbar { height: var(--header); display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 0 18px; border-bottom: 1px solid var(--line); background: var(--panel); }
.back { justify-self: start; color: var(--muted); font-size: .74rem; text-decoration: none; }
.back:hover { color: var(--ink); text-decoration: underline; }
.title-block { text-align: center; }
.title-block h1 { margin: 0; font-family: Georgia, "Times New Roman", serif; font-size: 1.45rem; font-weight: 500; letter-spacing: -.02em; }
.title-block p { margin: 2px 0 0; color: var(--muted); font-family: Georgia, serif; font-size: .7rem; font-style: italic; }
.top-actions { justify-self: end; display: flex; gap: 7px; }
.icon-button { width: 36px; height: 36px; display: grid; place-items: center; padding: 0; background: transparent; }
.icon-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; }
.theme-toggle .moon, :root[data-theme="dark"] .theme-toggle .sun { display: none; }
:root[data-theme="dark"] .theme-toggle .moon { display: block; }

.workspace { height: calc(100vh - var(--header)); height: calc(100svh - var(--header)); display: grid; grid-template-columns: 286px minmax(380px, 1fr) 248px; min-width: 0; min-height: 0; }
.panel { min-width: 0; overflow-y: auto; background: var(--panel); scrollbar-width: thin; }
.editor-panel { border-right: 1px solid var(--line); }
.readout-panel { border-left: 1px solid var(--line); }
.panel-head { min-height: 67px; display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 11px 13px; border-bottom: 1px solid var(--line); }
.panel-head div { display: grid; gap: 4px; min-width: 0; }
.panel-head strong { overflow: hidden; font-family: Georgia, serif; font-size: .94rem; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.panel-head select { width: 104px; min-height: 34px; padding: 0 5px; font-size: .72rem; }
.eyebrow { color: var(--accent); font-size: .58rem; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }

.letter-row { min-height: 46px; display: flex; align-items: center; justify-content: space-between; gap: 7px; padding: 7px 10px; border-bottom: 1px solid var(--line); }
.letter-tabs { display: flex; min-width: 0; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.letter-tabs::-webkit-scrollbar { display: none; }
.letter-tab { min-width: 34px; min-height: 30px; padding: 0 8px; border: 0; background: transparent; font-family: Georgia, serif; }
.letter-tab.active { color: #111419; background: var(--gold); }
.small-button { flex: none; min-height: 30px; padding: 0 7px; color: var(--accent); font-size: .68rem; }

.editor-wrap { position: relative; height: 276px; padding: 14px; background: var(--paper); }
#rule-editor { width: 100%; height: 100%; touch-action: none; }
#rule-editor:focus-visible, #limit:focus-visible { box-shadow: inset 0 0 0 2px var(--cyan); outline: 0; }
.canvas-hint { position: absolute; right: 20px; bottom: 14px; left: 20px; margin: 0; padding: 5px 8px; border-radius: 6px; color: var(--muted); background: color-mix(in srgb, var(--panel) 88%, transparent); font-size: .62rem; text-align: center; pointer-events: none; }
.selection, .numbers { padding: 13px; border-top: 1px solid var(--line); }
.section-heading { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 9px; }
.section-heading h2 { margin: 0; font-size: .7rem; font-weight: 750; letter-spacing: .04em; text-transform: uppercase; }
.section-heading output, .section-heading span { color: var(--muted); font-size: .64rem; }
.button-row { display: grid; grid-template-columns: 1fr 1fr 1.2fr; gap: 6px; }
.button-row button { font-size: .7rem; }
.button-row .danger { color: var(--red); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 9px; }
.field-grid label { display: grid; gap: 4px; color: var(--muted); font-size: .62rem; }
.field-grid select, .field-grid input { width: 100%; min-height: 31px; padding: 0 7px; }
.table-scroll { max-width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .62rem; }
th { padding: 4px; color: var(--muted); font-weight: 600; text-align: left; }
td { padding: 3px; border-top: 1px solid var(--line); }
tr.selected { background: color-mix(in srgb, var(--gold) 18%, transparent); }
td:first-child { cursor: pointer; font-family: Georgia, serif; font-size: .72rem; }
td input { width: 54px; min-height: 27px; padding: 0 4px; border-radius: 4px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .6rem; }
.table-note { margin: 8px 0 0; color: var(--quiet); font-size: .58rem; line-height: 1.4; }

.visuals { min-width: 0; min-height: 0; display: grid; grid-template-rows: 142px minmax(0, 1fr); background: var(--stage); }
.iteration-panel { min-width: 0; display: grid; grid-template-columns: 73px minmax(0, 1fr); border-bottom: 1px solid rgba(255,255,255,.12); color: #edf0ec; background: #171b22; }
.iteration-title { display: flex; flex-direction: column; justify-content: space-between; padding: 12px 8px; border-right: 1px solid rgba(255,255,255,.1); color: #9da6af; font-size: .57rem; writing-mode: vertical-rl; transform: rotate(180deg); }
.iteration-strip { min-width: 0; display: grid; grid-template-columns: repeat(5, minmax(70px, 1fr)); gap: 1px; overflow-x: auto; background: rgba(255,255,255,.08); }
.iteration-strip figure { position: relative; min-width: 70px; margin: 0; background: #11151b; }
.iteration-strip canvas { width: 100%; height: 100%; }
.iteration-strip figcaption { position: absolute; right: 7px; bottom: 5px; color: #89939d; font: .62rem ui-monospace, monospace; }

.main-stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: var(--stage); container-type: size; }
.main-stage::before { content: ""; position: absolute; inset: 0; opacity: .8; background-image: linear-gradient(var(--stage-grid) 1px, transparent 1px), linear-gradient(90deg, var(--stage-grid) 1px, transparent 1px); background-size: 38px 38px; mask-image: radial-gradient(circle, black, transparent 76%); }
#limit { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#limit.dragging { cursor: grabbing; }
.corner { position: absolute; z-index: 2; display: flex; gap: 5px; padding: 5px; border: 1px solid rgba(255,255,255,.15); border-radius: 9px; color: #dfe5e3; background: rgba(20,24,30,.86); box-shadow: 0 4px 20px rgba(0,0,0,.22); backdrop-filter: blur(8px); }
.corner label { display: grid; gap: 3px; color: #9da6af; font-size: .52rem; text-transform: uppercase; }
.corner select, .corner input[type="number"], .corner button { min-height: 29px; border-color: rgba(255,255,255,.15); color: #edf0ec; background: rgba(255,255,255,.08); font-size: .64rem; }
.corner-tl { top: 12px; left: 12px; }
.corner-tl select { width: 88px; }
.corner-tl label:last-child select { width: 53px; }
.corner-tr { top: 12px; right: 12px; align-items: end; }
.grid-fields { display: flex; gap: 4px; }
.grid-fields label { position: relative; }
.grid-fields input[type="number"] { width: 45px; padding: 0 5px; }
.grid-fields input[type="range"] { width: 70px; margin: 1px 0; accent-color: var(--gold); }
.grid-fields output { position: absolute; right: 0; top: 0; color: #dfe5e3; font-size: .52rem; }
.menu-button { width: 78px; }
.toggle-popover { position: absolute; top: calc(100% + 6px); right: 0; width: min(310px, calc(100vw - 24px)); max-height: min(560px, calc(100cqh - 74px)); overflow-y: auto; display: grid; gap: 11px; padding: 11px; border: 1px solid rgba(255,255,255,.15); border-radius: 8px; background: rgba(20,24,30,.97); }
.toggle-popover[hidden] { display: none; }
.toggle-popover label { color: #dfe5e3; font-size: .65rem; text-transform: none; }
.toggle-popover input { accent-color: var(--gold); }
.rule-toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.rule-toggles label { display: flex; align-items: center; gap: 7px; }
.limits { padding-top: 10px; border-top: 1px solid rgba(255,255,255,.12); }
.popover-heading { display: block; margin-bottom: 8px; color: #9da6af; font-size: .57rem; font-weight: 750; letter-spacing: .1em; text-transform: uppercase; }
.preset-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; padding: 3px; border-radius: 8px; background: rgba(255,255,255,.06); }
.preset-row button { min-width: 0; min-height: 29px; padding: 0 4px; border: 0; font-size: .57rem; }
.preset-row button.active { color: #111419; background: var(--gold); }
.preset-row button:disabled { opacity: 1; }
.limits details { margin-top: 8px; }
.limits summary { color: #c6cdd1; cursor: pointer; font-size: .63rem; }
.limit-row { display: grid; gap: 3px; margin-top: 9px; }
.limit-row span { display: flex; justify-content: space-between; gap: 10px; color: #cdd3d5; }
.limit-row output { color: var(--gold); font: .58rem ui-monospace, SFMono-Regular, Menlo, monospace; }
.limit-row input[type="range"] { width: 100%; margin: 0; }
.limit-select { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 10px; }
.limit-select select { width: 105px; min-height: 29px; padding: 0 5px; }
.corner-bl { bottom: 31px; left: 12px; align-items: center; color: #9da6af; font-size: .57rem; }
.palette-key { display: flex; gap: 3px; }
.palette-key i { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(255,255,255,.25); }
.corner-br { right: 12px; bottom: 31px; }
.corner-br button { padding: 0 8px; }
.corner-br button[aria-pressed="true"] { color: #111419; background: var(--gold); }
.stage-caption { position: absolute; right: 12px; bottom: 8px; left: 12px; display: flex; justify-content: space-between; color: #7f8993; font-size: .55rem; pointer-events: none; }
.notice { position: absolute; z-index: 5; top: 50%; left: 50%; max-width: min(380px, 80%); transform: translate(-50%,-50%); padding: 9px 13px; border: 1px solid var(--gold); border-radius: 8px; color: #f7f0dc; background: rgba(20,24,30,.94); font-size: .72rem; text-align: center; }

.readout-panel section { padding: 18px 16px; border-bottom: 1px solid var(--line); }
.status-card strong { display: block; margin: 7px 0 5px; font-family: Georgia, serif; font-size: 2rem; font-weight: 400; line-height: 1; }
.status-card strong[data-status="converges"] { color: var(--cyan); }
.status-card strong[data-status="cycles"], .status-card strong[data-status="unclear"] { color: var(--gold); }
.status-card strong[data-status="grows"] { color: var(--red); }
.status-card p { margin: 0; color: var(--muted); font-family: Georgia, serif; font-size: .75rem; line-height: 1.45; }
.readouts { margin: 0; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.readouts div { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; }
.readouts dt { color: var(--muted); font-size: .68rem; }
.readouts dd { margin: 0; font: .68rem ui-monospace, SFMono-Regular, Menlo, monospace; text-align: right; }
.explanation h2 { margin: 0 0 8px; font-family: Georgia, serif; font-size: 1rem; font-weight: 500; }
.explanation p, .help p { margin: 0; color: var(--muted); font-family: Georgia, serif; font-size: .76rem; line-height: 1.52; }
.share button { width: 100%; color: var(--accent); }
.share p { margin: 6px 0 0; color: var(--quiet); font-size: .58rem; text-align: center; }
.help { margin: 14px 16px 24px; color: var(--muted); font-size: .7rem; }
.help summary { cursor: pointer; }
.help p { margin-top: 9px; }

@media (max-width: 1120px) {
  .workspace { grid-template-columns: 250px minmax(350px, 1fr) 210px; }
  .editor-wrap { height: 240px; }
  .visuals { grid-template-rows: 124px minmax(0,1fr); }
}

@media (max-width: 850px) {
  :root { --header: 56px; }
  .topbar { padding: 0 10px; }
  .back { width: 36px; overflow: hidden; font-size: 0; }
  .back::before { content: "←"; font-size: 1.15rem; }
  .title-block h1 { font-size: 1.15rem; }
  .title-block p { display: none; }
  .workspace { position: relative; display: block; }
  .visuals { position: absolute; inset: 0 0 222px; display: grid; grid-template-rows: 104px minmax(0,1fr); }
  .panel { position: absolute; z-index: 8; bottom: 0; height: 222px; display: flex; overflow-x: auto; overflow-y: hidden; border: 0; border-top: 1px solid var(--line); scroll-snap-type: x proximity; }
  .editor-panel { left: 0; width: 66%; }
  .readout-panel { right: 0; width: 34%; background: var(--panel); }
  .editor-panel > *, .readout-panel > * { flex: 0 0 235px; height: 100%; overflow-y: auto; border-right: 1px solid var(--line); border-bottom: 0; scroll-snap-align: start; }
  .panel-head { display: grid; align-content: center; }
  .letter-row { display: grid; align-content: center; }
  .editor-wrap { flex-basis: 250px; height: 100%; padding: 10px; }
  .selection, .numbers { flex-basis: 260px; }
  .readout-panel .status-card { flex-basis: 190px; }
  .readouts { flex-basis: 205px; padding-top: 11px; }
  .readout-panel section { padding: 13px; }
  .readout-panel .explanation, .readout-panel .share { flex-basis: 210px; }
  .help { flex: 0 0 210px; height: 100%; margin: 0; padding: 15px; overflow-y: auto; }
  .status-card strong { font-size: 1.55rem; }
  .iteration-panel { grid-template-columns: 48px minmax(0,1fr); }
  .corner { transform: scale(.9); }
  .corner-tl { transform-origin: top left; }
  .corner-tr { transform-origin: top right; }
  .corner-bl { transform-origin: bottom left; }
  .corner-br { transform-origin: bottom right; }
}

@media (max-width: 520px) {
  .visuals { bottom: 235px; }
  .panel { height: 235px; }
  .editor-panel { width: 100%; }
  .readout-panel { display: none; }
  .iteration-panel { grid-template-columns: 34px minmax(0,1fr); }
  .iteration-title { padding: 7px 5px; }
  .corner-tr .grid-fields label:not(:last-child) { display: none; }
  .corner-bl { display: none; }
  .corner-br { bottom: 25px; }
  .corner-tl select { width: 78px; }
  .stage-caption span:last-child { display: none; }
}

@media (max-height: 430px) and (pointer: coarse) {
  :root { --header: 42px; }
  .topbar { padding: 0 7px; }
  .title-block h1 { font-size: 1rem; }
  .top-actions { gap: 4px; }
  .icon-button { width: 32px; height: 32px; }
  .workspace { position: relative; display: block; }
  .visuals { position: absolute; inset: 0 190px 0 0; grid-template-rows: 78px minmax(0,1fr); }
  .iteration-panel { grid-template-columns: 29px minmax(0,1fr); }
  .iteration-title { padding: 5px 3px; }
  .editor-panel { position: absolute; z-index: 8; top: 0; right: 0; bottom: 0; left: auto; width: 190px; height: 100%; display: block; overflow-x: hidden; overflow-y: auto; border-top: 0; border-left: 1px solid var(--line); }
  .readout-panel { display: none; }
  .editor-panel > * { width: 100%; height: auto; min-height: 0; overflow: visible; border-right: 0; border-bottom: 1px solid var(--line); }
  .editor-panel .panel-head { min-height: 48px; display: flex; padding: 6px; }
  .panel-head div, .panel-head strong { display: none; }
  .panel-head select { width: 100%; }
  .letter-row { min-height: 38px; display: flex; padding: 4px; }
  .editor-wrap { height: 170px; padding: 6px; }
  .selection, .numbers { display: none; }
  .canvas-hint { display: none; }
  .corner { padding: 3px; transform: scale(.82); }
  .corner-tl, .corner-tr { top: 5px; }
  .corner-tl { left: 5px; }
  .corner-tr { right: 5px; }
  .corner-bl { display: none; }
  .corner-br { right: 5px; bottom: 20px; }
  .stage-caption { right: 5px; bottom: 4px; left: 5px; }
}

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }
