:root {
    --bg: #f4f8fb;
    --panel: #ffffff;
    --panel-soft: #f8fbfd;
    --ink: #18243d;
    --muted: #667085;
    --line: #d8e3ee;
    --line-strong: #c7d6e5;
    --primary: #253080;
    --primary-dark: #18205d;
    --primary-soft: #edf1ff;
    --brand-blue: #1c75bc;
    --brand-cyan: #27aae1;
    --accent: #27aae1;
    --danger: #b42318;
    --success: #177245;
    --warning: #b7791f;
    --shadow: 0 14px 40px rgba(37, 48, 128, 0.08);
    --sidebar-width: 292px;
    --sidebar-collapsed-width: 86px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: "Montserrat", "Segoe UI", Arial, Helvetica, sans-serif;
    font-size: 15px;
    line-height: 1.45;
}

a {
    color: var(--primary);
    text-decoration: none;
}

button,
input,
select,
textarea {
    font: inherit;
}

.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    transition: grid-template-columns 180ms ease;
}

.sidebar-collapsed .app-shell {
    grid-template-columns: var(--sidebar-collapsed-width) minmax(0, 1fr);
}

.sidebar {
    background: #ffffff;
    color: #fff;
    padding: 22px;
    display: flex;
    flex-direction: column;
    gap: 22px;
    border-right: 1px solid var(--line);
    box-shadow: 8px 0 28px rgba(37, 48, 128, 0.05);
    min-width: 0;
}

.sidebar-head {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 38px;
    gap: 10px;
    align-items: start;
    padding: 10px 0 18px;
    border-bottom: 1px solid var(--line);
}

.brand {
    display: grid;
    gap: 8px;
    min-width: 0;
}

.brand-logo {
    width: min(210px, 100%);
    height: auto;
}

.brand-icon {
    display: none;
    width: 36px;
    height: 36px;
    object-fit: contain;
}

.brand strong,
.brand small {
    display: block;
}

.brand strong {
    color: var(--primary);
    line-height: 1.2;
}

.brand small {
    margin-top: 3px;
    color: var(--muted);
    font-weight: 700;
    letter-spacing: 0;
}

.sidebar-toggle {
    width: 38px;
    height: 38px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--primary);
    display: inline-grid;
    place-items: center;
    cursor: pointer;
}

.sidebar-toggle:hover {
    background: var(--primary-soft);
    border-color: #d6ddff;
}

.side-nav {
    display: grid;
    gap: 6px;
}

.side-nav a,
.ghost-button {
    color: #40516a;
    border-radius: 8px;
    padding: 11px 12px;
    display: flex;
    align-items: center;
    gap: 11px;
    font-weight: 700;
}

.side-nav i,
.ghost-button i {
    width: 18px;
    min-width: 18px;
    color: var(--brand-blue);
    text-align: center;
}

.side-nav a.active,
.side-nav a:hover {
    background: var(--primary-soft);
    color: var(--primary);
}

.ghost-button {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    cursor: pointer;
    justify-content: flex-start;
    margin-top: auto;
}

.sidebar-collapsed .sidebar {
    padding: 18px 14px;
    align-items: center;
}

.sidebar-collapsed .sidebar-head {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 12px;
    padding: 8px 0 16px;
    width: 100%;
}

.sidebar-collapsed .brand {
    justify-items: center;
}

.sidebar-collapsed .brand-logo,
.sidebar-collapsed .brand small,
.sidebar-collapsed .side-nav span,
.sidebar-collapsed .ghost-button span {
    display: none;
}

.sidebar-collapsed .brand-icon {
    display: block;
}

.sidebar-collapsed .side-nav,
.sidebar-collapsed .sidebar form {
    width: 100%;
}

.sidebar-collapsed .side-nav a,
.sidebar-collapsed .ghost-button {
    width: 100%;
    justify-content: center;
    padding: 12px;
}

.sidebar-collapsed .side-nav i,
.sidebar-collapsed .ghost-button i {
    width: 20px;
    min-width: 20px;
    font-size: 17px;
}

.content {
    padding: 30px;
    min-width: 0;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

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

.topbar-sidebar-toggle {
    display: none;
}

.topbar h1,
.panel h2,
.auth-card h1 {
    margin: 0;
    color: var(--ink);
}

.topbar h1 {
    font-size: 28px;
    line-height: 1.15;
}

.topbar p,
.panel-header p,
.muted {
    color: var(--muted);
    margin: 6px 0 0;
}

.user-pill,
.status,
.mode {
    border-radius: 999px;
    padding: 6px 10px;
    font-size: 13px;
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 700;
}

.status.active {
    background: #e8f6ef;
    color: var(--success);
}

.status.inactive {
    background: #f4e9e8;
    color: var(--danger);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}

.stats-grid.compact {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

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

.stat-card {
    padding: 18px;
    border-top: 4px solid var(--brand-cyan);
}

.stat-card span,
.stat-card small {
    color: var(--muted);
}

.stat-card strong {
    display: block;
    margin: 8px 0 4px;
    font-size: 26px;
    color: var(--primary);
    line-height: 1.15;
}

.panel {
    margin-bottom: 20px;
    overflow: hidden;
}

.panel.narrow {
    max-width: 780px;
}

.panel-header {
    padding: 20px;
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    background: linear-gradient(180deg, #ffffff 0%, #f9fcfe 100%);
}

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

.table-scroll-top {
    overflow-x: auto;
    overflow-y: hidden;
    border-bottom: 1px solid var(--line);
    background: #f8fbfd;
}

.table-scroll-inner {
    height: 14px;
}

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

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

th {
    color: #344553;
    font-size: 13px;
    background: #f2f7fb;
    border-bottom-color: var(--line-strong);
}

tbody tr:hover {
    background: #fbfdff;
}

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

.sort-icons {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.sort-icon {
    width: 22px;
    height: 22px;
    border-radius: 6px;
    display: inline-grid;
    place-items: center;
    color: #8795a1;
    border: 1px solid transparent;
}

.sort-icon:hover,
.sort-icon.active {
    color: var(--primary);
    background: var(--primary-soft);
    border-color: #d6ddff;
}

td small {
    color: var(--muted);
}

.empty-cell {
    color: var(--muted);
    text-align: center;
    padding: 28px;
}

.actions {
    display: flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
    vertical-align: middle;
}

td.actions {
    vertical-align: middle;
}

.actions form {
    margin: 0;
}

.actions button {
    border: 0;
    background: none;
    color: var(--danger);
    padding: 0;
    cursor: pointer;
}

.icon-button {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--brand-blue);
    display: inline-grid;
    place-items: center;
    cursor: pointer;
}

.icon-button:hover,
.icon-button.is-active {
    background: var(--primary-soft);
    border-color: #d6ddff;
    color: var(--primary);
}

.icon-button.danger {
    color: var(--danger);
}

.actions button.icon-button {
    border: 1px solid var(--line);
    background: #fff;
    color: var(--primary);
    padding: 0;
}

.actions button.icon-button.danger {
    color: var(--danger);
}

.primary-button,
.secondary-button {
    border-radius: 8px;
    padding: 11px 14px;
    border: 1px solid transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    cursor: pointer;
}

.primary-button {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.primary-button:hover {
    background: var(--primary-dark);
}

.primary-button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.secondary-button {
    background: #fff;
    color: var(--primary);
    border-color: var(--line);
}

.secondary-button:hover,
.secondary-button.is-active,
.ghost-button:hover {
    background: var(--primary-soft);
    border-color: #d6ddff;
    color: var(--primary);
}

.form-grid,
.filter-grid {
    display: grid;
    gap: 16px;
}

.form-grid {
    padding: 20px;
}

.filter-grid {
    padding: 20px;
    grid-template-columns: repeat(6, minmax(130px, 1fr));
    align-items: end;
}

.column-filter {
    grid-column: 1 / -1;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    background: var(--panel-soft);
}

.column-filter legend {
    padding: 0 6px;
    color: #334450;
    font-weight: 800;
}

.column-filter-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 10px 14px;
}

.column-filter .checkbox-row {
    color: #40516a;
    font-size: 13px;
    font-weight: 700;
}

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

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

.sub-panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    display: grid;
    gap: 14px;
    background: var(--panel-soft);
}

.sub-panel h3 {
    margin: 0;
    font-size: 16px;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    padding: 20px;
    border-top: 1px solid var(--line);
    background: var(--panel-soft);
}

.summary-grid div {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    padding: 12px;
    display: grid;
    gap: 5px;
}

.summary-grid span,
.form-note {
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}

.summary-grid strong {
    color: var(--ink);
    font-size: 14px;
    font-weight: 700;
}

.form-note {
    margin: 0;
    line-height: 1.5;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.checkbox-row input {
    width: auto;
}

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

input,
select,
textarea {
    width: 100%;
    border: 1px solid #cfd9df;
    border-radius: 8px;
    padding: 10px 11px;
    color: var(--ink);
    background: #fff;
    font-weight: 400;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--brand-blue);
    box-shadow: 0 0 0 3px rgba(39, 170, 225, 0.18);
    outline: none;
}

textarea {
    resize: vertical;
}

.hint {
    padding: 12px;
    border-radius: 8px;
    background: #fff8e8;
    color: #715112;
    border: 1px solid #f4dfaa;
}

.form-actions {
    display: flex;
    gap: 12px;
    align-items: center;
}

.filter-actions {
    align-self: end;
}

.flash {
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
}

.flash.success {
    background: #e8f6ef;
    color: var(--success);
    border: 1px solid #bee7d1;
}

.flash.error {
    background: #fcebea;
    color: var(--danger);
    border: 1px solid #f5c2c0;
}

.update-banner {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 100;
    width: min(360px, calc(100vw - 36px));
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 14px;
    border: 1px solid #d6ddff;
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 18px 46px rgba(37, 48, 128, 0.18);
}

.update-banner strong,
.update-banner span {
    display: block;
}

.update-banner strong {
    color: var(--primary);
    font-size: 14px;
}

.update-banner span {
    color: var(--muted);
    font-size: 12px;
    margin-top: 2px;
}

.update-banner button {
    border: 0;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    padding: 9px 11px;
    font-weight: 700;
    white-space: nowrap;
}

.update-banner button:hover {
    background: var(--primary-dark);
}

.pwa-install-banner {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 90;
    width: min(430px, calc(100vw - 36px));
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    padding: 14px;
    border: 1px solid #d6ddff;
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 18px 46px rgba(37, 48, 128, 0.18);
}

.pwa-install-banner img {
    width: 44px;
    height: 44px;
    object-fit: contain;
}

.pwa-install-banner strong,
.pwa-install-banner span {
    display: block;
}

.pwa-install-banner strong {
    color: var(--primary);
    font-size: 14px;
}

.pwa-install-banner span {
    color: var(--muted);
    font-size: 12px;
    margin-top: 2px;
}

.pwa-install-action {
    border: 0;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    padding: 10px 12px;
    font-weight: 800;
    white-space: nowrap;
}

.pwa-install-action:hover {
    background: var(--primary-dark);
}

.pwa-install-close {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 30px;
    height: 30px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    color: var(--muted);
    cursor: pointer;
    display: grid;
    place-items: center;
    box-shadow: 0 8px 20px rgba(37, 48, 128, 0.12);
}

.pwa-install-close:hover {
    color: var(--primary);
    background: var(--primary-soft);
}

.login-shell {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.84) 0%, rgba(244, 248, 251, 0.94) 100%),
        linear-gradient(135deg, rgba(39, 170, 225, 0.18) 0%, rgba(37, 48, 128, 0.12) 100%);
}

.auth-card {
    width: min(460px, 100%);
    padding: 28px;
}

.auth-logo {
    display: block;
    width: min(230px, 78%);
    height: auto;
    margin-bottom: 24px;
}

.auth-card .form-grid {
    padding: 18px 0 0;
}

.install-list {
    display: grid;
    gap: 10px;
    margin: 20px 0;
}

.install-list div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--line);
}

.install-list dt {
    color: var(--muted);
}

.install-list dd {
    margin: 0;
    font-weight: 700;
}

@media (max-width: 980px) {
    .stats-grid,
    .stats-grid.compact,
    .filter-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

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

@media (max-width: 760px) {
    .summary-grid {
        grid-template-columns: 1fr;
    }

    .table-scroll-top {
        display: none !important;
    }

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

    table,
    thead,
    tbody,
    tr,
    th,
    td {
        display: block;
        width: 100%;
    }

    thead {
        display: none;
    }

    tbody {
        display: grid;
        gap: 12px;
    }

    tr {
        border: 1px solid var(--line);
        border-radius: 8px;
        background: #fff;
        box-shadow: 0 10px 28px rgba(37, 48, 128, 0.06);
        overflow: hidden;
    }

    tbody tr:hover {
        background: #fff;
    }

    td {
        border-bottom: 1px solid var(--line);
        display: grid;
        grid-template-columns: minmax(112px, 42%) minmax(0, 1fr);
        gap: 12px;
        align-items: start;
        padding: 12px 14px;
        text-align: right;
        word-break: break-word;
    }

    td:last-child {
        border-bottom: 0;
    }

    td::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: 12px;
        font-weight: 800;
        text-align: left;
    }

    td[colspan] {
        display: block;
        text-align: center;
    }

    td[colspan]::before {
        display: none;
    }

    td .actions,
    td.actions {
        justify-content: flex-end;
        white-space: normal;
    }
}

@media (max-width: 760px) {
    :root {
        --sidebar-width: 84px;
        --sidebar-collapsed-width: 0px;
    }

    .app-shell {
        display: grid;
        grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
        overflow-x: hidden;
    }

    .sidebar-collapsed .app-shell {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
    }

    .sidebar-collapsed .content {
        grid-column: 1;
        width: 100%;
        max-width: 100vw;
    }

    .sidebar {
        position: static;
        width: var(--sidebar-width);
        height: auto;
        min-height: 100vh;
        overflow: hidden;
        align-items: center;
        padding: 18px 14px;
        box-shadow: 8px 0 28px rgba(37, 48, 128, 0.05);
        transition: transform 180ms ease;
    }

    .sidebar-head {
        grid-template-columns: 1fr;
        justify-items: center;
        gap: 12px;
        padding: 8px 0 16px;
        width: 100%;
    }

    .sidebar-collapsed .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 50;
        overflow: visible;
        transform: translateX(calc(-1 * var(--sidebar-width)));
    }

    .sidebar-collapsed .sidebar-head {
        grid-template-columns: 1fr;
        justify-items: center;
    }

    .brand {
        justify-items: center;
    }

    .brand-logo,
    .brand small,
    .side-nav span,
    .ghost-button span {
        display: none;
    }

    .brand-icon {
        display: block;
    }

    .side-nav {
        grid-template-columns: 1fr;
        width: 100%;
    }

    .sidebar form {
        width: 100%;
    }

    .side-nav a,
    .ghost-button {
        width: 100%;
        justify-content: center;
        padding: 12px;
    }

    .side-nav i,
    .ghost-button i {
        width: 20px;
        min-width: 20px;
        font-size: 17px;
    }

    .sidebar-toggle {
        position: static;
        box-shadow: none;
    }

    .sidebar-head .sidebar-toggle {
        display: none;
    }

    .topbar-sidebar-toggle {
        display: inline-grid;
        flex: 0 0 auto;
    }

    .topbar-title {
        align-items: flex-start;
    }

    .topbar-title h1 {
        font-size: 22px;
    }

    .add-button {
        width: 42px;
        min-width: 42px;
        padding-inline: 0;
    }

    .add-button span {
        display: none;
    }

    .update-banner {
        right: 12px;
        bottom: 12px;
        align-items: stretch;
        flex-direction: column;
    }

    .pwa-install-banner {
        right: 12px;
        bottom: 12px;
        grid-template-columns: 40px minmax(0, 1fr);
    }

    .pwa-install-banner img {
        width: 40px;
        height: 40px;
    }

    .pwa-install-action {
        grid-column: 1 / -1;
        width: 100%;
    }

    .content {
        padding: 16px;
    }

    .table-scroll-top {
        display: none !important;
    }

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

    table,
    thead,
    tbody,
    tr,
    th,
    td {
        display: block;
        width: 100%;
    }

    thead {
        display: none;
    }

    tbody {
        display: grid;
        gap: 12px;
    }

    tr {
        border: 1px solid var(--line);
        border-radius: 8px;
        background: #fff;
        box-shadow: 0 10px 28px rgba(37, 48, 128, 0.06);
        overflow: hidden;
    }

    tbody tr:hover {
        background: #fff;
    }

    td {
        border-bottom: 1px solid var(--line);
        display: grid;
        grid-template-columns: minmax(112px, 42%) minmax(0, 1fr);
        gap: 12px;
        align-items: start;
        padding: 12px 14px;
        text-align: right;
        word-break: break-word;
    }

    td:last-child {
        border-bottom: 0;
    }

    td::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: 12px;
        font-weight: 800;
        text-align: left;
    }

    td[colspan] {
        display: block;
        text-align: center;
    }

    td[colspan]::before {
        display: none;
    }

    td .actions,
    td.actions {
        justify-content: flex-end;
        white-space: normal;
    }

    .topbar,
    .panel-header,
    .form-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .topbar {
        align-items: flex-start;
        flex-direction: row;
    }

    .stats-grid,
    .stats-grid.compact,
    .filter-grid,
    .two-col,
    .payment-grid {
        grid-template-columns: 1fr;
    }
}
