/* Billing — base styles.
 * Dark-first via :root vars; light theme via prefers-color-scheme media query.
 * Phase 3 batch 1: auth screen + app shell. Page-specific styles land later.
 */

:root {
  --bg: #0c0e14;
  --surface: #161922;
  --surface-2: #1d2230;
  --border: #2a2f3d;
  --text: #e8ecf2;
  --text-muted: #8a92a6;
  --accent: #00e5a0;
  --accent-2: #4d9fff;
  --danger: #ff5c7a;
  --warn: #ffd166;
  --ok: #00e5a0;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  --header-h: 56px;
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-2: #f0f3f8;
    --border: #d8dde6;
    --text: #1a1d24;
    --text-muted: #6b7280;
    --accent: #00a878;
    --accent-2: #2d7fe8;
    --shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }

/* Make the HTML `hidden` attribute beat element-class display rules. Without
   this, .auth-screen { display: flex } overrides [hidden]'s default display:none
   and the auth screen paints on top of the signed-in shell after sign-in. */
[hidden] { display: none !important; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  scroll-behavior: smooth;
}

h1, h2, h3 { margin: 0 0 var(--s-3); font-weight: 600; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
p { margin: 0 0 var(--s-3); }
button { font: inherit; color: inherit; }

/* ---------- Loading overlay ---------- */
.loading {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 9999;
  gap: var(--s-3);
  color: var(--text-muted);
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Auth screen ---------- */
.auth-screen {
  position: fixed; inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--s-4);
  background: var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--s-6) var(--s-5);
  text-align: center;
  box-shadow: var(--shadow);
}
.auth-logo { display: flex; justify-content: center; margin-bottom: var(--s-3); }
.auth-title { font-size: 1.6rem; margin-bottom: var(--s-2); }
.auth-subtitle { color: var(--text-muted); margin-bottom: var(--s-5); }
.auth-btn {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
}
.auth-version {
  margin-top: var(--s-5);
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ---------- Buttons ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: var(--surface); border-color: var(--accent-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #051a13;
  border-color: var(--accent);
  font-weight: 600;
}
.btn-primary:hover { background: #00cf90; border-color: #00cf90; }

.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }

/* Compact icon-only button — used for header chrome. */
.icon-btn {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 8px;
  border-radius: var(--radius);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- App shell ---------- */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  position: sticky; top: 0;
  display: flex;
  align-items: center;
  gap: var(--s-2);
  padding: 0 var(--s-3);
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 50;
  padding-top: env(safe-area-inset-top, 0px);
  min-height: calc(var(--header-h) + env(safe-area-inset-top, 0px));
}
.app-title { font-size: 1.05rem; margin: 0; }
.app-header-spacer { flex: 1; }

/* Sync status badge — three states driven by sync.js classes. */
.sync-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.sync-badge .sync-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-muted);
}
.sync-badge--synced .sync-dot { background: var(--ok); }
.sync-badge--pending .sync-dot {
  background: var(--warn);
  animation: pulse 1.4s ease-in-out infinite;
}
.sync-badge--offline .sync-dot { background: var(--text-muted); }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
}
.user-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent-2);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.8rem;
  overflow: hidden;
}
.user-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.user-name {
  font-size: 0.85rem;
  color: var(--text-muted);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- App body (sidebar + page area) ---------- */
.app-body {
  display: flex;
  flex: 1;
  position: relative;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: var(--s-3) var(--s-2);
  position: sticky;
  top: var(--header-h);
  align-self: flex-start;
  height: calc(100vh - var(--header-h));
  overflow-y: auto;
}
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  appearance: none;
  display: flex;
  align-items: center;
  gap: var(--s-2);
  padding: 10px 12px;
  background: transparent;
  border: none;
  color: var(--text);
  border-radius: var(--radius);
  text-align: left;
  font-size: 0.95rem;
  cursor: pointer;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item[aria-current="page"] {
  background: var(--surface-2);
  color: var(--accent);
  font-weight: 600;
}
.nav-emoji { font-size: 1.05rem; width: 1.4em; text-align: center; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 40;
}

.page-area {
  flex: 1;
  padding: var(--s-5) var(--s-4);
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
  min-width: 0;
}

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

.empty-state {
  margin-top: 6vh;
  text-align: center;
  color: var(--text-muted);
}
.empty-state-emoji {
  font-size: 3rem;
  margin: 0 0 var(--s-3);
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--s-4);
  z-index: 100;
}
.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--s-5);
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}
.modal-title { margin: 0 0 var(--s-3); }
.modal-body { margin: 0 0 var(--s-4); color: var(--text-muted); }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--s-2);
  flex-wrap: wrap;
}
.field-section-title {
  margin: var(--s-4) 0 var(--s-2);
  color: var(--accent-2);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}

/* Hide the mobile menu button on desktop — sidebar is always visible. */
.mob-menu-btn { display: none; }

/* ---------- Contractor switcher ---------- */
.contractor-switcher {
  position: relative;
  min-width: 0;
}
.switcher-btn {
  appearance: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.92rem;
  max-width: 240px;
  min-width: 0;
}
.switcher-btn:hover { background: var(--surface); border-color: var(--accent-2); }
.switcher-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.switcher-caret { font-size: 0.7rem; color: var(--text-muted); flex-shrink: 0; }

.dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  min-width: 220px;
  max-width: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 70;
  padding: 4px;
}
.dropdown-list { list-style: none; margin: 0; padding: 0; }
.dropdown-row {
  appearance: none;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  background: transparent;
  border: none;
  color: var(--text);
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  font-size: 0.9rem;
}
.dropdown-row:hover { background: var(--surface-2); }
.dropdown-row--active { color: var(--accent); font-weight: 600; }
.dropdown-row--new {
  margin-top: 4px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
  color: var(--accent-2);
}

/* ---------- Page header + card list ---------- */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.page-header h1 { margin: 0; }

.card-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}
.card {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.card--active { border-color: var(--accent); }
.card--clickable { cursor: pointer; transition: border-color 0.15s, background 0.15s; }
.card--clickable:hover { border-color: var(--accent-2); background: var(--surface-2); }

/* Bill grand total pill on dashboard cards */
.bill-card-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--s-2);
}
.bill-total-pill {
  background: var(--accent);
  color: #051a13;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 999px;
  font-family: ui-monospace, "Courier New", monospace;
  font-size: 0.9rem;
}

/* Contractor branding image pickers */
.image-picker {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.image-preview {
  width: 64px;
  height: 64px;
  object-fit: contain;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  flex-shrink: 0;
}
.image-preview[src=""] { display: none; }
.image-picker-controls { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--s-2); align-items: flex-start; }
.image-picker-controls input[type="file"] { font-size: 0.85rem; max-width: 100%; }

/* Dashboard bill cards: action buttons row at the bottom (full-text labels) */
.bill-card-item { align-items: stretch; flex-direction: column; }
.bill-card-actions {
  display: flex;
  gap: var(--s-2);
  flex-shrink: 0;
  margin-top: var(--s-2);
  padding-top: var(--s-2);
  border-top: 1px solid var(--border);
}
.bill-card-actions .btn { flex: 1; }
@media (max-width: 600px) {
  .bill-card-actions { flex-direction: column; }
}
.card-main { flex: 1; min-width: 0; }
.card-title {
  font-weight: 600;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: var(--s-2);
}
.card-line { font-size: 0.88rem; color: var(--text); }
.text-muted { color: var(--text-muted); }
.card-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  flex-shrink: 0;
}
.btn-sm { padding: 4px 10px; font-size: 0.85rem; }
.badge {
  background: var(--accent);
  color: #051a13;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---------- Search input (universal across master + dashboard pages) ---------- */
.search-row {
  margin-bottom: var(--s-3);
}
.search-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: var(--radius);
  font: inherit;
  font-size: 0.95rem;
}
.search-input:focus {
  outline: none;
  border-color: var(--accent-2);
}
.search-input::placeholder { color: var(--text-muted); }

/* ---------- Picker row (e.g. customer picker on Sites page) ---------- */
.picker-row {
  margin-bottom: var(--s-3);
}
.picker-select {
  width: 100%;
  max-width: 360px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: var(--radius);
  font: inherit;
}
.picker-select:focus { outline: none; border-color: var(--accent-2); }

/* ---------- Status filter chips + bill cards ---------- */
.status-filter {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
  margin-bottom: var(--s-3);
}
.chip {
  appearance: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip:hover { color: var(--text); border-color: var(--accent-2); }
.chip--active {
  background: var(--accent);
  color: #051a13;
  border-color: var(--accent);
  font-weight: 600;
}

.bill-number {
  font-family: ui-monospace, "Courier New", monospace;
  font-weight: 700;
  font-size: 1rem;
}
.bill-number-preview {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  font-family: ui-monospace, "Courier New", monospace;
  font-size: 1rem;
  color: var(--accent);
}
.mono { font-family: ui-monospace, "Courier New", monospace; }

/* ---------- Bill-edit page ---------- */
.bill-edit-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--s-3);
}
.bill-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--s-4);
  margin-bottom: var(--s-4);
}
.bill-card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  margin-bottom: var(--s-3);
}
.bill-card-row .bill-number { font-size: 1.3rem; }
.bill-meta {
  display: grid;
  gap: var(--s-2);
  font-size: 0.95rem;
}
.bill-meta strong { color: var(--text-muted); font-weight: 500; }

.bill-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--s-4);
  margin-bottom: var(--s-3);
}
.bill-section-title {
  margin: 0 0 var(--s-3);
  font-size: 1rem;
  color: var(--text-muted);
  font-weight: 500;
}
.status-actions {
  display: flex;
  gap: var(--s-2);
  flex-wrap: wrap;
}
.hint {
  margin: var(--s-3) 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.danger-section { border-color: var(--danger); }

/* ---------- Items section / Abstract table ---------- */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  margin-bottom: var(--s-3);
}
.section-head .bill-section-title { margin: 0; }

.abstract-table-wrap {
  overflow-x: auto;
  margin: 0 calc(-1 * var(--s-4));
}
.abstract-table {
  width: 100%;
  min-width: 520px;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.abstract-table th, .abstract-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}
.abstract-table th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.abstract-table .th-sr      { width: 36px; text-align: center; color: var(--text-muted); }
.abstract-table .th-unit    { width: 70px; }
.abstract-table .th-num     { text-align: right; white-space: nowrap; }
.abstract-table .th-actions { width: 64px; text-align: right; }
.abstract-table .text-right { text-align: right; }
.grand-total-row td {
  font-weight: 700;
  color: var(--accent);
  border-top: 2px solid var(--accent);
  border-bottom: none;
}

/* ---------- Sections tree ---------- */
.active-section-banner {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  padding: var(--s-2) var(--s-3);
  background: var(--surface-2);
  border: 1px dashed var(--accent);
  border-radius: var(--radius);
  margin-bottom: var(--s-3);
}
.section-tree, .section-tree-children {
  list-style: none;
  padding: 0;
  margin: 0;
}
.section-tree-children { margin-left: var(--s-4); margin-top: 4px; }
.section-tree-node { margin-bottom: 4px; }
.section-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}
.section-row--active {
  border-color: var(--accent);
  background: var(--surface);
}
.section-pick {
  appearance: none;
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  flex: 1;
  min-width: 0;
}
.section-actions { display: flex; gap: 2px; flex-shrink: 0; }

/* ---------- Entries ---------- */
.entries-actions { display: flex; gap: var(--s-2); align-items: center; flex-wrap: wrap; }
.entries-actions-stack {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  margin-bottom: var(--s-3);
}
.entries-action-row {
  display: flex;
  gap: var(--s-2);
}
.entries-action-row .btn { flex: 1; }

.seg-toggle { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.seg-toggle--wide { display: flex; width: 100%; }
.seg-toggle--wide button { flex: 1; }
.seg-toggle button {
  appearance: none; background: var(--surface-2); border: none; color: var(--text-muted);
  padding: 8px 12px; cursor: pointer; font-size: 0.9rem; min-height: 40px;
}
.seg-toggle button.on { background: var(--accent); color: #051a13; font-weight: 600; }

.entry-group { margin-bottom: var(--s-3); }
.entry-group-title {
  margin: 0 0 var(--s-2);
  font-size: 0.95rem;
  color: var(--accent-2);
  display: flex;
  align-items: center;
  gap: var(--s-2);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  user-select: none;
}
.entry-group-title:hover { background: var(--surface); }
.entry-group-title .caret {
  display: inline-block;
  width: 12px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  transition: transform 0.15s ease-out;
}
.entry-group-title .entry-group-label { flex: 1; min-width: 0; }
.entry-group-title .entry-group-count {
  background: var(--accent);
  color: #051a13;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.entry-group--collapsed .entry-group-body { display: none; }
.entry-group-body { margin-top: var(--s-2); }
.entry-group--depth-1 { margin-left: var(--s-3); }
.entry-group--depth-2 { margin-left: var(--s-3); }
.entry-group--depth-1 > .entry-group-title { background: var(--surface); border-color: var(--border); }
.entry-group--depth-2 > .entry-group-title {
  background: transparent;
  border-color: var(--border);
  font-size: 0.85rem;
}
.entry-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.entry-row {
  display: flex; align-items: flex-start; gap: var(--s-2);
  padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.entry-row--deduction { border-left: 3px solid var(--warn); }
.entry-row-main { flex: 1; min-width: 0; }
.entry-row-numbers { font-size: 0.82rem; margin-top: 2px; }
.entry-row-actions { display: flex; gap: 2px; flex-shrink: 0; }
.entry-section-chip {
  display: inline-block; margin-left: var(--s-2);
  font-size: 0.72rem; padding: 1px 6px; border-radius: 4px;
  background: var(--surface); color: var(--text-muted);
}
.entry-qty { font-weight: 600; }
.entry-qty.negative, .negative { color: var(--danger); }
.badge-warn { background: var(--warn); color: #051a13; padding: 1px 6px; border-radius: 4px; font-size: 0.7rem; font-weight: 700; }

/* ---------- Reorganize ---------- */
.reorg-actions { display: flex; gap: var(--s-2); align-items: center; flex-wrap: wrap; margin-bottom: var(--s-3); }
.reorg-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.reorg-row {
  display: flex; align-items: center; gap: var(--s-3);
  padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); cursor: pointer;
}
.reorg-row--selected { border-color: var(--accent); background: var(--surface); }
.reorg-row-main { flex: 1; min-width: 0; }

/* ---------- STT FAB ---------- */
.stt-fab {
  position: fixed; right: 16px; bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--surface); color: var(--text);
  font-size: 24px; cursor: pointer; box-shadow: var(--shadow);
  z-index: 80; display: flex; align-items: center; justify-content: center;
}
.stt-fab.on { background: var(--danger); color: #fff; animation: pulse 1.4s ease-in-out infinite; }

/* Hide the global FAB whenever ANY modal is open — the modal that needs
   voice input exposes its own mic button (entry modal does). Prevents
   the FAB sitting behind the modal where the user can't reach it. */
body:has(.modal-overlay:not([hidden])) .stt-fab { display: none; }

/* ---------- Entry modal customisations ---------- */
.entry-modal-card { display: flex; flex-direction: column; }
.entry-modal-top {
  position: sticky; top: 0;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  padding-bottom: var(--s-3);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--s-3);
  z-index: 6;
}
.entry-modal-top-row {
  display: flex;
  align-items: center;
  gap: var(--s-2);
}
.entry-modal-top-row-1 .qty-pill { flex: 1; min-width: 0; justify-content: space-between; }
.entry-modal-top-row-2 .entry-modal-title { flex: 1; min-width: 0; margin: 0; font-size: 1.05rem; }
.entry-mic-btn {
  width: 40px; height: 40px; font-size: 20px;
  background: var(--surface-2);
}
.entry-mic-btn.on { background: var(--danger); color: #fff; animation: pulse 1.4s ease-in-out infinite; }
.entry-modal-body { flex: 1; min-height: 0; overflow-y: auto; padding-bottom: var(--s-4); }

/* Qty pill — always visible in sticky top bar */
.qty-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  padding: 8px 14px;
  background: var(--surface-2);
  border: 2px solid var(--accent);
  border-radius: 999px;
}
.qty-pill-label { font-size: 0.75rem; color: var(--text-muted); font-weight: 500; }
.qty-pill-value {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
  font-family: ui-monospace, "Courier New", monospace;
  white-space: nowrap;
}
.qty-pill-value.negative { color: var(--danger); }
.qty-pill:has(.qty-pill-value.negative) { border-color: var(--danger); }

/* Picker with inline + button — used for Section / Item in entry modal */
.picker-with-add {
  display: flex;
  gap: var(--s-2);
  align-items: stretch;
}
.picker-with-add select { flex: 1; min-width: 0; }
.btn-add-inline {
  appearance: none;
  background: var(--accent);
  color: #051a13;
  border: none;
  border-radius: var(--radius);
  font-size: 1.4rem;
  font-weight: 700;
  cursor: pointer;
  min-width: 44px;
  padding: 0 var(--s-2);
  flex-shrink: 0;
}
.btn-add-inline:hover { background: #00cf90; }

/* Toggle switch (used for voice-only mode) */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  cursor: pointer;
  user-select: none;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.toggle-switch input { display: none; }
.toggle-track {
  width: 38px; height: 22px;
  background: var(--border);
  border-radius: 999px;
  position: relative;
  transition: background 0.15s;
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s;
}
.toggle-switch input:checked + .toggle-track { background: var(--accent); }
.toggle-switch input:checked + .toggle-track::after { transform: translateX(16px); }

/* Deduction row — checkbox + label aligned, big checkbox */
.deduction-row {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: var(--s-3);
  cursor: pointer;
}
.big-checkbox {
  width: 26px;
  height: 26px;
  cursor: pointer;
  accent-color: var(--warn);
  flex-shrink: 0;
}
.deduction-label { font-size: 0.95rem; flex: 1; }

/* Big qty preview block */
.qty-preview-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-4);
  background: var(--surface-2);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  margin-bottom: var(--s-3);
}
.qty-preview-label { font-size: 0.85rem; color: var(--text-muted); font-weight: 500; }
.qty-preview-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
  font-family: ui-monospace, "Courier New", monospace;
}
.qty-preview-value.negative { color: var(--danger); }

/* ---------- Offline + update banners ---------- */
.offline-banner {
  position: fixed; top: 0; left: 0; right: 0;
  background: var(--warn); color: #051a13;
  padding: 8px 16px;
  font-size: 0.88rem; font-weight: 500; text-align: center;
  z-index: 90;
}
body:has(.offline-banner) .app-header { top: 38px; }
.update-banner {
  position: fixed;
  left: 50%; bottom: calc(env(safe-area-inset-bottom, 0px) + 80px);
  transform: translateX(-50%);
  background: var(--accent-2); color: #fff;
  padding: 10px 14px; border-radius: var(--radius);
  display: flex; align-items: center; gap: var(--s-3);
  box-shadow: var(--shadow); z-index: 110;
  font-size: 0.9rem;
}

.status-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 4px;
}
.status-badge--draft     { background: var(--surface-2); color: var(--text-muted); }
.status-badge--finalized { background: var(--accent-2);  color: #fff; }
.status-badge--delivered { background: var(--accent);    color: #051a13; }

/* ---------- Modal form fields ---------- */
.field {
  display: block;
  margin-bottom: var(--s-3);
}
.field-label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.field input, .field select, .field textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: var(--radius);
  font: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent-2);
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + var(--s-4));
  transform: translateX(-50%);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 0.9rem;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: var(--s-3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
#toast.show {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}
#toast.error { border-color: var(--danger); }
#toast.success { border-color: var(--accent); }
.undo-btn {
  appearance: none;
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
}

/* ---------- Mobile tweaks (360–415px design targets) ---------- */
@media (max-width: 600px) {
  /* Header chrome */
  .user-name { display: none; }
  .auth-card { padding: var(--s-5) var(--s-4); }
  .sync-label { display: none; }
  .sync-badge { padding: 4px 8px; }
  .mob-menu-btn { display: inline-flex; }
  .switcher-btn { max-width: 140px; }

  /* Stack page headers + cards */
  .page-header { flex-direction: column; align-items: stretch; gap: var(--s-2); }
  .page-header h1 { font-size: 1.15rem; }
  .card { flex-direction: column; align-items: stretch; }
  .card-actions { justify-content: flex-end; }

  /* Sidebar drawer */
  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    height: calc(100vh - var(--header-h));
    width: 80vw;
    max-width: 280px;
    transform: translateX(-100%);
    transition: transform 0.2s ease-out;
    z-index: 60;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.open { display: block; }
  .page-area { padding: var(--s-3) var(--s-3) calc(env(safe-area-inset-bottom, 0px) + 80px); }

  /* Bigger touch targets (Apple HIG / Material recommend ≥44px) */
  .btn { min-height: 44px; padding-top: 10px; padding-bottom: 10px; }
  .btn-sm { min-height: 36px; padding-top: 6px; padding-bottom: 6px; }
  .icon-btn { min-width: 40px; min-height: 40px; justify-content: center; }
  .nav-item { padding: 14px 14px; font-size: 1rem; }
  .chip { min-height: 36px; padding: 6px 14px; }

  /* Bill section + cards tighter on phones */
  .bill-section { padding: var(--s-3); }
  .bill-card { padding: var(--s-3); }
  .bill-card-row { flex-wrap: wrap; gap: var(--s-2); }
  .bill-card-row .bill-number { font-size: 1.1rem; }
  .bill-meta { font-size: 0.9rem; }

  /* Abstract table — drop the REMARK column (mostly empty in practice)
     to give the meaningful columns more breathing room. */
  .abstract-table { min-width: 420px; font-size: 0.85rem; }
  .abstract-table th, .abstract-table td { padding: 6px 6px; }
  .abstract-table th:nth-child(7), .abstract-table td:nth-child(7) { display: none; }

  /* Entries panel — make action row tappable */
  .entries-actions { width: 100%; }
  .entries-actions > * { flex: 1; min-width: 0; }
  .seg-toggle { flex: 1; }
  .seg-toggle button { flex: 1; }

  /* Entry row tighter */
  .entry-row { padding: 6px 8px; }
  .entry-row-numbers { font-size: 0.78rem; }

  /* Section tree — reduce nested indent on phones */
  .section-tree-children { margin-left: var(--s-3); }
  .section-row { padding: 8px 10px; min-height: 44px; }

  /* Modals: full-screen bottom-sheet feel on phones */
  .modal-overlay { padding: 0; align-items: stretch; }
  .modal-card {
    max-width: none;
    width: 100%;
    max-height: 100vh;
    border-radius: 0;
    overflow-y: auto;
    padding: var(--s-4);
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--s-4));
  }
  .modal-actions {
    position: sticky;
    bottom: 0;
    background: var(--surface);
    margin: var(--s-4) calc(-1 * var(--s-4)) calc(-1 * var(--s-4));
    padding: var(--s-3) var(--s-4) calc(env(safe-area-inset-bottom, 0px) + var(--s-3));
    border-top: 1px solid var(--border);
    z-index: 5;
  }
  .modal-actions .btn { flex: 1; }

  /* Form fields — bigger inputs for phone typing */
  .field input, .field select, .field textarea, .picker-select {
    padding: 12px 14px;
    font-size: 16px;        /* iOS won't auto-zoom on focus when ≥16px */
  }
  .field-label { font-size: 0.85rem; }

  /* Reorganize page tighter */
  .reorg-row { padding: 10px 10px; min-height: 56px; }

  /* STT FAB above iOS home indicator */
  .stt-fab {
    width: 52px; height: 52px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
    right: 14px;
  }

  /* Toast above FAB so they don't overlap */
  #toast {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 88px);
    left: var(--s-3);
    right: var(--s-3);
    transform: none;
    max-width: none;
    width: calc(100vw - 2 * var(--s-3));
  }
  #toast.show { transform: none; }

  /* Update + offline banners */
  .offline-banner { font-size: 0.82rem; padding: 6px 10px; }
  .update-banner { left: var(--s-3); right: var(--s-3); transform: none; bottom: calc(env(safe-area-inset-bottom, 0px) + 100px); }
}

/* ---------- Very narrow phones (≤380px — older Android etc.) ---------- */
@media (max-width: 380px) {
  .app-title { display: none; }
  .switcher-btn { max-width: 110px; font-size: 0.85rem; padding: 4px 8px; }
  .auth-card { padding: var(--s-4) var(--s-3); }
  .auth-title { font-size: 1.4rem; }
  .abstract-table { min-width: 360px; font-size: 0.78rem; }
  .abstract-table th, .abstract-table td { padding: 5px 4px; }
}

/* ---------- Landscape phones — modals shouldn't take the whole screen ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .modal-card { max-height: 100vh; padding: var(--s-3) var(--s-4); }
  .modal-actions { position: static; padding: var(--s-2) 0 0; }
  .field { margin-bottom: var(--s-2); }
}
