:root {
    --ink: #0F1E26;
    --ink-2: #1B2F3A;
    --canvas: #F1F4F4;
    --surface: #FFFFFF;
    --line: #DDE3E5;
    --line-strong: #C3CDD1;
    --text: #0F1E26;
    --muted: #5B6B72;
    --primary: #0E8A74;
    --primary-d: #0A6B59;
    --primary-t: #E3F4EF;
    --danger: #B42318;
    --danger-t: #FDECEA;
    --warn: #8A5300;
    --warn-t: #FFF1D6;
    --r-sm: 6px;
    --r-md: 10px;
    --r-lg: 14px;
    --font-body: "Instrument Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
    --font-head: "Bricolage Grotesque", "Instrument Sans", system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--canvas);
}
h1, h2, h3 { font-family: var(--font-head); font-weight: 650; letter-spacing: -.015em; line-height: 1.15; margin: 0; }
a { color: inherit; text-decoration: none; }
img { display: block; max-width: 100%; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.ico { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* App shell */
.shell { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 100vh; }
.side {
    background: var(--ink);
    color: #A9BBC2;
    padding: 24px 16px;
    position: sticky; top: 0; height: 100vh;
    display: flex; flex-direction: column; gap: 28px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px; color: #fff; font-family: var(--font-head); font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--primary); display: grid; place-items: center; }
.brand-mark svg { width: 17px; height: 17px; fill: none; stroke: #fff; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--r-sm); font-weight: 500; transition: background .15s, color .15s; }
.nav a:hover { background: var(--ink-2); color: #fff; }
.nav a[aria-current="page"] { background: var(--ink-2); color: #fff; box-shadow: inset 2px 0 0 var(--primary); }
.nav svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.main { padding: 32px 40px 64px; width: 100%; max-width: 1320px; }

/* Page header */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 24px; }
.page-head h1 { font-size: 30px; }
.page-head p { margin: 6px 0 0; color: var(--muted); }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    height: 38px; padding: 0 14px;
    border-radius: var(--r-sm); border: 1px solid var(--line);
    background: var(--surface); color: var(--text);
    font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
    transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--line-strong); background: #F8FAFA; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); border-color: var(--primary-d); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-t); border-color: #F3C3BE; }
.btn-sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn[aria-disabled="true"] { opacity: .45; pointer-events: none; }

/* Messages */
.flash { display: flex; gap: 10px; padding: 12px 16px; margin-bottom: 20px; border-radius: var(--r-md); background: var(--primary-t); color: var(--primary-d); border: 1px solid #BFE5DB; font-weight: 500; }
.flash.error { background: var(--danger-t); color: var(--danger); border-color: #F3C3BE; }
.flash ul { margin: 0; padding-left: 18px; }

/* Toolbar, chips */
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; }
.search { position: relative; flex: 1; max-width: 400px; }
.search .ico { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input[type="search"] { padding-left: 40px; -webkit-appearance: none; appearance: none; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; height: 32px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-weight: 500; font-size: 14px; color: var(--muted); transition: color .15s, border-color .15s; }
.chip:hover { color: var(--text); border-color: var(--line-strong); }
.chip[aria-current="true"] { background: var(--ink); border-color: var(--ink); color: #fff; }
.chips.sub .chip { height: 28px; font-size: 13px; background: transparent; }
.chips.sub .chip[aria-current="true"] { background: var(--primary-t); border-color: var(--primary-t); color: var(--primary-d); }

/* Product grid */
.products { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 18px; margin-top: 20px; }
.product { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; display: flex; flex-direction: column; transition: border-color .15s; }
.product:hover { border-color: #B1BFC4; }
.media { aspect-ratio: 4 / 3; background: #E6ECEE; display: grid; place-items: center; overflow: hidden; }
.media img { width: 100%; height: 100%; object-fit: cover; }
.media .initial { font-family: var(--font-head); font-size: 56px; font-weight: 700; color: #B1C1C7; }
.p-body { padding: 14px 16px 14px; display: flex; flex-direction: column; flex: 1; }
.crumb { margin: 0; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-name { font-size: 17px; font-weight: 600; margin: 2px 0 0; }
.p-price { display: flex; align-items: baseline; gap: 10px; margin: 10px 0 12px; }
.price { font-family: var(--font-head); font-size: 25px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.price small { font-family: var(--font-body); font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 5px; letter-spacing: 0; }
.margin { margin-left: auto; font-size: 12.5px; font-weight: 600; color: var(--primary-d); background: var(--primary-t); padding: 2px 9px; border-radius: 999px; }
.margin.neg { color: var(--danger); background: var(--danger-t); }
.facts { display: grid; gap: 5px; margin: 0; font-size: 13px; }
.facts div { display: flex; justify-content: space-between; gap: 12px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.p-foot { display: flex; gap: 8px; padding: 10px 16px; border-top: 1px solid var(--line); }
.p-foot form { margin: 0; }

/* Lists (categories, suppliers) */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; }
.row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 16px; align-items: center; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row.has-id { grid-template-columns: 44px minmax(0, 1fr) auto auto; }
.row-id { color: var(--muted); font-size: 13.5px; font-variant-numeric: tabular-nums; }
.cell-main a.name:hover { text-decoration: underline; }
.who { min-width: 0; display: grid; line-height: 1.3; }
.who .contact { color: var(--muted); font-weight: 400; font-size: 13px; }
.who .contact a:hover { text-decoration: underline; }
.toolbar .sort { display: flex; align-items: center; gap: 8px; margin-left: auto; white-space: nowrap; }
.toolbar .sort span { color: var(--muted); font-size: 13.5px; }
.toolbar .sort select { width: auto; min-width: 190px; }

/* Supplier balance on the list card */
.balance { text-align: right; line-height: 1.25; min-width: 130px; }
.balance small { display: block; color: var(--muted); font-size: 12px; }
.balance strong { font-family: var(--font-head); font-size: 16px; font-variant-numeric: tabular-nums; }
.balance.owed strong { color: var(--danger); }
.balance.settled strong, .balance.credit strong { color: var(--primary-d); }

/* Supplier page */
.head-id { display: flex; align-items: center; gap: 14px; }
.head-id p { margin: 4px 0 0; }
.head-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.thumb.lg { width: 56px; height: 56px; border-radius: 12px; font-size: 22px; }
.link { text-decoration: underline; text-underline-offset: 2px; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 22px; }
.stats.single { grid-template-columns: minmax(0, 360px); }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 16px 20px; }
.stat span { display: block; color: var(--muted); font-size: 13.5px; font-weight: 500; }
.stat strong { display: block; margin-top: 4px; font-family: var(--font-head); font-size: 26px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat strong small { font-family: var(--font-body); font-size: 13px; font-weight: 600; color: var(--muted); margin-left: 5px; letter-spacing: 0; }
.stat.owed { background: var(--danger-t); border-color: #F3C3BE; }
.stat.owed strong { color: var(--danger); }
.stat.settled strong, .stat.credit strong { color: var(--primary-d); }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 13px; font-weight: 600; color: var(--muted); padding: 10px 16px; background: #F8FAFA; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 12px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .pay { color: var(--primary-d); }
.table .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.table .row-actions { justify-content: flex-end; }

.badge { display: inline-block; padding: 2px 9px; margin-right: 6px; border-radius: 999px; font-size: 12.5px; font-weight: 600; background: #E6ECEE; color: var(--ink-2); }
.badge.invoice { background: var(--warn-t); color: var(--warn); }
.badge.installment, .badge.paid { background: var(--primary-t); color: var(--primary-d); }
.badge.partial { background: var(--warn-t); color: var(--warn); }
.badge.unpaid { background: var(--danger-t); color: var(--danger); }
.cell-main { display: flex; align-items: center; gap: 12px; min-width: 0; padding-left: calc(var(--depth, 0) * 30px); font-weight: 600; }
.cell-main .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.elbow { width: 14px; height: 14px; border-left: 2px solid var(--line-strong); border-bottom: 2px solid var(--line-strong); border-bottom-left-radius: 6px; margin-top: -10px; flex: none; }
.thumb { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: #E6ECEE; flex: none; }
.thumb.ph { display: grid; place-items: center; font-family: var(--font-head); font-weight: 700; color: #8FA3AA; }
.count { color: var(--muted); font-size: 13.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 6px; }
.row-actions form { margin: 0; }

/* Forms */
.form-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; align-items: start; }
.form-layout.narrow { grid-template-columns: minmax(0, 560px); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 24px; min-width: 0; }
.fields { display: grid; gap: 18px; }
.fields.two { grid-template-columns: 1fr 1fr; }
.field { display: grid; gap: 6px; align-content: start; min-width: 0; }
.field > span { font-weight: 600; font-size: 13.5px; }
.field small { color: var(--muted); }
textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff; font: inherit; color: inherit; resize: vertical; }
input[type="text"], input[type="search"], input[type="number"], input[type="date"], input[type="tel"], select {
    width: 100%; height: 40px; padding: 0 12px;
    border: 1px solid var(--line); border-radius: var(--r-sm);
    background: #fff; font: inherit; color: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(14, 138, 116, .18); }
input[type="file"] { width: 100%; min-width: 0; font: inherit; font-size: 13px; color: var(--muted); }
input[type="file"]::file-selector-button { height: 32px; padding: 0 12px; margin-right: 10px; border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff; font: inherit; font-weight: 600; color: var(--text); cursor: pointer; }
.preview { position: relative; width: 100%; aspect-ratio: 4 / 3; border-radius: var(--r-md); border: 1px dashed var(--line-strong); background: #F7F9F9; display: grid; place-items: center; color: var(--muted); font-size: 13px; overflow: hidden; }
.preview img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.form-actions { display: flex; gap: 10px; margin-top: 24px; }

/* Empty state, pager */
.empty { padding: 56px 24px; text-align: center; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: var(--r-lg); }
.empty h3 { color: var(--text); font-size: 18px; margin-bottom: 6px; }
.empty .btn { margin-top: 16px; }
.pager { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 28px; color: var(--muted); font-size: 14px; }

@media (max-width: 900px) {
    .shell { grid-template-columns: 1fr; }
    .side { position: static; height: auto; flex-direction: row; align-items: center; gap: 12px; padding: 12px 16px; overflow-x: auto; }
    .nav { flex-direction: row; }
    .main { padding: 20px 16px 48px; }
    .page-head { flex-direction: column; align-items: flex-start; }
    .form-layout { grid-template-columns: 1fr; }
    .fields.two { grid-template-columns: 1fr; }
    .stats { grid-template-columns: 1fr; }
    .row.has-id { grid-template-columns: 44px minmax(0, 1fr); }
    .row.has-id .count, .row.has-id .balance, .row.has-id .row-actions { grid-column: 2; text-align: left; }
    .toolbar { flex-wrap: wrap; }
    .search { max-width: none; }
}
@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}
