:root {
  color-scheme: light;
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-2: #f2efea;
  --border: #e4dfd7;
  --text: #1c1917;
  --text-2: #57534e;
  --muted: #8a847c;
  --accent: #c2410c;
  --accent-hover: #9a3412;
  --accent-soft: #ffedd5;
  --accent-text: #9a3412;
  --good: #15803d;
  --good-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --grid: #ebe7e1;
  --shadow: 0 1px 2px rgba(28, 25, 23, .06), 0 4px 16px rgba(28, 25, 23, .05);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #151412;
    --surface: #1f1d1b;
    --surface-2: #2a2825;
    --border: #3a3733;
    --text: #f5f5f4;
    --text-2: #c9c4bd;
    --muted: #938d85;
    --accent: #f97316;
    --accent-hover: #fb923c;
    --accent-soft: #3b2415;
    --accent-text: #fdba74;
    --good: #4ade80;
    --good-soft: #14301f;
    --warn: #fbbf24;
    --warn-soft: #362a10;
    --danger: #f87171;
    --series-1: #3987e5;
    --series-2: #d95926;
    --grid: #2e2c29;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151412;
  --surface: #1f1d1b;
  --surface-2: #2a2825;
  --border: #3a3733;
  --text: #f5f5f4;
  --text-2: #c9c4bd;
  --muted: #938d85;
  --accent: #f97316;
  --accent-hover: #fb923c;
  --accent-soft: #3b2415;
  --accent-text: #fdba74;
  --good: #4ade80;
  --good-soft: #14301f;
  --warn: #fbbf24;
  --warn-soft: #362a10;
  --danger: #f87171;
  --series-1: #3987e5;
  --series-2: #d95926;
  --grid: #2e2c29;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .95rem; }
a { color: var(--accent-text); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
[hidden] { display: none !important; }

/* Layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 20px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.brand-logo { width: 140px; height: 140px; object-fit: contain; flex: none; margin: 0 auto; }
/* On wide screens the logo carries the name; keep the text for screen readers. */
.brand-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.brand-name { font-weight: 700; }
.brand-sub { font-size: .8rem; color: var(--muted); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  color: var(--text-2); text-decoration: none; padding: 9px 12px; border-radius: 8px; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent-text); }
.storage-badge { margin-top: auto; font-size: .78rem; color: var(--muted); padding: 0 6px; }
.main { padding: 24px 28px 60px; max-width: 1200px; width: 100%; min-width: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head .muted { font-size: .85rem; }
.kpi .label { font-size: .8rem; color: var(--muted); font-weight: 500; }
.kpi .value { font-size: 1.55rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .8rem; color: var(--text-2); margin-top: 2px; }

/* Controls */
button, .btn {
  font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 8px 14px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; line-height: 1.2;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: .85rem; }
.btn-ghost { border-color: transparent; background: transparent; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .82rem; color: var(--text-2); font-weight: 500; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border-radius: 999px; padding: 6px 14px; border: 1px solid var(--border); background: var(--surface);
  font-weight: 500; font-size: .9rem;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; }
.tab { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 10px 14px; color: var(--text-2); }
.tab:hover { background: none; color: var(--text); }
.tab.active { border-bottom-color: var(--accent); color: var(--text); }

/* Quick add pickers */
.step-label { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; margin: 4px 0 8px; }
.block-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 8px; }
.block-picker button { justify-content: center; font-size: 1.15rem; font-weight: 700; padding: 14px 0; }
.floor-picker { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 6px; }
.floor-picker button { justify-content: center; padding: 10px 0; font-variant-numeric: tabular-nums; }
.unit-picker { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.unit {
  flex-direction: column; align-items: flex-start; padding: 12px; gap: 4px; min-height: 76px; justify-content: space-between;
}
.unit .unit-no { font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.unit .unit-meta { font-size: .75rem; color: var(--muted); font-weight: 400; }
.block-picker button.active, .floor-picker button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.has-dot { position: relative; }
.has-dot::after { content: ""; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tfoot td { font-weight: 700; border-bottom: 0; }
td.house { font-weight: 600; white-space: nowrap; }
td .items { color: var(--text-2); }

.badge { display: inline-flex; align-items: center; gap: 4px; border-radius: 999px; padding: 2px 9px; font-size: .78rem; font-weight: 600; white-space: nowrap; border: 1px solid transparent; }
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-neutral { background: var(--surface-2); color: var(--text-2); }
button.badge { cursor: pointer; }
button.badge:hover { border-color: currentColor; }

.empty { text-align: center; color: var(--muted); padding: 28px 10px; }
.prep-list { display: flex; flex-wrap: wrap; gap: 8px; }
.prep-item { background: var(--surface-2); border-radius: 8px; padding: 6px 12px; font-weight: 500; }
.prep-item b { color: var(--accent-text); font-variant-numeric: tabular-nums; }

/* Charts */
.hbars { display: flex; flex-direction: column; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(80px, 34%) 1fr auto; gap: 10px; align-items: center; font-size: .88rem; }
.hbar .hlabel { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.hbar .track { height: 14px; display: flex; align-items: center; }
.hbar .fill { height: 14px; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.hbar .hval { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.cols { display: flex; align-items: flex-end; gap: 4px; height: 200px; padding-top: 22px; border-bottom: 1px solid var(--border); position: relative; }
.cols .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 0; position: relative; }
.cols .bar { width: 100%; max-width: 24px; background: var(--series-1); border-radius: 4px 4px 0 0; min-height: 2px; }
.cols .col.current .bar { background: var(--series-1); }
.cols .cap { position: absolute; font-size: .72rem; font-weight: 600; color: var(--text-2); white-space: nowrap; padding-bottom: 2px; font-variant-numeric: tabular-nums; }
.col-labels { display: flex; gap: 4px; margin-top: 6px; }
.col-labels span { flex: 1; text-align: center; font-size: .72rem; color: var(--muted); min-width: 0; overflow: hidden; white-space: nowrap; }
.col-labels span.current { color: var(--text); font-weight: 600; }
.gridline { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--grid); pointer-events: none; }
[data-tip] { cursor: default; }
.tooltip {
  position: fixed; z-index: 60; pointer-events: none; background: var(--text); color: var(--bg);
  padding: 6px 10px; border-radius: 6px; font-size: .8rem; max-width: 240px; box-shadow: var(--shadow); white-space: pre-line;
}

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(12, 10, 9, .5); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center; padding: 32px 16px; overflow-y: auto;
}
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 680px; box-shadow: 0 20px 60px rgba(0,0,0,.3); border: 1px solid var(--border); }
.modal.modal-sm { max-width: 420px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); gap: 10px; }
.modal-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 16px; }
.modal-foot { display: flex; justify-content: space-between; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.section-title { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.item-rows { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 10px; max-height: 320px; overflow-y: auto; }
.item-row { display: grid; grid-template-columns: 1fr 90px 116px 80px; gap: 8px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.item-row:last-child { border-bottom: 0; }
.item-row.selected { background: var(--accent-soft); }
.item-row .iname { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.item-row input { width: 100%; padding: 5px 8px; }
.item-row .line { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.qty { display: flex; align-items: center; justify-content: center; gap: 4px; }
.qty button { width: 32px; height: 32px; padding: 0; justify-content: center; font-size: 1.1rem; }
.qty span { min-width: 26px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.total-box { display: flex; justify-content: space-between; align-items: center; background: var(--surface-2); border-radius: 10px; padding: 12px 14px; gap: 10px; flex-wrap: wrap; }
.total-box .total { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; }
.seg button.active.good { background: var(--good-soft); color: var(--good); }
.seg button.active.warn { background: var(--warn-soft); color: var(--warn); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 70;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-weight: 500; box-shadow: var(--shadow);
}
.toast.error { background: var(--danger); color: #fff; }

.group { border: 1px solid var(--border); border-radius: 10px; }
.group + .group { margin-top: 8px; }
.group-head { cursor: pointer; list-style: none; display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; flex-wrap: wrap; }
.group-head .who { font-weight: 600; }
.group-body { border-top: 1px solid var(--border); padding: 4px 12px 8px; }
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 10px 14px; font-size: .9rem; }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: sticky; top: 0; height: auto; z-index: 20; flex-direction: column; gap: 8px;
    padding: 10px 16px 0; border-right: 0; border-bottom: 1px solid var(--border);
  }
  .brand img { width: 36px; height: 36px; margin: 0; }
  .brand-text { position: static; width: auto; height: auto; overflow: visible; clip: auto; }
  .brand-sub, .storage-badge { display: none; }
  .nav { flex-direction: row; overflow-x: auto; gap: 2px; scrollbar-width: none; }
  .nav a { white-space: nowrap; padding: 8px 10px; border-radius: 8px 8px 0 0; }
  .main { padding: 16px 16px 60px; }
  .floor-picker { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .unit-picker { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .item-row { grid-template-columns: 1fr 116px; }
  .item-row .price-cell, .item-row .line { font-size: .85rem; }
  .modal-backdrop { padding: 0; }
  .modal { border-radius: 0; min-height: 100%; }
  .kpi .value { font-size: 1.3rem; }
}
.group-head::-webkit-details-marker { display: none; }

/* Sign-in page */
.nav .nav-signout { display: none; }
.login-page {
  min-height: 100vh;
  background:
    radial-gradient(circle at 15% 10%, var(--accent-soft), transparent 45%),
    radial-gradient(circle at 90% 90%, var(--accent-soft), transparent 40%),
    var(--bg);
}
.login-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 32px 16px; gap: 16px; }
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow); padding: 32px 28px;
}
.login-brand { text-align: center; margin-bottom: 24px; }
.login-brand img { display: block; margin: 0 auto 12px; }
.login-logo { width: 200px; height: 200px; object-fit: contain; filter: drop-shadow(0 6px 14px rgba(28, 25, 23, .18)); }
.login-logo.fallback { width: 56px; height: 56px; filter: none; }
.has-logo .login-brand h1 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.has-logo .login-brand { margin-top: -8px; }
.login-brand h1 { font-size: 1.45rem; }
.login-brand p { margin: 6px 0 0; font-size: .92rem; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form input[type="email"], .login-form input[type="password"], .login-form input[type="text"] { padding: 11px 12px; font-size: 1rem; width: 100%; }
.password-box { position: relative; display: block; }
.password-box input { padding-right: 70px !important; }
.password-box button { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); color: var(--text-2); }
.login-submit { justify-content: center; padding: 12px; font-size: 1rem; margin-top: 4px; }
.login-error { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); color: var(--danger); border-radius: 8px; padding: 10px 12px; font-size: .9rem; font-weight: 500; }
.login-foot { margin: 0; }
.account { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 0 6px; }
.account .who { font-size: .8rem; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account .storage-badge { margin-top: 0; padding: 0; }
@media (max-width: 760px) {
  .login-card { padding: 28px 20px; }
  .account { display: none; }
  .nav .nav-signout { display: inline-block; }
}
.theme-seg button.active { background: var(--accent); color: #fff; }
