:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-2: #d1d5db;
  --text: #111827;
  --muted: #6b7280;
  --faint: #9ca3af;
  --accent: #111827;
  --accent-hover: #374151;
  --hover: #f9fafb;
  --logo-bg: #f3f4f6;
  --row-border: #f3f4f6;
  --row-hover: #fafafa;
  --focus: #111827;
  --backdrop: rgba(17, 24, 39, 0.45);
  --green: #059669;
  --green-bg: #d1fae5;
  --green-text: #065f46;
  --orange: #d97706;
  --orange-bg: #ffedd5;
  --orange-text: #9a3412;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --red-text: #b91c1c;
  --radius: 16px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 360px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.06);
}
.login-card .logo {
  width: 44px; height: 44px;
  background: var(--logo-bg);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 16px;
}
.login-card h1 { font-size: 18px; margin: 0 0 4px; font-weight: 700; }
.login-card .sub { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
.login-card input {
  width: 100%;
  padding: 11px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-2);
  border-radius: 10px;
  font-size: 14px;
  outline: none;
}
.login-card input:focus { border-color: var(--focus); }
.login-card button {
  width: 100%;
  margin-top: 12px;
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 10px;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:hover { background: var(--accent-hover); }
#login-error { color: var(--red); font-size: 13px; margin: 10px 0 0; }

/* ---------- page / card ---------- */
.page { max-width: 1060px; margin: 36px auto; padding: 0 20px; }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
  overflow: hidden;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.title-block { display: flex; align-items: center; gap: 14px; }
.logo {
  width: 44px; height: 44px;
  background: var(--logo-bg);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  color: var(--accent);
  flex: none;
}
.title-block h1 { font-size: 17px; font-weight: 700; margin: 0; }
.title-block .sub { color: var(--muted); font-size: 13px; margin: 1px 0 0; }
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--green-bg);
  color: var(--green-text);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12.5px;
  font-weight: 600;
}
.me-user {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  padding: 4px 10px;
  background: var(--logo-bg);
  border-radius: 999px;
}
.login-card input + input { margin-top: 10px; }

/* tabs */
.tabs { display: flex; gap: 4px; padding: 14px 24px 0; }
.tab {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 13.5px;
  font-weight: 500;
  padding: 8px 12px 12px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

.content { padding: 22px 24px 28px; }

/* section head */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.section-head h2 { font-size: 19px; font-weight: 700; margin: 0; }

/* buttons */
.btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-2);
  border-radius: 9px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn.dark {
  background: var(--accent);
  color: var(--surface);
  border: 1px solid var(--accent);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}
.btn.dark:hover { background: var(--accent-hover); }
.btn.danger { color: var(--red); border-color: transparent; background: transparent; }
.btn.danger:hover { background: var(--red-bg); }
.btn.link { border: none; background: none; color: var(--muted); padding: 8px 10px; }
.btn.link:hover { color: var(--text); background: var(--hover); }
.btn svg { vertical-align: -2px; }

/* spreadsheet ledger */
td.cellq { cursor: text; }
td.cellq:focus { outline: 2px solid var(--focus); outline-offset: -2px; border-radius: 4px; }
.total-cell { color: var(--muted); font-weight: 600; }
table tfoot td {
  border-top: 1px solid var(--border);
  font-weight: 700;
  padding: 12px;
  background: var(--hover);
}

/* consume modal */
.modal.wide { width: 620px; }
.consume-list { max-height: 300px; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
.consume-list thead th { position: sticky; top: 0; background: var(--surface); }
.consume-list input[type="number"] {
  width: 72px;
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: 7px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-align: right;
  padding: 5px 8px;
  font-family: inherit;
}

/* stat cards */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}
.stat {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  background: var(--surface);
}
.stat .label { font-size: 13px; color: var(--muted); font-weight: 500; }
.stat .value { font-size: 30px; font-weight: 700; margin: 6px 0 4px; letter-spacing: -0.02em; }
.stat .value.warn { color: var(--orange); }
.stat .value.bad { color: var(--red); }
.stat .sub { font-size: 12.5px; color: var(--faint); }
.stat.clickable { cursor: pointer; }
.stat.clickable:hover { border-color: var(--border-2); background: var(--hover); }

/* search + filter */
.filter-row { display: flex; gap: 12px; margin-bottom: 18px; }
.search-wrap { position: relative; flex: 1; }
.search-wrap svg {
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
  pointer-events: none;
}
.search-wrap input {
  width: 100%;
  height: 46px;
  padding: 0 14px 0 42px;
  border: 1px solid var(--border-2);
  border-radius: 12px;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  outline: none;
}
.search-wrap input::placeholder { color: var(--faint); }
.search-wrap input:focus { border-color: var(--focus); }
.search-wrap.noicon input { padding-left: 14px; }
.select-wrap { position: relative; }
.select-wrap select {
  height: 46px;
  width: 150px;
  padding: 0 36px 0 14px;
  border: 1px solid var(--border-2);
  border-radius: 12px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  appearance: none;
  cursor: pointer;
  outline: none;
}
.select-wrap select:focus { border-color: var(--focus); }
.select-wrap svg {
  position: absolute;
  right: 13px; top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
  pointer-events: none;
}
.select-wrap.wide { width: 170px; flex: none; }
.select-wrap.wide select { width: 100%; }

/* table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
thead th.right { text-align: right; }
tbody td {
  padding: 14px 12px;
  border-bottom: 1px solid var(--row-border);
  vertical-align: middle;
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--row-hover); }
.part { font-size: 13.5px; font-weight: 500; color: var(--text); }
.desc-name { font-size: 14px; font-weight: 600; color: var(--text); }
.desc-sub { font-size: 12.5px; color: var(--faint); margin-top: 1px; }
.qty {
  text-align: right;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.pill {
  display: inline-block;
  border-radius: 999px;
  padding: 3px 11px;
  font-size: 12.5px;
  font-weight: 600;
}
.pill.ok { background: var(--green-bg); color: var(--green-text); }
.pill.warn { background: var(--orange-bg); color: var(--orange-text); }
.pill.bad { background: var(--red-bg); color: var(--red-text); }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.delta-pos { color: var(--green); font-weight: 700; }
.delta-neg { color: var(--red); font-weight: 700; }
.empty-row td { text-align: center; color: var(--faint); padding: 34px 0; }

/* modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 16px;
  z-index: 50;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 480px;
  max-width: 100%;
  padding: 22px;
  box-shadow: 0 12px 32px rgba(16, 24, 40, 0.18);
}
.modal h2 { font-size: 16px; font-weight: 700; margin: 0 0 14px; }
.modal label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 12px 0 5px; }
.modal input, .modal select, .modal textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-2);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.modal input[type="color"] { padding: 4px; height: 40px; cursor: pointer; }
.modal input:focus, .modal select:focus, .modal textarea:focus { border-color: var(--focus); }
.modal textarea { resize: vertical; min-height: 56px; }
.modal .row { display: flex; gap: 10px; }
.modal .row > div { flex: 1; }
.modal .hint { font-size: 13px; color: var(--muted); margin: 0 0 4px; }
.modal .hint strong { color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.modal-actions .left { margin-right: auto; }

/* toast */
#toast-root {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.12);
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 500;
  animation: toast-in 0.18s ease;
}
.toast.err { color: var(--red-text); border-color: #fecaca; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* themes / settings */
.settings-sub {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 22px 0 8px;
}
.settings-sub h3 { font-size: 15px; font-weight: 700; margin: 0; }
.theme-note { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
.theme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.theme-card {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.theme-card.active { outline: 2px solid var(--accent); outline-offset: -1px; }
.theme-swatch-row { display: flex; gap: 6px; }
.swatch {
  width: 100%;
  height: 26px;
  border-radius: 6px;
  border: 1px solid rgba(128, 128, 128, 0.28);
}
.theme-name { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.theme-tag {
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border: 1px solid var(--border-2);
  border-radius: 999px;
  padding: 1px 7px;
}
.theme-active {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
}
.theme-actions { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.theme-actions .grow { flex: 1; }
.color-row { display: flex; align-items: center; gap: 10px; }
.color-row input[type="color"] { width: 52px; flex: none; }
.color-row .hex {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  color: var(--muted);
  text-transform: uppercase;
}
.theme-preview { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin-top: 4px; }
.tp-bg { background: var(--bg); padding: 12px; }
.tp-card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.tp-title { color: var(--text); font-weight: 700; font-size: 13.5px; }
.tp-sub { color: var(--muted); font-size: 11.5px; margin: 2px 0 0; }
.tp-btn {
  background: var(--accent);
  color: var(--surface);
  font-size: 11.5px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 6px;
  display: inline-block;
  margin-top: 10px;
}

@media (max-width: 640px) {
  .page { margin: 12px auto; }
  .title-block .sub { display: none; }
  .filter-row { flex-direction: column; }
  .select-wrap select { width: 100%; }
}
