/* Spreadsheet editor */

.sheet-editor {
  --sheet-bg: #0d1016;
  --sheet-line: rgba(255, 255, 255, .065);
  --sheet-line-strong: rgba(255, 255, 255, .12);
  --sheet-head: #12161e;
  --sheet-head-ink: #8b93a7;
  --sheet-ink: #e8eaf0;
  background: var(--bg-2);
}
html[data-theme="light"] .sheet-editor {
  --sheet-bg: #ffffff;
  --sheet-line: #e7e9f0;
  --sheet-line-strong: #d3d7e2;
  --sheet-head: #f6f7fa;
  --sheet-head-ink: #667085;
  --sheet-ink: #1b1e26;
}

.sheet-toolbar { gap: 2px; padding: 6px 12px; }

.sheet-formula {
  flex: none;
  display: flex; align-items: center; gap: 8px;
  height: 38px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 11;
}
.ref-box {
  min-width: 74px; height: 24px;
  display: inline-flex; align-items: center;
  padding: 0 8px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  font-family: var(--font-mono);
  font-size: 11.5px; font-weight: 600;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
}
.fx-label {
  width: 24px; height: 22px;
  display: grid; place-items: center;
  font-family: Georgia, serif;
  font-style: italic;
  font-size: 14px;
  color: var(--accent);
  border-right: 1px solid var(--line);
  padding-right: 8px;
  margin-right: 2px;
  user-select: none;
}
.formula-input {
  flex: 1; min-width: 0;
  height: 28px;
  border: none; outline: none;
  background: transparent;
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--text);
}
.formula-input::placeholder { color: var(--faint); font-family: var(--font-ui); }

.sheet-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  grid-template-rows: 27px minmax(0, 1fr);
  background: var(--sheet-bg);
}
.grid-corner {
  background: var(--sheet-head);
  border-right: 1px solid var(--sheet-line);
  border-bottom: 1px solid var(--sheet-line);
  position: relative;
}
.grid-corner::after {
  content: ""; position: absolute; right: 4px; bottom: 4px;
  width: 0; height: 0;
  border-left: 5px solid transparent; border-bottom: 5px solid var(--sheet-line-strong);
}
.col-head { position: relative; overflow: hidden; background: var(--sheet-head); border-bottom: 1px solid var(--sheet-line); }
.row-head { position: relative; overflow: hidden; background: var(--sheet-head); border-right: 1px solid var(--sheet-line); }
.head-inner { position: absolute; left: 0; top: 0; will-change: transform; }

.col-head-cell {
  position: absolute; top: 0;
  height: 27px;
  display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--sheet-line);
  font-size: 11px; font-weight: 620; letter-spacing: .02em;
  color: var(--sheet-head-ink);
  user-select: none;
}
.col-head-cell.is-sel { color: #c2b4ff; background: rgba(124, 92, 255, .12); }
html[data-theme="light"] .col-head-cell.is-sel { color: #5b3ed6; }
.col-resize {
  position: absolute; right: -3px; top: 0; bottom: 0; width: 7px;
  cursor: col-resize; z-index: 2;
}
.col-resize:hover { background: linear-gradient(90deg, transparent, rgba(124, 92, 255, .6), transparent); }

.row-head-cell {
  position: absolute; left: 0; width: 48px;
  display: flex; align-items: center; justify-content: center;
  border-bottom: 1px solid var(--sheet-line);
  font-size: 11px; font-weight: 600;
  color: var(--sheet-head-ink);
  font-variant-numeric: tabular-nums;
  user-select: none;
}
.row-head-cell.is-sel { color: #c2b4ff; background: rgba(124, 92, 255, .12); }
html[data-theme="light"] .row-head-cell.is-sel { color: #5b3ed6; }

.grid-scroll {
  position: relative;
  overflow: auto;
  outline: none;
  background: var(--sheet-bg);
  overscroll-behavior: contain;
}
.grid-canvas { position: relative; }
.cells-layer { position: absolute; inset: 0; z-index: 1; }

.cell {
  position: absolute;
  display: flex; align-items: center;
  padding: 0 7px;
  border-right: 1px solid var(--sheet-line);
  border-bottom: 1px solid var(--sheet-line);
  overflow: hidden;
  white-space: nowrap;
  font-size: 13px;
  color: var(--sheet-ink);
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
  user-select: none;
}
.cell.is-error { color: #fb7185; font-weight: 600; }
.cell.is-active { background: rgba(124, 92, 255, .06); }

.sel-range {
  position: absolute; z-index: 2;
  background: rgba(124, 92, 255, .10);
  border: 1px solid rgba(124, 92, 255, .55);
  pointer-events: none;
}
.sel-active {
  position: absolute; z-index: 4;
  border: 2px solid #7c5cff;
  box-shadow: 0 0 0 1px rgba(124, 92, 255, .18);
  pointer-events: none;
  transition: opacity 120ms var(--ease);
}
.sel-active::after {
  content: ""; position: absolute; right: -4px; bottom: -4px;
  width: 6px; height: 6px;
  background: #7c5cff;
  border-radius: 1px;
}

.cell-input {
  position: absolute; z-index: 8;
  border: 2px solid #7c5cff;
  background: var(--sheet-bg);
  color: var(--sheet-ink);
  padding: 0 5px;
  font-size: 13px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  outline: none;
  border-radius: 1px;
  box-shadow: 0 8px 24px -8px rgba(0, 0, 0, .5);
  min-width: 60px;
}

.charts-layer { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.chart-card {
  position: absolute;
  pointer-events: auto;
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  box-shadow: 0 18px 40px -20px rgba(0, 0, 0, .55);
  overflow: hidden;
}
.chart-head {
  height: 34px; flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 0 6px 0 10px;
  border-bottom: 1px solid var(--line);
  cursor: grab;
  user-select: none;
}
.chart-head:active { cursor: grabbing; }
.chart-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
.chart-title {
  flex: 1; min-width: 0;
  font-size: 11px; font-weight: 600; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chart-del { width: 22px; height: 22px; border-radius: 5px; display: grid; place-items: center; color: var(--faint); flex: none; }
.chart-del:hover { background: var(--hover); color: var(--danger); }
.chart-del .i { width: 13px; height: 13px; }
.chart-body { flex: 1; min-height: 0; color: var(--text-2); padding: 0 2px 2px; }
.chart-body svg { display: block; }

.sheet-tabs {
  flex: none;
  height: 36px;
  display: flex; align-items: flex-end; gap: 2px;
  padding: 0 10px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}
.sheet-tabs::-webkit-scrollbar { display: none; }
.sheet-tab {
  height: 29px; padding: 0 13px;
  display: inline-flex; align-items: center; gap: 6px;
  border-radius: 7px 7px 0 0;
  font-size: 12px; font-weight: 550;
  color: var(--muted);
  border: 1px solid transparent; border-bottom: none;
  white-space: nowrap;
  flex: none;
}
.sheet-tab:hover { color: var(--text); background: var(--hover); }
.sheet-tab.is-active {
  color: var(--text);
  background: var(--bg-2);
  border-color: var(--line);
  box-shadow: inset 0 2px 0 var(--accent);
}
.sheet-add {
  width: 28px; height: 27px; margin-left: 4px;
  display: grid; place-items: center;
  border-radius: 7px 7px 0 0;
  color: var(--muted);
  flex: none;
}
.sheet-add:hover { background: var(--hover); color: var(--text); }
.sheet-add .i { width: 14px; height: 14px; }

/* toolbar active */
.sheet-toolbar .fmt-bold.is-active,
.sheet-toolbar .fmt-italic.is-active,
.sheet-toolbar .fmt-left.is-active,
.sheet-toolbar .fmt-center.is-active,
.sheet-toolbar .fmt-right.is-active { color: #c3b2ff; background: var(--accent-soft); }
html[data-theme="light"] .sheet-toolbar .fmt-bold.is-active,
html[data-theme="light"] .sheet-toolbar .fmt-italic.is-active,
html[data-theme="light"] .sheet-toolbar .fmt-left.is-active,
html[data-theme="light"] .sheet-toolbar .fmt-center.is-active,
html[data-theme="light"] .sheet-toolbar .fmt-right.is-active { color: #5b3ed6; }

@media (max-width: 820px) {
  .sheet-formula { padding: 0 8px; }
  .ref-box { min-width: 58px; }
}
