/* =========================================================
   BillingApp styles
   Desktop: sidebar layout. Mobile (< 900px): top bar + bottom tab bar,
   tables become cards. Print: only the bill.
   ========================================================= */

/* ---------- Theme ---------- */
:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-soft: #eef2ff;
    --success: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --text: #1e293b;
    --muted: #64748b;
    --border: #e2e8f0;
    --bg: #f4f6fb;
    --card: #fff;
    --sidebar: linear-gradient(180deg, #312e81 0%, #1e1b4b 100%);
    --radius: 14px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 16px rgba(15, 23, 42, .06);
    --sidebar-w: 240px;
    --tabbar-h: 64px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.25; }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -.02em; }
h3 { font-size: 17px; }
h3 .icon { vertical-align: -4px; margin-right: 4px; color: var(--primary); }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-weight: normal; font-size: 14px; }
.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- App layout: sidebar ---------- */
.sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); z-index: 20;
    display: flex; flex-direction: column; padding: 20px 14px; background: var(--sidebar); color: #c7d2fe;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 17px; margin: 0 6px 28px; min-width: 0; }
.brand-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.logo {
    display: grid; place-items: center; width: 38px; height: 38px; flex: none; border-radius: 11px;
    background: linear-gradient(135deg, #818cf8, #c084fc); color: #fff; font-size: 18px; box-shadow: 0 4px 12px rgba(129, 140, 248, .4);
}
.logo.has-image { padding: 3px; background: #fff; object-fit: contain; }
.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav a, .nav-logout { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-radius: 10px; color: #c7d2fe; font-weight: 500; transition: background .15s, color .15s; }
.nav a:hover, .nav-logout:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav a.active { background: rgba(255, 255, 255, .14); color: #fff; }
.nav a.nav-primary { background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; margin: 4px 0; box-shadow: 0 6px 16px rgba(99, 102, 241, .35); }
.nav .short { display: none; }
.nav-logout { font-size: 14px; }
.nav-logout span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.mobile-top { display: none; }
.main { margin-left: var(--sidebar-w); min-height: 100vh; }
.container { max-width: 1200px; margin: 0 auto; padding: 28px 28px 40px; }

.dev-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 10px 28px; background: #fef3c7; color: #92400e; font-size: 14px; }
.dev-bar a { margin-left: auto; font-weight: 600; color: #92400e; }
.dev-bar .icon { width: 18px; height: 18px; }

/* ---------- Page blocks ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-head h1 { margin: 0; }
.page-head > div { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; margin-bottom: 20px; box-shadow: var(--shadow); }
.card > h3:first-child { margin-bottom: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.card-head h3 { margin: 0; }
.table-wrap { overflow-x: auto; margin: 0 -22px; padding: 0 22px; }

.grid-2 { display: grid; grid-template-columns: minmax(280px, 1fr) 2fr; gap: 20px; align-items: start; }
.grid-2.even { grid-template-columns: 1fr 1fr; gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 16px; }

/* Dashboard */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 20px; }
.stats .card { margin: 0; }
.stat { display: flex; align-items: center; gap: 14px; padding: 18px; }
.stat .chip { display: grid; place-items: center; width: 46px; height: 46px; flex: none; border-radius: 12px; }
.stat .chip .icon { width: 22px; height: 22px; }
.stat small { display: block; color: var(--muted); font-size: 13px; }
.stat strong { display: block; font-size: 21px; letter-spacing: -.01em; white-space: nowrap; }
.chip.c1 { background: #eef2ff; color: #4f46e5; }
.chip.c2 { background: #ecfdf5; color: #059669; }
.chip.c3 { background: #f0f9ff; color: #0284c7; }
.chip.c4 { background: #fff7ed; color: #ea580c; }
.chip.c5 { background: #fdf4ff; color: #c026d3; }
.chip.c6 { background: #f1f5f9; color: #475569; }

.quick { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.quick a {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 18px 10px;
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
    color: var(--text); font-weight: 600; text-align: center; transition: transform .15s, border-color .15s;
}
.quick a:hover { transform: translateY(-2px); border-color: var(--primary); }
.quick a .icon { width: 26px; height: 26px; color: var(--primary); }
.quick a.main-action { background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; border: 0; }
.quick a.main-action .icon { color: #fff; }

/* ---------- Forms ---------- */
label { display: block; margin-bottom: 14px; font-weight: 600; font-size: 14px; color: #334155; }
input, select, textarea {
    width: 100%; min-height: 44px; padding: 10px 12px; margin-top: 6px; border: 1px solid #cbd5e1; border-radius: 10px;
    background: #fff; font: inherit; font-weight: normal; color: var(--text); transition: border-color .15s, box-shadow .15s;
}
textarea { min-height: 0; }
input:focus, select:focus, textarea:focus { outline: 0; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79, 70, 229, .15); }
.image-field { margin-bottom: 14px; }
.image-field .field-label { display: block; font-weight: 600; font-size: 14px; color: #334155; }
.image-row { display: flex; align-items: center; gap: 12px; margin-top: 6px; }
.image-row input { margin: 0; padding: 8px; }
.image-preview { width: 64px; height: 64px; flex: none; object-fit: contain; border: 1px solid var(--border); border-radius: 12px; background: #fff; padding: 4px; }
.image-field label.check { margin: 8px 0 0; font-weight: 500; }
.image-field small { display: block; margin-top: 4px; font-size: 13px; }
.thumb { width: 28px; height: 28px; object-fit: contain; vertical-align: middle; margin-right: 8px; border-radius: 6px; border: 1px solid var(--border); }
label.check { display: flex; align-items: center; gap: 8px; }
label.check input { width: auto; min-height: 0; margin: 0; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 10px 20px;
    border: 0; border-radius: 10px; background: var(--primary); color: #fff; font: inherit; font-weight: 600;
    cursor: pointer; transition: background .15s, transform .1s; text-align: center;
}
.btn:hover { background: var(--primary-dark); }
.btn:active { transform: scale(.98); }
.btn .icon { width: 18px; height: 18px; }
.btn-light { background: #eef2f7; color: var(--text); }
.btn-light:hover { background: #e2e8f0; }
.btn-block { width: 100%; font-size: 17px; min-height: 52px; }
.btn-block + .btn-block { margin-top: 8px; }
.link { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; font-weight: 600; cursor: pointer; }
.danger { color: var(--danger); }
.mobile-only { display: none !important; }

.search { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.search input, .search select { margin: 0; flex: 1 1 160px; }
.search select { max-width: 200px; }
.search input[type="date"] { flex-basis: 130px; }
.search .btn { flex: none; }

details { margin-top: 8px; }
details summary { cursor: pointer; color: var(--primary); font-weight: 600; padding: 10px 0; list-style: none; }
details summary::-webkit-details-marker { display: none; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 32px 10px; }
.actions { white-space: nowrap; text-align: right; }
.actions a, .actions form { display: inline; margin-left: 12px; font-weight: 600; }

/* Bill lines (invoice form + billing) */
.items { margin: 16px 0; }
.items td { padding: 6px 4px; }
.items tbody tr:hover { background: none; }
.items input, .items select { margin: 0; min-width: 70px; }
.items input.unit { min-width: 0; width: 76px; }
.items .amount { font-weight: 600; white-space: nowrap; }
.remove-row { font-size: 18px; padding: 8px; }

/* When the bill area is narrow (phones, or the billing side panel), each line becomes a small card */
#invoice-form { container-type: inline-size; }
@container (max-width: 640px) {
    .items thead { display: none; }
    .items, .items tbody { display: block; }
    .items tr {
        display: grid; grid-template-columns: repeat(4, 1fr); align-items: end; gap: 8px; padding: 12px; margin-bottom: 10px;
        border: 1px solid var(--border); border-radius: 12px; background: #f8fafc;
    }
    .items td { display: block; min-width: 0; padding: 0; border: 0; }
    .items td::before {
        content: attr(data-label); display: block; margin-bottom: 2px; font-size: 12px; font-weight: 600; color: var(--muted);
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .items td.col-product, .items td.col-desc { grid-column: 1 / -1; }
    .items td.amount { grid-column: 1 / 4; align-self: center; font-size: 18px; text-align: left; }
    .items td.col-remove { text-align: right; align-self: center; }
    .items td.col-remove::before { display: none; }
    .items input { padding: 10px 8px; }
    .items input.unit { width: 100%; }
    .items input, .items select { min-width: 0; }
}

/* ---------- Messages & badges ---------- */
.alert { padding: 13px 16px; border-radius: 10px; margin-bottom: 16px; font-weight: 500; }
.alert-success { background: #dcfce7; color: #166534; }
.alert-error { background: #fee2e2; color: #991b1b; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.badge.paid { background: #dcfce7; color: #166534; }
.badge.partial { background: #fef3c7; color: #92400e; }
.badge.unpaid { background: #fee2e2; color: #991b1b; }

/* ---------- Invoice (A4) ---------- */
.invoice-bottom { grid-template-columns: 1fr 1fr; margin: 16px 0; }
.totals { max-width: 360px; margin-left: auto; }
.totals > div { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 7px 0; }
.totals input { max-width: 150px; margin: 0; text-align: right; }
.totals .grand { border-top: 2px solid var(--text); margin-top: 4px; padding-top: 10px; font-size: 20px; font-weight: 700; }
.invoice-head { display: flex; justify-content: space-between; gap: 20px; border-bottom: 2px solid var(--border); margin-bottom: 18px; }
.invoice .right { text-align: right; }
.bill-to small { color: var(--muted); font-weight: 700; letter-spacing: .05em; }
.bill-logo { display: block; max-width: 160px; max-height: 70px; object-fit: contain; margin-bottom: 8px; }
.receipt .bill-logo { max-width: 120px; max-height: 60px; margin: 0 auto 6px; filter: grayscale(1); } /* thermal printers are black & white */
.footer-note { text-align: center; color: var(--muted); margin-top: 30px; }

/* ---------- Billing (POS) ---------- */
.pos { grid-template-columns: 3fr 2fr; }
.pos-cart { position: sticky; top: 20px; }
.search-box { position: relative; margin-bottom: 16px; }
.search-box .icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search-box input { margin: 0; padding-left: 44px; min-height: 50px; font-size: 16px; border-radius: 12px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; max-height: 60vh; overflow-y: auto; padding: 2px; margin-bottom: 8px; }
.tile {
    position: relative; display: flex; flex-direction: column; gap: 2px; padding: 14px; min-height: 96px;
    border: 1px solid var(--border); border-radius: 12px; background: #fff; text-align: left; font: inherit; color: var(--text);
    cursor: pointer; transition: border-color .15s, box-shadow .15s, transform .1s;
}
.tile:hover { border-color: var(--primary); box-shadow: 0 4px 14px rgba(79, 70, 229, .15); }
.tile:active { transform: scale(.97); }
.tile b { font-size: 15px; line-height: 1.3; padding-right: 24px; }
.tile small { color: var(--muted); min-height: 1em; }
.tile .price { margin-top: auto; color: var(--primary); font-weight: 700; }
.tile .add { position: absolute; top: 10px; right: 10px; display: grid; place-items: center; width: 26px; height: 26px; border-radius: 8px; background: var(--primary-soft); color: var(--primary); }
.tile .add .icon { width: 16px; height: 16px; }
.tile.flash { animation: added .35s; }
@keyframes added { 50% { background: var(--primary-soft); border-color: var(--primary); } }
.pos-cart .col-product { display: none; }
.pos-cart .totals { max-width: none; margin-bottom: 16px; }
.cart-empty { text-align: center; color: var(--muted); padding: 20px; margin: 12px 0; border: 2px dashed var(--border); border-radius: 12px; }
#change { color: var(--success); }

/* Mobile checkout bar (billing page) */
.checkout-bar { display: none; }

/* ---------- Receipt (80mm thermal or normal paper) ---------- */
.receipt-page { background: #fff; }
.receipt { width: 300px; max-width: 100%; margin: 20px auto; font: 13px/1.4 "Courier New", monospace; color: #000; }
.receipt h2 { text-align: center; font-size: 17px; margin: 0 0 4px; }
.receipt .center { text-align: center; margin: 4px 0; }
.receipt hr { border: 0; border-top: 1px dashed #000; margin: 8px 0; }
.receipt .row { display: flex; justify-content: space-between; gap: 8px; }
.receipt .grand { font-weight: bold; font-size: 15px; }
.receipt th, .receipt td { padding: 2px 0; border: 0; background: none; color: #000; font-size: 12px; text-transform: none; letter-spacing: 0; vertical-align: top; }
.receipt tbody tr:hover { background: none; }
.receipt-actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin: 20px 16px; }

/* ---------- Login & install ---------- */
.center-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; background: linear-gradient(135deg, #312e81 0%, #6366f1 50%, #a855f7 100%); }
.center-page { grid-template-columns: minmax(0, 380px); justify-content: center; }
.login-box { width: 100%; padding: 32px; box-shadow: 0 20px 50px rgba(30, 27, 75, .35); }
.login-box .logo { width: 52px; height: 52px; font-size: 24px; margin: 0 auto 14px; }
.login-box h2 { text-align: center; margin-bottom: 4px; }
.login-box .muted { text-align: center; margin: 0 0 22px; }
.login-box .btn { width: 100%; margin-top: 6px; min-height: 50px; }
.install { max-width: 760px; }

/* =========================================================
   Mobile & tablet
   ========================================================= */
@media (max-width: 900px) {
    body { font-size: 16px; }
    h1 { font-size: 22px; }

    /* Top bar */
    .mobile-top {
        position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 8px;
        padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); background: #312e81; color: #fff;
    }
    .mobile-top .brand { margin: 0; font-size: 16px; }
    .mobile-top .logo { width: 34px; height: 34px; font-size: 16px; }
    .top-actions { display: flex; gap: 4px; }
    .icon-btn { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 10px; color: #c7d2fe; }
    .icon-btn.active, .icon-btn:hover { background: rgba(255, 255, 255, .14); color: #fff; }

    /* Sidebar becomes bottom tab bar */
    .sidebar {
        inset: auto 0 0 0; width: auto; height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding: 0 4px env(safe-area-inset-bottom);
        flex-direction: row; background: #fff; border-top: 1px solid var(--border); box-shadow: 0 -4px 16px rgba(15, 23, 42, .06);
    }
    .sidebar .brand, .nav-logout, .nav .desktop-only, .nav .long { display: none; }
    .nav { flex-direction: row; gap: 0; align-items: stretch; }
    .nav a {
        flex: 1 1 0; min-width: 0; flex-direction: column; justify-content: center; gap: 3px; padding: 6px 2px; border-radius: 0;
        color: var(--muted); font-size: 11px; font-weight: 600; background: none;
    }
    .nav a .short { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .nav a:hover { background: none; color: var(--text); }
    .nav a.active { background: none; color: var(--primary); }
    .nav a.nav-primary { background: none; box-shadow: none; margin: 0; color: var(--primary); }
    .nav a.nav-primary .icon {
        width: 46px; height: 46px; padding: 11px; margin-top: -22px; border-radius: 50%; color: #fff; border: 4px solid #fff;
        background: linear-gradient(135deg, #6366f1, #8b5cf6); box-shadow: 0 6px 16px rgba(99, 102, 241, .45);
    }
    .no-tabs .sidebar { display: none; }

    .main { margin-left: 0; padding-bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); }
    .no-tabs .main { padding-bottom: 16px; }
    .container { padding: 16px 14px 24px; }
    .dev-bar { padding: 10px 14px; }
    .card { padding: 16px; border-radius: 12px; margin-bottom: 14px; }
    .table-wrap { margin: 0; padding: 0; }

    .grid-2, .grid-2.even, .grid-3, .invoice-bottom, .pos { grid-template-columns: 1fr; gap: 0 14px; }
    .grid-2 { gap: 14px; }
    .page-head { margin-bottom: 14px; }
    .page-head > div { width: 100%; }
    .page-head > div .btn { flex: 1; }
    .mobile-only { display: inline-flex !important; }
    .list-first > .list-card { order: -1; }

    .stats { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
    .stat { flex-direction: column; align-items: flex-start; gap: 8px; padding: 14px; }
    .stat .chip { width: 38px; height: 38px; border-radius: 10px; }
    .stat strong { font-size: 17px; }
    .quick { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }

    .search select { max-width: none; }
    .totals { max-width: none; }

    /* Lists: each table row becomes a card */
    .table-cards thead { display: none; }
    .table-cards, .table-cards tbody, .table-cards tr, .table-cards td { display: block; width: 100%; }
    .table-cards tr { padding: 12px 0; border-bottom: 1px solid var(--border); }
    .table-cards tr:last-child { border-bottom: 0; }
    .table-cards tbody tr:hover { background: none; }
    .table-cards td { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border: 0; text-align: right; }
    .table-cards td::before { content: attr(data-label); color: var(--muted); font-size: 14px; text-align: left; }
    .table-cards td:first-child { font-size: 17px; font-weight: 700; }
    .table-cards td:first-child::before { display: none; }
    .table-cards td[data-label=""]::before { display: none; }
    .table-cards td:empty { display: none; }
    .table-cards td.empty { justify-content: center; }
    .table-cards .actions { justify-content: flex-start; gap: 8px; padding-top: 10px; }
    .table-cards .actions a, .table-cards .actions form { margin: 0; }
    .table-cards .actions a, .table-cards .actions button {
        display: inline-flex; align-items: center; min-height: 38px; padding: 0 14px; border-radius: 8px; background: #f1f5f9; font-size: 14px;
    }

    /* Billing page */
    .pos-cart { position: static; }
    .tiles { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); max-height: none; overflow: visible; gap: 10px; }
    .tile { min-height: 88px; padding: 12px; }
    .checkout-bar {
        position: fixed; left: 10px; right: 10px; z-index: 15; bottom: calc(var(--tabbar-h) + 10px + env(safe-area-inset-bottom));
        display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 10px 10px 18px;
        border-radius: 14px; background: #1e1b4b; color: #fff; box-shadow: 0 10px 30px rgba(30, 27, 75, .35);
    }
    .checkout-bar small { display: block; color: #c7d2fe; font-size: 12px; }
    .checkout-bar b { font-size: 19px; }
    .checkout-bar .btn { min-height: 44px; background: #fff; color: #312e81; }
    .has-checkout .main { padding-bottom: calc(var(--tabbar-h) + 90px + env(safe-area-inset-bottom)); }

    .invoice-head { flex-direction: column; gap: 0; }
    .invoice .right { text-align: left; }
}

@media (max-width: 380px) {
    .stats, .quick { grid-template-columns: 1fr 1fr; }
    .stat strong { font-size: 15px; }
    .nav a { font-size: 10px; }
}

/* ---------- Print: only the bill ---------- */
@media print {
    body { background: #fff; }
    .no-print, .sidebar, .mobile-top, .checkout-bar, .alert { display: none !important; }
    .main { margin: 0; padding: 0; }
    .container { margin: 0; padding: 0; max-width: none; }
    .card { border: 0; padding: 0; box-shadow: none; }
    .invoice-head { flex-direction: row; }
    .invoice .right { text-align: right; }
}
