/* style.css */
:root {
  color-scheme: dark;
  --bg: #0b1020;
  --bg-elev: #121826;
  --bg-soft: #171e2e;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #e8eef8;
  --muted: #93a0b8;
  --accent: #5b8cff;
  --accent-2: #3ee0ac;
  --danger: #ff6b8a;
  --header: #0f1524;
  --cell: #101624;
  --sel: rgba(91, 140, 255, 0.22);
  --sel-border: #5b8cff;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --font: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #eef2f8;
    --bg-elev: #ffffff;
    --bg-soft: #f5f7fc;
    --line: rgba(16, 24, 40, 0.08);
    --line-strong: rgba(16, 24, 40, 0.16);
    --text: #1c2434;
    --muted: #5b6b86;
    --header: #f3f6fb;
    --cell: #ffffff;
    --sel: rgba(53, 99, 233, 0.16);
    --shadow: 0 16px 40px rgba(28, 36, 52, 0.12);
  }
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #eef2f8;
  --bg-elev: #ffffff;
  --bg-soft: #f5f7fc;
  --line: rgba(16, 24, 40, 0.08);
  --line-strong: rgba(16, 24, 40, 0.16);
  --text: #1c2434;
  --muted: #5b6b86;
  --header: #f3f6fb;
  --cell: #ffffff;
  --sel: rgba(53, 99, 233, 0.16);
  --shadow: 0 16px 40px rgba(28, 36, 52, 0.12);
}

[data-theme="dark"] {
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background:
    radial-gradient(1200px 500px at 10% -10%, rgba(91, 140, 255, 0.18), transparent 50%),
    radial-gradient(900px 400px at 100% 0%, rgba(62, 224, 172, 0.08), transparent 45%),
    var(--bg);
  color: var(--text);
  overflow: hidden;
}

.material-symbols-outlined {
  font-family: "Material Symbols Outlined", "DM Sans", sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 22px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  font-variation-settings: "FILL" 0, "wght" 400, "GRAD" 0, "opsz" 24;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(var(--nav-h) + var(--safe-b));
}

.topbar, .toolbar, .formula-bar, .sheet-tabs, .dock-head, .overlay-bar {
  background: color-mix(in srgb, var(--bg-elev) 86%, transparent);
  backdrop-filter: blur(16px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px 8px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo {
  width: 36px; height: 36px; border-radius: 11px; flex: 0 0 auto;
  background:
    conic-gradient(from 210deg, var(--accent), var(--accent-2), #c084fc, var(--accent));
  box-shadow: 0 8px 20px rgba(91, 140, 255, 0.35);
}
.brand-text { min-width: 0; }
.wb-title {
  width: min(52vw, 280px);
  border: 0;
  background: transparent;
  color: var(--text);
  font: 700 16px/1.2 var(--font);
  padding: 4px 0;
}
.tagline { margin: 0; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 4px; }

.icon-btn, .tool, .nav-btn, .primary, .ghost, .tab, .chip, .export-btn, .chart-item {
  font-family: inherit;
  color: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  background: transparent;
}
.icon-btn {
  min-width: 44px; min-height: 44px;
  display: grid; place-items: center;
  border-radius: 12px;
}
.icon-btn:hover, .tool:hover, .tab:hover, .nav-btn:hover { background: var(--bg-soft); }
.icon-btn.sm { min-width: 40px; min-height: 40px; }

.toolbar { border-bottom: 1px solid var(--line); }
.tool-scroll {
  display: flex; align-items: center; gap: 6px;
  overflow-x: auto; padding: 8px 10px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tool-scroll::-webkit-scrollbar { display: none; }
.tool {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
  background: var(--bg-soft);
  border-color: var(--line);
}
.tool-label { font-size: 13px; font-weight: 600; }
.sep { width: 1px; height: 28px; background: var(--line-strong); margin: 0 4px; flex: 0 0 auto; }
.tool-select {
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  padding: 0 10px;
  font: 600 13px var(--font);
}

.formula-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}
.pos {
  min-width: 64px; min-height: 40px;
  display: grid; place-items: center;
  font: 600 12px var(--mono);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 8px;
}
.fx {
  font: 700 13px var(--mono);
  color: var(--accent);
}
#formula-input {
  flex: 1;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--cell);
  color: var(--text);
  padding: 0 12px;
  font: 400 14px var(--mono);
}

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
}
.grid-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.grid-scroller {
  flex: 1;
  overflow: auto;
  position: relative;
  -webkit-overflow-scrolling: touch;
  background: var(--cell);
  outline: none;
}
.sheet {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
  font-size: 13px;
}
.sheet th, .sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0 8px;
  height: 36px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}
.sheet .corner, .sheet .colh, .sheet .rowh {
  position: sticky;
  background: var(--header);
  z-index: 2;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  user-select: none;
}
.sheet .corner { left: 0; top: 0; z-index: 4; width: 44px; min-width: 44px; }
.sheet .colh { top: 0; z-index: 3; min-width: 108px; }
.sheet .rowh { left: 0; z-index: 3; width: 44px; min-width: 44px; }
.sheet td { background: var(--cell); cursor: cell; }
.sheet td.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12px; }
.sheet td.bold { font-weight: 700; }
.sheet td.error { color: var(--danger); font-weight: 600; }
.sheet td.formula { color: color-mix(in srgb, var(--text) 88%, var(--accent)); }
.sheet td.in-range { background: var(--sel); }
.sheet td.active {
  outline: 2px solid var(--sel-border);
  outline-offset: -2px;
  z-index: 1;
  position: relative;
}
.colh.on, .rowh.on { color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, var(--header)); }

.cell-editor {
  position: absolute;
  display: none;
  z-index: 5;
  border: 2px solid var(--accent);
  border-radius: 4px;
  padding: 0 8px;
  font: 400 13px var(--mono);
  background: var(--bg-elev);
  color: var(--text);
}
.cell-editor.show { display: block; }

.sheet-tabs {
  display: flex; gap: 6px; overflow-x: auto;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
  min-height: 56px;
}
.tab {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}
.tab.on {
  background: color-mix(in srgb, var(--accent) 22%, var(--bg-elev));
  border-color: color-mix(in srgb, var(--accent) 50%, transparent);
}
.tab.add { min-width: 44px; padding: 0; }

.dock {
  width: min(360px, 100%);
  border-left: 1px solid var(--line);
  background: var(--bg-elev);
  display: none;
  flex-direction: column;
  min-height: 0;
}
.dock.open, body.dock-open .dock { display: flex; }
.dock-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px 8px 16px;
  border-bottom: 1px solid var(--line);
}
.dock-head h2, .overlay-bar h2, .modal h2 { margin: 0; font-size: 16px; }
.dock-body {
  overflow: auto;
  padding: 14px 16px 28px;
  display: flex; flex-direction: column; gap: 22px;
}
.dock-body h3 { margin: 0 0 10px; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; margin-bottom: 10px; }
.field input, .field select, #fn-search {
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  padding: 0 12px;
  font: 400 14px var(--font);
}
.primary, .ghost {
  min-height: 44px;
  border-radius: 12px;
  padding: 0 16px;
  font-weight: 700;
}
.primary { background: var(--accent); color: #071018; width: 100%; border: 0; }
.ghost { border: 1px solid var(--line); background: var(--bg-soft); }
.chart-wrap {
  position: relative;
  height: 220px;
  margin-top: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg-soft);
  padding: 8px;
}
.muted { color: var(--muted); font-size: 13px; }
#chart-empty { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: 16px; text-align: center; }
#chart-empty[hidden] { display: none; }
.chart-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.chart-list li { display: flex; gap: 6px; align-items: center; }
.chart-item {
  flex: 1;
  text-align: left;
  min-height: 52px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  padding: 8px 12px;
  display: flex; flex-direction: column; gap: 2px;
}
.chart-item span { color: var(--muted); font-size: 12px; }
.status { margin: 0; font-size: 13px; }

.mobile-nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + var(--safe-b));
  padding: 6px 8px var(--safe-b);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(16px);
  z-index: 8;
}
.nav-btn {
  min-height: 48px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 11px; font-weight: 600; color: var(--muted);
}
.nav-btn[aria-current="page"] { color: var(--text); }

.modal { border: 0; padding: 0; background: transparent; }
.modal::backdrop { background: rgba(6, 10, 20, 0.55); }
.modal-card {
  width: min(440px, calc(100vw - 24px));
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 20px;
  box-shadow: var(--shadow);
}
.modal-card.wide { width: min(560px, calc(100vw - 24px)); }
.modal-card p { color: var(--muted); line-height: 1.45; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.drop {
  display: grid; place-items: center; gap: 4px;
  min-height: 140px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 16px;
  background: var(--bg-soft);
  cursor: pointer;
  text-align: center;
  padding: 16px;
}
.drop.hot { border-color: var(--accent); }
.drop input { display: none; }
.sample-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  font-weight: 600;
  font-size: 13px;
}
.export-grid { display: grid; gap: 8px; }
.export-btn {
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  font-weight: 700;
  text-align: left;
  padding: 0 14px;
}
.fn-list { list-style: none; margin: 12px 0 0; padding: 0; max-height: 46vh; overflow: auto; }
.fn-list button {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: inherit;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 2px;
  min-height: 52px;
  cursor: pointer;
  font-family: inherit;
}
.fn-list code { font-family: var(--mono); font-size: 12px; color: var(--accent); }
.fn-list span { font-size: 12px; color: var(--muted); }

.overlay-3d {
  position: fixed; inset: 0;
  z-index: 20;
  background: var(--bg);
  display: flex; flex-direction: column;
}
.overlay-3d[hidden] { display: none !important; }
.overlay-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
#canvas-3d { flex: 1; width: 100%; display: block; }
.overlay-hint {
  margin: 0;
  padding: 8px 12px calc(12px + var(--safe-b));
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-b) + 12px);
  transform: translateX(-50%) translateY(12px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s ease;
  z-index: 30;
  max-width: calc(100vw - 24px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 900px) {
  #app { padding-bottom: 0; }
  .mobile-nav { display: none; }
  .dock { display: flex; }
  .tool-label { display: inline; }
  .sheet th, .sheet td { height: 32px; }
}
@media (max-width: 899px) {
  .dock {
    position: fixed;
    left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-b));
    top: auto;
    height: min(62vh, 520px);
    width: 100%;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    box-shadow: var(--shadow);
    z-index: 7;
    display: none;
  }
  .dock.open { display: flex; }
  .tool-label { display: none; }
  .sheet th, .sheet td { height: 40px; }
}
@media (max-width: 380px) {
  .tagline { display: none; }
  .wb-title { font-size: 15px; }
}
