:root {
  color-scheme: dark;
  --bg: #0c1220; --panel: #121b2d; --surface: #182439; --border: #293750;
  --text: #eef3ff; --muted: #a4b1ca; --accent: #adc4ff; --blue: #9db7ff; --green: #79d8af;
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
button, input, textarea { font: inherit; }
button { cursor: pointer; border: 1px solid transparent; color: inherit; border-radius: 8px; padding: 8px 12px; background: transparent; white-space: nowrap; }
button:disabled { cursor: not-allowed; opacity: .5; }
button:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible, .sheet-grid:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 3px;
}
input, textarea { color: var(--text); background: #101827; border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
h1, h2, p { margin: 0; }
h1 { font-size: clamp(23px, 3vw, 35px); letter-spacing: -.035em; }
h2 { font-size: 17px; letter-spacing: -.02em; }
.kicker { color: var(--accent); text-transform: uppercase; letter-spacing: .13em; font-size: 10px; font-weight: 700; margin-bottom: 6px; }
.subtle { color: var(--muted); line-height: 1.55; }
.small { font-size: 12px; }
.primary { background: var(--blue); color: #111e39; font-weight: 700; }
.primary:hover:not(:disabled) { background: #bfd0ff; }
.secondary { background: var(--surface); border-color: var(--border); }
.secondary:hover:not(:disabled), .quiet:hover:not(:disabled) { background: #273753; }
.quiet { color: var(--muted); }
.boot { padding: 24px; color: var(--muted); }
.login-shell { min-height: 100svh; display: grid; place-items: center; padding: 24px; background: radial-gradient(ellipse at 60% 0%, #253557, transparent 70%), var(--bg); }
.login-card { width: min(100%, 440px); padding: clamp(24px, 6vw, 40px); border: 1px solid var(--border); background: var(--panel); border-radius: 20px; box-shadow: 0 22px 80px #0004; }
.login-card .brand-mark { margin-bottom: 25px; }
.login-card .subtle { margin: 10px 0 26px; }
.login-card form { display: grid; gap: 15px; }
.login-card label { display: grid; gap: 7px; font-size: 14px; }
.login-card .primary { display: flex; justify-content: space-between; padding: 12px 16px; }
.form-error { color: #ffadb6; font-size: 13px; }
.form-error:empty { display: none; }
.brand-mark { width: 41px; height: 41px; display: grid; place-items: center; border-radius: 11px; color: #112141; background: var(--blue); font-size: 24px; font-weight: 700; flex: none; }
.topbar { min-height: 66px; display: flex; justify-content: space-between; align-items: center; gap: 12px; border-bottom: 1px solid var(--border); padding: 10px clamp(16px,4vw,48px); background: #111a2a; }
.brand, .top-actions, .peers, .row-actions { display: flex; align-items: center; gap: 10px; }
.brand strong { display: block; font-size: 16px; }
.brand div > span { display: block; color: var(--muted); font-size: 11px; margin-top: 1px; white-space: nowrap; }
.peer { padding: 4px 8px; border-radius: 99px; background: #24354b; font-size: 11px; white-space: nowrap; }
.peer::before, .sync::before { content: ''; display: inline-block; width: 6px; height: 6px; background: var(--green); border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.sync { font-size: 12px; color: var(--muted); white-space: nowrap; }
.sync[data-state="offline"]::before { background: #ffb17f; }
.workspace { max-width: 1580px; margin: 0 auto; padding: 25px clamp(16px,4vw,48px) 38px; }
.workspace-heading { display: flex; justify-content: space-between; align-items: end; gap: 16px; margin-bottom: 20px; }
.tabs { display: flex; background: #101a2b; border: 1px solid var(--border); padding: 4px; border-radius: 11px; gap: 4px; }
.tabs button { color: var(--muted); font-weight: 650; }
.tabs button[aria-selected="true"] { background: #263953; color: var(--text); }
.tabs span { margin-left: 4px; }
.workspace-grid { display: grid; grid-template-columns: minmax(280px, 36%) minmax(0,1fr); gap: 16px; align-items: start; }
#room[data-mode="sheet"] .workspace-grid { grid-template-columns: minmax(220px, 24%) minmax(0,1fr); }
#room[data-has-image="true"] .workspace-grid { grid-template-columns: minmax(300px, 42%) minmax(0,1fr); }
#room[data-mode="sheet"][data-has-image="true"] .workspace-grid { grid-template-columns: minmax(250px, 32%) minmax(0,1fr); }
#room[data-mode="sheet"][data-condition-collapsed="true"] .workspace-grid { grid-template-columns: minmax(0,1fr); }
.card { min-width: 0; border: 1px solid var(--border); background: var(--panel); border-radius: 13px; overflow: hidden; }
.card-head, .console-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 16px; }
.card-head { border-bottom: 1px solid var(--border); min-height: 56px; }
.task-body { padding: 14px; }
.statement-header, .statement-tools { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; }
.statement-header { margin-bottom: 7px; }
.statement-label { color: var(--muted); font-size: 13px; }
#statement-open, #statement-edit { padding: 4px 6px; color: var(--accent); font-size: 13px; }
#statement { display: block; width: 100%; min-height: 78px; max-height: min(60vh, 550px); margin: 0 0 12px; resize: vertical; font-size: 16px; line-height: 1.55; }
@supports (field-sizing: content) { #statement { field-sizing: content; } }
.statement-help { margin: 0 0 12px; color: var(--muted); font-size: 12px; line-height: 1.5; }
.condition-text { padding: 13px; overflow: auto; overflow-wrap: anywhere; border: 1px solid var(--border); border-radius: 8px; background: #101827; font-size: 16px; line-height: 1.6; }
.condition-text > :first-child { margin-top: 0; }
.condition-text > :last-child { margin-bottom: 0; }
.condition-text p + *, .condition-text pre, .condition-text ul, .condition-text ol { margin-block: 0.65em; }
.condition-text ul, .condition-text ol { padding-left: 1.5em; }
.condition-text pre { overflow: auto; }
.condition-text code { font-family: Consolas, monospace; }
.condition-text .katex-display { overflow: auto hidden; padding: 2px 0; }
#statement-preview { max-height: min(60vh, 550px); margin-bottom: 12px; }
.dropzone { display: grid; justify-items: center; gap: 8px; text-align: center; padding: 24px 12px; border: 1px dashed #435579; background: #142136; border-radius: 9px; }
.dropzone[data-drag="true"] { background: #243a5c; border-color: var(--accent); }
.dropzone small { color: var(--muted); line-height: 1.45; }
.task-icon { font-size: 28px; color: var(--accent); }
.task-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 8px; }
.image-open { padding: 0; display: block; width: 100%; border-radius: 8px; overflow: hidden; }
.image-open img { display: block; width: 100%; max-height: min(52vh, 560px); object-fit: contain; background: #fff; }
.image-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 13px; }
.image-actions button { font-size: 13px; }
.file-label { display: flex; align-items: center; gap: 9px; font-family: Consolas, monospace; font-size: 14px; }
.file-dot { width: 9px; height: 9px; background: #ffd580; border-radius: 50%; }
.sheet-dot { background: var(--green); }
kbd { display: inline-block; color: #253862; background: #ffffff5e; padding: 2px 5px; border-radius: 4px; font-size: 10px; margin-left: 6px; }
.code-editor .cm-editor { min-height: 315px; height: min(48vh, 535px); font-size: 15px; background: #101827; }
.code-editor .cm-scroller { overflow: auto; font-family: Consolas, ui-monospace, monospace; }
.code-editor .cm-gutters { background: #101827; }
.cursor-roster { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; min-height: 39px; padding: 6px 14px; border-bottom: 1px solid var(--border); background: #101827; color: var(--muted); font-size: 12px; }
.cursor-chip { padding: 3px 8px; border-color: var(--border); background: #182439; font-size: 12px; white-space: normal; text-align: left; }
.cursor-chip::before { display: inline-block; width: 8px; height: 8px; margin-right: 7px; border-radius: 50%; background: var(--peer); content: ''; }
.cursor-chip:hover { border-color: var(--peer); }
.remote-caret { position: relative; display: inline-block; width: 3px; margin-inline: -1.5px; height: 1.16em; vertical-align: text-bottom; pointer-events: none; background: var(--peer); }
.remote-caret-name { position: absolute; bottom: calc(100% - 1px); left: -1px; z-index: 6; border-radius: 3px 3px 3px 0; padding: 0 4px; color: #0c1220; background: var(--peer); font: 650 10px/1.5 system-ui, sans-serif; white-space: nowrap; }
.remote-selection { background: color-mix(in srgb, var(--peer) 38%, transparent); border-bottom: 1px solid var(--peer); }
.code-editor .cm-content { padding-top: 15px; }
.stdin { border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 10px 16px; font-size: 13px; }
.stdin summary { cursor: pointer; color: var(--muted); }
.stdin textarea { margin-top: 8px; width: 100%; font-family: Consolas, monospace; resize: vertical; }
.console { background: #0d1525; }
.console-head { padding-block: 10px; }
.console-head h2 { font-size: 13px; }
.console pre { font-family: Consolas, monospace; font-size: 13px; min-height: 90px; max-height: 235px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; padding: 0 16px 14px; line-height: 1.6; }
.console pre[data-status="error"] { color: #ffb3ba; }
.sheet-controls { display: flex; flex-wrap: wrap; padding: 6px 10px; border-bottom: 1px solid var(--border); }
.sheet-controls button { font-size: 13px; }
.formula-bar { display: grid; grid-template-columns: 104px 32px minmax(0, 1fr); align-items: center; padding: 7px 11px; gap: 4px; border-bottom: 1px solid var(--border); }
.formula-bar output, .fx { padding: 0 7px; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.formula-bar input { padding: 6px 9px; font-family: Consolas, monospace; min-width: 0; }
.sheet-grid { background: #0e1728; height: min(58vh, 535px); min-height: 280px; overflow: auto; position: relative; outline-offset: -2px; user-select: none; }
.sheet-assist { position: relative; z-index: 5; background: #151f33; border-bottom: 1px solid var(--border); }
#formula-hint { min-height: 31px; padding: 7px 12px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
#formula-options { position: absolute; top: 100%; left: 12px; z-index: 6; width: min(460px, calc(100% - 24px)); max-height: 235px; overflow: auto; border: 1px solid #5c729b; border-radius: 8px; padding: 4px; background: #19263e; box-shadow: 0 8px 24px #0007; }
#formula-options button { display: grid; grid-template-columns: minmax(78px, auto) 1fr; align-items: center; gap: 10px; width: 100%; text-align: left; white-space: normal; }
#formula-options button:hover, #formula-options button[aria-selected="true"] { background: #2b4163; }
.formula-option-name { color: var(--accent); font: 650 13px Consolas, monospace; }
.formula-option-args { color: var(--muted); font-size: 12px; }
.grid-canvas { position: relative; }
.grid-header, .grid-row { display: grid; align-items: stretch; height: 30px; }
.grid-header { position: sticky; top: 0; z-index: 3; }
.grid-row { position: absolute; left: 0; }
.grid-label, .grid-cell { border-right: 1px solid #28364c; border-bottom: 1px solid #28364c; font-size: 13px; padding: 5px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.grid-label { background: #1c2a40; text-align: center; color: #b1bfd7; }
.row-label { position: sticky; left: 0; z-index: 2; }
.corner { position: sticky; left: 0; z-index: 4; }
.grid-cell { color: var(--text); background: #101a2b; }
.grid-cell[data-selected="true"] { background: #243857; }
.grid-cell[data-current="true"] { box-shadow: inset 0 0 0 2px var(--blue); }
.grid-cell[data-number="true"] { text-align: right; font-variant-numeric: tabular-nums; }
.grid-cell[data-formula="true"] { color: #afd9c8; }
.grid-cell[data-reference="true"] { background: #3f3156; box-shadow: inset 0 0 0 1px #d7a6ff; }
.grid-cell[data-fill="true"] { background: #193f43; box-shadow: inset 0 0 0 1px #69b9ae; }
.cell-editor { position: absolute; z-index: 2; width: 112px; height: 30px; border: 1px solid var(--blue); border-radius: 0; padding: 4px 7px; background: #1a2a45; font-size: 13px; }
.cell-editor:focus-visible { outline: none; }
.fill-handle { position: absolute; z-index: 2; width: 12px; height: 12px; cursor: crosshair; touch-action: none; }
.fill-handle::after { content: ''; position: absolute; top: 2px; left: 2px; width: 8px; height: 8px; border: 1px solid #111c32; background: var(--blue); }
.sheet-foot { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; gap: 10px; }
.sheet-tabs { display: flex; gap: 5px; overflow: auto; }
.sheet-tabs button { padding: 5px 8px; font-size: 12px; }
.sheet-tabs button[aria-pressed="true"] { background: var(--surface); color: var(--green); }
.sheet-message { color: #ffd6a0; font-size: 12px; padding: 0 12px 8px; }
.sheet-message:empty { display: none; }
dialog { background: #152138; color: var(--text); border: 1px solid var(--border); border-radius: 12px; max-width: min(94vw,1000px); max-height: 92vh; }
dialog::backdrop { background: #000b; }
.condition-dialog { width: min(97vw, 1560px); max-width: none; height: 94dvh; max-height: none; padding: 0; overflow: hidden; }
.condition-dialog[open] { display: flex; flex-direction: column; }
.condition-dialog-head { display: flex; flex: none; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.condition-dialog-head button { font-size: 13px; }
.condition-dialog-head .row-actions { flex-wrap: wrap; }
#statement-dialog { width: min(94vw, 1040px); }
#statement-large, #statement-large-preview { flex: 1; min-height: 0; margin: 14px; resize: none; font-size: clamp(16px, 2.3vw, 20px); line-height: 1.65; }
#statement-large-help { margin-inline: 14px; }
.condition-message { min-height: 1.4em; margin: 0 14px 12px; color: var(--muted); font-size: 13px; }
.image-controls, .ocr-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.image-controls output { min-width: 46px; color: var(--muted); text-align: center; font-size: 13px; }
.image-viewer { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); }
#image-dialog[data-ocr="open"] .image-viewer { grid-template-columns: minmax(0, 1.35fr) minmax(300px, 1fr); }
.image-viewport { overflow: auto; min-width: 0; min-height: 0; padding: 12px; background: #0b1425; }
.image-viewport img { display: block; width: 100%; max-width: none; height: auto; max-height: none; margin: 0; background: #fff; }
.ocr-panel { display: flex; flex-direction: column; min-height: 0; gap: 10px; padding: 14px; border-left: 1px solid var(--border); overflow: auto; }
#ocr-result, #ocr-preview { flex: 1; width: 100%; min-height: 90px; resize: none; line-height: 1.55; }
.ocr-actions button { font-size: 13px; }
.dialog-close { display: block; margin: 0 0 10px auto; }
.formula-dialog { width: min(92vw,590px); padding: 18px 22px; }
.formula-dialog dl { display: grid; grid-template-columns: 1fr 1.5fr; gap: 8px; }
.formula-dialog dd { margin: 0; overflow-wrap: anywhere; color: var(--green); }
.formula-dialog p { color: var(--muted); margin-top: 10px; line-height: 1.5; }
.toast { position: fixed; right: 18px; bottom: 18px; max-width: min(440px, calc(100vw - 36px)); padding: 12px 16px; color: var(--text); border: 1px solid #566d97; border-radius: 9px; background: #21324e; z-index: 5; }
.brand, .library-task a, .back-link { color: inherit; text-decoration: none; }
a:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.task-nav { display: flex; align-items: center; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.task-nav button { font-size: 13px; }
.task-switcher { display: flex; align-items: center; gap: 6px; margin-inline-start: auto; max-width: 100%; }
.task-switcher select { min-width: 0; max-width: min(270px, 60vw); padding: 8px; border: 1px solid var(--border); border-radius: 8px; color: var(--text); background: var(--surface); font: inherit; font-size: 13px; }
.back-link { display: inline-block; color: var(--accent); font-size: 14px; }
.back-link:hover { text-decoration: underline; }
#room-title, #library-title, #task-folder { overflow-wrap: anywhere; }
.library-grid { display: grid; grid-template-columns: minmax(190px, 27%) minmax(0, 1fr); gap: 16px; align-items: start; }
.folders nav { display: grid; gap: 4px; padding: 10px; }
.folder-button { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; text-align: left; white-space: normal; overflow-wrap: anywhere; }
.folder-button:hover, .folder-button[aria-pressed="true"] { background: var(--surface); border-color: var(--border); }
.folder-count { font-size: 12px; color: var(--muted); }
.library-task { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 0 12px 0 0; }
.library-task + .library-task { border-top: 1px solid var(--border); }
.library-task a { display: grid; flex: 1; gap: 5px; min-width: 0; padding: 14px 16px; overflow-wrap: anywhere; }
.library-task a:hover strong { color: var(--accent); }
.library-task a:focus-visible { outline-offset: -3px; }
.library-task a span { color: var(--muted); font-size: 12px; }
.library-empty { padding: 22px 16px; }
.dialog-content { padding: 4px 8px 12px; }
.dialog-content h2 { margin-bottom: 14px; }
#item-dialog { width: min(94vw, 470px); }
#item-form, #item-form label { display: grid; gap: 9px; }
#item-form { gap: 15px; }
#item-form .row-actions { justify-content: flex-end; }
#item-form select { color: inherit; background: #101827; border: 1px solid var(--border); border-radius: 8px; padding: 10px; font: inherit; }
#history-dialog { width: min(94vw, 700px); }
#history-list { display: grid; gap: 6px; }
.history-entry { display: flex; justify-content: space-between; align-items: center; gap: 10px; background: #101a2b; border-color: var(--border); text-align: left; white-space: normal; }
.history-entry:hover { background: #263953; }
.history-entry span { color: var(--muted); font-size: 12px; }
#history-detail h3 { font-size: 14px; margin-bottom: 6px; }
#history-detail pre { padding: 12px; white-space: pre-wrap; overflow-wrap: anywhere; background: #0e1628; border-radius: 8px; font: 13px/1.5 Consolas, monospace; }
#open-history { font-size: 12px; margin-left: auto; }
@media (max-width: 800px) {
  .workspace-grid, #room[data-mode="sheet"] .workspace-grid,
  #room[data-has-image="true"] .workspace-grid, #room[data-mode="sheet"][data-has-image="true"] .workspace-grid {
    grid-template-columns: minmax(0,1fr);
  }
  .topbar { flex-wrap: wrap; }
  #image-dialog[data-ocr="open"] .image-viewer { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); }
  .ocr-panel { border-left: 0; border-top: 1px solid var(--border); }
}
@media (max-width: 640px) {
  .library-grid { grid-template-columns: 1fr; }
  .folders nav { display: flex; flex-wrap: wrap; }
  .folder-button { width: auto; }
}
@media (max-width: 480px) {
  .task-switcher { margin-inline-start: 0; }
  .workspace-heading { align-items: start; flex-direction: column; }
  .top-actions { gap: 6px; flex-wrap: wrap; }
  .top-actions .quiet { padding-inline: 5px; }
  .card-head, .console-head { padding-inline: 10px; }
  .row-actions { gap: 4px; }
  .row-actions button { font-size: 12px; }
  .formula-bar { grid-template-columns: 66px 24px minmax(0,1fr); }
  kbd { display: none; }
}
