:root {
    --ink: #2B4F60;
    --blue: #5AAFC7;
    --mist: #BDC7C9;
    --wine: #845460;
    --shell: #EAD3CB;
    --paper: #fbf8f7;
    --line: rgba(43, 79, 96, 0.14);
    --shadow: 0 18px 45px rgba(43, 79, 96, 0.12);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-height: 100vh;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--paper);
    color: var(--ink);
}

a { color: inherit; text-decoration: none; }

.app-shell {
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 3000;
    width: min(292px, calc(100vw - 36px));
    height: 100vh;
    padding: 22px 18px;
    background: var(--ink);
    color: white;
    box-shadow: 18px 0 45px rgba(43, 79, 96, 0.22);
    transform: translateX(-105%);
    transition: transform 180ms ease;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.app-shell.sidebar-open .sidebar {
    transform: translateX(0);
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 2500;
    background: rgba(43, 79, 96, 0.34);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
}

.app-shell.sidebar-open .sidebar-overlay {
    opacity: 1;
    pointer-events: auto;
}

.leaflet-container {
    z-index: 1;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    font-weight: 800;
    margin-bottom: 24px;
}

.brand span:nth-child(2) {
    flex: 1;
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border-radius: 8px;
    background: var(--blue);
    color: var(--ink);
    font-weight: 900;
}

.nav {
    display: grid;
    gap: 8px;
    flex: 1;
    align-content: start;
}

.nav a,
.mobile-nav a,
.sidebar-logout {
    display: flex;
    align-items: center;
    gap: 10px;
    border-radius: 8px;
    padding: 11px 12px;
    color: rgba(255, 255, 255, 0.78);
    font-weight: 650;
}

.nav a svg {
    width: 19px;
    height: 19px;
    flex: 0 0 auto;
}

.nav a.is-active, .nav a:hover {
    background: rgba(234, 211, 203, 0.14);
    color: white;
}

.sidebar-account {
    display: grid;
    gap: 10px;
    margin-top: 22px;
    padding-top: 16px;
    border-top: 1px solid rgba(255, 255, 255, 0.13);
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.08);
}

.user-avatar {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: var(--shell);
    color: var(--ink);
    font-weight: 900;
}

.user-name,
.user-role {
    display: block;
}

.user-name {
    color: white;
    font-weight: 850;
    line-height: 1.15;
}

.user-role {
    margin-top: 2px;
    color: rgba(255, 255, 255, 0.62);
    font-size: 0.76rem;
    font-weight: 750;
    text-transform: capitalize;
}

.sidebar-logout {
    width: 100%;
    border: 1px solid rgba(234, 211, 203, 0.18);
    background: rgba(234, 211, 203, 0.10);
    color: white;
}

.sidebar-logout:hover {
    background: rgba(234, 211, 203, 0.18);
}

.main {
    min-width: 0;
    padding-bottom: 84px;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-height: 68px;
    padding: 12px 24px;
    background: rgba(251, 248, 247, 0.92);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(16px);
}

.topbar-main {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: white;
    color: var(--ink);
    cursor: pointer;
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

.table-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

.table-actions form {
    margin: 0;
}

.table-icon-action {
    width: 36px;
    height: 36px;
    background: white;
}

.table-icon-action.danger {
    border-color: rgba(132, 84, 96, 0.34);
    background: rgba(132, 84, 96, 0.10);
    color: #b42334;
}

.table-icon-action.danger:hover {
    border-color: #b42334;
    background: rgba(180, 35, 52, 0.13);
}

.menu-toggle {
    flex: 0 0 auto;
    background: var(--ink);
    border-color: var(--ink);
    color: white;
}

.topbar-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    min-width: 0;
}

.sidebar-close {
    width: 36px;
    height: 36px;
    border-color: rgba(255, 255, 255, 0.18);
    background: rgba(255, 255, 255, 0.08);
    color: white;
}

.page {
    padding: 24px;
}

.page-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--wine);
    font-size: 0.78rem;
    font-weight: 800;
    text-transform: uppercase;
}

h1, h2, h3 { margin: 0; letter-spacing: 0; }

h1 { font-size: clamp(1.55rem, 2.6vw, 2.3rem); }

.muted { color: #6f8187; }

.grid {
    display: grid;
    gap: 16px;
}

.grid.stats {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.card {
    background: white;
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.card-pad { padding: 18px; }

.stat-value {
    margin-top: 8px;
    font-size: 2rem;
    font-weight: 850;
}

.dashboard-stat p {
    margin: 0;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    padding: 0 14px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: var(--ink);
    color: white;
    font-weight: 750;
    cursor: pointer;
}

.btn.secondary {
    background: white;
    border-color: var(--line);
    color: var(--ink);
}

.btn.accent { background: var(--wine); }
.btn.is-disabled {
    opacity: 0.45;
    pointer-events: none;
}

.table-wrap { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

th {
    color: #66777d;
    font-size: 0.78rem;
    text-transform: uppercase;
}

th[data-sortable-column] {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

th[data-sortable-column]::after {
    content: "sort";
    display: inline-block;
    margin-left: 6px;
    color: #9aabb1;
    font-size: 0.64rem;
    text-transform: lowercase;
}

th[data-sortable-column][aria-sort="ascending"]::after {
    content: "asc";
    color: var(--ink);
}

th[data-sortable-column][aria-sort="descending"]::after {
    content: "desc";
    color: var(--ink);
}

th[data-sortable-column]:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 2px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.availability-filter-row {
    display: grid;
    grid-template-columns: minmax(180px, 1.4fr) minmax(110px, 0.7fr) minmax(90px, 0.55fr) auto;
    gap: 12px;
    align-items: end;
}

.availability-filter-action {
    display: flex;
    align-items: end;
}

.availability-filter-action .btn {
    width: 100%;
}

.admin-card-list {
    display: none;
}

.admin-card {
    display: grid;
    gap: 12px;
}

.admin-card dl {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 6px 12px;
    margin: 0;
}

.admin-card dt {
    color: #66777d;
    font-weight: 800;
}

.admin-card dd {
    margin: 0;
}

.autocomplete-field {
    position: relative;
}

.autocomplete-results {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 4px);
    z-index: 30;
    max-height: 280px;
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: white;
    box-shadow: var(--shadow);
}

.autocomplete-option {
    display: block;
    width: 100%;
    padding: 11px 12px;
    border: 0;
    border-bottom: 1px solid var(--line);
    background: white;
    color: var(--ink);
    text-align: left;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
}

.autocomplete-option:hover {
    background: var(--paper);
}

label {
    display: grid;
    gap: 7px;
    font-weight: 700;
}

.pattern-filter {
    display: grid;
    gap: 7px;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
}

.pattern-filter legend {
    padding: 0;
    font-weight: 700;
}

.pattern-inputs {
    display: grid;
    grid-template-columns: repeat(5, 42px);
    gap: 8px;
    align-items: center;
}

.pattern-inputs input {
    min-height: 42px;
    padding: 8px 0;
    text-align: center;
    font-weight: 850;
    font-variant-numeric: tabular-nums;
}

.check-inline {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

input, select, textarea {
    width: 100%;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--ink);
    background: white;
    font: inherit;
}

input[type="checkbox"] {
    width: 18px;
    min-height: 18px;
    padding: 0;
}

.ts-wrapper.single .ts-control,
.ts-control {
    width: 100%;
    min-height: 42px;
    border-color: var(--line);
    border-radius: 8px;
    padding: 8px 12px;
    color: var(--ink);
    font: inherit;
}

.ts-dropdown {
    z-index: 10000;
    max-height: 320px;
    border-color: var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.ts-dropdown .dropdown-input {
    min-height: 40px;
}

textarea { min-height: 110px; resize: vertical; }

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 16px;
}

.alert {
    margin-bottom: 16px;
    padding: 12px 14px;
    border-radius: 8px;
    border: 1px solid var(--line);
}

.alert.success { background: #edf8f5; color: #1c6559; }
.alert.error { background: #fff1ef; color: #914136; }

.availability-grid {
    display: grid;
    grid-template-columns: repeat(10, minmax(0, 1fr));
    gap: 8px;
}

.availability-cell {
    border-left: 6px solid var(--blue);
}

.availability-cell.card-pad {
    padding: 10px;
}

.availability-cell .pill {
    font-size: 0.72rem;
}

.availability-cell .stat-value {
    margin-top: 6px;
    font-size: 1.35rem;
    line-height: 1;
}

.availability-cell .muted {
    margin: 6px 0 0;
    font-size: 0.72rem;
    line-height: 1.2;
}

.availability-stock-list {
    display: grid;
    gap: 2px;
    margin: 8px 0 0;
    font-size: 0.72rem;
    line-height: 1.2;
    color: var(--muted);
}

.availability-stock-list div {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.availability-stock-list dt,
.availability-stock-list dd {
    margin: 0;
}

.availability-stock-list dt {
    font-weight: 700;
    color: var(--text);
}

.availability-stock-list dd {
    font-variant-numeric: tabular-nums;
}

.availability-cell.danger {
    border-left-color: #bb3e3e;
    background: #fff4f2;
}

.availability-cell.warn {
    border-left-color: #d8872f;
    background: #fff7ec;
}

.availability-cell.yellow {
    border-left-color: #d6b21f;
    background: #fffbe8;
}

.availability-cell.ok {
    border-left-color: #2f8b62;
    background: #eff8f2;
}
.availability-cell.is-empty {
    background: #fff7f5;
}

.availability-actions {
    display: grid;
    gap: 8px;
    margin-top: 10px;
}

.availability-actions .btn {
    min-height: 34px;
    width: 100%;
    padding: 0 8px;
    font-size: 0.78rem;
}

.stock-empty-label {
    margin: 6px 0 0;
    color: #bb3e3e;
    font-weight: 900;
    font-size: 0.72rem;
    line-height: 1.15;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 5000;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(43, 79, 96, 0.42);
}

.modal-backdrop[hidden] {
    display: none;
}

.modal-panel {
    width: min(720px, 100%);
    max-height: min(76vh, 720px);
    overflow: auto;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: white;
    box-shadow: var(--shadow);
}

.stock-list {
    display: grid;
    gap: 8px;
}

.stock-list-row {
    display: grid;
    grid-template-columns: minmax(78px, 0.8fr) minmax(70px, 0.7fr) minmax(90px, 0.8fr) minmax(0, 1.2fr);
    gap: 10px;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid var(--line);
}

.stock-list-row strong {
    font-size: 1.05rem;
}

.stock-count {
    color: #2f8b62;
    font-weight: 900;
}

.pill {
    display: inline-flex;
    padding: 4px 8px;
    border-radius: 999px;
    background: var(--shell);
    font-size: 0.78rem;
    font-weight: 800;
}

.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.exchange-lines {
    margin-top: 16px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: white;
}

.line-list {
    display: grid;
    gap: 12px;
}

.exchange-line {
    overflow: visible;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fffdfc;
}

.line-fields {
    display: grid;
    align-items: end;
    gap: 10px;
}

.delivered-fields {
    grid-template-columns: minmax(280px, 1.8fr) minmax(110px, 0.7fr) minmax(140px, 0.8fr) minmax(160px, 1fr) auto;
}

.received-fields {
    grid-template-columns: minmax(110px, 0.7fr) minmax(130px, 0.8fr) minmax(140px, 0.8fr) minmax(140px, 0.8fr) minmax(110px, 0.7fr) minmax(160px, 1fr) auto;
}

.line-stock {
    margin: 8px 0 0;
}

.line-error {
    margin: 10px 0 0;
}

.line-summary {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 12px;
}

.difference {
    color: #2f8b62;
}

.difference.is-warning {
    color: #914136;
}

.login-screen {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 24px;
    background: linear-gradient(145deg, var(--ink), #38677a 55%, var(--wine));
}

.login-card {
    width: min(420px, 100%);
    padding: 28px;
    background: white;
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.mobile-nav {
    display: none;
}

.origin-switcher label {
    display: grid;
    min-width: 230px;
}

.origin-switcher select {
    min-height: 42px;
    padding: 8px 38px 8px 12px;
    border-radius: 8px;
    border-color: rgba(43, 79, 96, 0.2);
    background-color: #fffdfc;
    color: var(--ink);
    font-weight: 800;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 980px) {
    .topbar { min-height: 64px; padding: 10px 14px; }
    .topbar-actions { flex: 1; }
    .origin-switcher { flex: 1; }
    .origin-switcher label { min-width: 0; }
    .page { padding: 16px; }
    .grid.stats { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
    .dashboard-stat.card-pad { padding: 12px 8px; }
    .dashboard-stat .muted {
        font-size: 0.72rem;
        font-weight: 800;
        line-height: 1.15;
    }
    .dashboard-stat .stat-value {
        margin-top: 5px;
        font-size: 1.25rem;
        line-height: 1;
    }
    .form-grid { grid-template-columns: 1fr; }
    .availability-grid { grid-template-columns: repeat(5, minmax(0, 1fr)); }
    .admin-table-card { display: none; }
    .admin-card-list { display: grid; gap: 12px; }
    .section-head { align-items: stretch; display: grid; }
    .delivered-fields, .received-fields { grid-template-columns: 1fr; }
    .line-remove { width: 100%; }
    .mobile-nav {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 20;
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 4px;
        padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
        background: rgba(255, 255, 255, 0.96);
        border-top: 1px solid var(--line);
        border-radius: 0;
        box-shadow: 0 -8px 22px rgba(43, 79, 96, 0.12);
        backdrop-filter: blur(16px);
    }
    .mobile-nav a {
        display: grid;
        justify-content: center;
        justify-items: center;
        gap: 4px;
        min-width: 0;
        padding: 8px 2px;
        border-radius: 14px;
        color: #66777d;
        font-size: 0.68rem;
        line-height: 1;
        text-align: center;
    }
    .mobile-nav a svg {
        width: 20px;
        height: 20px;
    }
    .mobile-nav a span {
        display: block;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .mobile-nav a.is-active {
        background: var(--ink);
        color: white;
    }
}

@media (max-width: 560px) {
    .grid.stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .page-head { display: grid; }
    .topbar-actions { max-width: calc(100vw - 76px); }
    .origin-switcher select { min-height: 40px; font-size: 0.88rem; }
    .availability-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .availability-filters {
        overflow-x: auto;
    }
    .availability-filter-row {
        grid-template-columns: minmax(145px, 1.2fr) minmax(92px, 0.75fr) minmax(76px, 0.55fr) minmax(98px, 0.8fr);
        min-width: 430px;
        gap: 8px;
    }
    .availability-filter-row label {
        font-size: 0.78rem;
    }
    .availability-filter-row select,
    .availability-filter-action .btn {
        min-height: 38px;
        padding-left: 8px;
        padding-right: 8px;
        font-size: 0.82rem;
    }
    .stock-list-row {
        grid-template-columns: 1fr;
        gap: 4px;
    }
    .pattern-inputs { grid-template-columns: repeat(5, minmax(38px, 1fr)); }
    th, td { padding: 10px 8px; }
}
