/* ===================== RESET & BASE ===================== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg:        #1e1e2e;
  --surface:   #252536;
  --surface2:  #2a2a3d;
  --border:    #3b3b54;
  --text:      #cdd6f4;
  --text-dim:  #7f849c;
  --accent:    #89b4fa;
  --accent2:   #74c7ec;
  --green:     #a6e3a1;
  --red:       #f38ba8;
  --yellow:    #f9e2af;
  --header-bg: #1b2a4a;
  --header-fg: #cdd6f4;
  --select:    rgba(137,180,250,0.18);
  --select-border: #89b4fa;
  --highlight: rgba(249,226,175,0.25);
  --row-alt:   rgba(244,246,249,0.04);
  --toolbar-h: 44px;
  --formula-h: 32px;
  --tabs-h:    36px;
  --col-h:     26px;
  --row-w:     46px;
  --cell-h:    26px;
  --cell-w:    100px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
}

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

/* ===================== TOOLBAR ===================== */
#toolbar {
  height: var(--toolbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 12px;
  flex-shrink: 0;
}
.toolbar-left { display: flex; align-items: center; gap: 8px; min-width: 220px; }
.logo { font-size: 18px; }
.title { font-weight: 600; font-size: 14px; white-space: nowrap; }
.toolbar-center { flex: 1; display: flex; justify-content: center; }
#search-box {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 6px;
  gap: 4px;
  max-width: 360px;
  width: 100%;
}
#search-input {
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 13px;
  padding: 5px 4px;
  flex: 1;
  min-width: 0;
}
#search-input::placeholder { color: var(--text-dim); }
#search-count { font-size: 11px; color: var(--text-dim); white-space: nowrap; }
#search-box button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 4px;
  border-radius: 3px;
}
#search-box button:hover { color: var(--text); background: var(--surface2); }
.toolbar-right { display: flex; align-items: center; min-width: 120px; justify-content: flex-end; }
#download-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--accent);
  color: #1e1e2e;
  padding: 5px 14px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  font-size: 12px;
  transition: opacity .15s;
}
#download-btn:hover { opacity: 0.85; }
#download-btn svg { flex-shrink: 0; }

/* ===================== FORMULA BAR ===================== */
#formula-bar {
  height: var(--formula-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 2px;
  flex-shrink: 0;
}
#cell-ref {
  width: var(--row-w);
  text-align: center;
  font-weight: 600;
  color: var(--accent);
  font-size: 12px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 0 4px;
}
#formula-sep { width: 1px; }
#cell-value {
  flex: 1;
  padding: 0 10px;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===================== SPREADSHEET LAYOUT ===================== */
#spreadsheet {
  flex: 1;
  display: grid;
  grid-template-columns: var(--row-w) 1fr;
  grid-template-rows: var(--col-h) 1fr;
  overflow: hidden;
  position: relative;
}

/* Corner */
#corner {
  background: var(--surface2);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  z-index: 4;
}
.corner-cell { width: 100%; height: 100%; }

/* Column headers */
#col-headers {
  overflow: hidden;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  z-index: 3;
}
#col-headers-inner {
  display: flex;
  height: var(--col-h);
}
.col-header {
  height: var(--col-h);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  user-select: none;
}
.col-header.active { color: var(--accent); background: rgba(137,180,250,0.08); }

/* Row headers */
#row-headers {
  overflow: hidden;
  background: var(--surface2);
  border-right: 1px solid var(--border);
  z-index: 3;
}
#row-headers-inner { display: flex; flex-direction: column; }
.row-header {
  width: var(--row-w);
  height: var(--cell-h);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  user-select: none;
}
.row-header.active { color: var(--accent); background: rgba(137,180,250,0.08); }

/* Grid viewport */
#grid-viewport {
  overflow: auto;
  position: relative;
  z-index: 1;
}
#grid {
  position: relative;
}

/* Cells */
.cell {
  position: absolute;
  height: var(--cell-h);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0 6px;
  display: flex;
  align-items: center;
  font-size: 12px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: cell;
  transition: background .05s;
}
.cell.selected {
  outline: 2px solid var(--select-border);
  outline-offset: -1px;
  background: var(--select) !important;
  z-index: 2;
}
.cell.search-match {
  background: var(--highlight) !important;
}
.cell.search-active {
  outline: 2px solid var(--yellow);
  outline-offset: -1px;
  z-index: 2;
}

/* Number alignment */
.cell.num { justify-content: flex-end; }

/* Positive / negative coloring for percentages */
.cell.pct-pos { color: var(--green); }
.cell.pct-neg { color: var(--red); }

/* Header-style cells (from Excel fill) */
.cell.header-fill {
  font-weight: 600;
}

/* ===================== SHEET TABS ===================== */
#sheet-tabs {
  height: var(--tabs-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 8px;
  flex-shrink: 0;
  overflow-x: auto;
  gap: 2px;
}
#tabs-container { display: flex; gap: 2px; }
.sheet-tab {
  padding: 5px 16px;
  font-size: 12px;
  color: var(--text-dim);
  background: transparent;
  border: none;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  transition: all .12s;
}
.sheet-tab:hover { color: var(--text); background: var(--surface2); }
.sheet-tab.active {
  color: var(--accent);
  background: var(--bg);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

/* ===================== SCROLLBAR ===================== */
#grid-viewport::-webkit-scrollbar { width: 10px; height: 10px; }
#grid-viewport::-webkit-scrollbar-track { background: var(--bg); }
#grid-viewport::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
#grid-viewport::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
#grid-viewport::-webkit-scrollbar-corner { background: var(--bg); }
