/* ==========================================================================
   app.css — layout and components (everything except the grid itself).
   Colours come from colors.css; sizes are defined just below.
   ========================================================================== */

:root {
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --radius: 6px;
  --radius-lg: 10px;
  --btn-size: 30px;
  --panel-width: 232px;
}

/* ---- Base ----------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

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

body {
  background: var(--bg-app);
  color: var(--text);
  font: 13px/1.4 var(--font-ui);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  display: block;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- App layout: top bar / main / bottom bar ------------------------------ */

.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  background: var(--bg-bar);
  border-bottom: 1px solid var(--line);
}

.main {
  display: flex;
  min-height: 0;
}

/* ---- Toolbar -------------------------------------------------------------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: var(--btn-size);
  height: var(--btn-size);
  padding: 0 6px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  flex: none;
}
.tb-btn:hover:not(:disabled) { background: var(--bg-hover); color: var(--text); }
.tb-btn:active:not(:disabled) { background: var(--bg-pressed); }
.tb-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-text); }
.tb-btn:disabled { opacity: 0.35; }
.tb-btn .chevron { width: 12px; height: 12px; opacity: 0.7; }

.tb-sep {
  width: 1px;
  height: 18px;
  margin: 0 6px;
  background: var(--line);
  flex: none;
}

/* Colour buttons: icon with the current colour as a bar underneath */
.tb-color-icon { position: relative; display: block; }
.tb-color-icon .icon { width: 16px; height: 16px; margin-bottom: 3px; }
.color-bar {
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 3px;
  border-radius: 2px;
  box-shadow: 0 0 0 1px var(--line-strong);
}

/* ---- Formula bar ---------------------------------------------------------- */

.formulabar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px 6px;
}

.name-box, .formula-input {
  height: 28px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-input);
  padding: 0 8px;
  outline: none;
}
.name-box:focus, .formula-input:focus { border-color: var(--accent); }

.name-box {
  width: 96px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 12px;
}

.formula-input {
  flex: 1;
  min-width: 0;
  font-family: var(--font-mono);
  font-size: 13px;
}

.fx-btn .icon { width: 17px; height: 17px; }

/* ---- Left panel ----------------------------------------------------------- */

.sidepanel {
  width: var(--panel-width);
  flex: none;
  display: none;
  flex-direction: column;
  background: var(--bg-bar);
  border-right: 1px solid var(--line);
  min-height: 0;
}
body.panel-open .sidepanel { display: flex; }

.sp-head {
  padding: 14px 14px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.wb-list {
  list-style: none;
  margin: 0;
  padding: 0 8px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.wb-item {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  padding: 0 4px 0 8px;
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
}
.wb-item:hover { background: var(--bg-hover); color: var(--text); }
.wb-item.active { background: var(--accent-soft); color: var(--accent-text); }
.wb-item .icon { width: 16px; height: 16px; }
.wb-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.wb-more {
  border: 0;
  background: transparent;
  color: inherit;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  display: grid;
  place-items: center;
  opacity: 0;
}
.wb-item:hover .wb-more, .wb-item.active .wb-more, .wb-more:focus-visible { opacity: 0.8; }
.wb-more:hover { background: var(--bg-pressed); opacity: 1; }

.sp-actions, .sp-foot {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px;
  border-top: 1px solid var(--line);
}

.sp-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  padding: 0 8px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  text-align: left;
}
.sp-btn:hover { background: var(--bg-hover); color: var(--text); }
.sp-btn .icon { width: 16px; height: 16px; }
#new-wb { color: var(--accent-text); }

/* ---- Sheet area ------------------------------------------------------------ */

.sheet-area {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
}

/* ---- Bottom bar ------------------------------------------------------------ */

.bottombar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 38px;
  padding: 0 8px;
  background: var(--bg-bar);
  border-top: 1px solid var(--line);
}

.sheetbar {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.wb-name {
  max-width: 220px;
  height: 28px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: none;
}
.wb-name:hover { background: var(--bg-hover); }
.wb-name::after {
  content: "";
  display: inline-block;
  width: 1px;
  height: 14px;
  margin-left: 12px;
  vertical-align: -2px;
  background: var(--line-strong);
}

.sheet-tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  min-width: 0;
}

.sheet-tab {
  height: 28px;
  padding: 0 14px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  white-space: nowrap;
  flex: none;
}
.sheet-tab:hover { background: var(--bg-hover); color: var(--text); }
.sheet-tab.active {
  background: var(--accent-soft);
  color: var(--accent-text);
  font-weight: 600;
}

.statusbar {
  display: flex;
  gap: 16px;
  color: var(--text-faint);
  font-size: 12px;
  white-space: nowrap;
}
.statusbar b {
  color: var(--text-muted);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  margin-left: 4px;
}
.statusbar [hidden] { display: none; }

.zoom {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-left: 12px;
  border-left: 1px solid var(--line);
}
.zoom .tb-btn { min-width: 26px; height: 26px; padding: 0; }
.zoom .icon { width: 14px; height: 14px; }
.zoom-value {
  min-width: 48px;
  height: 26px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.zoom-value:hover { background: var(--bg-hover); color: var(--text); }

/* ---- Popovers & menus ------------------------------------------------------ */

.pop {
  position: fixed;
  inset: auto;
  margin: 0;
  padding: 6px;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
  max-height: calc(100vh - 16px);
  overflow: auto;
}

.menu { min-width: 190px; }
.menu-row { gap: 2px; min-width: 0; }
/* Never set `display` on a closed popover: it would override the browser's
   "hidden until open" rule. Layout is applied only while open. */
.menu-row:popover-open { display: flex; }

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  height: 32px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  text-align: left;
  white-space: nowrap;
}
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: var(--bg-hover); outline: none; }
.menu-item:disabled { opacity: 0.4; }
.menu-item .icon { width: 16px; height: 16px; color: var(--text-muted); }
.menu-item.danger, .menu-item.danger .icon { color: var(--danger); }
.menu-item.danger:hover:not(:disabled) { background: var(--danger-soft); }
.menu-label { flex: 1; }
.menu-hint { color: var(--text-faint); font-size: 12px; margin-left: 16px; }
.menu-sep { height: 1px; margin: 5px 6px; background: var(--line); }

/* Colour swatch */
.swatch-pop { padding: 10px; }
.swatch-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.swatch-title { font-weight: 600; }
.swatch-head .tb-btn, .swatch-custom { gap: 6px; font-size: 12px; }
.swatch-head .icon, .swatch-custom .icon { width: 15px; height: 15px; }
.swatch-grid {
  display: grid;
  grid-template-columns: repeat(var(--swatch-cols), 20px);
  gap: 3px;
}
.swatch-cell {
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px hsl(0 0% 0% / 0.15);
}
.swatch-cell:hover { transform: scale(1.18); box-shadow: 0 0 0 2px var(--text); position: relative; }
.swatch-custom { width: 100%; justify-content: flex-start; margin-top: 8px; }

/* Function picker */
.fn-pop { width: 380px; padding: 0; flex-direction: column; }
.fn-pop:popover-open { display: flex; }
.fn-search {
  margin: 8px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-input);
  outline: none;
}
.fn-search:focus { border-color: var(--accent); }
.fn-list { overflow-y: auto; max-height: 360px; padding: 0 6px 6px; }
.fn-cat {
  padding: 10px 8px 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.fn-item {
  display: block;
  width: 100%;
  padding: 6px 8px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  text-align: left;
}
.fn-item:hover { background: var(--bg-hover); }
.fn-sig { display: block; font-family: var(--font-mono); font-size: 12px; color: var(--text-muted); }
.fn-sig b { color: var(--accent-text); font-weight: 600; }
.fn-desc { display: block; font-size: 12px; color: var(--text-faint); }
.fn-item[hidden], .fn-cat[hidden] { display: none; }

/* "+" add rows / columns */
.add-pop form { display: flex; align-items: flex-end; gap: 8px; padding: 4px; }
.add-pop label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-muted); }
.add-pop input {
  width: 110px;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-input);
  outline: none;
}
.add-pop input:focus { border-color: var(--accent); }

/* ---- Buttons --------------------------------------------------------------- */

.btn {
  height: 32px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: transparent;
  font-weight: 500;
}
.btn:hover { background: var(--bg-hover); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-accent:hover { background: var(--accent-hover); }
.btn-accent.danger { background: var(--danger); border-color: var(--danger); }

/* ---- Dialog ----------------------------------------------------------------- */

.dlg {
  width: min(400px, calc(100vw - 32px));
  padding: 20px;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
}
.dlg::backdrop { background: var(--backdrop); }
.dlg-title { margin: 0 0 8px; font-size: 15px; font-weight: 600; }
.dlg-message { margin: 0 0 12px; color: var(--text-muted); }
.dlg-input {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-input);
  outline: none;
}
.dlg-input:focus { border-color: var(--accent); }
.dlg-input[hidden] { display: none; }
.dlg-error { min-height: 1em; margin: 6px 0 0; color: var(--danger); font-size: 12px; }
.dlg-error:empty { display: none; }
.dlg-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ---- Toasts ----------------------------------------------------------------- */

#toasts {
  position: fixed;
  left: 50%;
  bottom: 52px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  z-index: 10;
}
.toast {
  padding: 8px 14px;
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s, transform 0.2s;
  max-width: 480px;
}
.toast.show { opacity: 1; transform: none; }
.toast-error { color: var(--danger); }

.fatal { position: fixed; inset: 40% 0 auto; text-align: center; color: var(--danger); }
