:root {
    color-scheme: light;
    --bg: #f5f7fb;
    --panel: #ffffff;
    --ink: #1f2937;
    --muted: #667085;
    --line: #d9e1ec;
    --primary: #1f7a5a;
    --primary-dark: #155d45;
    --accent: #c27429;
    --danger: #b42318;
    --ok: #167647;
    --shadow: 0 10px 30px rgba(31, 41, 55, .08);
}

* { box-sizing: border-box; }
body {
    margin: 0;
    min-height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
    color: var(--ink);
    background: var(--bg);
    display: grid;
    grid-template-columns: 236px 1fr;
}

body.auth-body {
    display: block;
    background: #eef3f6;
}
.auth-main {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 28px;
}
.auth-card {
    width: min(420px, 100%);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 26px;
}
.auth-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 22px;
}
.auth-brand strong {
    display: block;
    font-size: 20px;
}
.auth-brand small {
    display: block;
    margin-top: 3px;
}
.auth-form {
    display: grid;
    gap: 14px;
}
.auth-submit {
    width: 100%;
    margin-top: 4px;
}

.sidebar {
    background: #17222c;
    color: #fff;
    min-height: 100vh;
    padding: 22px 16px;
    position: sticky;
    top: 0;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 28px; }
.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border-radius: 8px;
    background: var(--primary);
    font-weight: 800;
}
.brand small { display: block; color: #b8c4d1; margin-top: 3px; }
nav { display: grid; gap: 6px; }
nav a {
    color: #cdd6df;
    text-decoration: none;
    padding: 11px 12px;
    border-radius: 8px;
}
nav a.active, nav a:hover { background: rgba(255,255,255,.1); color: #fff; }

.main { padding: 28px; min-width: 0; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 22px;
}
h1, h2, p { margin: 0; }
h1 { font-size: 26px; }
h2 { font-size: 18px; }
.topbar p, .muted, small { color: var(--muted); }
.actions, .form-actions, .filters { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.logout-form { margin: 0; }
.logout-form input { width: auto; }
.user-badge {
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
}

.button, button.button {
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--ink);
    text-decoration: none;
    border-radius: 8px;
    padding: 9px 14px;
    font: inherit;
    cursor: pointer;
}
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.button.primary:hover { background: var(--primary-dark); }
.button.ghost { background: transparent; }
.button.small { padding: 5px 9px; font-size: 13px; }
.button.danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.button.block { display: block; text-align: center; margin-top: 16px; }

.metric-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(140px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}
.metric, .panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}
.metric { padding: 16px; }
.metric span { color: var(--muted); display: block; margin-bottom: 8px; }
.metric strong { font-size: 22px; }
.metric.warning strong { color: var(--accent); }
.dashboard-filter-panel { padding: 12px 14px; }
.dashboard-filters { margin-bottom: 0; }

.split { display: grid; grid-template-columns: 2fr 1fr; gap: 18px; margin-bottom: 18px; }
.panel { padding: 18px; margin-bottom: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel-head.compact { margin-top: 18px; }

.trend-line-chart { min-width: 0; }
.trend-line-chart svg {
    display: block;
    width: 100%;
    min-height: 360px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
}
.trend-grid-line {
    stroke: #e7edf4;
    stroke-width: 1;
}
.trend-line {
    fill: none;
    stroke-width: 4;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.trend-line.sales { stroke: url(#salesLine); }
.trend-line.purchase { stroke: url(#purchaseLine); }
.trend-dot {
    stroke: #fff;
    stroke-width: 2;
}
.trend-dot.sales { fill: var(--primary); }
.trend-dot.purchase { fill: var(--accent); }
.trend-month-label {
    fill: #667085;
    font-size: 12px;
    text-anchor: middle;
}
.trend-value-label {
    font-size: 11px;
    font-weight: 700;
    text-anchor: middle;
    paint-order: stroke;
    stroke: #fff;
    stroke-width: 5px;
    stroke-linejoin: round;
    font-variant-numeric: tabular-nums;
}
.trend-value-label.sales { fill: var(--primary); }
.trend-value-label.purchase { fill: var(--accent); }
.trend-legend {
    display: flex;
    gap: 16px;
    justify-content: flex-end;
    margin-top: 12px;
    color: var(--muted);
    font-size: 13px;
}
.trend-legend span { display: inline-flex; align-items: center; gap: 6px; }
.trend-legend i { width: 10px; height: 10px; border-radius: 3px; }
.trend-legend .sales { background: var(--primary); }
.trend-legend .purchase { background: var(--accent); }
.facts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.facts div { border: 1px solid var(--line); border-radius: 8px; padding: 14px; }
.facts strong { display: block; font-size: 26px; }
.facts span { color: var(--muted); }

.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { background: #f8fafc; font-size: 13px; color: #475467; }
td small { display: block; margin-top: 2px; }
.actions-col { width: 78px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.neg { color: var(--danger); }
.tag { display: inline-block; border-radius: 999px; padding: 3px 9px; font-size: 12px; }
.tag.danger { color: var(--danger); background: #fff1f0; }
.tag.ok { color: var(--ok); background: #edfdf3; }
.notice { margin-bottom: 14px; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); }
.notice.danger { color: var(--danger); background: #fff1f0; border-color: #ffd2cc; }

input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 9px 10px;
    font: inherit;
    background: #fff;
}
label { display: grid; gap: 6px; color: #344054; font-size: 13px; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(160px, 1fr)); gap: 14px; }
.full { margin-top: 14px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: auto; }
.filters { margin-bottom: 14px; }
.filters input[type="search"] { max-width: 320px; }
.filters-inline {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
}
.filters-inline select { width: 128px; min-width: 128px; }
.filters-inline .product-filter-select { width: 176px; min-width: 176px; }
.filters-inline label {
    display: flex;
    align-items: center;
    grid-template-columns: none;
    gap: 6px;
    white-space: nowrap;
    min-width: max-content;
}
.filters-inline input[type="date"] { width: 126px; min-width: 126px; }
.filters-inline .button { white-space: nowrap; }
.partners-table { table-layout: fixed; }
.partners-table th, .partners-table td {
    overflow: hidden;
    text-overflow: ellipsis;
}
.partner-name-col { width: 180px; }
.partner-contact-col { width: 110px; }
.partner-phone-col { width: 130px; }
.partner-address-col { width: 240px; }
.date-col { width: 118px; }
.partner-notes-col { width: 108px; }
.product-ledger-table { table-layout: fixed; }
.product-ledger-table th, .product-ledger-table td {
    overflow: hidden;
    text-overflow: ellipsis;
}
.product-category-col { width: 120px; }
.product-sku-col { width: 300px; }
.product-supplier-col { width: 360px; }
.price-col { width: 116px; }
.count-col { width: 82px; }
.status-col { width: 82px; }
.product-ledger-table .product-supplier-col {
    overflow: visible;
    text-overflow: clip;
    white-space: normal;
    word-break: keep-all;
    line-height: 1.4;
}
.product-ledger-table .product-sku-col strong { font-weight: 600; }
.partner-actions { flex-wrap: nowrap; justify-content: flex-end; }
.partner-search {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: nowrap;
}
.partner-search input[type="search"] { width: 220px; min-width: 220px; }
.partner-search .button { white-space: nowrap; }
.orders-table {
    table-layout: fixed;
    font-size: 13px;
}
.orders-table th, .orders-table td {
    padding: 9px 8px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.order-date-col { width: 82px; }
.party-col { width: 96px; }
.order-category-col { width: 88px; }
.order-customer-col { width: 88px; }
.order-model-col { width: 152px; }
.orders-table .count-col { width: 52px; }
.qty-col { width: 66px; }
.amount-col { width: 84px; }
.orders-table .actions-col { width: 104px; }
.purchases-table th, .purchases-table td { padding: 7px 5px; }
.purchases-table {
    table-layout: fixed;
    width: 100%;
}
.purchases-table th,
.purchases-table td {
    white-space: nowrap;
}
.purchases-table .purchase-date-col { width: 7%; }
.purchases-table .party-col { width: 12%; }
.purchases-table .order-category-col { width: 6%; }
.purchases-table .order-customer-col { width: 15%; }
.purchases-table .order-model-col { width: 11%; }
.purchases-table .count-col { width: 4%; }
.purchases-table .qty-col { width: 5%; }
.purchases-table .amount-col { width: 7%; }
.purchases-table .amount-supply-gap-col { width: 2%; padding: 0; }
.purchases-table .supply-date-col { width: 8%; }
.purchases-table .invoice-date-col { width: 7%; }
.purchases-table .payment-date-col { width: 7%; }
.purchases-table .actions-col { width: 7%; }
.orders-table.purchases-table .order-actions { gap: 0; }
.orders-table.purchases-table .button.small {
    padding-left: 2px;
    padding-right: 2px;
    font-size: 12px;
}
.sales-table th, .sales-table td { padding: 7px 5px; }
.sales-table {
    table-layout: fixed;
    width: 100%;
}
.sales-table th,
.sales-table td {
    white-space: nowrap;
}
.sales-table .sale-date-col { width: 7%; }
.sales-table .party-col { width: 17%; }
.sales-table .order-category-col { width: 6%; }
.sales-table .order-model-col { width: 13%; }
.sales-table .count-col { width: 4%; }
.sales-table .qty-col { width: 5%; }
.sales-table .sale-amount-col { width: 7%; }
.sales-table .profit-col { width: 7%; }
.sales-table .profit-delivery-gap-col { width: 2%; padding: 0; }
.sales-table .delivery-date-col { width: 8%; }
.sales-table .invoice-date-col { width: 7%; }
.sales-table .receipt-date-col { width: 7%; }
.sales-table .actions-col { width: 10%; }
.orders-table.sales-table .order-actions { gap: 3px; }
.orders-table.sales-table .button.small { padding-left: 7px; padding-right: 7px; }
.orders-table .button.small {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    padding: 5px 9px;
    font-size: 13px;
    line-height: 1.2;
}
.orders-table td:last-child { overflow: visible; }
.order-actions {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-wrap: nowrap;
    white-space: nowrap;
}
.export-menu {
    position: relative;
    display: inline-block;
    vertical-align: top;
}
.export-menu summary {
    box-sizing: border-box;
    list-style: none;
    cursor: pointer;
    user-select: none;
}
.export-menu summary::-webkit-details-marker { display: none; }
.export-menu div {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 58px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 8px 18px rgba(16, 24, 40, 0.10);
}
.export-menu:not([open]) div { display: none; }
.export-menu a {
    display: block;
    padding: 5px 8px;
    border-radius: 6px;
    color: var(--text);
    font-size: 12px;
    line-height: 1.1;
}
.export-menu a:hover { background: #f8fafc; }

.line-table { display: grid; gap: 8px; }
.line-head, .line-row {
    display: grid;
    grid-template-columns: minmax(280px, 2fr) 110px 130px 130px 110px 110px 42px;
    gap: 8px;
    align-items: center;
}
.line-head.sale, .line-row.sale {
    grid-template-columns: 150px minmax(280px, 2fr) 90px 120px 120px 100px 100px 42px;
}
.line-head.purchase, .line-row.purchase {
    grid-template-columns: 150px minmax(280px, 2fr) 90px 120px 120px 100px 42px;
}
.line-head { color: var(--muted); font-size: 13px; }
output { text-align: right; font-variant-numeric: tabular-nums; }
.icon-button {
    width: 34px;
    height: 34px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
    font-size: 20px;
}
.form-actions { justify-content: flex-end; margin-top: 18px; }
.danger-zone {
    border: 1px solid #ffd2cc;
    border-radius: 8px;
    padding: 12px;
    margin-top: 14px;
    background: #fff8f7;
}
.danger-zone summary { cursor: pointer; font-weight: 700; color: var(--danger); }
.danger-zone p { margin: 8px 0 12px; }

@media (max-width: 1080px) {
    body { grid-template-columns: 1fr; }
    .sidebar { position: static; min-height: auto; }
    nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .metric-grid { grid-template-columns: repeat(2, 1fr); }
    .split, .form-grid { grid-template-columns: 1fr; }
    .line-head { display: none; }
    .line-row, .line-row.sale, .line-row.purchase { grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 8px; padding: 10px; }
}
