:root {
  color-scheme: light;
  --ink: #18181b;
  --muted: #71717a;
  --line: #e4e4e7;
  --line-strong: #d4d4d8;
  --paper: #ffffff;
  --canvas: #f4f4f5;
  --header: #fafafa;
  --accent: #137a4a;
  --accent-soft: #e6f3eb;
  --error: #b42318;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #f4f4f5;
  --muted: #a1a1aa;
  --line: #3f3f46;
  --line-strong: #52525b;
  --paper: #18181b;
  --canvas: #09090b;
  --header: #27272a;
  --accent-soft: color-mix(in srgb, var(--accent) 22%, transparent);
  --error: #ff8f87;
}

:root[data-accent="blue"] { --accent: #2678d4; --accent-soft: #e5f0fb; }
:root[data-accent="violet"] { --accent: #7357c8; --accent-soft: #eeeafb; }
:root[data-accent="orange"] { --accent: #c65d18; --accent-soft: #faecdf; }
:root[data-accent="yellow"] { --accent: #ca8a04; --accent-soft: #fef3c7; }
:root[data-accent="red"] { --accent: #dc2626; --accent-soft: #fee2e2; }
:root[data-accent="pink"] { --accent: #db2777; --accent-soft: #fce7f3; }
:root[data-theme="dark"][data-accent] {
  --accent-soft: color-mix(in srgb, var(--accent) 22%, transparent);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  min-width: 720px;
  background: var(--canvas);
  color: var(--ink);
}

.app-shell {
  display: grid;
  grid-template-rows: 54px 48px minmax(0, 1fr) 34px;
  height: 100vh;
  padding: 12px;
}

.topbar,
.formula-row,
.sheet-frame,
footer {
  background: var(--paper);
}

.topbar {
  position: relative;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 12px 12px 0 0;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.settings,
.download { position: relative; }

.icon-button,
.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--header);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.icon-button { width: 32px; height: 32px; }

.theme-toggle {
  width: 100%;
  height: 34px;
  justify-content: flex-start;
  gap: 6px;
  padding: 0 10px;
}

.icon-button:hover,
.theme-toggle:hover {
  border-color: var(--accent);
}

.theme-toggle:focus-visible,
.icon-button:focus-visible,
.swatch:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon-button > i,
.theme-toggle i { font-size: 17px; line-height: 1; }

.popup-menu {
  position: absolute;
  z-index: 20;
  top: 40px;
  right: 0;
  width: 210px;
  padding: 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--paper);
  box-shadow: 0 12px 32px rgb(0 0 0 / 16%);
}

.download-menu { width: 220px; }

.settings-heading { margin-bottom: 10px; font-size: 12px; font-weight: 700; }
.accent-picker { margin: 12px 0 0; padding: 0; border: 0; }
.accent-picker legend { margin-bottom: 8px; color: var(--muted); font-size: 11px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }

.swatch {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 2px solid var(--paper);
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: 0 0 0 1px var(--line-strong);
  cursor: pointer;
}

.swatch[data-accent-value="green"] { --swatch: #137a4a; }
.swatch[data-accent-value="blue"] { --swatch: #2678d4; }
.swatch[data-accent-value="violet"] { --swatch: #7357c8; }
.swatch[data-accent-value="orange"] { --swatch: #c65d18; }
.swatch[data-accent-value="yellow"] { --swatch: #ca8a04; }
.swatch[data-accent-value="red"] { --swatch: #dc2626; }
.swatch[data-accent-value="pink"] { --swatch: #db2777; }
.swatch.active { box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px var(--accent); }

.menu-action {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 10px;
  padding: 8px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.menu-action:hover { background: var(--accent-soft); }
.menu-action > i { color: var(--accent); font-size: 18px; line-height: 1; }
.menu-action span { display: grid; gap: 1px; }
.menu-action strong { font-size: 12px; }
.menu-action small { color: var(--muted); font-size: 10px; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 14px;
}

.brand-mark i { font-size: 17px; line-height: 1; }

.status {
  color: var(--muted);
  font-size: 12px;
}

.formula-row {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: 72px 42px minmax(0, 1fr);
  align-items: center;
  border: 1px solid var(--line);
}

.name-box {
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.fx {
  color: var(--accent);
  text-align: center;
  font-size: 18px;
  border-left: 1px solid var(--line);
}

.formula-input {
  height: 32px;
  margin-right: 8px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  outline: none;
  background: var(--paper);
  color: var(--ink);
  font: 14px/1.2 ui-monospace, SFMono-Regular, Consolas, monospace;
}

.formula-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.sheet-frame {
  position: relative;
  z-index: 1;
  overflow: auto;
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  scrollbar-color: #a1a1aa transparent;
}

.grid {
  display: grid;
  grid-template-columns: 46px repeat(26, 112px);
  grid-template-rows: 32px repeat(100, 30px);
  width: max-content;
  min-width: 100%;
}

.corner,
.column-header,
.row-header,
.cell {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.corner,
.column-header,
.row-header {
  position: sticky;
  z-index: 4;
  display: grid;
  place-items: center;
  background: var(--header);
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  user-select: none;
}

.corner {
  top: 0;
  left: 0;
  z-index: 6;
}

.column-header { top: 0; }
.row-header { left: 0; }

.resize-handle {
  position: absolute;
  z-index: 8;
  touch-action: none;
}

.column-resize-handle {
  top: 0;
  right: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
}

.row-resize-handle {
  left: 0;
  bottom: -4px;
  width: 100%;
  height: 8px;
  cursor: row-resize;
}

.resize-handle:hover::after,
.resize-handle.resizing::after {
  content: "";
  position: absolute;
  background: var(--accent);
}

.column-resize-handle:hover::after,
.column-resize-handle.resizing::after {
  top: 0;
  left: 3px;
  width: 2px;
  height: 100%;
}

.row-resize-handle:hover::after,
.row-resize-handle.resizing::after {
  top: 3px;
  left: 0;
  width: 100%;
  height: 2px;
}

body.resizing-columns,
body.resizing-columns * { cursor: col-resize !important; user-select: none !important; }

body.resizing-rows,
body.resizing-rows * { cursor: row-resize !important; user-select: none !important; }

.column-header.active,
.row-header.active {
  z-index: 5;
  background: var(--accent-soft);
  color: var(--accent);
}

.cell {
  position: relative;
  min-width: 0;
  padding: 5px 8px;
  overflow: hidden;
  background: var(--paper);
  font-size: 13px;
  line-height: 19px;
  white-space: nowrap;
  text-overflow: clip;
  outline: none;
  cursor: cell;
  user-select: none;
}

.cell.range-selected { background: var(--accent-soft); }

.cell.selected {
  z-index: 2;
  box-shadow: inset 0 0 0 2px var(--accent);
}

.cell.error { color: var(--error); }

.cell-editor {
  width: calc(100% + 2px);
  height: calc(100% + 2px);
  margin: -6px -9px;
  padding: 5px 8px;
  border: 2px solid var(--accent);
  outline: none;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
}

footer {
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 0 0 12px 12px;
  color: var(--muted);
  font-size: 11px;
}

kbd, code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

kbd {
  padding: 1px 5px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--header);
  color: var(--ink);
  box-shadow: 0 1px 0 var(--line-strong);
}

@media (max-width: 850px) {
  .app-shell { padding: 0; }
  .topbar { border-radius: 0; }
  footer { border-radius: 0; }
  .status { display: none; }
}
