/* ============ SheetForge layout ============ */
body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.formula-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.formula-bar .cellref {
  min-width: 64px;
  text-align: center;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  color: var(--accent);
}
.formula-bar .fx { color: var(--muted); font-style: italic; font-family: Georgia, serif; }
.formula-bar input#formulaInput {
  flex: 1;
  font-family: ui-monospace, monospace;
}

.grid-scroll {
  flex: 1;
  overflow: auto;
  background: var(--bg-deep);
  position: relative;
}
table.grid {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
  table-layout: fixed;
}
table.grid th, table.grid td {
  border-right: 1px solid #2c2c36;
  border-bottom: 1px solid #2c2c36;
  padding: 0 8px;
  height: 26px;
  min-width: 96px; max-width: 96px; width: 96px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: left;
  vertical-align: middle;
}
table.grid th {
  background: var(--panel);
  color: var(--muted);
  font-weight: 600;
  font-size: 11px;
  text-align: center;
  position: sticky;
  user-select: none;
  z-index: 10;
}
table.grid thead th { top: 0; }
table.grid tbody th {
  left: 0;
  min-width: 46px; max-width: 46px; width: 46px;
  z-index: 9;
}
table.grid thead th:first-child { left: 0; z-index: 11; }
table.grid td {
  background: #17171c;
  color: var(--text);
  cursor: cell;
}
/* dragging selects cells, never the text inside them */
.grid-scroll, .grid-scroll * {
  -webkit-user-select: none;
  user-select: none;
}
.grid-scroll input {
  -webkit-user-select: text;
  user-select: text;      /* the in-cell editor still needs a text caret */
}
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid td.formula { color: #9fd0ff; }
table.grid td.err { color: var(--danger); }
table.grid td.selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: rgba(91,140,255,.08);
}
table.grid td input {
  width: 100%;
  height: 100%;
  border: none;
  background: #202030;
  color: var(--text);
  font: inherit;
  outline: none;
  padding: 0;
  font-family: ui-monospace, monospace;
}

/* ---- formatting bar ---- */
.fmt-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.fmt-bar .ftn {
  min-width: 28px; height: 26px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}
.fmt-bar .ftn:hover { background: var(--panel-3); }
.fmt-bar .ftn.on { background: var(--accent); color: #fff; }
.sel-stats { display: flex; gap: 6px; margin-left: auto; flex-wrap: wrap; }
.sel-stats .stat {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 10px;
  font-size: 11.5px;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.sel-stats .stat b { color: var(--text); }
.sel-stats .stat:hover { border-color: var(--accent); }

/* ---- frozen panes ---- */
table.grid td.frzR, table.grid td.frzC {
  position: sticky;
  z-index: 3;
  background: #1d1d27;
}
table.grid td.frzR.frzC { z-index: 4; }
table.grid tbody th.frzRh { z-index: 12; }  /* row header inside frozen rows */

/* ---- embedded live charts ---- */
.sheet-chart {
  position: absolute;
  z-index: 8;
  background: #17171cee;
  border: 1px solid var(--border-lt);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.45);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sheet-chart .sc-head {
  height: 24px;
  display: flex; align-items: center; gap: 4px;
  padding: 0 6px 0 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  cursor: grab;
  user-select: none;
}
.sheet-chart .sc-head .sc-title {
  flex: 1;
  font-size: 11px;
  color: var(--muted);
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.sheet-chart .sc-head button {
  border: none; background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 4px;
  border-radius: 4px;
}
.sheet-chart .sc-head button:hover { color: var(--text); background: var(--panel-3); }
.sheet-chart canvas { flex: 1; display: block; }
.sheet-chart .sc-resize {
  position: absolute;
  right: 0; bottom: 0;
  width: 14px; height: 14px;
  cursor: nwse-resize;
  background: linear-gradient(135deg, transparent 50%, var(--border-lt) 50%);
  border-bottom-right-radius: 9px;
}

/* ---- selection & fill handle ---- */
/* one clean overlay rectangle over the whole selected range (Excel-style) */
#selOverlay {
  position: absolute;
  pointer-events: none;
  border: 2px solid var(--accent);
  background: rgba(91,140,255,.13);
  border-radius: 2px;
  z-index: 7;
  display: none;
}
table.grid td.fillpreview { outline: 1.5px dashed var(--accent); outline-offset: -2px; }
table.grid th { cursor: pointer; }
table.grid th:hover { color: var(--text); }
#fillHandle {
  position: absolute;
  width: 9px; height: 9px;
  background: var(--accent);
  border: 1.5px solid #fff;
  border-radius: 2px;
  cursor: crosshair;
  z-index: 30;
  display: none;
}

/* ---- context menu ---- */
#ctxMenu {
  position: fixed;
  z-index: 130;
  background: var(--panel);
  border: 1px solid var(--border-lt);
  border-radius: 10px;
  padding: 5px;
  box-shadow: var(--shadow);
  display: none;
  min-width: 200px;
}
#ctxMenu .mi {
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12.5px;
  display: flex; justify-content: space-between; gap: 14px;
  color: var(--text);
  user-select: none;
}
#ctxMenu .mi:hover { background: var(--panel-3); }
#ctxMenu .mi .k { color: var(--muted); font-size: 11px; }
#ctxMenu hr { border: none; border-top: 1px solid var(--border); margin: 4px 6px; }

/* ---- chart modal ---- */
.chart-overlay {
  position: fixed; inset: 0;
  background: rgba(10,10,14,.8);
  display: none;
  align-items: center; justify-content: center;
  z-index: 120;
}
.chart-box {
  background: var(--panel);
  border: 1px solid var(--border-lt);
  border-radius: 14px;
  padding: 18px 20px;
  width: 720px; max-width: 95vw;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.chart-box h2 { font-size: 15px; }
.ch-row { display: flex; gap: 10px; align-items: center; }
#chartCanvas {
  width: 100%;
  background: #17171c;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.sheet-tabs {
  display: flex; align-items: center; gap: 4px;
  padding: 5px 10px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
}
.sheet-tab {
  padding: 4px 14px;
  border-radius: 6px 6px 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
  user-select: none;
}
.sheet-tab.active {
  background: var(--panel-3);
  color: var(--text);
  border-color: var(--accent);
}
.sheet-tabs .btn { padding: 3px 9px; }
.sheet-tabs .hint-right { margin-left: auto; color: #55555f; font-size: 11.5px; white-space: nowrap; }
