/* ---------------------------------------------------------------------
   Bill Bag — design tokens
   Paper-and-ink ledger aesthetic: flat surfaces, rule lines instead of
   card shadows, a deep money-green for money coming in, ochre/brick for
   due/overdue. Tabular numerals so amounts line up like a real receipt.
------------------------------------------------------------------------ */
:root {
  --paper: #F6F5F1;
  --surface: #FFFFFF;
  --ink: #1F2933;
  --ink-soft: #667080;
  --rule: #DCD7CB;
  --rule-strong: #C6C0B1;

  --accent: #2F5D50;
  --accent-soft: #E3ECE8;
  --due: #B5762C;
  --due-soft: #F4E9D7;
  --overdue: #A13D3D;
  --overdue-soft: #F5E2E0;
  --draft: #7C7568;
  --draft-soft: #ECE9E2;

  --font-serif: Georgia, 'Iowan Old Style', 'Palatino Linotype', 'Book Antiqua', serif;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;

  --radius: 3px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

:root {
  --content-max: 100%;
}
@media (min-width: 640px) and (max-width: 879px) {
  :root { --content-max: 760px; }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: anywhere;
  overflow-x: hidden;
}

body {
  overscroll-behavior-y: contain;
}

h1, h2, h3 { font-family: var(--font-serif); font-weight: 600; margin: 0; }

button, input, select, textarea { font-family: inherit; font-size: 1rem; color: inherit; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}
@media (min-width: 880px) {
  #app { flex-direction: row; }
}

.main-col {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
}
@media (min-width: 880px) {
  .main-col { max-width: none; }
}

/* ------------------------------------------------------------- Sidebar */
.sidebar { display: none; }
@media (min-width: 880px) {
  .sidebar {
    display: flex;
    flex-direction: column;
    width: 228px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--rule);
    padding: 22px 14px calc(22px + var(--safe-bottom));
    gap: 2px;
  }
}
.sidebar-brand { font-family: var(--font-serif); font-size: 1.3rem; font-weight: 700; padding: 4px 10px 18px; }
.topbar h1.brand-title { font-weight: 700; }
.brand-logo {
  height: 1em;
  width: 1em;
  vertical-align: -0.2em;
  margin: 0 0.12em;
  border-radius: 22%;
  object-fit: contain;
}
.sidebar-new { width: 100%; justify-content: center; margin-bottom: 16px; }
.sidebar-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 0.92rem;
}
.sidebar-link .icon { width: 19px; height: 19px; }
.sidebar-link.tab-active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ------------------------------------------------------------- Header */
.topbar {
  padding: calc(14px + var(--safe-top)) 20px 14px;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
  flex-shrink: 0;
}
.topbar h1 { font-size: 1.4rem; letter-spacing: 0.01em; }
@media (min-width: 880px) {
  .topbar { padding: 26px 40px 18px; }
  .topbar h1 { font-size: 1.55rem; }
}

/* -------------------------------------------------------------- Main */
#view {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px 20px calc(90px + var(--safe-bottom));
  width: 100%;
  max-width: 1500px;
  margin: 0 auto;
}
@media (min-width: 880px) {
  #view { padding: 28px 40px 40px; }
}

/* --------------------------------------------------------- Tab bar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: var(--content-max);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: var(--surface);
  border-top: 1px solid var(--rule);
  padding: 8px 8px calc(8px + var(--safe-bottom));
  z-index: 10;
}
@media (min-width: 880px) {
  .tabbar { display: none; }
}
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 0.68rem;
  padding: 4px 10px;
  border-radius: var(--radius);
}
.tabbar a.tab-active { color: var(--accent); }
.tabbar a .icon { width: 22px; height: 22px; }
.tab-add {
  background: var(--accent);
  color: #fff !important;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  margin-top: -22px;
  box-shadow: 0 2px 0 rgba(0,0,0,0.08);
}
.tab-add .icon { width: 22px; height: 22px; }

/* ------------------------------------------------------- Summary cards */
.summary-cards {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 10px;
  margin-bottom: 18px;
}
.summary-card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.summary-card-hero { background: var(--accent); border-color: var(--accent); color: #fff; }
.summary-card-hero .summary-label { color: rgba(255,255,255,0.75); }
.summary-label { font-size: 0.72rem; color: var(--ink-soft); }
.summary-value {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  font-variant-numeric: tabular-nums;
}
.summary-cards.cols-2 { grid-template-columns: 1fr 1fr; }

/* --------------------------------------------------------------- Chips */
.chip-row { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 14px; padding-bottom: 2px; }
.chip {
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-soft);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  white-space: nowrap;
}
.chip-active { background: var(--ink); color: var(--paper); border-color: var(--ink); }

/* ----------------------------------------------------------- Row lists */
.row-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
@media (min-width: 640px) {
  .row-list-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 10px;
    border-top: none;
  }
  .row-list-grid .row {
    border: 1px solid var(--rule);
    border-left: 1px solid var(--rule);
    border-radius: var(--radius);
    padding: 14px;
    overflow: hidden;
  }
}
.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 13px 4px;
  border-bottom: 1px solid var(--rule);
  border-left: 3px solid transparent;
  cursor: pointer;
}
.row-status-overdue { border-left-color: var(--overdue); }
.row-status-sent { border-left-color: var(--due); }
.row-status-paid { border-left-color: var(--accent); }
.row-status-draft { border-left-color: var(--draft); }
.row:active { background: rgba(0,0,0,0.02); }
.row-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.row-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 0.8rem; color: var(--ink-soft); }
.row-end { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.row-amount { font-variant-numeric: tabular-nums; font-weight: 600; }

/* --------------------------------------------------------------- Tags */
.tag { font-size: 0.7rem; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.tag-draft { background: var(--draft-soft); color: var(--draft); }
.tag-sent { background: var(--due-soft); color: var(--due); }
.tag-overdue { background: var(--overdue-soft); color: var(--overdue); }
.tag-paid { background: var(--accent-soft); color: var(--accent); }

/* -------------------------------------------------------------- Empty */
.empty { text-align: center; padding: 48px 12px; color: var(--ink-soft); }
.empty-title { font-family: var(--font-serif); font-size: 1.1rem; color: var(--ink); margin-bottom: 6px; }
.empty-body { font-size: 0.9rem; max-width: 34ch; margin: 0 auto 18px; }

/* ---------------------------------------------------------------- Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--rule-strong);
  background: var(--surface);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  text-decoration: none;
}
.btn .icon { width: 17px; height: 17px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-ghost { background: transparent; }
.btn-text { border: none; background: none; padding: 8px 4px; }
.btn-danger { color: var(--overdue); }
.file-btn { position: relative; overflow: hidden; }

.detail-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }

.page-narrow { max-width: 900px; margin: 0 auto; width: 100%; }
.page-medium { max-width: 1000px; margin: 0 auto; width: 100%; }

.client-info-card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 20px;
}
.client-info-name { font-family: var(--font-serif); font-size: 1.15rem; margin: 0 0 6px; }
.section-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}
.section-header-row .section-label { border-top: none; padding-top: 0; margin: 0; }
.section-header-row .detail-actions { margin-bottom: 0; }

/* ----------------------------------------------------------------- Forms */
.form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 0.82rem; color: var(--ink-soft); }
.field input, .field select, .field textarea {
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  padding: 10px 11px;
  background: var(--surface);
  width: 100%;
  min-width: 0;
}

/* Date fields: a decorative box in normal flow (defines the height), with the
   real native date input laid directly on top of it, invisible (opacity:0).
   Taps land on the actual control, so the native picker opens on every
   platform, while iOS's native widget rendering is never actually visible. */
.date-field-shell {
  position: relative;
  width: 100%;
  overflow: hidden;
  border-radius: var(--radius);
}
.date-field-display {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  padding: 10px 11px;
  background: var(--surface);
  width: 100%;
  box-sizing: border-box;
  color: var(--ink);
}
.date-field-display .icon { width: 18px; height: 18px; color: var(--ink-soft); flex-shrink: 0; }
.date-field-overlay-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}
.date-field-shell:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.field input:focus, .field select:focus, .field textarea:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.field-row { display: flex; gap: 8px; align-items: center; }
.field-row select { flex: 1; }

/* Phones: plain stacking (CSS Grid has known sizing quirks with native
   controls). Tablets/desktops: grid that reflows into more columns. */
.field-grid { display: flex; flex-direction: column; gap: 12px; }
@media (min-width: 640px) {
  .field-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
}
@media (min-width: 880px) {
  .field-grid { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
}
.field-grid .field { min-width: 0; width: 100%; }

.field-hint { font-size: 0.8rem; color: var(--ink-soft); margin: -6px 0 0; }
.field-error { font-size: 0.8rem; color: var(--overdue); margin: -2px 0 0; font-weight: 600; }
.page-hint { font-size: 0.8rem; color: var(--ink-soft); margin: 8px 0 0; }
.section-label {
  font-family: var(--font-serif);
  font-size: 1rem;
  margin: 6px 0 -4px;
  padding-top: 10px;
  border-top: 1px solid var(--rule);
}
.logo-preview { max-height: 48px; margin-top: 8px; }

.items-block { border: 1px solid var(--rule); border-radius: var(--radius); padding: 10px; }
.items-header, .item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 52px 70px 30px;
  gap: 6px;
  align-items: center;
}
.items-header { font-size: 0.72rem; color: var(--ink-soft); padding: 0 2px 6px; }
.item-row { padding: 4px 0; min-width: 0; }
.item-row input { border: 1px solid var(--rule); border-radius: var(--radius); padding: 8px; width: 100%; min-width: 0; }
.icon-btn { background: none; border: none; color: var(--ink-soft); cursor: pointer; padding: 4px; }
.icon-btn .icon { width: 17px; height: 17px; }

.totals-block { align-self: flex-end; width: 100%; max-width: 260px; margin-left: auto; }
.totals-row { display: flex; justify-content: space-between; padding: 4px 0; font-variant-numeric: tabular-nums; }
.totals-row-total { font-weight: 700; border-top: 1px solid var(--rule); margin-top: 4px; padding-top: 8px; font-size: 1.05rem; }

.form-actions { display: flex; flex-wrap: wrap; gap: 10px; }

/* --------------------------------------------------------- Invoice sheet */
.invoice-sheet {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 24px 20px;
}
.invoice-sheet-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--rule); padding-bottom: 16px; margin-bottom: 16px; }
.invoice-logo { max-height: 40px; margin-bottom: 8px; }
.invoice-business-name { font-family: var(--font-serif); font-size: 1.05rem; margin: 0 0 4px; }
.invoice-business-detail { font-size: 0.82rem; color: var(--ink-soft); margin: 0; }
.invoice-meta { text-align: right; }
.invoice-number { font-family: var(--font-serif); font-size: 1.2rem; margin: 0 0 6px; }
.invoice-meta-line { font-size: 0.8rem; color: var(--ink-soft); margin: 4px 0 0; }
.invoice-label { font-size: 0.72rem; text-transform: none; color: var(--ink-soft); margin: 0 0 4px; }
.invoice-bill-to { margin-bottom: 20px; }
.invoice-client-name { font-weight: 600; margin: 0 0 2px; }
.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.invoice-table th { text-align: left; font-size: 0.72rem; color: var(--ink-soft); font-weight: 600; border-bottom: 1px solid var(--rule); padding: 6px 4px; }
.invoice-table td { padding: 8px 4px; border-bottom: 1px solid var(--rule); font-variant-numeric: tabular-nums; }
.invoice-table .num, .invoice-table th.num { text-align: right; }
.invoice-totals { max-width: 260px; margin-left: auto; }
.invoice-notes { margin-top: 20px; border-top: 1px solid var(--rule); padding-top: 12px; font-size: 0.88rem; }

/* -------------------------------------------------------------- Reports */
.report-client-search {
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  padding: 9px 11px;
  background: var(--surface);
  width: 100%;
  margin-bottom: 8px;
}
.report-client-list {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 4px 12px;
}
.report-client-list[hidden] { display: none; }
.report-client-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--rule);
  cursor: pointer;
}
.report-client-row[hidden] { display: none; }
.report-client-list .report-client-row:last-child { border-bottom: none; }
.report-client-all { padding: 4px 0 10px; font-weight: 600; }
.report-client-row input[type="checkbox"] { width: 18px; height: 18px; flex-shrink: 0; accent-color: var(--accent); }
.chip-radio {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--rule);
  background: var(--surface);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.chip-radio input { accent-color: var(--accent); }
.report-results { display: flex; flex-direction: column; gap: 16px; }
.report-invoice-block {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 18px;
}
.report-invoice-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--rule); padding-bottom: 12px; margin-bottom: 12px; }
.report-invoice-client { font-weight: 700; margin: 0 0 2px; }
.report-invoice-total { text-align: right; font-weight: 700; margin-top: 10px; font-variant-numeric: tabular-nums; }

/* ----------------------------------------------------------------- Print */
@page {
  margin: 15mm 12mm;
}
@media print {
  .no-print, .tabbar, .sidebar, .topbar, .detail-actions, #reportFilters { display: none !important; }
  #view { padding: 0; overflow: visible; }
  #app { display: block; height: auto; }
  .main-col { max-width: none; margin: 0; display: block; }
  body { background: #fff; }
  .invoice-sheet { border: none; padding: 0; }
}

/* -------------------------------------------------------------- Motion */
@media (prefers-reduced-motion: no-preference) {
  #view { animation: rise 180ms ease-out; }
}
@keyframes rise { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
