:root {
  /* ---- surfaces ---- */
  --bg: #eef2f6;
  --card: #ffffff;
  --ink: #0f1f1b;
  --muted: #5b6b78;
  --line: #e3e9ef;
  --line-strong: #cbd5e1;

  /* ---- brand & accents ---- */
  --brand: #0d7d72;
  --brand-dark: #075049;
  --brand-ink: #ffffff;
  --brand-soft: #e3f2f0;
  --brand-grad: linear-gradient(155deg, #13917f 0%, #0a6b62 55%, #075049 100%);
  --accent: #f59e0b;

  --danger: #dc2626;
  --ok: #15803d;
  --warn: #b45309;
  --water: #2563eb;
  --elec: #d97706;

  --ok-soft: #dcfce7;
  --warn-soft: #fef3c7;
  --danger-soft: #fee2e2;
  --info-soft: #e0f2fe;

  /* ---- radius / shadow ---- */
  --r-sm: 9px;
  --r: 13px;
  --r-lg: 18px;
  --shadow: 0 1px 2px rgba(16, 32, 28, 0.05), 0 2px 8px rgba(16, 32, 28, 0.07);
  --shadow-lg: 0 6px 18px rgba(16, 32, 28, 0.10), 0 14px 36px rgba(16, 32, 28, 0.08);

  /* ---- shell metrics (measured at runtime where noted) ---- */
  --sidebar-w: 224px;
  --tabbar-h: 4.15rem;
  --topbar-h: 3.25rem;
  --savebar-h: 0px; /* measured at runtime on pages that have a save bar */
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", system-ui,
    -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  font-size: 16px;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* =========================================================================
   APP SHELL
   Phone: slim top bar + fixed bottom tab bar.  Tablet/desktop (>=768px):
   persistent left sidebar, no top/bottom bars.  The standalone login page has
   no .app wrapper, so none of this layout applies to it.
   ========================================================================= */
.app { min-height: 100vh; }
.app-main { min-width: 0; }

/* ---- mobile top bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--brand-grad);
  color: var(--brand-ink);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.7rem;
  padding-top: max(0.4rem, env(safe-area-inset-top));
  padding-left: max(0.7rem, env(safe-area-inset-left));
  padding-right: max(0.7rem, env(safe-area-inset-right));
  box-shadow: var(--shadow);
}
.topbar .brand {
  color: var(--brand-ink);
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  display: inline-flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
}
.topbar .brand:hover { text-decoration: none; }
.topbar-version {
  display: inline-block;
  margin-left: 0.4rem;
  padding: 0.04rem 0.4rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.2);
  color: var(--brand-ink);
  font-size: 0.58rem;
  font-weight: 700;
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  opacity: 0.9;
}

/* ---- building switcher (shown only when >1 building) ---- */
.bldg-switch { flex: 0 1 auto; min-width: 0; margin: 0; }
.bldg-switch select {
  max-width: 42vw;
  min-height: 32px;
  padding: 0.2rem 1.5rem 0.2rem 0.55rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.95);
  color: var(--brand-dark);
  font-weight: 700;
  font-size: 0.8rem;
}

/* ---- kebab (secondary links) on mobile top bar ---- */
.kebab { position: relative; flex-shrink: 0; }
.kebab > summary {
  list-style: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  color: var(--brand-ink);
}
.kebab > summary::-webkit-details-marker { display: none; }
.kebab > summary:hover { background: rgba(255, 255, 255, 0.18); }
.kebab[open] > summary { background: rgba(255, 255, 255, 0.24); }
.kebab-menu {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  min-width: 12rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  padding: 0.35rem;
  z-index: 40;
}
.kebab-menu a {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.7rem;
  border-radius: var(--r-sm);
  color: var(--ink);
  font-weight: 600;
  font-size: 0.95rem;
}
.kebab-menu a:hover { background: var(--brand-soft); text-decoration: none; }
.kebab-menu a.is-active { background: var(--brand-soft); color: var(--brand-dark); }
.kebab-menu a.danger { color: var(--danger); }
.kebab-menu .menu-sep { height: 1px; background: var(--line); margin: 0.3rem 0.3rem; }
.nav-ico { flex: 0 0 auto; width: 22px; height: 22px; display: inline-block; }
.nav-ico svg { width: 100%; height: 100%; display: block; }

/* ---- bottom tab bar (phone) ---- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--card);
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 12px rgba(16, 32, 28, 0.07);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar .tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  padding: 0.5rem 0.2rem 0.45rem;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 600;
  position: relative;
  min-height: 3.5rem;
}
.tabbar .tab:hover { text-decoration: none; color: var(--ink); }
.tabbar .tab .nav-ico { width: 24px; height: 24px; }
.tabbar .tab.is-active { color: var(--brand); }
.tabbar .tab.is-active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 2.2rem;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--brand);
}

/* ---- sidebar (tablet/desktop) ---- */
.sidebar { display: none; }

/* ---- content container ---- */
.container {
  max-width: 1000px;
  margin: 0.8rem auto 1rem;
  padding: 0 0.9rem;
  padding-left: max(0.9rem, env(safe-area-inset-left));
  padding-right: max(0.9rem, env(safe-area-inset-right));
  /* reserve room for the fixed tab bar + (when present) the save bar above it */
  padding-bottom: calc(var(--tabbar-h) + var(--savebar-h) + 0.6rem);
}

h1 { font-size: 1.3rem; margin: 0.3rem 0 0.8rem; letter-spacing: -0.01em; }
h2 { font-size: 1.08rem; margin: 1rem 0 0.5rem; letter-spacing: -0.01em; }

/* =========================================================================
   CARDS & METRICS
   ========================================================================= */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 0.85rem 0.95rem;
  margin-bottom: 0.8rem;
  box-shadow: var(--shadow);
  overflow-x: auto; /* wide tables scroll inside the card instead of off-screen */
}

.cards { display: flex; gap: 0.7rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.cards .card {
  flex: 1 1 calc(50% - 0.35rem);
  margin-bottom: 0;
  padding: 0.85rem 0.95rem;
  position: relative;
  overflow: hidden;
}
/* Tinted metric cards (graceful: browsers without :has() just show white). */
.cards .card::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--line-strong);
}
.cards .card:has(.metric.ok)::before { background: var(--ok); }
.cards .card:has(.metric.warn)::before { background: var(--warn); }
.cards .card:has(.metric.ok) { background: linear-gradient(0deg, #f4fdf7, #fff); }
.cards .card:has(.metric.warn) { background: linear-gradient(0deg, #fffbf2, #fff); }
@media (min-width: 600px) {
  .cards .card { flex: 1 1 160px; }
}
.metric { font-size: 1.3rem; font-weight: 800; letter-spacing: -0.02em; line-height: 1.2; font-variant-numeric: tabular-nums; }
.metric.ok { color: var(--ok); }
.metric.warn { color: var(--warn); }
.label { color: var(--muted); font-size: 0.74rem; font-weight: 600; margin-bottom: 0.2rem; }

/* =========================================================================
   TABLES
   ========================================================================= */
table { width: 100%; min-width: 460px; border-collapse: collapse; }
th, td { padding: 0.6rem 0.55rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.is-paid { opacity: 0.55; }
tr.row-active { background: var(--brand-soft); }

/* =========================================================================
   BULK PAYMENTS (compact per-room cards)
   ========================================================================= */
.pay-list {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.pay-card { padding: 0.7rem 1.25rem; border-bottom: 1px solid var(--line); }
.pay-card:last-child { border-bottom: none; }
.pay-card.is-paid { opacity: 0.5; }
.pay-card.row-active { background: var(--brand-soft); }
.pay-head { display: flex; align-items: center; gap: 0.55rem; }
.pay-pick { display: inline-flex; align-items: center; gap: 0.45rem; margin: 0; }
.pay-pick input { width: 22px; height: 22px; min-height: 0; accent-color: var(--brand); }
.pay-room { font-weight: 800; font-size: 1.05rem; }
.pay-tenant { flex: 1 1 auto; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pay-amounts {
  display: flex;
  gap: 1.2rem;
  margin-top: 0.25rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.pay-amounts strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.pay-amounts strong.warn { color: var(--warn); }
/* Same flex-wrap pattern the rest of the app's forms (.row/.field) use, which
   handles the iOS date input fine: fields keep their natural width and wrap,
   instead of being forced into rigid columns that clip the date picker. */
.pay-controls { display: flex; flex-wrap: wrap; gap: 0.45rem 0.6rem; margin-top: 0.45rem; }
.pay-controls .pc-field { flex: 1 1 8rem; min-width: 0; display: flex; flex-direction: column; gap: 0.12rem; margin: 0; }
.pay-controls .pc-field span { font-size: 0.68rem; color: var(--muted); font-weight: 600; }
.pay-controls .pc-field input, .pay-controls .pc-field select { width: 100%; }
.pay-controls .pc-amount input { text-align: right; }
/* Pin the date field to a fixed comfortable width: with min-width:0 a flexed
   date field could be squeezed below the date's natural width, so the input
   overflowed its column and landed on the card border. Fixed width = it never
   shrinks/overflows; the amount field flexes to take the rest of the row. */
.pay-controls .pc-date { flex: 0 0 9.5rem; }

/* =========================================================================
   RESPONSIVE LIST TABLES
   On phones/small tablets (<768px) tables marked .stack / .cardlist collapse
   to one block per row; from 768px up they render as real columns.
   ========================================================================= */
@media (max-width: 767px) {
  table.stack { min-width: 0; }
  table.stack thead {
    position: absolute; width: 1px; height: 1px; margin: -1px;
    padding: 0; overflow: hidden; clip: rect(0 0 0 0);
  }
  table.stack tr {
    display: block;
    padding: 0.45rem 0;
    border-bottom: 1px solid var(--line);
  }
  table.stack tr:last-child { border-bottom: none; }
  table.stack td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.8rem;
    border: none;
    padding: 0.08rem 0.1rem;
    text-align: right;
    white-space: normal;
    line-height: 1.35;
  }
  table.stack td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 600;
    text-align: left;
  }
  table.stack td.cell-key {
    font-size: 1rem;
    font-weight: 800;
    padding-top: 0.1rem;
  }
  table.stack td.cell-key::before { content: none; }
  table.stack td.cell-action {
    justify-content: flex-end;
    padding-top: 0.3rem;
  }
  table.stack td.cell-action::before { content: none; }
  table.stack td.cell-action .btn { width: 100%; min-height: 34px; padding: 0.3rem 0.6rem; font-size: 0.85rem; }
  table.stack td:empty { display: none; }

  /* Card list (phone): each row is a structured card —
       line 1: room no. + status pill + action button (grouped right)
       line 2: tenant / name (full width)
       line 3: the figures, each a self-contained "label value" chunk that wraps
               as a unit so a label never separates from its value. */
  table.cardlist { min-width: 0; }
  table.cardlist thead {
    position: absolute; width: 1px; height: 1px; margin: -1px;
    padding: 0; overflow: hidden; clip: rect(0 0 0 0);
  }
  table.cardlist tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem 0.85rem;
    padding: 0.7rem 0;
    border-bottom: 1px solid var(--line);
  }
  table.cardlist tr:last-child { border-bottom: none; }
  table.cardlist td {
    order: 5;
    display: inline-flex;
    align-items: baseline;
    gap: 0.3rem;
    border: none;
    padding: 0;
    white-space: nowrap;
  }
  table.cardlist td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 600;
  }
  table.cardlist td.cell-key { order: 1; font-size: 1.1rem; font-weight: 800; }
  table.cardlist td.cell-key::before { content: none; }
  /* status pill: push it (and the action after it) to the right of line 1 */
  table.cardlist td:has(.pill) { order: 2; margin-left: auto; }
  table.cardlist td:has(.pill)::before { content: none; }
  table.cardlist td.cell-action { order: 3; margin-left: auto; }
  table.cardlist td.cell-action::before { content: none; }
  table.cardlist td.cell-action .btn { min-height: 34px; padding: 0.25rem 0.85rem; font-size: 0.85rem; }
  table.cardlist td.cell-name {
    order: 4;
    flex: 1 1 100%;
    font-weight: 600;
    white-space: normal;
  }
  table.cardlist td.cell-name::before { content: none; }
  table.cardlist td.num { white-space: nowrap; }
  /* empty-state message row spans normally */
  table.cardlist tr:has(td[colspan]) { display: block; }
  table.cardlist tr:has(td[colspan]) td { display: block; white-space: normal; }
  table.cardlist tr:has(td[colspan]) td::before { content: none; }
}

/* =========================================================================
   FORMS
   ========================================================================= */
input, select, textarea {
  font: inherit;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  background: #fff;
  color: var(--ink);
  max-width: 100%; /* never let an input overflow its container */
  min-width: 0;    /* override iOS' UA min-width on type=date/select so it can shrink */
  min-height: 38px; /* iOS auto-zooms inputs below 16px — body stays 16px */
}
input:focus, select:focus, textarea:focus {
  outline: 3px solid color-mix(in srgb, var(--brand) 30%, transparent);
  outline-offset: 0;
  border-color: var(--brand);
}
label { display: block; font-size: 0.76rem; color: var(--muted); font-weight: 600; margin-bottom: 0.15rem; }

.field { margin-bottom: 0.55rem; }
.row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1 1 130px; min-width: 0; margin-bottom: 0.3rem; }
.toolbar { display: flex; gap: 0.55rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 0.85rem; }
.toolbar .field { margin-bottom: 0; }

/* =========================================================================
   BUTTONS
   ========================================================================= */
button, .btn {
  font: inherit;
  background: var(--brand);
  color: #fff;
  border: 1px solid transparent;
  cursor: pointer;
  border-radius: var(--r-sm);
  padding: 0.45rem 0.9rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  line-height: 1.1;
  box-shadow: 0 1px 1px rgba(16, 32, 28, 0.08);
  transition: background 0.12s ease, transform 0.04s ease;
}
button:hover, .btn:hover { background: var(--brand-dark); text-decoration: none; }
button:active, .btn:active { transform: translateY(1px); }
.btn.secondary, button.secondary { background: #eef3f6; color: var(--ink); border-color: var(--line-strong); box-shadow: none; }
.btn.secondary:hover, button.secondary:hover { background: #e2e9ee; }
.btn.danger, button.danger { background: var(--danger); }
.btn.danger:hover, button.danger:hover { background: #b91c1c; }
.btn.small, button.small { padding: 0.3rem 0.7rem; font-size: 0.84rem; min-height: 34px; }

/* =========================================================================
   PILLS
   ========================================================================= */
.pill {
  display: inline-block;
  padding: 0.12rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
  letter-spacing: 0.01em;
}
.pill.unpaid { background: var(--danger-soft); color: var(--danger); }
.pill.partial { background: var(--warn-soft); color: var(--warn); }
.pill.paid { background: var(--ok-soft); color: var(--ok); }
.pill.occupied { background: var(--ok-soft); color: var(--ok); }
.pill.vacant { background: #e6edf2; color: var(--muted); }

/* =========================================================================
   MISC
   ========================================================================= */
.flash {
  padding: 0.6rem 0.85rem;
  border-radius: var(--r-sm);
  margin-bottom: 0.7rem;
  font-weight: 700;
  font-size: 0.9rem;
  border: 1px solid transparent;
}
.flash.ok { background: var(--ok-soft); color: var(--ok); border-color: #bbf7d0; }
.flash.info { background: var(--info-soft); color: #075985; border-color: #bae6fd; }
.muted { color: var(--muted); }
.right { text-align: right; }

.form-actions { margin: 0.5rem 0 1rem; display: flex; gap: 0.5rem; }
.form-actions button { flex: 1; min-height: 46px; font-size: 1rem; }
@media (min-width: 600px) {
  .form-actions { justify-content: flex-end; }
  .form-actions button { flex: 0 1 auto; min-width: 14rem; }
}
.inline { display: inline; }

/* =========================================================================
   METER RECORDING
   ========================================================================= */
.meter-seg {
  display: inline-flex;
  background: #e3e9ef;
  border-radius: var(--r);
  padding: 3px;
  gap: 3px;
  margin-bottom: 0.8rem;
}
.meter-seg .seg-btn {
  background: transparent;
  color: var(--ink);
  border: none;
  min-height: 42px;
  padding: 0.4rem 1.6rem;
  border-radius: 10px;
  font-weight: 700;
  box-shadow: none;
}
.meter-seg .seg-btn:hover { background: rgba(255, 255, 255, 0.55); }
.meter-seg .seg-btn.is-active,
.meter-seg .seg-btn.is-active:hover {
  background: #fff;
  color: var(--brand);
  box-shadow: var(--shadow);
}

.meter-rate {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.5rem 0.85rem;
  background: #f6f9fb;
  border: 1px solid var(--line);
  border-left-width: 4px;
  border-radius: var(--r) var(--r) 0 0;
  font-size: 0.9rem;
}
.meter-rate.water { border-left-color: var(--water); }
.meter-rate.elec { border-left-color: var(--elec); }
.meter-rate .rate-label { font-weight: 800; }
.meter-rate .rate-value { color: var(--muted); }

.meter-list {
  background: var(--card);
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 var(--r) var(--r);
  margin-bottom: 1rem;
  overflow: hidden;
}

.meter-row {
  display: grid;
  grid-template-columns: minmax(3.2rem, max-content) 4rem 1fr 4.8rem;
  grid-template-areas:
    "room prev cur result"
    "roll roll roll roll"
    "warn warn warn warn";
  column-gap: 0.4rem;
  row-gap: 0.15rem;
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--line);
  align-items: center;
  font-variant-numeric: tabular-nums;
}
.meter-row:last-child { border-bottom: none; }
.meter-row.is-vacant { opacity: 0.55; }
.meter-row .cell.room { grid-area: room; font-weight: 800; }
.meter-row .cell.prev { grid-area: prev; text-align: right; color: var(--muted); }
.meter-row .cell.cur { grid-area: cur; }
.meter-row .cell.cur input {
  width: 100%;
  text-align: right;
  font-size: 1rem;
  padding: 0.3rem 0.55rem;
  min-height: 40px;
}
.meter-row .cell.cur .rec {
  display: block;
  text-align: right;
  font-size: 0.68rem;
  margin-top: 2px;
}
.meter-row .cell.result { grid-area: result; text-align: right; line-height: 1.15; }
.meter-row .cell.result .u { display: block; font-weight: 800; font-size: 0.85rem; white-space: nowrap; }
.meter-row .cell.result .c { display: block; color: var(--muted); font-size: 0.7rem; }
.meter-row .cell.roll {
  grid-area: roll;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.72rem;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  margin: 0;
  justify-self: end;
}
.meter-row .cell.roll input {
  width: 16px;
  height: 16px;
  min-height: 0;
  margin: 0;
  padding: 0;
  border-radius: 3px;
  accent-color: var(--brand);
  cursor: pointer;
}
.meter-row .cell.warn { grid-area: warn; margin: 0; }

.meter-row.meter-row-head {
  grid-template-areas: "room prev cur result";
  background: #f6f9fb;
  padding: 0.35rem 0.6rem;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.meter-row.meter-row-head .cell { color: var(--muted); }
.meter-row.meter-row-head .cell.cur,
.meter-row.meter-row-head .cell.result { text-align: right; }

.sr {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.meter-progress {
  position: sticky;
  top: var(--topbar-h);
  z-index: 10;
  background: var(--brand-soft);
  color: var(--brand-dark);
  border: 1px solid #a7e3d8;
  border-radius: var(--r);
  padding: 0.55rem 0.9rem;
  margin-bottom: 0.9rem;
  font-size: 0.95rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.meter-warn { font-size: 0.78rem; font-weight: 600; margin: 0.25rem 0 0; }
.meter-warn.low { color: var(--danger); }
.meter-warn.high { color: var(--warn); }
input.input-warn { border-color: var(--danger); background: #fef2f2; }
input.input-high { border-color: var(--warn); background: #fffbeb; }

/* =========================================================================
   SAVE BAR
   On phones it's a fixed bar pinned just ABOVE the bottom tab bar, so the save
   action and the nav tabs are both always reachable.  On desktop (no tab bar)
   it falls back to a sticky bar inside the content column (see >=768px block).
   ========================================================================= */
.save-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--tabbar-h);
  z-index: 25;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  background: #ffffff;
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 12px rgba(16, 32, 28, 0.09);
  padding: 0.55rem 0.9rem;
  padding-left: max(0.9rem, env(safe-area-inset-left));
  padding-right: max(0.9rem, env(safe-area-inset-right));
}
.save-bar-actions { display: flex; gap: 0.5rem; }
.save-bar .btn { min-height: 44px; }

/* =========================================================================
   TABLET / DESKTOP (>=768px): sidebar layout
   ========================================================================= */
@media (min-width: 768px) {
  .app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    align-items: start;
  }
  .topbar, .tabbar { display: none; }

  .sidebar {
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    background: var(--brand-grad);
    color: var(--brand-ink);
    padding: 0.9rem 0.7rem;
    gap: 0.25rem;
    box-shadow: var(--shadow-lg);
  }
  .sidebar .brand {
    color: var(--brand-ink);
    font-weight: 800;
    font-size: 1.2rem;
    letter-spacing: -0.01em;
    padding: 0.4rem 0.6rem 0.9rem;
    display: flex;
    align-items: center;
    gap: 0.2rem;
  }
  .sidebar .brand:hover { text-decoration: none; }
  .sidebar-switch { width: 100%; margin: 0 0 0.6rem; padding: 0 0.6rem; }
  .sidebar-switch select { max-width: 100%; width: 100%; font-size: 0.9rem; }
  .sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.6rem 0.7rem;
    border-radius: 10px;
    color: rgba(255, 255, 255, 0.92);
    font-weight: 600;
    font-size: 0.95rem;
  }
  .sidebar-link:hover { background: rgba(255, 255, 255, 0.14); text-decoration: none; }
  .sidebar-link.is-active {
    background: rgba(255, 255, 255, 0.96);
    color: var(--brand-dark);
    box-shadow: var(--shadow);
  }
  .sidebar-sep { flex: 1 1 auto; }
  .sidebar-link.danger { color: #ffe1e1; }
  .sidebar-link.danger:hover { background: rgba(220, 38, 38, 0.35); }

  .container {
    margin: 1.1rem auto 2rem;
    padding-bottom: 2rem;
  }
  h1 { font-size: 1.5rem; }
  /* no tab bar on desktop — sticky save bar inside the content column */
  .save-bar {
    position: sticky;
    left: auto;
    right: auto;
    bottom: 0;
    margin: 0.5rem -0.9rem 0; /* break out to the container's padding edges */
    padding-bottom: max(0.55rem, env(safe-area-inset-bottom));
    border-radius: 0 0 var(--r) var(--r);
  }
}

/* =========================================================================
   LOGIN (standalone page, no .app shell)
   ========================================================================= */
.login { max-width: 380px; margin-top: 10vh; padding-bottom: 2rem; }
.login-card { padding: 0; overflow: hidden; }
.login-head {
  background: var(--brand-grad);
  color: var(--brand-ink);
  padding: 1.5rem 1.3rem;
}
.login-head .lh-title { font-size: 1.4rem; font-weight: 800; letter-spacing: -0.01em; }
.login-head .lh-sub { opacity: 0.9; font-size: 0.92rem; margin-top: 0.25rem; }
.login-body { padding: 1.2rem 1.3rem 1.4rem; }
.flash.err { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }
