/* ==========================================================================
   Invoice Generator — design tokens
   No component below this block hard-codes a colour, radius or font size.
   That rule is what makes both themes and the accent picker work everywhere.
   ========================================================================== */

:root {
  /* Light — warm paper rather than flat white */
  --bg:          #F6F4F0;
  --surface:     #FFFFFF;
  --surface-2:   #F0EDE7;
  --surface-3:   #E8E4DC;

  --ink:         #14181F;
  --ink-2:       #4A5260;
  --ink-3:       #838C9A;

  --line:        #E2DED6;
  --line-strong: #CFC9BE;

  --accent:      #1E3A5F;
  --accent-hover:#17304F;
  --accent-ink:  #FFFFFF;
  --accent-soft: #1E3A5F14;
  --accent-line: #1E3A5F33;

  --ok:      #1B6E46;  --ok-soft:     #1B6E4614;
  --warn:    #9A6511;  --warn-soft:   #9A651114;
  --danger:  #A32218;  --danger-soft: #A3221814;

  --radius:    10px;
  --radius-sm: 7px;
  --radius-lg: 14px;

  --shadow-1: 0 1px 2px rgb(20 24 31 / .05), 0 1px 8px rgb(20 24 31 / .04);
  --shadow-2: 0 4px 12px rgb(20 24 31 / .08), 0 16px 40px rgb(20 24 31 / .10);

  /* Spacing scale — swapped wholesale by density */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px;
  --row-y: 13px;
  --control-h: 42px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  --fs-xs: 11.5px; --fs-sm: 13px;  --fs-md: 14.5px;
  --fs-lg: 17px;   --fs-xl: 22px;  --fs-2xl: 30px;

  --ease: cubic-bezier(.4, 0, .2, 1);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg:          #0D1015;
  --surface:     #151920;
  --surface-2:   #1C222B;
  --surface-3:   #252C37;

  --ink:         #EDF0F5;
  --ink-2:       #AAB3C0;
  --ink-3:       #737C8A;

  --line:        #262D38;
  --line-strong: #38404E;

  --accent-ink:  #FFFFFF;

  --ok:      #4ADE9A;  --ok-soft:     #4ADE9A1A;
  --warn:    #E7B45C;  --warn-soft:   #E7B45C1A;
  --danger:  #F98A80;  --danger-soft: #F98A801A;

  --shadow-1: 0 1px 2px rgb(0 0 0 / .45), 0 1px 10px rgb(0 0 0 / .30);
  --shadow-2: 0 4px 14px rgb(0 0 0 / .45), 0 20px 48px rgb(0 0 0 / .55);

  color-scheme: dark;
}

:root[data-density="compact"] {
  --s3: 9px; --s4: 12px; --s5: 18px; --s6: 24px; --s7: 34px;
  --row-y: 8px;
  --control-h: 37px;
  --fs-md: 14px;
}

/* ========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -.012em; }
h1 { font-size: var(--fs-xl); }
h2 { font-size: var(--fs-lg); }
h3 { font-size: var(--fs-md); }
p  { margin: 0 0 var(--s3); }
a  { color: var(--accent); text-decoration: none; }

/* Money and quantities line up on the decimal everywhere they appear. */
.num, .money, td.num, th.num, input[inputmode="decimal"] {
  font-variant-numeric: tabular-nums lining-nums;
  font-feature-settings: "tnum" 1;
}

/* Base icon size. Without this an inline SVG expands to fill its button. */
.icon {
  width: 18px;
  height: 18px;
  flex: none;
  vertical-align: -.18em;
}
.btn-sm .icon { width: 16px; height: 16px; }
.btn-lg .icon { width: 20px; height: 20px; }

::selection { background: var(--accent-soft); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ==========================================================================
   App shell
   ========================================================================== */

#app { min-height: 100%; }

.shell {
  display: grid;
  grid-template-columns: 268px 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
}

.brand {
  display: flex;
  align-items: flex-start;
  gap: var(--s3);
  padding: var(--s5) var(--s4) var(--s4);
}
.brand-mark {
  width: 34px; height: 34px;
  border-radius: 9px;
  display: grid; place-items: center;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: var(--font-serif);
  font-size: 17px;
  font-weight: 600;
  flex: none;
  overflow: hidden;
}
.brand-mark img { width: 100%; height: 100%; object-fit: cover; }
.brand-text { min-width: 0; }
.brand-name {
  font-family: var(--font-serif);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -.01em;
  line-height: 1.25;
  /* Business names are long and often contain an unbroken run like
     "Engineering&Constructions". Wrap to two lines rather than truncating --
     this is the one label the owner should always be able to read in full --
     and allow a mid-word break only if a single word still cannot fit. */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.brand-sub {
  font-size: var(--fs-xs);
  color: var(--ink-3);
  letter-spacing: .04em;
  text-transform: uppercase;
}

.nav { flex: 1; padding: var(--s2) var(--s3); overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: var(--s3);
  padding: 10px var(--s3);
  border-radius: var(--radius-sm);
  color: var(--ink-2);
  font-size: var(--fs-md);
  font-weight: 500;
  cursor: pointer;
  transition: background .14s var(--ease), color .14s var(--ease);
  user-select: none;
}
.nav-item:hover { background: var(--surface-2); color: var(--ink); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
:root[data-theme="dark"] .nav-item.active { color: var(--ink); background: var(--surface-3); }
.nav-item .icon { width: 18px; height: 18px; flex: none; }
.nav-badge {
  margin-left: auto;
  background: var(--warn-soft); color: var(--warn);
  font-size: var(--fs-xs); font-weight: 600;
  padding: 1px 7px; border-radius: 999px;
}

.sidebar-foot { padding: var(--s3); border-top: 1px solid var(--line); }

.main { min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s4) var(--s5);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page { padding: var(--s5); flex: 1; }
.page-narrow { max-width: 940px; }

/* ==========================================================================
   Controls
   ========================================================================== */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: var(--control-h);
  padding: 0 var(--s4);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font: inherit; font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  transition: background .14s var(--ease), border-color .14s var(--ease), transform .06s var(--ease), opacity .14s var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(.5px); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
  box-shadow: var(--shadow-1);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }

.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.btn-danger:hover { background: var(--danger-soft); }

.btn-sm { min-height: 32px; padding: 0 var(--s3); font-size: var(--fs-sm); }
.btn-lg { min-height: 50px; padding: 0 var(--s5); font-size: var(--fs-lg); border-radius: var(--radius); }
.btn-block { width: 100%; }
.btn-icon { width: var(--control-h); padding: 0; flex: none; }

.field { margin-bottom: var(--s4); min-width: 0; }
.field > label,
.label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 550;
  color: var(--ink-2);
  margin-bottom: 6px;
}
.hint { font-size: var(--fs-xs); color: var(--ink-3); margin-top: 5px; }

input[type="text"], input[type="password"], input[type="number"], input[type="date"],
input[type="email"], input[type="search"], input[type="tel"], select, textarea {
  width: 100%;
  min-height: var(--control-h);
  padding: 8px var(--s3);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  transition: border-color .14s var(--ease), box-shadow .14s var(--ease);
}
textarea { min-height: 78px; resize: vertical; line-height: 1.5; }
select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-3) 50%), linear-gradient(135deg, var(--ink-3) 50%, transparent 50%);
  background-position: calc(100% - 17px) calc(50% + 1px), calc(100% - 12px) calc(50% + 1px);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: var(--s6);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder, textarea::placeholder { color: var(--ink-3); }
input[readonly] { background: var(--surface-2); color: var(--ink-2); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--s4); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 var(--s4); }

.check {
  display: flex; align-items: flex-start; gap: var(--s3);
  padding: 10px 0;
  cursor: pointer;
  user-select: none;
}
.check input { width: 18px; height: 18px; margin: 1px 0 0; flex: none; accent-color: var(--accent); cursor: pointer; }
.check-text { font-size: var(--fs-md); }
.check-sub { font-size: var(--fs-xs); color: var(--ink-3); }

/* ==========================================================================
   Surfaces
   ========================================================================== */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s5);
  margin-bottom: var(--s4);
}
.card-tight { padding: var(--s4); }
.card-head {
  display: flex; align-items: center; gap: var(--s3);
  margin-bottom: var(--s4);
}
.card-head h2, .card-head h3 { flex: 1; min-width: 0; }
.card-title-sub { font-size: var(--fs-sm); color: var(--ink-3); font-weight: 400; }

.section-label {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: var(--s3);
}

.toolbar {
  display: flex; align-items: center; gap: var(--s3);
  margin-bottom: var(--s4);
  flex-wrap: wrap;
}
.toolbar .search { flex: 1; min-width: 200px; position: relative; }
.toolbar .search input { padding-left: 38px; }
.toolbar .search .icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--ink-3); pointer-events: none; width: 17px; height: 17px;
}

/* Tables --------------------------------------------------------------- */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
table.data th {
  text-align: left;
  padding: 11px var(--s4);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--ink-3);
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.data td {
  padding: var(--row-y) var(--s4);
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr { transition: background .12s var(--ease); }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr.clickable:hover { background: var(--surface-2); }
.num { text-align: right; }
.strong { font-weight: 600; }
.muted { color: var(--ink-3); }
.small { font-size: var(--fs-sm); }
.nowrap { white-space: nowrap; }

/* Pills ---------------------------------------------------------------- */

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: .02em;
  white-space: nowrap;
}
.pill-ok      { background: var(--ok-soft);     color: var(--ok); }
.pill-warn    { background: var(--warn-soft);   color: var(--warn); }
.pill-danger  { background: var(--danger-soft); color: var(--danger); }
.pill-muted   { background: var(--surface-3);   color: var(--ink-2); }
.pill-accent  { background: var(--accent-soft); color: var(--accent); }
:root[data-theme="dark"] .pill-accent { color: var(--ink); background: var(--surface-3); }

/* Stats ---------------------------------------------------------------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: var(--s4); margin-bottom: var(--s5); }
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s4) var(--s5);
}
.stat-label {
  font-size: var(--fs-xs); font-weight: 600; letter-spacing: .07em;
  text-transform: uppercase; color: var(--ink-3);
}
.stat-value {
  font-family: var(--font-serif);
  font-size: var(--fs-2xl);
  font-variant-numeric: tabular-nums lining-nums;
  letter-spacing: -.02em;
  margin-top: 6px;
  line-height: 1.15;
}
.stat-sub { font-size: var(--fs-sm); color: var(--ink-3); margin-top: 2px; }
.stat-value.is-warn { color: var(--warn); }
.stat-value.is-danger { color: var(--danger); }

/* Empty + loading ------------------------------------------------------ */

.empty {
  text-align: center;
  padding: var(--s7) var(--s5);
  color: var(--ink-3);
}
.empty-icon {
  width: 44px; height: 44px; margin: 0 auto var(--s4);
  color: var(--ink-3); opacity: .55;
}
.empty h3 { color: var(--ink-2); margin-bottom: 5px; }
.empty p { font-size: var(--fs-sm); margin-bottom: var(--s4); }

.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
  border-radius: var(--radius-sm);
  height: 14px;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }
.skeleton-row { padding: var(--row-y) var(--s4); border-bottom: 1px solid var(--line); }

/* Toast ---------------------------------------------------------------- */

#toasts {
  position: fixed; z-index: 200;
  bottom: var(--s5); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: var(--s2);
  align-items: center;
  pointer-events: none;
  width: max-content; max-width: min(92vw, 460px);
}
.toast {
  background: var(--ink); color: var(--bg);
  padding: 11px var(--s4);
  border-radius: var(--radius);
  font-size: var(--fs-sm); font-weight: 500;
  box-shadow: var(--shadow-2);
  animation: toast-in .2s var(--ease);
  display: flex; align-items: center; gap: var(--s2);
}
.toast.is-error { background: var(--danger); color: #fff; }
.toast.is-ok    { background: var(--ok); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* Modal / sheet -------------------------------------------------------- */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgb(10 12 16 / .45);
  backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center;
  padding: var(--s4);
  animation: fade-in .15s var(--ease);
}
@keyframes fade-in { from { opacity: 0; } }
.modal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  width: min(560px, 100%);
  max-height: min(88vh, 900px);
  display: flex; flex-direction: column;
  animation: modal-in .2s var(--ease);
}
.modal-wide { width: min(760px, 100%); }
@keyframes modal-in { from { opacity: 0; transform: translateY(10px) scale(.99); } }
.modal-head {
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s4) var(--s5);
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { flex: 1; }
.modal-body { padding: var(--s5); overflow-y: auto; flex: 1; }
.modal-foot {
  display: flex; gap: var(--s3); justify-content: flex-end;
  padding: var(--s4) var(--s5);
  border-top: 1px solid var(--line);
}
.modal-foot .btn { min-width: 96px; }

/* ==========================================================================
   Login
   ========================================================================== */

.login-wrap {
  min-height: 100vh; min-height: 100dvh;
  display: grid; place-items: center;
  padding: var(--s5);
  background:
    radial-gradient(1100px 520px at 50% -12%, var(--accent-soft), transparent 70%),
    var(--bg);
}
.login-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  padding: var(--s6);
}
.login-brand { text-align: center; margin-bottom: var(--s6); }
.login-brand .brand-mark { width: 52px; height: 52px; font-size: 24px; margin: 0 auto var(--s4); border-radius: 13px; }
.login-brand h1 { font-family: var(--font-serif); font-size: 25px; }
.login-brand p { color: var(--ink-3); font-size: var(--fs-sm); margin: 5px 0 0; }
.form-error {
  background: var(--danger-soft); color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 28%, transparent);
  border-radius: var(--radius-sm);
  padding: 10px var(--s3);
  font-size: var(--fs-sm);
  margin-bottom: var(--s4);
}

/* ==========================================================================
   Mobile
   ========================================================================== */

.mobile-only { display: none; }
.tabbar { display: none; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .mobile-only { display: initial; }

  .topbar { padding: var(--s3) var(--s4); }
  .page {
    padding: var(--s4);
    padding-bottom: calc(70px + env(safe-area-inset-bottom) + var(--s4));
  }

  .tabbar {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: saturate(1.6) blur(12px);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tab {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px;
    padding: 9px 2px 7px;
    min-height: 56px;
    color: var(--ink-3);
    font-size: 10.5px;
    font-weight: 550;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .tab .icon { width: 21px; height: 21px; }
  .tab.active { color: var(--accent); }
  :root[data-theme="dark"] .tab.active { color: var(--ink); }

  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .card { padding: var(--s4); }
  .stats { grid-template-columns: 1fr 1fr; gap: var(--s3); }
  .stat { padding: var(--s3) var(--s4); }
  .stat-value { font-size: var(--fs-xl); }

  .modal { max-height: 92dvh; width: 100%; }
  .modal-foot { padding: var(--s3) var(--s4) calc(var(--s3) + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1; }

  /* Tables collapse into stacked cards */
  table.data.responsive thead { display: none; }
  table.data.responsive, table.data.responsive tbody,
  table.data.responsive tr, table.data.responsive td { display: block; width: 100%; }
  table.data.responsive tr {
    padding: var(--s3) var(--s4);
    border-bottom: 1px solid var(--line);
  }
  table.data.responsive td {
    padding: 2px 0;
    border: 0;
    display: flex; justify-content: space-between; gap: var(--s3);
    text-align: right;
  }
  table.data.responsive td::before {
    content: attr(data-label);
    color: var(--ink-3);
    font-size: var(--fs-sm);
    font-weight: 500;
    text-align: left;
    flex: none;
  }
  table.data.responsive td[data-label=""]::before { content: none; }
  table.data.responsive td.cell-title {
    display: block; text-align: left;
    font-weight: 600; font-size: var(--fs-lg);
    margin-bottom: 3px;
  }
}

@media (min-width: 861px) { .desktop-hide { display: none !important; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}

/* Utilities ------------------------------------------------------------ */
.row { display: flex; align-items: center; gap: var(--s3); }
.row-tight { gap: var(--s2); }
.spacer { flex: 1; }
.stack > * + * { margin-top: var(--s3); }
.mt-0 { margin-top: 0; } .mb-0 { margin-bottom: 0; }
.mt-4 { margin-top: var(--s4); } .mb-4 { margin-bottom: var(--s4); }
.mt-5 { margin-top: var(--s5); }
.text-ok { color: var(--ok); } .text-warn { color: var(--warn); } .text-danger { color: var(--danger); }
.hidden { display: none !important; }
