/* =====================================================================
   Chicks Management System - shared styles
   Desktop first, works down to phone width. Colours are defined once
   here as variables; use them rather than new colour values.
   ===================================================================== */

:root {
  --color-primary: #2f6f4e;
  --color-primary-dark: #245a3e;
  --color-primary-soft: #e6f0ea;
  --color-accent: #d98e04;
  --color-bg: #f4f6f3;
  --color-surface: #ffffff;
  --color-text: #1f2a24;
  --color-muted: #5f6b64;
  --color-border: #dde3de;
  --color-danger: #b3261e;
  --color-danger-soft: #fdecea;
  --color-warning: #8a5a00;
  --color-warning-soft: #fff4e0;
  --color-info-soft: #e8f1fb;
  --color-info: #1d4f86;

  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 1px 6px rgba(0, 0, 0, 0.04);
  --sidebar-width: 230px;
  --topbar-height: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
}

h1, h2, h3 { line-height: 1.25; margin: 0 0 0.5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }

a { color: var(--color-primary); }

[hidden] { display: none !important; }

/* Visible to screen readers only. */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 40px;
  padding: 0.5rem 1rem;
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn:hover { background: var(--color-bg); }
.btn:focus-visible, .input:focus-visible { outline: 3px solid rgba(47, 111, 78, 0.35); outline-offset: 1px; }
.btn:disabled { opacity: 0.65; cursor: wait; }
.btn-primary { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; color: inherit; padding: 0.25rem 0.5rem; min-height: 0; font-weight: 500; }
.btn-link:hover { background: rgba(255, 255, 255, 0.12); }

/* ---------- Forms ---------- */
.field { margin-bottom: 1rem; }
.field label { display: block; font-weight: 600; margin-bottom: 0.3rem; }
.input {
  width: 100%;
  min-height: 42px;
  padding: 0.5rem 0.75rem;
  font: inherit;
  color: inherit;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}
.input[aria-invalid="true"] { border-color: var(--color-danger); }
.field-error { color: var(--color-danger); font-size: 0.875rem; margin-top: 0.25rem; }
.field-hint { color: var(--color-muted); font-size: 0.875rem; margin-top: 0.25rem; }

/* ---------- Alerts ---------- */
.alert { padding: 0.75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.alert-error { background: var(--color-danger-soft); color: var(--color-danger); }
.alert-info { background: var(--color-info-soft); color: var(--color-info); }
.alert-success { background: var(--color-primary-soft); color: var(--color-primary-dark); }

/* ---------- Loading ---------- */
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
.loading { display: flex; align-items: center; gap: 0.75rem; color: var(--color-muted); padding: 2rem 0; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Sign-in / account pages ---------- */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--color-surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 2rem;
}
.brand { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 1.5rem; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--color-accent);
  box-shadow: inset -6px -6px 0 rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
}
.brand-name { font-weight: 700; font-size: 1.1rem; }
.brand-sub { color: var(--color-muted); font-size: 0.875rem; }

/* ---------- App shell ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0;
  height: var(--topbar-height);
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0 16px;
  background: var(--color-primary);
  color: #fff;
  z-index: 20;
}
.topbar .brand-mark { width: 28px; height: 28px; }
.topbar-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-spacer { flex: 1; }
.topbar-user { font-size: 0.9rem; text-align: right; line-height: 1.2; }
.topbar-user small { display: block; opacity: 0.8; }
.topbar .btn-link { white-space: nowrap; }
.menu-toggle { display: none; }

.sidebar {
  position: fixed;
  top: var(--topbar-height); bottom: 0; left: 0;
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  padding: 0.75rem 0;
  z-index: 10;
}
.nav-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 1.25rem;
  color: var(--color-text);
  text-decoration: none;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--color-bg); }
.nav-item.active { border-left-color: var(--color-primary); background: var(--color-primary-soft); font-weight: 600; }
.nav-item.disabled { color: var(--color-muted); cursor: default; }
.nav-item.nav-phone-only { display: none; }
.nav-item.nav-sub { padding-left: 2.25rem; font-size: 0.92rem; }
.nav-item.disabled:hover { background: none; }
.badge-soon { font-size: 0.7rem; padding: 0.05rem 0.45rem; border-radius: 999px; background: var(--color-bg); color: var(--color-muted); }

/* Signed-in pages stay hidden until Layout.init has confirmed the session. */
body:not(.ready) > .content { visibility: hidden; }

.content {
  margin-left: var(--sidebar-width);
  padding: calc(var(--topbar-height) + 24px) 24px 32px;
  max-width: calc(1200px + var(--sidebar-width));
}
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-header p { margin: 0; color: var(--color-muted); }

/* ---------- Dashboard ---------- */
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
  border-top: 3px solid var(--color-primary);
}
.stat-card.warning { border-top-color: var(--color-accent); }
.stat-label { color: var(--color-muted); font-size: 0.875rem; font-weight: 600; }
.stat-value { font-size: 1.6rem; font-weight: 700; margin: 0.25rem 0; font-variant-numeric: tabular-nums; }
.stat-note { color: var(--color-muted); font-size: 0.8rem; }

.panel { background: var(--color-surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 24px; }
.section-title { margin: 0 0 0.75rem; }

.table-wrap { overflow-x: auto; position: relative; }  /* relative: hidden labels inside stay within the scroll box */
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--color-border); white-space: nowrap; }
.table th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--color-muted); }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { color: var(--color-muted); padding: 1rem 0; }

/* ---------- List pages ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-bottom: 16px; }
.toolbar .input { width: auto; }
.toolbar .search { flex: 1 1 260px; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover { background: var(--color-bg); }
.table td.wrap { white-space: normal; min-width: 160px; }
.muted { color: var(--color-muted); }
.status { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 0.1rem 0.55rem; border-radius: 999px; }
.status-active { background: var(--color-primary-soft); color: var(--color-primary-dark); }
.status-inactive { background: var(--color-bg); color: var(--color-muted); border: 1px solid var(--color-border); }
.status-good { background: var(--color-primary-soft); color: var(--color-primary-dark); }
.status-info { background: var(--color-info-soft); color: var(--color-info); }
.status-warn { background: var(--color-warning-soft); color: var(--color-warning); }
.status-bad { background: var(--color-danger-soft); color: var(--color-danger); }
.status-muted { background: var(--color-bg); color: var(--color-muted); border: 1px solid var(--color-border); text-decoration: line-through; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; flex-wrap: wrap; }
.pager-buttons { display: flex; gap: 8px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--color-border); margin-bottom: 16px; }
.tab {
  font: inherit; font-weight: 600; color: var(--color-muted);
  background: none; border: none; border-bottom: 3px solid transparent;
  padding: 0.6rem 1rem; cursor: pointer;
}
.tab[aria-selected="true"] { color: var(--color-primary); border-bottom-color: var(--color-primary); }
.check { display: inline-flex; align-items: center; gap: 0.4rem; white-space: nowrap; }
.badge-low { display: inline-block; font-size: 0.7rem; font-weight: 700; padding: 0.05rem 0.45rem; border-radius: 999px;
  background: var(--color-warning-soft); color: var(--color-warning); margin-left: 0.35rem; }

/* Horizontal share bars (e.g. expenses per category) */
.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 160px) 1fr auto; gap: 12px; align-items: center; }
.bar-track { background: var(--color-bg); border-radius: 999px; height: 10px; overflow: hidden; }
.bar-fill { background: var(--color-accent); height: 100%; border-radius: 999px; }
.bar-value { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ---------- Dialogs ---------- */
dialog.modal {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0;
  border: none;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  color: var(--color-text);
  overflow: hidden;
}
/* Header and buttons stay put; only the fields scroll. */
dialog.modal > form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
dialog.modal .modal-body { flex: 1 1 auto; min-height: 0; }
dialog.modal::backdrop { background: rgba(20, 30, 25, 0.45); }
.modal-header, .modal-footer { padding: 1rem 1.25rem; display: flex; align-items: center; gap: 12px; }
.modal-header { border-bottom: 1px solid var(--color-border); justify-content: space-between; }
.modal-header h2 { margin: 0; }
.modal-body { padding: 1.25rem; overflow-y: auto; }
.modal-footer { border-top: 1px solid var(--color-border); justify-content: flex-end; flex-wrap: wrap; }
.modal-footer .spacer { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.form-grid .span-2 { grid-column: 1 / -1; }
.read-only-facts { display: flex; flex-wrap: wrap; gap: 8px 24px; margin-bottom: 1rem; color: var(--color-muted); }
.read-only-facts strong { color: var(--color-text); }
textarea.input { min-height: 70px; resize: vertical; }
.btn-danger-outline { color: var(--color-danger); border-color: var(--color-danger); }
.btn-danger-outline:hover { background: var(--color-danger-soft); }

/* ---------- Document entry (purchases, sales) ---------- */
.lines-table td { vertical-align: top; }
.lines-table .input { min-height: 38px; padding: 0.35rem 0.5rem; }
.input.num { text-align: right; }
.facts-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px 24px; margin-bottom: 1rem; }
.facts-grid .label { color: var(--color-muted); font-size: 0.85rem; }
.facts-grid .value { font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.lines-table td.col-product { min-width: 220px; }
.lines-table td.col-qty, .lines-table td.col-cost { min-width: 110px; }
.lines-table td.col-age { min-width: 90px; }
.lines-table .line-error { color: var(--color-danger); font-size: 0.8rem; white-space: normal; }
.summary { margin-left: auto; max-width: 360px; display: grid; grid-template-columns: 1fr auto; gap: 0.35rem 1rem; align-items: center; }
.summary .total { font-size: 1.2rem; font-weight: 700; }
.summary .input { width: 140px; text-align: right; }
.form-section { border-top: 1px solid var(--color-border); padding-top: 1rem; margin-top: 1rem; }
.form-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 1rem; flex-wrap: wrap; }

/* ---------- Statements ---------- */
.toolbar .field { margin-bottom: 0; }
form.toolbar.panel { align-items: flex-end; }
.statement-head { display: flex; justify-content: space-between; gap: 24px; flex-wrap: wrap; margin-bottom: 1.25rem; }
.statement-head h2 { margin: 0.25rem 0; }
.statement-business { font-weight: 700; color: var(--color-primary); }
.statement-customer { text-align: right; }
.statement-subtotal td { font-weight: 600; background: var(--color-bg); }
.statement .table th, .statement .table td { padding: 0.5rem; }
.statement .table td.wrap { min-width: 120px; }
.table tfoot td { border-bottom: none; }
.statement-closing { text-align: right; margin-top: 1rem; font-size: 1.1rem; }
.statement-closing strong { font-size: 1.3rem; margin-left: 0.5rem; }

/* ---------- Invoice ---------- */
.invoice-head { display: flex; justify-content: space-between; gap: 24px; flex-wrap: wrap; margin-bottom: 1.5rem; }
.invoice-business .name { font-size: 1.3rem; font-weight: 700; color: var(--color-primary); }
.invoice-meta { text-align: right; }
.invoice-meta .doc-title { font-size: 1.4rem; font-weight: 700; letter-spacing: 0.05em; }
.invoice-parties { display: flex; justify-content: space-between; gap: 24px; flex-wrap: wrap; margin-bottom: 1rem; }
.invoice-footer { margin-top: 1.5rem; text-align: center; color: var(--color-muted); }
.void-stamp { border: 2px solid var(--color-danger); color: var(--color-danger); font-weight: 700; padding: 0.5rem 1rem;
  border-radius: var(--radius); display: inline-block; margin-bottom: 1rem; }

/* ---------- Printing: just the document, no app chrome ---------- */
@media print {
  body { background: #fff; font-size: 12px; }
  .topbar, .sidebar, .no-print, #message { display: none !important; }
  .content { margin: 0; padding: 0; max-width: none; }
  .panel { box-shadow: none; padding: 0; }
  .table th, .table td { padding: 0.35rem 0.5rem; }
  .table-wrap { overflow: visible; }
  a { color: inherit; text-decoration: none; }
}

/* ---------- Tablet and phone ---------- */
@media (max-width: 900px) {
  .menu-toggle { display: inline-flex; }
  .sidebar { transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow); }
  body.nav-open .sidebar { transform: none; }
  .content { margin-left: 0; padding: calc(var(--topbar-height) + 16px) 16px 24px; }
  .topbar-user small { display: none; }
  .hide-md { display: none; }
}

@media (max-width: 520px) {
  .topbar-user, .topbar .brand-mark, .topbar-password { display: none; }
  .nav-item.nav-phone-only { display: flex; }
  .card-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
  .form-grid { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .toolbar .input, .toolbar .btn { flex: 1 1 100%; }
  .stat-value { font-size: 1.3rem; }
  .auth-card { padding: 1.5rem; }
}

/* ---------- Sync (offline queue) ---------- */
.sync-status {
  font-size: 0.8rem; font-weight: 600; white-space: nowrap; text-decoration: none;
  padding: 0.2rem 0.6rem; border-radius: 999px; border: 1px solid rgba(255, 255, 255, 0.5); color: #fff;
}
.sync-status:hover { background: rgba(255, 255, 255, 0.12); }
.sync-status.sync-good { opacity: 0.85; }
.sync-status.sync-warn { background: var(--color-warning-soft); color: var(--color-warning); border-color: transparent; }
.sync-status.sync-bad { background: var(--color-danger-soft); color: var(--color-danger); border-color: transparent; }
.sync-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.sync-item { border-top: 1px solid var(--color-border); padding: 0.75rem 0; display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-start; }
.sync-item:first-child { border-top: 0; }
.sync-item-main { flex: 1 1 260px; min-width: 0; }
.sync-item-main ul { margin: 0.25rem 0 0; padding-left: 1.1rem; color: var(--color-muted); font-size: 0.9rem; }
.sync-item .sync-error { color: var(--color-danger); margin-top: 0.35rem; }
.sync-item .actions { flex: 0 0 auto; }
