:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --line: #334155;
  --grid-line: #263246;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #34d399;
  --accent-soft: rgba(52, 211, 153, 0.14);
  --danger: #f87171;
  --radius: 12px;
  --shadow: 0 8px 24px rgba(2, 6, 23, 0.45);
  --ease: 180ms ease;
  --row-h: 30px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--font);
}

body { overflow: hidden; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 1400px;
  margin: 0 auto;
  padding: 10px 16px 6px;
  gap: 8px;
}

/* ---- header and toolbar ---------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.logo {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.4;
}

.top-actions, .toolbar, .group { display: flex; align-items: center; gap: 6px; }
.toolbar { flex-wrap: wrap; gap: 6px 14px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 40px;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--ease), border-color var(--ease), color var(--ease), transform var(--ease);
}

.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn:focus-visible, .bar-input:focus-visible, .grid-wrap:focus-visible, .menu button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn.icon { padding: 0; width: 40px; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.icon circle { fill: currentColor; stroke: none; }
.btn.lang { color: var(--accent); min-width: 48px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #052e1f; }
.btn.fmt[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.caret { color: var(--muted); font-size: 0.8em; }

/* ---- formula bar -------------------------------------------------------- */

.formula-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.addr {
  min-width: 64px;
  padding: 0 8px;
  font: 600 13px var(--mono);
  color: var(--accent);
  white-space: nowrap;
}

.fx { color: var(--muted); font-style: italic; font-weight: 600; }

.bar-input {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: 14px var(--mono);
}

/* ---- grid ------------------------------------------------------------------- */

.grid-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
  transition: border-color var(--ease), box-shadow var(--ease);
}

.grid-wrap.dropping { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.grid-inner { position: relative; width: max-content; }

.grid {
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  user-select: none;
  -webkit-user-select: none;
}

.grid th, .grid td {
  height: var(--row-h);
  padding: 0 8px;
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.grid td { cursor: cell; font-variant-numeric: tabular-nums; }
.grid td.num { text-align: right; }
.grid td.bool { text-align: center; color: var(--accent); }
.grid td.err { color: var(--danger); font-weight: 600; text-align: center; background: rgba(248, 113, 113, 0.08); }
.grid td.active-cell { touch-action: none; }

.grid thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--surface);
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  text-align: center;
  cursor: pointer;
}

.grid th.rowhead {
  position: sticky;
  left: 0;
  z-index: 3;
  padding: 0 4px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
}

.grid th.corner { left: 0; z-index: 4; cursor: default; }
.grid th.in-sel { color: var(--accent); background: var(--surface-2); }

.col-resize {
  position: absolute;
  top: 0;
  right: -1px;
  width: 9px;
  height: 100%;
  cursor: col-resize;
  touch-action: none;
}

.col-resize:hover { background: var(--accent-soft); box-shadow: inset -2px 0 0 var(--accent); }

.sel-box, .active-box, .fill-box, .fill-handle, .cell-editor {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.sel-box {
  z-index: 1;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  transition: transform 90ms ease, width 90ms ease, height 90ms ease;
}

.active-box { z-index: 2; border: 2px solid var(--accent); border-radius: 2px; }
.fill-box { z-index: 2; border: 2px dashed var(--accent); }

.fill-handle {
  z-index: 2;
  width: 10px;
  height: 10px;
  background: var(--accent);
  border: 2px solid var(--bg);
  border-radius: 2px;
  cursor: crosshair;
  pointer-events: auto;
  touch-action: none;
}

.cell-editor {
  z-index: 2;
  padding: 0 7px;
  border: 2px solid var(--accent);
  border-radius: 2px;
  outline: none;
  background: var(--surface);
  color: var(--text);
  font: 14px var(--mono);
  pointer-events: auto;
  box-shadow: var(--shadow);
}

[hidden] { display: none !important; }

/* ---- status bar, footer -------------------------------------------------------- */

.statusbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-height: 22px;
  color: var(--muted);
  font-size: 13px;
}

.status-msg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-msg.is-error { color: var(--danger); }
.status-stats {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.footer { text-align: center; color: var(--muted); font-size: 12px; }
.footer a { color: var(--accent); text-decoration: none; display: inline-block; padding: 4px 0; }
.footer a:hover { text-decoration: underline; }

/* ---- menu and dialog ------------------------------------------------------------- */

.menu {
  position: fixed;
  z-index: 20;
  min-width: 210px;
  max-height: calc(100dvh - 16px);
  overflow: auto;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  animation: pop 150ms ease;
}

.menu button {
  display: block;
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background var(--ease);
}

.menu button:hover:not(:disabled), .menu button:focus-visible { background: var(--surface-2); }
.menu button:disabled { color: var(--muted); opacity: 0.5; cursor: default; }
.menu-sep { height: 1px; margin: 4px 6px; background: var(--line); }

.dialog {
  width: min(520px, calc(100vw - 32px));
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.dialog::backdrop { background: rgba(2, 6, 23, 0.6); }
.dialog h2 { margin: 0 0 4px; font-size: 1.1rem; }
.muted { color: var(--muted); margin: 0 0 10px; }

.dialog textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: 13px var(--mono);
  resize: vertical;
}

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

@keyframes pop {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}

/* ---- phones ---------------------------------------------------------------------- */

@media (max-width: 640px), (pointer: coarse) {
  :root { --row-h: 40px; }
  .fill-handle { display: none; }
  .col-resize { width: 16px; right: -4px; }
  /* 40px hit area, grown into its own header so the next header cannot cover it */
  .col-resize::before { content: ""; position: absolute; inset: 0 0 0 -24px; }
  .bar-input, .cell-editor { font-size: 16px; }
}

@media (max-width: 640px) {
  .app { padding: 8px 16px 4px; gap: 6px; }
  .brand { font-size: 1.1rem; }
  .toolbar { gap: 6px; }
  .btn { padding: 0 10px; }
  .addr { min-width: 48px; padding: 0 4px; }
  .statusbar { font-size: 12px; }
}

@media (max-width: 480px) {
  .grid th, .grid td { padding: 0 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
