/* Billdesk — ledger-inspired UI. One accent: khata red. */
:root {
  --ink: #1b2430;
  --ink-2: #4a5563;
  --ink-3: #7a8491;
  --paper: #f3f4f1;
  --surface: #ffffff;
  --line: #dadfd6;
  --line-2: #eceee9;
  --khata: #b3261e;
  --khata-dark: #8e1d17;
  --khata-tint: #fbeeed;
  --green: #1e7a4c;
  --green-tint: #e7f3ec;
  --amber: #a5670f;
  --amber-tint: #fbf1e1;
  --blue: #2457a6;
  --focus: #2457a6;
  --radius-s: 6px;
  --radius-m: 10px;
  --shadow: 0 1px 2px rgba(27, 36, 48, .06), 0 4px 16px rgba(27, 36, 48, .06);
  --font: "Hanken Grotesk", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --side-w: 236px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #e8ebef; --ink-2: #b7bec8; --ink-3: #8a93a0; --paper: #14181e; --surface: #1c2129;
    --line: #313843; --line-2: #262c35; --khata: #e0574e; --khata-dark: #f07a72; --khata-tint: #3a1f1e;
    --green: #4cc38a; --green-tint: #17301f; --amber: #e3a54a; --amber-tint: #33280f; --blue: #7aa7ff; --focus: #7aa7ff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --ink: #e8ebef; --ink-2: #b7bec8; --ink-3: #8a93a0; --paper: #14181e; --surface: #1c2129;
  --line: #313843; --line-2: #262c35; --khata: #e0574e; --khata-dark: #f07a72; --khata-tint: #3a1f1e;
  --green: #4cc38a; --green-tint: #17301f; --amber: #e3a54a; --amber-tint: #33280f; --blue: #7aa7ff; --focus: #7aa7ff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font); font-size: 15px; line-height: 1.5; color: var(--ink);
  background: var(--paper); -webkit-font-smoothing: antialiased; font-variant-numeric: tabular-nums;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.2; margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
.muted { color: var(--ink-3); }
.num { text-align: right; white-space: nowrap; }
.hidden { display: none !important; }
.green { color: var(--green); }
.amber { color: var(--amber); }
.red { color: var(--khata); }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: var(--side-w) 1fr; min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh; overflow-y: auto; background: var(--surface);
  border-right: 1px solid var(--line); display: flex; flex-direction: column;
  box-shadow: inset 5px 0 0 var(--khata); /* the ledger spine */
}
.brand { padding: 18px 18px 10px 24px; display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 30px; height: 30px; border-radius: 7px; background: var(--khata); color: #fff; display: grid; place-items: center; font-weight: 800; }
.brand-name { font-weight: 800; font-size: 1.1rem; }
.biz-switch { margin: 4px 14px 10px 20px; }
.biz-switch select { width: 100%; font-weight: 600; }
.nav { padding: 4px 10px 16px 14px; display: flex; flex-direction: column; gap: 1px; }
.nav-group { margin: 14px 10px 4px; font-size: .78rem; color: var(--ink-3); font-weight: 600; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-s);
  color: var(--ink-2); font-weight: 500;
}
.nav a:hover { background: var(--line-2); text-decoration: none; color: var(--ink); }
.nav a.active { background: var(--khata-tint); color: var(--khata-dark); font-weight: 650; }
.nav svg { width: 18px; height: 18px; flex: none; }
.side-foot { margin-top: auto; padding: 12px 18px 16px 24px; border-top: 1px solid var(--line-2); font-size: .85rem; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 28px; min-height: 64px;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.topbar h1 { font-size: 1.25rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content { padding: 24px 28px 96px; max-width: 1280px; width: 100%; }
.plan-banner { background: var(--amber-tint); color: var(--amber); padding: 10px 28px; font-weight: 600; font-size: .9rem; border-bottom: 1px solid var(--line); }
.plan-banner.expired { background: var(--khata-tint); color: var(--khata-dark); }

.tabbar { display: none; }
.btn.menu-btn { display: none; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 50; left: 0; top: 0; width: 280px; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; box-shadow: inset 5px 0 0 var(--khata), 0 0 0 100vmax rgba(0,0,0,.35); }
  .btn.menu-btn { display: inline-flex; }
  .topbar { padding: 10px 16px; position: sticky; top: 0; z-index: 20; }
  .content { padding: 16px 16px 110px; }
  .topbar { flex-wrap: wrap; row-gap: 8px; }
  .topbar h1 { flex: 1 1 calc(100% - 60px); }
  #page-actions:not(:empty) { width: 100%; justify-content: flex-end; }
  .plan-banner { padding: 8px 16px; }
  .tabbar {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .72rem; color: var(--ink-3); padding: 4px 0; }
  .tabbar a.active { color: var(--khata); }
  .tabbar a:hover { text-decoration: none; }
  .tabbar svg { width: 22px; height: 22px; }
  .tabbar .tab-new span.plus { width: 44px; height: 44px; margin-top: -18px; border-radius: 50%; background: var(--khata); color: #fff; display: grid; place-items: center; box-shadow: var(--shadow); }
}

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 14px;
  border-radius: var(--radius-s); border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-3); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--khata); border-color: var(--khata); color: #fff; }
.btn.primary:hover { background: var(--khata-dark); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--line-2); }
.btn.danger { color: var(--khata); }
.btn.small { height: 30px; padding: 0 10px; font-size: .85rem; }
.btn svg { width: 18px; height: 18px; }
.icon-btn { width: 38px; padding: 0; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--ink-2); min-width: 0; }
label.field .hint { font-weight: 400; color: var(--ink-3); font-size: .8rem; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-s); padding: 8px 10px; min-height: 38px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 20%, transparent); }
textarea { min-height: 72px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; accent-color: var(--khata); }
input[type=color] { padding: 2px; height: 38px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; font-size: .92rem; cursor: pointer; }

.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 900px) { .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 560px) { .g2, .g3, .g4 { grid-template-columns: 1fr; } }
.span2 { grid-column: span 2; }
@media (max-width: 560px) { .span2 { grid-column: auto; } }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 16px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line-2); }
.panel-head h2 { flex: 1; font-size: 1rem; }
.panel-body { padding: 18px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.toolbar input[type=search] { max-width: 280px; }
.toolbar input[type=date] { width: 150px; }
.toolbar select { width: auto; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: .92rem; }
.t th { text-align: left; font-weight: 600; color: var(--ink-3); font-size: .8rem; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--surface); }
.t td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.t tbody tr.clickable { cursor: pointer; }
.t tbody tr.clickable:hover td { background: var(--line-2); }
.t tfoot td { font-weight: 700; border-top: 1px solid var(--line); border-bottom: none; }
.empty { padding: 48px 20px; text-align: center; color: var(--ink-3); }
.empty strong { display: block; color: var(--ink); font-size: 1.05rem; margin-bottom: 4px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: .78rem; font-weight: 600; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.paid { background: var(--green-tint); color: var(--green); }
.badge.partial, .badge.open { background: var(--amber-tint); color: var(--amber); }
.badge.unpaid { background: var(--khata-tint); color: var(--khata-dark); }

/* ---------- dashboard ---------- */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); margin-bottom: 18px; }
.stat { padding: 16px 18px; border-left: 1px solid var(--line-2); }
.stat:first-child { border-left: none; }
.stat .label { font-size: .82rem; color: var(--ink-3); font-weight: 600; }
.stat .value { font-size: 1.45rem; font-weight: 750; margin-top: 2px; }
.stat .sub { font-size: .8rem; color: var(--ink-3); }
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat:nth-child(3) { border-left: none; }
  .stat:nth-child(n+3) { border-top: 1px solid var(--line-2); }
}
.quick { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 18px; }
@media (max-width: 700px) { .quick { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.quick a { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 12px 14px; color: var(--ink); font-weight: 600; display: flex; align-items: center; gap: 10px; }
.quick a:hover { border-color: var(--khata); text-decoration: none; }
.quick svg { width: 20px; height: 20px; color: var(--khata); }
.dash-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; }
@media (max-width: 1000px) { .dash-grid { grid-template-columns: 1fr; } }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 150px; padding: 8px 0 0; }
.bars .bar { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.bars .bar i { display: block; width: 100%; max-width: 26px; background: var(--ink-2); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .8; }
.bars .bar.today i { background: var(--khata); opacity: 1; }
.bars .bar span { font-size: .68rem; color: var(--ink-3); }

/* ---------- invoice editor ---------- */
.editor { display: grid; grid-template-columns: 1fr 320px; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .editor { grid-template-columns: 1fr; } }
.lines { width: 100%; border-collapse: collapse; }
.lines th { font-size: .78rem; color: var(--ink-3); text-align: left; padding: 6px 6px; font-weight: 600; }
.lines td { padding: 4px 4px; vertical-align: top; }
.lines input, .lines select { min-height: 34px; padding: 6px 8px; }
.lines .c-item { min-width: 200px; }
.lines .c-n { width: 130px; }
.lines .c-s { width: 70px; }
.lines .c-amt { width: 110px; text-align: right; padding-top: 12px; font-weight: 600; }
@media (max-width: 760px) {
  .lines thead { display: none; }
  .lines tr { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; padding: 10px 0; border-bottom: 1px solid var(--line-2); }
  .lines td { padding: 0; }
  .lines td.c-item { grid-column: 1 / -1; min-width: 0; }
  .lines td::before { content: attr(data-l); display: block; font-size: .72rem; color: var(--ink-3); }
  .lines td.c-item::before { display: none; }
  .lines .c-n, .lines .c-s, .lines .c-amt { width: auto; }
}
.picker { position: relative; }
.picker-list {
  position: absolute; z-index: 40; left: 0; right: 0; top: calc(100% + 2px); max-height: 280px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); box-shadow: var(--shadow);
}
.picker-list div { padding: 8px 10px; cursor: pointer; display: flex; gap: 8px; justify-content: space-between; }
.picker-list div:hover, .picker-list div.hl { background: var(--line-2); }
.picker-list small { color: var(--ink-3); }

.bill-total { position: sticky; top: 84px; }
.sumrow { display: flex; justify-content: space-between; padding: 5px 0; font-size: .93rem; }
.sumrow.grand { border-top: 2px solid var(--ink); margin-top: 8px; padding-top: 10px; font-size: 1.05rem; font-weight: 700; }
.sumrow.grand span:last-child { color: var(--khata); font-size: 1.6rem; font-weight: 800; letter-spacing: -0.02em; }

/* ---------- POS ---------- */
.pos { display: grid; grid-template-columns: 1fr 380px; gap: 16px; height: calc(100vh - 64px - 48px); }
@media (max-width: 1000px) { .pos { grid-template-columns: 1fr; height: auto; } }
.pos-items { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; align-content: start; overflow-y: auto; padding-bottom: 12px; }
.pos-item { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 12px; text-align: left; cursor: pointer; font: inherit; color: var(--ink); min-height: 86px; display: flex; flex-direction: column; justify-content: space-between; }
.pos-item:hover { border-color: var(--khata); }
.pos-item b { font-weight: 600; font-size: .92rem; line-height: 1.25; }
.pos-item span { color: var(--ink-2); font-weight: 700; margin-top: 6px; }
.pos-item small { color: var(--ink-3); font-size: .75rem; }
.pos-cart { display: flex; flex-direction: column; min-height: 0; }
.pos-cart .cart-lines { flex: 1; overflow-y: auto; }
.cart-line { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 8px 14px; border-bottom: 1px solid var(--line-2); }
.qty-ctl { display: flex; align-items: center; border: 1px solid var(--line); border-radius: var(--radius-s); }
.qty-ctl button { border: 0; background: none; width: 30px; height: 30px; font-size: 1.1rem; cursor: pointer; color: var(--ink); }
.qty-ctl input { border: 0; width: 46px; text-align: center; min-height: 30px; padding: 0; }
.pay-modes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.pay-modes button.sel { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.pos-total { font-size: 2rem; font-weight: 800; color: var(--khata); letter-spacing: -0.02em; }

/* ---------- modal & toast ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(15, 20, 28, .45); z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius-m); width: 100%; max-width: 560px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { max-width: 820px; }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line-2); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; padding: 14px 20px; border-top: 1px solid var(--line-2); }
.toast { position: fixed; z-index: 200; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--ink); color: var(--surface); padding: 10px 18px; border-radius: 99px; font-weight: 600; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }
.toast.err { background: var(--khata); color: #fff; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.auth-side { background: var(--khata); color: #fff; padding: 48px; display: flex; flex-direction: column; justify-content: space-between; position: relative; overflow: hidden; }
.auth-side::after { content: ""; position: absolute; inset: 0 auto 0 28px; width: 2px; background: rgba(255,255,255,.25); }
.auth-side h1 { font-size: 2.6rem; max-width: 12ch; line-height: 1.05; letter-spacing: -0.03em; }
.auth-side p { max-width: 38ch; opacity: .9; font-size: 1.05rem; }
.auth-side ul { padding-left: 18px; opacity: .92; }
.auth-form { display: grid; place-items: center; padding: 32px 20px; }
.auth-form form { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
@media (max-width: 860px) {
  .auth { grid-template-columns: 1fr; }
  .auth-side { padding: 28px 24px; }
  .auth-side h1 { font-size: 1.8rem; }
  .auth-side ul { display: none; }
}

/* ---------- settings ---------- */
.subnav { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 16px; border-bottom: 1px solid var(--line); }
.subnav a { padding: 8px 12px; color: var(--ink-2); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.subnav a.active { color: var(--khata-dark); border-color: var(--khata); }
.subnav a:hover { text-decoration: none; color: var(--ink); }
.designer { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .designer { grid-template-columns: 1fr; } }
.preview-frame { background: #e9ebe6; border-radius: var(--radius-m); padding: 20px; overflow: hidden; }
.preview-frame .inv-doc { transform-origin: top left; }
.theme-pick { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.theme-pick button { padding: 10px; border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--surface); cursor: pointer; font: inherit; color: var(--ink); text-align: left; }
.theme-pick button.sel { border-color: var(--khata); box-shadow: inset 0 0 0 1px var(--khata); }
.theme-pick small { display: block; color: var(--ink-3); }
.plans { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.plan-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 18px; display: flex; flex-direction: column; gap: 8px; }
.plan-card.hl { border-color: var(--khata); box-shadow: inset 0 0 0 1px var(--khata); }
.plan-card.current { background: var(--green-tint); }
.plan-card .price { font-size: 1.7rem; font-weight: 800; }
.plan-card ul { margin: 0; padding-left: 18px; font-size: .88rem; color: var(--ink-2); }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px; }

/* ---------- invoice view ---------- */
.inv-view { display: grid; grid-template-columns: 1fr 300px; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .inv-view { grid-template-columns: 1fr; } }
.paper-wrap { background: #e9ebe6; border-radius: var(--radius-m); padding: 20px; overflow: hidden; }

@media print {
  body { background: #fff; }
  .no-print, .side, .topbar, .tabbar, .plan-banner, .toast { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; max-width: none; }
  .inv-view { display: block; }
  .paper-wrap { padding: 0; background: none; }
  .inv-doc { box-shadow: none !important; margin: 0 !important; }
}
