/* Calc — Numbers-inspired spreadsheet styles */
:root {
  --bg: #f2f2f6;
  --panel: #ffffff;
  --panel-2: #f7f7f9;
  --border: #d8d8de;
  --grid: #e4e4ea;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.14);
  --head: #f4f4f7;
  --head-text: #55555a;
  --head-active: #dfe9f7;
  --sel: rgba(10, 132, 255, 0.16);
  --err: #d70015;
  --hover: rgba(0, 0, 0, 0.06);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 10px 30px rgba(0, 0, 0, 0.08);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #1b1b1d;
  --panel: #2a2a2d;
  --panel-2: #232326;
  --border: #3b3b40;
  --grid: #3a3a3f;
  --text: #f5f5f7;
  --muted: #9a9aa0;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.22);
  --head: #222225;
  --head-text: #b4b4ba;
  --head-active: #2d3b52;
  --sel: rgba(10, 132, 255, 0.26);
  --err: #ff453a;
  --hover: rgba(255, 255, 255, 0.08);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 10px 30px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.toolbar .title {
  flex: 1 1 160px;
  min-width: 120px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 6px 8px;
  font-weight: 600;
  font-size: 15px;
}
.toolbar .title:hover { border-color: var(--border); }
.toolbar .title:focus { border-color: var(--accent); outline: none; background: var(--panel-2); }
.group { display: flex; align-items: center; gap: 2px; padding: 2px; border-radius: 9px; background: var(--panel-2); }
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 32px; height: 30px; padding: 0 8px;
  border: 0; border-radius: 7px; background: transparent; color: var(--text);
  font-size: 13px; line-height: 1;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn.active { background: var(--accent-soft); color: var(--accent); }
.btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn.b { font-weight: 800; font-family: Georgia, serif; }
.btn.i { font-style: italic; font-family: Georgia, serif; }
.btn.pill { border-radius: 999px; font-weight: 600; letter-spacing: .02em; }
.btn.help-btn.active { background: var(--accent); color: #fff; }
.sel {
  height: 30px; border: 0; border-radius: 7px; background: transparent; padding: 0 6px; max-width: 160px;
}
.sel:hover { background: var(--hover); }
.spacer { flex: 1; }
.hide-sm { display: inline-flex; }

/* ---------- formula bar ---------- */
.formulabar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.namebox {
  width: 84px; height: 30px; border: 1px solid var(--border); border-radius: 7px;
  background: var(--panel-2); text-align: center; font-family: var(--mono); font-size: 12.5px;
}
.fx { color: var(--muted); font-style: italic; font-family: Georgia, serif; font-size: 15px; }
.formula {
  flex: 1; height: 30px; border: 1px solid var(--border); border-radius: 7px; padding: 0 10px;
  background: var(--panel-2); font-family: var(--mono); font-size: 12.5px; min-width: 0;
}
.formula:focus { border-color: var(--accent); outline: none; }

/* ---------- workspace ---------- */
.workspace { flex: 1; display: flex; min-height: 0; position: relative; overflow: hidden; }
.sheet-wrap {
  flex: 1; min-width: 0; overflow: auto; padding: 14px; outline: none;
}
.sheet-card {
  display: inline-block; min-width: 100%;
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
/* rounded corners without overflow:hidden (which would break sticky headers) */
table.grid { border-radius: var(--radius); }
table.grid th.corner { border-top-left-radius: var(--radius); }
table.grid thead th:last-child { border-top-right-radius: var(--radius); border-right: 0; }
table.grid tbody tr:last-child th.rh { border-bottom-left-radius: var(--radius); border-bottom: 0; }
table.grid tbody tr:last-child td { border-bottom: 0; }
table.grid tbody tr:last-child td:last-child { border-bottom-right-radius: var(--radius); }
table.grid tr td:last-child { border-right: 0; }
table.grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; user-select: none; }
table.grid th, table.grid td {
  width: 108px; min-width: 108px; height: 28px; padding: 0 7px;
  border-right: 1px solid var(--grid); border-bottom: 1px solid var(--grid);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; text-align: left;
}
table.grid th {
  position: sticky; top: 0; z-index: 2; background: var(--head); color: var(--head-text);
  font-weight: 500; text-align: center; font-size: 12px;
}
table.grid th.corner { left: 0; z-index: 4; width: 46px; min-width: 46px; }
table.grid th.rh { position: sticky; left: 0; z-index: 3; width: 46px; min-width: 46px; top: auto; }
table.grid th.hl { background: var(--head-active); color: var(--accent); }
table.grid td { background: var(--panel); cursor: cell; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid td.err { color: var(--err); font-family: var(--mono); font-size: 12px; }
table.grid td.bool { text-align: center; color: var(--muted); font-size: 12px; }
table.grid td.in-sel { background: var(--sel); }
table.grid td.active { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--panel); }
table.grid td.in-sel.active { background: var(--sel); }
table.grid td.b { font-weight: 700; }
table.grid td.i { font-style: italic; }
table.grid td.al { text-align: left; }
table.grid td.ac { text-align: center; }
table.grid td.ar { text-align: right; }
table.grid td.editing { padding: 0; overflow: visible; }
.cell-editor {
  width: 100%; height: 100%; border: 0; padding: 0 7px; margin: 0; background: var(--panel);
  outline: 2px solid var(--accent); outline-offset: -2px; font-size: 13px; font-family: var(--font);
}
.cell-editor.fx { font-family: var(--mono); }

/* ---------- help sidebar ---------- */
.help {
  width: 340px; max-width: 100%; background: var(--panel); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; transform: translateX(100%); transition: transform .22s ease;
  position: absolute; top: 0; right: 0; bottom: 0; z-index: 20;
}
.help.open { transform: translateX(0); }
.help-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.help-body { flex: 1; overflow: auto; padding: 4px 16px 24px; font-size: 13.5px; line-height: 1.5; }
.help-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 6px; }
.help-body p { margin: 6px 0; }
.help-body table { width: 100%; border-collapse: collapse; margin: 4px 0 10px; table-layout: fixed; }
.help-body td { padding: 4px 6px 4px 0; vertical-align: top; border-bottom: 1px solid var(--grid); overflow-wrap: anywhere; }
.help-body td:first-child { width: 52%; }
.help-body code, .help-body kbd {
  font-family: var(--mono); font-size: 12px; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 5px;
}
.help-body kbd { box-shadow: 0 1px 0 var(--border); }
@media (min-width: 900px) {
  .help { position: relative; width: 0; border-left: 0; transform: none; overflow: hidden; transition: width .22s ease; }
  .help.open { width: 340px; border-left: 1px solid var(--border); }
  .help > * { width: 340px; }
}

/* ---------- status bar ---------- */
.statusbar {
  display: flex; align-items: center; gap: 10px; padding: 0 8px 0 12px; height: 38px;
  background: var(--panel); border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted);
}
.tabs { display: flex; align-items: center; gap: 2px; overflow-x: auto; min-width: 0; flex: 1; }
.tab {
  border: 0; background: transparent; border-radius: 7px; padding: 5px 12px; color: var(--muted);
  white-space: nowrap; font-weight: 500;
}
.tab:hover { background: var(--hover); }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.stats { display: flex; gap: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stats b { color: var(--text); font-weight: 600; }
.save { white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
.save::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: #30d158; }
.save.busy::before { background: #ff9f0a; }
.save.err::before { background: var(--err); }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .toolbar { padding: 6px 8px; gap: 4px; }
  .toolbar .title { flex-basis: 100%; order: -1; }
  .hide-sm { display: none !important; }
  .formulabar { padding: 6px 8px; }
  .namebox { width: 64px; }
  .sheet-wrap { padding: 8px; }
  .help { width: 100%; }
  table.grid th, table.grid td { width: 92px; min-width: 92px; }
  .stats { display: none; }
  .statusbar .stats.force { display: flex; }
}
@media (max-width: 480px) {
  .group.fmt-group { display: none; }
}
