/* Grid -- the CSV editor at /grid, inside the site's page.
 *
 * Loaded after site.css: the page keeps the site's header, typeface and
 * light/dark (theme.js), and the editor below it draws in the site's colours.
 * Paper and ink for the panes, indigo for the selection on light paper and
 * weld on night paper -- the same "filled cell" rule as everywhere else. The
 * editor's own names (--panel, --accent ...) are mapped onto the site's
 * tokens here, so the rest of this file and the canvas (which reads these
 * names) need no knowledge of the site.
 */
:root, [data-theme="light"] {
  --font-ui: var(--sans);
  --font-mono: var(--mono);
  --bg: var(--paper); --panel: #fbfbfe; --panel-2: var(--paper-2); --border: var(--rule); --border-strong: var(--rule-strong);
  --text: var(--ink); --text-dim: var(--ink-3); --accent: var(--cell); --accent-ink: var(--on-cell); --accent-soft: rgba(27, 35, 102, .09);
  --warn: #8a5a00;
  --grid-bg: #fbfbfe; --grid-line: #e1e4ef; --label-bg: var(--paper); --label-text: var(--ink-3);
  --header-bg: #e4e7f4; --header-text: var(--ink); --sel-fill: rgba(42, 54, 160, .13); --sel-label: #d3d8f0;
  --shade: #f4f5fa; --main-line: #8c93c4; --virtual-bg: #f7f8fb;
  --shadow: 0 10px 30px rgba(20, 26, 74, .16), 0 2px 6px rgba(20, 26, 74, .08);
}
[data-theme="dark"] {
  --bg: var(--paper); --panel: #0e1336; --panel-2: var(--paper-2); --border: var(--rule); --border-strong: var(--rule-strong);
  --text: var(--ink); --text-dim: var(--ink-3); --accent: var(--weld); --accent-ink: var(--weld-ink); --accent-soft: var(--weld-wash);
  --warn: var(--weld);
  --grid-bg: #0b0f2e; --grid-line: #1c2258; --label-bg: #10153b; --label-text: var(--ink-3);
  --header-bg: #161c4c; --header-text: var(--ink); --sel-fill: rgba(240, 199, 58, .16); --sel-label: #2a3170;
  --shade: #0e1334; --main-line: #6d6a3c; --virtual-bg: #0a0d29;
  --shadow: 0 10px 30px rgba(0, 0, 0, .5), 0 2px 6px rgba(0, 0, 0, .3);
}
[hidden] { display: none !important; }
/* The page is the window: the site header, then the editor filling the rest. */
html, body { height: 100%; }
body.grid-page { margin: 0; background: var(--bg); color: var(--text); font: 13px/1.4 var(--font-ui); overflow: hidden; display: flex; flex-direction: column; }
body.grid-page > .site-header { flex: none; }
/* site.css's own .btn, .muted and .small are for the marketing pages; inside
 * the editor these names mean the editor's smaller controls. */
#app .btn, #modal-root .btn, .dropdown .btn { min-height: 0; font: inherit; font-stretch: normal; letter-spacing: normal; transition: none; }
#app .btn:active, #modal-root .btn:active { transform: none; }
#app h1, #modal-root p { font-stretch: normal; }
* { box-sizing: border-box; }
button, input, select, textarea { font: inherit; color: inherit; }
kbd { font: 11px var(--font-mono); padding: 1px 5px; border: 1px solid var(--border); border-radius: 4px; background: var(--panel-2); color: var(--text-dim); white-space: nowrap; }
.muted { color: var(--text-dim); }
.grow { flex: 1; min-width: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#app { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#app > * { flex: none; }
#app > #main { flex: 1; min-height: 0; }
#menubar { display: flex; align-items: center; gap: 4px; padding-block: 4px; padding-left: max(10px, env(safe-area-inset-left, 0px)); padding-right: max(10px, env(safe-area-inset-right, 0px)); background: var(--panel); border-bottom: 1px solid var(--border); min-width: 0; }
.brand { display: flex; align-items: center; gap: 7px; font-weight: 600; color: var(--accent); padding-right: 8px; letter-spacing: .01em; }
.brand span { color: var(--text); }
#menus { display: flex; gap: 1px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
.menu-btn { background: none; border: 0; padding: 5px 9px; border-radius: 5px; cursor: pointer; white-space: nowrap; }
.menu-btn:hover, .menu-btn.open { background: var(--panel-2); }
#app .btn, #modal-root .btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--panel); border-radius: 6px; padding: 5px 11px; cursor: pointer; white-space: nowrap; }
#app .btn:hover, #modal-root .btn:hover { border-color: var(--border-strong); background: var(--panel-2); }
#app .btn.primary, #modal-root .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
#app .btn.primary:hover, #modal-root .btn.primary:hover { filter: brightness(1.08); }
#app .btn.ghost, #modal-root .btn.ghost { border-color: transparent; background: none; }
#app .btn.ghost:hover, #modal-root .btn.ghost:hover { background: var(--panel-2); }
#app .btn.small, #modal-root .btn.small { padding: 2px 8px; font-size: 12px; }
#app .btn.icon, #modal-root .btn.icon { width: 28px; height: 28px; justify-content: center; padding: 0; font-size: 17px; }
#btn-palette { color: var(--text-dim); }
.input { width: 100%; border: 1px solid var(--border); background: var(--panel); border-radius: 6px; padding: 5px 8px; }
.input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 2px var(--accent-soft); }

#tabbar { display: flex; align-items: flex-end; gap: 4px; padding: 4px 8px 0; background: var(--panel-2); border-bottom: 1px solid var(--border); min-width: 0; }
#tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: thin; min-width: 0; }
.tab { display: flex; align-items: center; gap: 6px; padding: 5px 6px 6px 12px; border: 1px solid transparent; border-bottom: 0; border-radius: 7px 7px 0 0; cursor: pointer; white-space: nowrap; color: var(--text-dim); max-width: 240px; user-select: none; }
.tab .tname { overflow: hidden; text-overflow: ellipsis; }
.tab:hover { color: var(--text); background: color-mix(in srgb, var(--panel) 50%, transparent); }
.tab.active { background: var(--panel); border-color: var(--border); color: var(--text); margin-bottom: -1px; padding-bottom: 7px; }
.tab .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.tab .lock { font-size: 10px; color: var(--warn); }
.tab .tclose { border: 0; background: none; width: 20px; height: 20px; border-radius: 4px; cursor: pointer; color: var(--text-dim); line-height: 1; }
.tab .tclose:hover { background: var(--panel-2); color: var(--text); }

#main { display: flex; min-height: 0; }
#sidebar { flex: none; }
#sidebar { width: 220px; background: var(--panel); border-right: 1px solid var(--border); overflow: auto; font-size: 12.5px; }
.sb-sec { padding: 10px 0 6px; }
.sb-title { display: flex; align-items: center; gap: 6px; padding: 0 12px 6px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim); font-weight: 600; }
.sb-item { display: flex; align-items: center; gap: 6px; padding: 4px 12px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sb-item:hover { background: var(--panel-2); }
.sb-item.active { background: var(--accent-soft); color: var(--text); }
.sb-item .grow { overflow: hidden; text-overflow: ellipsis; }
.sb-dir { color: var(--text-dim); font-size: 11px; padding: 6px 12px 2px; }
.sb-empty { padding: 2px 12px 8px; color: var(--text-dim); }
#center { flex: 1; display: grid; grid-template-rows: 1fr auto; min-width: 0; min-height: 0; position: relative; }
#gridwrap { position: relative; min-height: 0; overflow: hidden; background: var(--grid-bg); }
#grid-canvas { position: absolute; left: 0; top: 0; pointer-events: none; }
#grid-scroller { position: absolute; inset: 0; overflow: auto; outline: none; }
#grid-spacer { width: 1px; height: 1px; }
#cell-editor { position: absolute; display: none; z-index: 5; resize: none; border: 2px solid var(--accent); background: var(--panel); color: var(--text); padding: 2px 5px; line-height: 1.35; box-shadow: var(--shadow); outline: none; white-space: pre; overflow: hidden; }
#welcome { position: absolute; inset: 0; display: grid; place-items: center; background: var(--grid-bg); padding-inline: 16px; overflow: auto; }
.welcome-card { max-width: 560px; width: 100%; display: grid; gap: 18px; padding-block: 24px; }
.welcome-card h1 { font-size: 26px; font-weight: 600; margin: 0; letter-spacing: -.01em; text-wrap: balance; }
.welcome-card p { margin: 0; color: var(--text-dim); max-width: 60ch; }
.welcome-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.drop-zone { border: 1.5px dashed var(--border-strong); border-radius: 10px; padding: 18px; text-align: center; color: var(--text-dim); }
.welcome-keys { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 12.5px; color: var(--text-dim); }
body.dragging #gridwrap::after { content: 'Drop files to open'; position: absolute; inset: 10px; border: 2px dashed var(--accent); border-radius: 12px; display: grid; place-items: center; font-size: 18px; background: color-mix(in srgb, var(--accent-soft) 70%, transparent); color: var(--text); z-index: 20; }

#editorpane { display: grid; grid-template-columns: auto 1fr; border-top: 1px solid var(--border); background: var(--panel); height: 76px; }
#ep-label { padding: 7px 10px; font: 12px var(--font-mono); color: var(--text-dim); border-right: 1px solid var(--border); min-width: 120px; display: grid; align-content: start; gap: 2px; }
#ep-label b { color: var(--text); font-weight: 500; }
#ep-text { border: 0; resize: none; padding: 7px 10px; background: transparent; font: 13px/1.45 var(--font-mono); outline: none; }
#ep-text:focus { background: var(--panel-2); }

#statusbar { display: flex; gap: 12px; justify-content: space-between; align-items: center; padding-block: 3px; padding-left: max(10px, env(safe-area-inset-left, 0px)); padding-right: max(10px, env(safe-area-inset-right, 0px)); border-top: 1px solid var(--border); background: var(--panel-2); color: var(--text-dim); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; min-width: 0; }
#st-left, #st-right { display: flex; gap: 14px; overflow: hidden; min-width: 0; align-items: center; }
.st-chip { padding: 0 6px; border-radius: 4px; background: var(--panel); border: 1px solid var(--border); cursor: pointer; }
.st-chip:hover { border-color: var(--accent); color: var(--text); }
.st-chip.warn { color: var(--warn); }
.st-chip.accent { color: var(--accent); border-color: var(--accent); }
.st-editing { color: var(--accent); font-weight: 600; }

/* find bar */
#findbar { position: absolute; top: 8px; right: 18px; z-index: 8; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 6px; display: grid; gap: 5px; width: min(520px, calc(100% - 36px)); }
.fb-row { display: flex; gap: 4px; align-items: center; }
.fb-row .input { flex: 1; min-width: 80px; padding: 3px 7px; font-family: var(--font-mono); font-size: 12.5px; }
.fb-tog { border: 1px solid transparent; background: none; border-radius: 4px; font: 11.5px var(--font-mono); padding: 3px 5px; cursor: pointer; color: var(--text-dim); }
.fb-tog.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
#fb-status { font-size: 11.5px; padding: 0 2px; min-height: 0; }
#fb-status:empty { display: none; }

/* menus */
.dropdown { position: fixed; z-index: 50; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 4px; min-width: 260px; max-width: min(420px, calc(100vw - 16px)); max-height: calc(100vh - 60px); overflow: auto; }
.dd-item { display: flex; align-items: center; gap: 10px; padding: 5px 10px 5px 8px; border-radius: 5px; cursor: pointer; white-space: nowrap; }
.dd-item:hover, .dd-item.hl { background: var(--accent-soft); }
.dd-item .chk { width: 14px; color: var(--accent); flex: none; text-align: center; }
.dd-item .lbl { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.dd-item kbd { margin-left: 12px; }
.dd-group { font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim); padding: 8px 10px 3px 30px; font-weight: 600; }
.dd-sep { height: 1px; background: var(--border); margin: 4px 6px; }

/* modals */
.modal-back { position: fixed; inset: 0; z-index: 60; background: rgba(10,20,20,.35); display: grid; place-items: start center; padding-top: 9vh; padding-inline: 16px; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); width: min(480px, 100%); max-height: 82vh; display: grid; grid-template-rows: auto 1fr auto; }
.modal.wide { width: min(820px, 100%); }
.modal-head { padding: 13px 16px 4px; font-weight: 600; font-size: 14.5px; }
.modal-body { padding: 8px 16px; overflow: auto; display: grid; gap: 11px; align-content: start; min-height: 0; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 16px 14px; flex-wrap: wrap; }
.field { display: grid; gap: 4px; }
.field > span { font-size: 12px; color: var(--text-dim); font-weight: 500; }
.field.check { display: flex; align-items: center; gap: 8px; }
.field.check > span { color: var(--text); font-weight: 400; font-size: 13px; }
.field small { color: var(--text-dim); font-size: 11.5px; }
textarea.input { min-height: 110px; font-family: var(--font-mono); font-size: 12.5px; resize: vertical; }
.kv { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.kv th { text-align: left; font-weight: 500; color: var(--text-dim); padding: 4px 16px 4px 0; white-space: nowrap; vertical-align: top; }
.kv td { padding: 4px 0; overflow-wrap: anywhere; font-family: var(--font-mono); font-size: 12.5px; }
.kv tr + tr th, .kv tr + tr td { border-top: 1px solid var(--grid-line); }
.palette .modal-body { padding: 0 0 6px; gap: 0; }
.palette input.pal-in { border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 12px 16px; font-size: 15px; background: transparent; outline: none; width: 100%; }
.pal-list { overflow: auto; max-height: 56vh; padding: 4px; }
.pal-item { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.pal-item.hl { background: var(--accent-soft); }
.pal-item .cat { font-size: 11px; color: var(--text-dim); white-space: nowrap; }
.pal-item .lbl { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pal-empty { padding: 14px 16px; color: var(--text-dim); }
.checklist { max-height: 46vh; overflow: auto; border: 1px solid var(--border); border-radius: 6px; padding: 4px; }
.check-row { display: flex; gap: 8px; align-items: center; padding: 3px 6px; border-radius: 4px; cursor: pointer; }
.check-row:hover { background: var(--panel-2); }
.check-row span.grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-gap { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.filter-list { display: grid; gap: 6px; }
.filter-item { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.chip { font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; padding: 1px 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.settings-grid { display: grid; gap: 4px; }
.set-row { display: grid; grid-template-columns: 1fr minmax(120px, 200px); gap: 12px; align-items: center; padding: 5px 0; border-bottom: 1px solid var(--grid-line); }
.set-row .scope { font-size: 10.5px; color: var(--text-dim); margin-left: 6px; }
.set-head { font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim); font-weight: 600; padding: 12px 0 2px; }
.sc-row { display: grid; grid-template-columns: 1fr minmax(140px, 220px) auto; gap: 8px; align-items: center; padding: 3px 0; border-bottom: 1px solid var(--grid-line); }
.sc-row .input { font-family: var(--font-mono); font-size: 12px; padding: 3px 6px; }
.sc-row.changed .input { border-color: var(--accent); }
.ext-table { width: 100%; border-collapse: collapse; }
.ext-table td, .ext-table th { padding: 3px 4px; text-align: left; font-weight: 500; font-size: 12px; }
.ext-table .input { padding: 3px 6px; font-family: var(--font-mono); font-size: 12px; }
.help h3 { margin: 6px 0 0; font-size: 13px; }
.help p { margin: 0; max-width: 70ch; }
.help ul { margin: 0; padding-left: 18px; display: grid; gap: 3px; }
.help-keys { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 3px 18px; }
.help-keys div { display: flex; justify-content: space-between; gap: 10px; border-bottom: 1px solid var(--grid-line); padding: 3px 0; }

#toasts { position: fixed; bottom: calc(34px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); display: grid; gap: 6px; z-index: 80; justify-items: center; pointer-events: none; width: min(560px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 7px 14px; border-radius: 7px; box-shadow: var(--shadow); font-size: 12.5px; pointer-events: auto; max-width: 100%; }
.toast.error { background: var(--danger); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
@media (prefers-reduced-motion: no-preference) { .toast { animation: tin .16s ease-out; } @keyframes tin { from { opacity: 0; transform: translateY(6px); } } }
#busy { position: fixed; inset: 0; z-index: 90; display: flex; gap: 12px; align-items: center; justify-content: center; background: rgba(10,20,20,.25); color: var(--text); font-weight: 500; }
#busy:not([hidden]) { display: flex; }
#busy span { background: var(--panel); padding: 8px 14px; border-radius: 7px; box-shadow: var(--shadow); }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

body.no-sidebar #sidebar { display: none; }
body.no-tabbar #tabbar { display: none; }
body.no-editorpane #editorpane { display: none; }
@media (max-width: 720px) {
  #sidebar { display: none; }
  .brand span, #btn-palette span { display: none; }
  #editorpane { height: 60px; }
  #ep-label { min-width: 84px; }
  .set-row, .sc-row { grid-template-columns: 1fr; gap: 4px; }
}

#fill-opts { position: absolute; z-index: 6; height: 22px; padding: 0 5px; display: inline-flex; align-items: center; border: 1px solid var(--border-strong); background: var(--panel); color: var(--accent); border-radius: 4px; cursor: pointer; box-shadow: 0 2px 6px rgba(20,40,40,.15); }
#fill-opts:hover { border-color: var(--accent); background: var(--accent-soft); }
