:root {
  --ink: #241f1a;
  --muted: #6d645b;
  --line: #e3dcd0;
  --paper: #f6f3ec;
  --panel: #fffcf7;
  --header: #243024;
  --header-ink: #f3efe4;
  --accent: #9a5b3c;
  --select: #f3e1d4;
  --freeze: #f3f0e6;
  --dot: #9a5b3c;
  --neg: #8d2f2f;
  --shadow: 0 1px 0 rgba(36, 31, 26, 0.06);
  color-scheme: light;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; width: 100%; }
body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  background: var(--header);
  color: var(--header-ink);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -0.02em; }
.brand p { margin: 2px 0 0; color: #d9d3c5; font-size: 12px; }
.mark {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #f3efe4;
  color: var(--header);
  font-weight: 700;
}
.source {
  color: var(--header-ink);
  text-decoration: none;
  border: 1px solid rgba(243, 239, 228, 0.35);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
}
.source:hover { background: rgba(243, 239, 228, 0.08); }
.top-actions { display: flex; gap: 8px; align-items: center; }
#sign-out { background: transparent; cursor: pointer; }

.login {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--header);
}
.login[hidden] { display: none; }
.login-card {
  width: min(380px, 100%);
  display: grid;
  gap: 12px;
  padding: 28px;
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
}
.login-card h2 { margin: 8px 0 0; font-size: 28px; letter-spacing: -0.03em; }
.login-card p { margin: 0; color: var(--muted); }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 10px 12px;
}
.login-card input:focus { outline: 2px solid #b08972; border-color: transparent; }
.login-card button {
  margin-top: 4px;
  border: 0;
  border-radius: 8px;
  padding: 11px 14px;
  background: var(--header);
  color: var(--header-ink);
  cursor: pointer;
}
.login-error { min-height: 18px; margin: 0; color: var(--neg); font-size: 13px; }

.layout {
  flex: 1;
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}
aside {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.finder { display: block; padding: 12px; }
.finder input, .jump input, #formula-text {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
}
.finder input:focus, .jump input:focus, #formula-text:focus {
  border-color: #b08972;
}
#sheet-list {
  overflow: auto;
  padding: 0 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sheet-btn, .group-label {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}
.sheet-btn small, .group-label {
  display: block;
  color: var(--muted);
  font-size: 11px;
}
.sheet-btn strong { font-weight: 600; font-size: 13px; }
.sheet-btn.active { background: #efe7dc; }
.sheet-btn:hover { background: #f4efe7; }
.sheet-btn.hidden-sheet { opacity: 0.7; }
.group-label { cursor: default; padding-bottom: 0; letter-spacing: 0.04em; text-transform: uppercase; }

main {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 12px 16px 16px;
  gap: 8px;
}
.sheet-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
}
.sheet-head h2 { margin: 0; font-size: 22px; letter-spacing: -0.03em; }
.sheet-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.tools .grow { padding: 0; min-width: 220px; }
.check { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.jump { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.jump input { width: 88px; padding: 7px 8px; }

.formula-bar {
  display: grid;
  grid-template-columns: auto 1fr minmax(120px, 280px) auto;
  gap: 8px;
  align-items: center;
}
#cell-ref, #copy-formula {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}
#cell-ref { min-width: 64px; font-variant-numeric: tabular-nums; font-weight: 600; }
#formula-text { font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
#cell-value {
  border: 1px solid var(--line);
  background: var(--freeze);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 36px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
/* One line that never spills over the grid; the full text is in the tooltip. */
.status {
  flex-shrink: 0;
  margin: 0;
  min-height: 18px;
  line-height: 18px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--muted);
  font-size: 12px;
}

#scroller {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  position: relative;
  box-shadow: var(--shadow);
}
#canvas { position: relative; }
.frozen {
  position: sticky;
  top: 0;
  /* Above the pinned body cells (z-index 5–6), so the header rows stay visible when scrolling down. */
  z-index: 10;
  background: var(--freeze);
  border-bottom: 1px solid #d9d0c3;
}
.cell, .gutter, .ruler {
  position: absolute;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
  line-height: 32px;
  padding: 0 8px;
  border-right: 1px solid #f0ebe3;
  border-bottom: 1px solid #f3eee6;
  font-variant-numeric: tabular-nums;
}
.gutter, .ruler {
  background: #f7f4ee;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
  padding: 0;
  z-index: 3;
}
.ruler { line-height: 22px; font-weight: 600; }
.gutter { line-height: 32px; }
.cell.num { text-align: right; }
.cell.neg { color: var(--neg); }
.cell.formula { box-shadow: inset -7px 0 0 -5px var(--dot); }
.cell.stick, .gutter.stick, .ruler.stick { z-index: 5; }
.cell.stick { background: #fff; }
.cell.head.stick { background: #efeae0; }
.cell.stick.selected, .gutter.stick.selected, .ruler.stick.selected { z-index: 6; }
.add-week-tool {
  height: 32px;
  border: 0;
  border-radius: 999px;
  padding: 0 14px;
  background: #243024;
  color: #f3efe4;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}
.add-week-tool:hover { background: #33432f; }
.delete-columns {
  height: 32px;
  border: 0;
  border-radius: 999px;
  padding: 0 12px;
  background: #8d2f2f;
  color: #f3efe4;
  font-size: 13px;
  cursor: pointer;
}
.delete-columns:disabled {
  background: #e6e0d6;
  color: #8a8178;
  cursor: default;
}
.ruler.marked, .cell.marked { background: #f6d5d2; }
.cell.head.marked { background: #f0c4c0; }
.add-week {
  position: absolute;
  z-index: 7;
  width: 20px;
  height: 18px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: #243024;
  color: #f3efe4;
  font-size: 14px;
  font-weight: 700;
  line-height: 18px;
  cursor: pointer;
}
#scroller:focus { outline: 2px solid transparent; }
.cell.selected, .gutter.selected, .ruler.selected {
  background: var(--select);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 2;
}
.cell.head {
  font-weight: 650;
  background: #efeae0;
  line-height: 32px;
}
.empty {
  position: absolute;
  inset: 80px 24px auto;
  color: var(--muted);
}

/* CSV upload dialog */
.csv-dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
}
.csv-dialog::backdrop { background: rgba(36, 48, 36, 0.45); }
.csv-card { display: grid; gap: 14px; padding: 24px; }
.csv-card h2 { margin: 0; font-size: 22px; letter-spacing: -0.03em; }
.csv-help { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.csv-help code {
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--freeze);
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
}
.csv-field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.csv-note[hidden] { display: none; }
.csv-field input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
}
.csv-field input:focus { outline: 2px solid #b08972; border-color: transparent; }
.csv-columns {
  display: grid;
  gap: 8px;
  max-height: 260px;
  overflow: auto;
  padding-right: 4px;
  font-size: 13px;
}
.csv-columns[hidden] { display: none; }
.csv-subhead { margin: 0; font-weight: 600; }
.csv-name {
  display: grid;
  grid-template-columns: minmax(120px, 200px) 1fr;
  gap: 10px;
  align-items: center;
  color: var(--muted);
}
.csv-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.csv-name input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 7px 10px;
  color: var(--ink);
}
.csv-name input:focus { outline: 2px solid #b08972; border-color: transparent; }
.csv-name input.warn { border-color: var(--neg); }
.csv-preview { display: grid; gap: 10px; min-height: 20px; font-size: 13px; }
.csv-note { margin: 0; color: var(--muted); }
.csv-note.warn, .csv-summary .warn { color: var(--neg); }
.csv-summary { margin: 0; padding-left: 18px; display: grid; gap: 3px; }
.csv-table-wrap {
  max-height: 240px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.csv-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.csv-table th, .csv-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #f0ebe3;
  text-align: left;
  white-space: nowrap;
}
.csv-table th { position: sticky; top: 0; background: var(--freeze); font-weight: 600; color: var(--muted); }
.csv-table .num { text-align: right; }
.csv-preview details { display: grid; gap: 6px; }
.csv-preview summary { cursor: pointer; color: var(--muted); }
.csv-preview textarea {
  width: 100%;
  min-height: 96px;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  resize: vertical;
}
/* Stays at the bottom of the dialog while the rest scrolls, so Add is always reachable. */
.csv-actions {
  position: sticky;
  bottom: 0;
  z-index: 1;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin: 0 -24px -24px;
  padding: 12px 24px 20px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.csv-actions .spacer { flex: 1; }
.csv-actions button {
  border: 0;
  border-radius: 8px;
  padding: 9px 14px;
  background: var(--header);
  color: var(--header-ink);
  cursor: pointer;
}
.csv-actions button:disabled { background: #e6e0d6; color: #8a8178; cursor: default; }
.csv-actions .csv-secondary {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
}
.link-btn {
  border: 0;
  padding: 0;
  background: none;
  color: var(--accent);
  font-size: inherit;
  text-decoration: underline;
  cursor: pointer;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sheet-head, .formula-bar { display: flex; flex-direction: column; align-items: stretch; }
  .formula-bar { display: grid; grid-template-columns: auto 1fr auto; }
  #cell-value { display: none; }
}

/* Toast */
.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(460px, calc(100vw - 32px));
  padding: 12px 12px 12px 14px;
  border-radius: 12px;
  background: var(--header);
  color: var(--header-ink);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
  font-size: 13px;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast[hidden] { display: none; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast-icon {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  background: #3f8f5a;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}
.toast[data-kind="info"] .toast-icon { background: #6d645b; }
.toast-text { flex: 1; line-height: 1.4; }
.toast-action, .toast-close {
  flex-shrink: 0;
  border: 0;
  background: transparent;
  color: var(--header-ink);
  cursor: pointer;
}
.toast-action {
  padding: 5px 10px;
  border: 1px solid rgba(243, 239, 228, 0.4);
  border-radius: 8px;
  font-weight: 600;
}
.toast-action:hover { background: rgba(243, 239, 228, 0.1); }
.toast-close { padding: 0 4px; font-size: 18px; line-height: 1; opacity: 0.7; }
.toast-close:hover { opacity: 1; }

/* Sheet tabs under the grid */
.tabbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.add-tab {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 999px;
  background: var(--header);
  color: var(--header-ink);
  font-size: 18px;
  line-height: 30px;
  cursor: pointer;
}
.add-tab:hover { background: #33432f; }
.tab-list {
  display: flex;
  gap: 4px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
}
.tab {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: 220px;
  height: 30px;
  padding: 0 6px 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.tab.main { padding-right: 12px; }
.tab:hover { background: #f4efe7; color: var(--ink); }
.tab:focus-visible { outline: 2px solid #b08972; outline-offset: 1px; }
.tab.active {
  border-color: var(--header);
  background: #fff;
  color: var(--ink);
  font-weight: 600;
  box-shadow: inset 0 -2px 0 var(--header);
}
.tab-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-close {
  border: 0;
  border-radius: 6px;
  padding: 0 5px;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
  line-height: 20px;
  cursor: pointer;
}
.tab-close:hover { background: #f0c4c0; color: var(--neg); }
.tab-rename {
  width: 140px;
  border: 1px solid #b08972;
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 13px;
  outline: none;
}
.tab-menu {
  position: fixed;
  z-index: 60;
  display: grid;
  min-width: 150px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.16);
}
.tab-menu[hidden] { display: none; }
.tab-menu button {
  border: 0;
  border-radius: 6px;
  padding: 8px 10px;
  background: transparent;
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}
.tab-menu button:hover, .tab-menu button:focus-visible { background: #f4efe7; outline: none; }
.tab-menu .danger { color: var(--neg); }

/* Confirmation popup */
.confirm-dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
}
.confirm-dialog::backdrop { background: rgba(36, 48, 36, 0.45); }
.confirm-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 14px 16px;
  padding: 24px;
}
.confirm-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background: #f6d5d2;
  color: var(--neg);
  font-weight: 700;
  font-size: 18px;
}
.confirm-body h2 { margin: 4px 0 6px; font-size: 18px; letter-spacing: -0.02em; }
.confirm-body p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.confirm-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}
.confirm-actions button {
  border: 0;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 13px;
  cursor: pointer;
}
.confirm-secondary { border: 1px solid var(--line) !important; background: transparent; color: var(--ink); }
.confirm-secondary:hover { background: #f4efe7; }
.confirm-primary { background: var(--header); color: var(--header-ink); }
.confirm-primary.danger { background: var(--neg); color: #fff; }
.confirm-primary.danger:hover { background: #742525; }
.confirm-actions button:focus-visible { outline: 2px solid #b08972; outline-offset: 2px; }

/* Product images in column A */
.cell.image {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 4px;
  background: #fff;
}
.cell.image img {
  height: 28px;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  border-radius: 4px;
  pointer-events: none;
}
.cell.image.selected { background: var(--select); }
.image-preview {
  position: fixed;
  z-index: 40;
  margin: 0;
  width: 240px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  pointer-events: none;
}
.image-preview[hidden] { display: none; }
.image-preview img {
  display: block;
  width: 100%;
  height: 300px;
  object-fit: contain;
  border-radius: 8px;
  background: var(--freeze);
}
.image-preview figcaption {
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.add-week-tool:disabled { opacity: 0.45; cursor: default; }
.tab-menu button[hidden] { display: none; }
.confirm-dialog.info .confirm-icon { background: #e4ece2; color: var(--header); font-family: Georgia, serif; font-style: italic; }
