:root {
  --olive: #4a4a33;
  --olive-dark: #34331f;
  --gold: #b99745;
  --gold-light: #d9c088;
  --surface: #fcfcfb;
  --surface-2: #f3f1ea;
  --text-primary: #201f18;
  --text-secondary: #5c5a4d;
  --text-muted: #8a8875;
  --border: #e3dfd0;
  --good: #0ca30c;
  --warning: #b9860f;
  --critical: #d03b3b;

  --series-1: #2a78d6; /* Coffee */
  --series-2: #008300; /* Non-Coffee */
  --series-3: #e87ba4; /* Snacks & Light Meals */
  --series-4: #eda100; /* Breakfast */
  --series-5: #1baf7a; /* Beverages */
  --series-6: #eb6834; /* Clothing / other */
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --olive: #b7b389;
    --olive-dark: #8f8c68;
    --gold: #d9c088;
    --surface: #1a1a19;
    --surface-2: #242320;
    --text-primary: #f6f5ef;
    --text-secondary: #c3c2b7;
    --text-muted: #918f80;
    --border: #3a382f;
    --series-1: #3987e5;
    --series-2: #1fae1f;
    --series-3: #d55181;
    --series-4: #c98500;
    --series-5: #199e70;
    --series-6: #d95926;
  }
}
:root[data-theme="dark"] {
  --olive: #b7b389;
  --olive-dark: #8f8c68;
  --gold: #d9c088;
  --surface: #1a1a19;
  --surface-2: #242320;
  --text-primary: #f6f5ef;
  --text-secondary: #c3c2b7;
  --text-muted: #918f80;
  --border: #3a382f;
  --series-1: #3987e5;
  --series-2: #1fae1f;
  --series-3: #d55181;
  --series-4: #c98500;
  --series-5: #199e70;
  --series-6: #d95926;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--surface);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ---------- Login ---------- */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--olive-dark), var(--olive));
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 40px 36px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  text-align: center;
}
.login-card h1 {
  font-size: 1.3rem;
  margin: 12px 0 4px;
  letter-spacing: 0.02em;
}
.login-card p.tag {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 28px;
}
.login-card input {
  width: 100%;
  padding: 12px 14px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 0.95rem;
}
.login-card button {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--gold);
  color: #201f18;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.95rem;
}
.login-card button:hover { filter: brightness(1.05); }
#login-error {
  color: var(--critical);
  font-size: 0.82rem;
  margin-top: 10px;
  min-height: 1em;
}
.login-logo {
  width: 56px; height: 56px;
  border-radius: 50%;
  margin: 0 auto;
  background: var(--olive);
  display: flex; align-items: center; justify-content: center;
  color: var(--gold);
  font-family: Georgia, serif;
  font-size: 1.4rem;
}

/* ---------- Shell ---------- */
#app { display: none; min-height: 100vh; flex-direction: column; }
#app.visible { display: flex; }

header.topbar {
  background: var(--olive);
  color: #f6f2e6;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .mark {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--gold); color: var(--olive-dark);
  display: flex; align-items: center; justify-content: center;
  font-family: Georgia, serif; font-weight: bold;
}
.brand h1 { font-size: 1.05rem; margin: 0; font-weight: 600; letter-spacing: 0.02em; }
.brand span { font-size: 0.72rem; color: var(--gold-light); display: block; }

.header-actions { display: flex; align-items: center; gap: 10px; }

#logout-btn, #sop-link {
  background: transparent;
  border: 1px solid rgba(246,242,230,0.4);
  color: #f6f2e6;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  text-decoration: none;
  display: inline-block;
}
#logout-btn:hover, #sop-link:hover { background: rgba(246,242,230,0.1); }

nav.tabs-top {
  display: flex;
  gap: 4px;
  padding: 0 24px;
  background: var(--olive-dark);
  overflow-x: auto;
}
nav.tabs-top button {
  border: none;
  background: transparent;
  padding: 10px 20px;
  min-height: 44px;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: rgba(246,242,230,0.65);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
nav.tabs-top button.active {
  color: #f6f2e6;
  border-bottom-color: var(--gold);
}

nav.tabs {
  display: flex;
  gap: 4px;
  padding: 0 24px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
nav.tabs button {
  border: none;
  background: transparent;
  padding: 12px 20px;
  min-height: 48px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
nav.tabs button.active {
  background: var(--surface);
  color: var(--olive);
  border-bottom-color: var(--gold);
  font-weight: 600;
}

main { flex: 1; padding: 20px 24px 60px; max-width: 1200px; margin: 0 auto; width: 100%; }

.panel { display: none; }
.panel.active { display: block; }

/* ---------- Filter bar ---------- */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.filter-bar label { font-size: 0.78rem; color: var(--text-muted); }
.filter-bar input[type="date"] {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  background: var(--surface);
  color: var(--text-primary);
  font-size: 0.85rem;
}
.filter-bar .presets { display: flex; gap: 6px; margin-left: auto; }
.filter-bar .presets button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.78rem;
  cursor: pointer;
}
.filter-bar .presets button.active { background: var(--gold); color: #201f18; border-color: var(--gold); font-weight: 600; }

/* ---------- KPI tiles ---------- */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.kpi-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}
.kpi-tile .label { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-tile .value { font-size: 1.5rem; font-weight: 700; margin-top: 4px; color: var(--text-primary); }
.kpi-tile .sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }
.kpi-tile.profit-pos .value { color: var(--good); }
.kpi-tile.profit-neg .value { color: var(--critical); }

/* ---------- Cards / sections ---------- */
.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  margin-bottom: 20px;
}
.card h2 { font-size: 0.95rem; margin: 0 0 14px; color: var(--text-primary); }
.card h2 small { color: var(--text-muted); font-weight: 400; }

/* ---------- Forms (Products / Suppliers / Purchase Entry) ---------- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}
.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.form-grid label#purchase-product-label { transition: opacity 0.1s; }
.form-grid input, .form-grid select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 10px;
  min-height: 44px;
  background: var(--surface);
  color: var(--text-primary);
  font-size: 0.9rem;
}
.form-grid label.checkbox-label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.form-grid label.checkbox-label input { min-height: auto; width: auto; }
.form-actions {
  grid-column: 1 / -1;
  display: flex;
  gap: 10px;
  align-items: center;
}
.form-actions button {
  border: none;
  border-radius: 6px;
  padding: 10px 18px;
  min-height: 44px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--gold);
  color: #201f18;
}
.form-actions button:hover { filter: brightness(1.05); }
.form-actions button.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-weight: 500;
}
table.data button.secondary {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  padding: 6px 12px;
  min-height: 36px;
  border-radius: 6px;
  font-size: 0.78rem;
  cursor: pointer;
}
.form-error { grid-column: 1 / -1; color: var(--critical); font-size: 0.8rem; min-height: 1em; }
.form-success { grid-column: 1 / -1; color: var(--good); font-size: 0.8rem; min-height: 1em; }

/* ---------- Chart (SVG bars) ---------- */
.chart-wrap { overflow-x: auto; }
svg.chart text { font-size: 10px; fill: var(--text-secondary); font-family: inherit; }
svg.chart .axis-line { stroke: var(--border); stroke-width: 1; }
svg.chart .bar { rx: 3; }
svg.chart .bar-label { font-size: 10px; fill: var(--text-muted); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: 0.75rem; color: var(--text-secondary); }
.legend .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.tooltip-svg {
  position: absolute;
  background: var(--olive-dark);
  color: #f6f2e6;
  font-size: 0.72rem;
  padding: 6px 9px;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  white-space: nowrap;
  z-index: 10;
}

/* ---------- Tables ---------- */
#sales-table, #wages-table, #stock-table, #menu-costing-table, #pnl-table,
#products-table, #suppliers-table, #purchase-history-table, #stock-valuation-table {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
table.data { width: 100%; min-width: 560px; border-collapse: collapse; font-size: 0.83rem; }
table.data th {
  text-align: left;
  padding: 8px 10px;
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
table.data td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: rgba(185,151,69,0.08); }
table.data tr.total-row td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }

table.data.expandable tr.menu-row { cursor: pointer; }
table.data.expandable tr.menu-row:hover { background: rgba(185,151,69,0.1); }
table.data.expandable td.expand-toggle {
  width: 18px;
  color: var(--text-muted);
  font-size: 0.7rem;
}
tr.detail-row td {
  background: var(--surface);
  padding: 10px 10px 14px 30px;
  border-bottom: 1px solid var(--border);
}
tr.detail-row.hidden { display: none; }
table.ingredient-detail { font-size: 0.78rem; }
table.ingredient-detail th { font-size: 0.66rem; }

.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
}
.status-computed { background: rgba(12,163,12,0.15); color: var(--good); }
.status-partial { background: rgba(185,134,15,0.18); color: var(--warning); }
.status-manual { background: rgba(42,120,214,0.15); color: var(--series-1); }
.status-unpriced { background: rgba(208,59,59,0.15); color: var(--critical); }

.empty-state { color: var(--text-muted); font-size: 0.85rem; padding: 20px 0; text-align: center; }
.loading { color: var(--text-muted); font-size: 0.85rem; padding: 20px 0; text-align: center; }

footer.note {
  color: var(--text-muted);
  font-size: 0.72rem;
  text-align: center;
  padding: 20px 0 10px;
}

/* ---------- Responsive (tablet-first; general viewport ranges since the
   exact Samsung tablet model wasn't specified) ---------- */
@media (max-width: 900px) {
  header.topbar { padding: 12px 16px; }
  nav.tabs-top, nav.tabs { padding: 0 16px; }
  main { padding: 16px 16px 50px; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar .presets { margin-left: 0; }
  .kpi-row { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}

@media (max-width: 600px) {
  .brand h1 { font-size: 0.95rem; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
  nav.tabs-top, nav.tabs { padding: 0 12px; }
}

/* ---------- Purchase Entry: mode toggle + photo review ---------- */
.mode-tabs { display: flex; gap: 8px; margin: 0; }
.mode-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  padding: 8px 16px;
  min-height: 44px;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
}
.mode-tab.active { background: var(--gold); color: #201f18; border-color: var(--gold); font-weight: 600; }

.purchase-mode { display: none; }
.purchase-mode.active { display: block; }

.hint { color: var(--text-muted); font-size: 0.82rem; margin: 0 0 12px; }

#receipt-photo-input { display: block; margin-bottom: 10px; }
#receipt-upload-status { color: var(--text-muted); font-size: 0.85rem; min-height: 1.2em; }

.reconciliation-banner {
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.85rem;
  margin-bottom: 14px;
}
.reconciliation-banner.ok { background: rgba(12,163,12,0.12); color: var(--good); }
.reconciliation-banner.warn { background: rgba(208,59,59,0.12); color: var(--critical); }
.reconciliation-banner.neutral { background: var(--surface); color: var(--text-muted); border: 1px solid var(--border); }

table.receipt-review select,
table.receipt-review input {
  width: 100%;
  min-width: 110px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-primary);
  font-size: 0.82rem;
  box-sizing: border-box;
}
table.receipt-review .receipt-total-input,
table.receipt-review .receipt-price-input,
table.receipt-review .receipt-qty-input { min-width: 80px; }
table.receipt-review .receipt-paid-checkbox { width: auto; min-width: 0; }
.receipt-sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }
.receipt-sub.receipt-note { color: var(--warning); }
.receipt-product-cell { min-width: 160px; }
.receipt-new-product-form {
  margin-top: 8px;
  padding: 10px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.receipt-new-product-form input,
.receipt-new-product-form select { width: 100%; }
.new-product-actions { display: flex; gap: 6px; justify-content: flex-end; }
.new-product-actions button { padding: 6px 10px; font-size: 0.78rem; min-height: 32px; }
