:root {
  --bg: #f4f6fb;
  --card: #fff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #4f46e5;
  --brand-dark: #4338ca;
  --ok: #059669;
  --err: #dc2626;
  --changed: #fef3c7;
  --added: #dcfce7;
  --deleted: #fee2e2;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; display: flex; flex-direction: column;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, sans-serif;
  background: var(--bg); color: var(--text); font-size: 13px;
}
.top { display: flex; align-items: center; justify-content: space-between; padding: 12px 20px; background: #0f172a; color: #fff; }
.brand { font-weight: 700; font-size: 16px; display: flex; gap: 10px; align-items: center; }
.brand span { font-weight: 400; color: #94a3b8; }
.top-actions { display: flex; gap: 8px; }
.top .btn.ghost { color: #fff; border-color: #334155; background: transparent; }

.btn {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer; text-decoration: none; white-space: nowrap;
  background: var(--brand); color: #fff; border: 1px solid var(--brand); border-radius: 8px;
  padding: 8px 14px; font: inherit; font-weight: 600;
}
.btn:hover { background: var(--brand-dark); }
.btn.ghost { background: #fff; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: #f8fafc; }
.btn.small { padding: 5px 10px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.notice { margin: 40px auto; padding: 18px 22px; background: #fff; border: 1px solid var(--line); border-radius: 12px; max-width: 520px; }

#editor { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 14px 20px 10px; gap: 10px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 12px; color: var(--muted); }
.field.grow { flex: 1; min-width: 200px; }
select, input[type=search] {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: inherit; color: var(--text); background: #fff; min-width: 140px;
}
.tb-actions { display: flex; gap: 8px; margin-left: auto; }

.formula { display: flex; gap: 8px; align-items: stretch; }
.cell-ref { min-width: 90px; display: flex; align-items: center; justify-content: center; background: #fff; border: 1px solid var(--line); border-radius: 8px; font-weight: 700; color: var(--muted); }
.formula textarea { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: 12px/1.45 ui-monospace, Consolas, monospace; resize: vertical; min-height: 38px; }

.msg { min-height: 18px; white-space: pre-wrap; }
.msg.ok { color: var(--ok); }
.msg.err { color: var(--err); }
.msg.info { color: var(--muted); }

.grid-wrap { flex: 1; min-height: 200px; overflow: auto; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
#grid { border-collapse: separate; border-spacing: 0; font-size: 12.5px; table-layout: fixed; }
#grid thead th.col { cursor: grab; user-select: none; position: sticky; overflow: hidden; text-overflow: ellipsis; padding-right: 14px; }
#grid thead th.col.dragging { opacity: .45; }
#grid thead th.col.drop-before { box-shadow: inset 3px 0 0 var(--brand); }
#grid thead th.col.drop-after { box-shadow: inset -3px 0 0 var(--brand); }
#grid .resizer { position: absolute; top: 0; right: 0; width: 8px; height: 100%; cursor: col-resize; z-index: 4; }
#grid .resizer:hover, #grid .resizer.active { background: rgba(79, 70, 229, .35); }
body.resizing, body.resizing * { cursor: col-resize !important; user-select: none !important; }
#grid th, #grid td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0; }
#grid thead th {
  position: sticky; top: 0; z-index: 2; background: #f8fafc; color: var(--muted); font-weight: 700;
  padding: 7px 8px; text-align: left; white-space: nowrap;
}
#grid .rn { position: sticky; left: 0; z-index: 1; background: #f8fafc; color: var(--muted); text-align: right; padding: 0 8px; font-variant-numeric: tabular-nums; }
#grid thead .rn { z-index: 3; }
#grid .del { text-align: center; background: #fff; }
#grid .del button { border: 0; background: transparent; color: #94a3b8; cursor: pointer; padding: 6px; }
#grid .del button:hover { color: var(--err); }
#grid input.cell {
  display: block; width: 100%; min-width: 0; text-overflow: ellipsis; border: 0; padding: 6px 8px; font: inherit; background: transparent; color: var(--text);
}
#grid input.cell.t-n { text-align: right; font-variant-numeric: tabular-nums; }
#grid input.cell.t-d { color: #0369a1; }
#grid input.cell.t-b { color: #7c3aed; }
#grid input.cell:focus { outline: 2px solid var(--brand); outline-offset: -2px; background: #eef2ff; }
#grid td.changed input.cell { background: var(--changed); }
#grid tr.added td { background: var(--added); }
#grid tr.deleted td { background: var(--deleted); }
#grid tr.deleted input.cell { text-decoration: line-through; color: #991b1b; }
#grid .empty { padding: 30px; color: var(--muted); text-align: center; }

.pager { display: flex; justify-content: space-between; align-items: center; color: var(--muted); }
.pager > div { display: flex; gap: 6px; }
.hint { margin: 0; color: var(--muted); line-height: 1.5; font-size: 12px; }
.hint code { background: #eef2ff; padding: 1px 5px; border-radius: 4px; }
