:root {
  --bg:      #1a1a1a;
  --bar:     #212121;
  --border:  #2e2e2e;
  --surface: #2a2a2a;
  --text:    #e6e6e6;
  --muted:   #888;
  --accent:  #5cba7d;
  --sel:     rgba(92,186,125,0.18);
  --sel-hdr: rgba(92,186,125,0.30);
  --r:       6px;
  --row-h:   24px;
  --col-w:   100px;
  --hdr-w:   46px;
}

html, body { height: 100%; }

body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

#app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ── Top bar ── */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bar);
  border-bottom: 1px solid var(--border);
  padding: 0 14px;
  height: 44px;
  flex-shrink: 0;
  gap: 10px;
}

#topbar-left { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

#home-link {
  display: flex; align-items: center;
  color: var(--muted); padding: 4px; border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
#home-link:hover { color: var(--text); background: var(--surface); }

.sep { width: 1px; height: 18px; background: var(--border); }
#app-title { font-size: 13px; font-weight: 600; }

#topbar-actions { display: flex; align-items: center; gap: 6px; overflow: hidden; }

.tbtn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r);
  color: var(--text); font-size: 12.5px; white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.tbtn svg { width: 14px; height: 14px; flex-shrink: 0; }
.tbtn:hover:not(:disabled) { background: #363636; border-color: #444; }
.tbtn:disabled { opacity: 0.35; cursor: not-allowed; }

#file-badge {
  font-size: 12px; color: var(--muted);
  max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding-left: 4px;
}

/* ── Sheet bar ── */
#sheet-bar {
  display: flex;
  flex-direction: column;
  background: var(--bar);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#sheet-tabs {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: 6px 10px 0;
  overflow-x: auto;
  scrollbar-width: thin;
}

.sheet-tab {
  padding: 5px 14px;
  font-size: 12px;
  border-radius: 4px 4px 0 0;
  border: 1px solid transparent;
  border-bottom: none;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, background 0.15s;
  background: none;
}
.sheet-tab:hover { color: var(--text); background: var(--surface); }
.sheet-tab.active {
  color: var(--text);
  background: var(--bg);
  border-color: var(--border);
}

#formula-bar {
  display: flex;
  align-items: center;
  gap: 0;
  border-top: 1px solid var(--border);
  height: 30px;
}

#cell-ref {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11.5px;
  color: var(--muted);
  padding: 0 10px;
  width: 56px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  height: 100%;
  display: flex;
  align-items: center;
}

#formula-input {
  flex: 1;
  height: 100%;
  background: transparent;
  border: none;
  outline: none;
  padding: 0 10px;
  font-size: 12.5px;
  color: var(--text);
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
}

/* ── Main ── */
#main { flex: 1; overflow: hidden; display: flex; flex-direction: column; min-height: 0; }

/* ── Drop zone ── */
#drop-zone {
  flex: 1; display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
#drop-zone.drag-over { background: rgba(92,186,125,0.06); }

#drop-inner {
  display: flex; flex-direction: column; align-items: center;
  gap: 13px; text-align: center; padding: 24px;
}
#drop-icon svg { width: 60px; height: 60px; color: #444; margin-bottom: 4px; }
#drop-inner h2 { font-size: 18px; font-weight: 600; }
#drop-inner p { font-size: 13.5px; color: var(--muted); }
.hint { font-size: 11.5px !important; color: #555 !important; margin-top: -4px; }

.open-btn {
  display: inline-flex; align-items: center;
  padding: 9px 22px;
  background: var(--accent); color: #0d1a11;
  border-radius: var(--r); font-size: 13.5px; font-weight: 600;
  transition: opacity 0.15s; margin-top: 2px;
}
.open-btn:hover { opacity: 0.85; }

/* ── Grid ── */
#grid-wrapper { flex: 1; overflow: hidden; display: flex; flex-direction: column; min-height: 0; }

#grid-scroll {
  flex: 1; overflow: auto;
  background: #1e1e1e;
  position: relative;
}

#grid { table-layout: fixed; }

/* Corner cell */
#grid .corner {
  position: sticky; top: 0; left: 0; z-index: 4;
  width: var(--hdr-w); min-width: var(--hdr-w);
  background: var(--bar);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

/* Column headers */
#grid thead th.col-hdr {
  position: sticky; top: 0; z-index: 3;
  background: var(--bar);
  border-right: 1px solid var(--border);
  border-bottom: 2px solid var(--border);
  font-size: 11px; font-weight: 500; color: var(--muted);
  text-align: center;
  height: var(--row-h);
  min-width: var(--col-w);
  user-select: none;
  padding: 0 4px;
  white-space: nowrap;
}
#grid thead th.col-hdr.selected { background: var(--sel-hdr); color: var(--text); }

/* Row headers */
#grid tbody th.row-hdr {
  position: sticky; left: 0; z-index: 2;
  width: var(--hdr-w); min-width: var(--hdr-w);
  background: var(--bar);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid #252525;
  font-size: 11px; color: var(--muted);
  text-align: center;
  height: var(--row-h);
  user-select: none;
  white-space: nowrap;
}
#grid tbody th.row-hdr.selected { background: var(--sel-hdr); color: var(--text); }

/* Virtual-scroll spacer rows */
#grid tbody tr.row-spacer > td {
  height: auto !important;
  min-width: unset !important;
  max-width: unset !important;
  padding: 0 !important;
  border: none !important;
  background: none !important;
}

/* Data cells */
#grid tbody td {
  height: var(--row-h);
  min-width: var(--col-w);
  max-width: var(--col-w);
  border-right: 1px solid #252525;
  border-bottom: 1px solid #252525;
  padding: 0 5px;
  font-size: 12px;
  font-family: 'Inter', sans-serif;
  color: var(--text);
  background: #1e1e1e;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: default;
  outline: none;
  vertical-align: middle;
}

#grid tbody td.selected { background: var(--sel); }
#grid tbody td.active-cell {
  background: #1e1e1e !important;
  outline: 2px solid var(--accent) !important;
  outline-offset: -2px;
  overflow: visible;
  z-index: 1;
  position: relative;
}
#grid tbody td[contenteditable="true"] {
  white-space: pre;
  overflow: visible;
  caret-color: var(--accent);
  cursor: text;
}

/* Number alignment */
#grid tbody td.num { text-align: right; }

/* ── Status bar ── */
#statusbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--bar); border-top: 1px solid var(--border);
  padding: 3px 14px; font-size: 11.5px; color: var(--muted);
  flex-shrink: 0; height: 24px;
}

/* ── Toast ── */
#toast {
  position: fixed; bottom: 34px; left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: #2e2e2e; color: #eee;
  padding: 7px 18px; border-radius: 20px;
  font-size: 12.5px; opacity: 0; pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 999; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err  { background: #7f2929; }

/* ── Loader ── */
#loader {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
#loader.hidden { display: none; }
.spin {
  width: 34px; height: 34px;
  border: 3px solid #444; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.65s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
