:root {
    --bg: #0b1622;
    --card: #122233;
    --card-soft: #172b3f;
    --line: #2c4358;
    --text: #e8f0f7;
    --muted: #9eb2c5;
    --accent: #0bb17d;
    --accent-strong: #099a6d;
    --danger: #cf4b4b;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, sans-serif;
    background-color: #0f2e68;
    background-image:
        radial-gradient(42vw 38vw at 10% 14%, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0) 64%),
        radial-gradient(36vw 36vw at 94% 82%, rgba(112, 0, 22, 0.34), rgba(112, 0, 22, 0) 62%),
        linear-gradient(104deg, #0f2e68 0%, #1d2b63 46%, #3a182f 76%, #4c1217 100%);
    background-size: 100% 100%, 100% 100%, 100% 100%;
    background-position: center center, center center, center center;
    background-repeat: no-repeat;
    color: var(--text);
}

.page {
    max-width: 1180px;
    margin: 2rem auto;
    padding: 0 1rem 2rem;
}

.card {
    background: linear-gradient(180deg, var(--card) 0%, var(--card-soft) 100%);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 1rem;
}

#app-view.card {
    background: transparent;
    border: none;
    box-shadow: none;
}

.login-card {
    max-width: 560px;
    margin: 2rem auto;
}

.logo-wrap {
    text-align: center;
    margin-bottom: 1rem;
}

.logo-image {
    width: 100%;
    max-width: 420px;
    max-height: 150px;
    object-fit: contain;
}

.header-logo {
    width: 100%;
    max-width: 180px;
    max-height: 56px;
    object-fit: contain;
    flex: 0 0 auto;
}

.form-grid {
    display: grid;
    gap: 0.8rem;
}

label {
    display: grid;
    gap: 0.35rem;
    color: var(--muted);
}

input,
select,
button {
    font: inherit;
}

input,
select {
    width: 100%;
    background: #0f1d2b;
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0.65rem 0.75rem;
}

.btn {
    background: var(--accent);
    color: white;
    border: 0;
    border-radius: 10px;
    padding: 0.65rem 0.9rem;
    cursor: pointer;
    font-weight: 600;
}

.btn:hover {
    background: var(--accent-strong);
}

.btn-ghost {
    background: transparent;
    border: 1px solid var(--line);
}

.error {
    color: #ff9f9f;
    min-height: 1.2rem;
}

.hidden {
    display: none !important;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 0.8rem;
}

.toolbar-actions {
    display: flex;
    gap: 0.6rem;
    align-items: center;
}

.muted {
    color: var(--muted);
}

.text-warning {
    color: #ffb74d;
}

.tabs {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    margin-bottom: 0.8rem;
}

.tabs #logout-btn {
    margin-left: auto;
}

.tab {
    border: 1px solid var(--line);
    background: #102131;
    color: var(--text);
    border-radius: 10px;
    padding: 0.55rem 0.8rem;
    cursor: pointer;
}

.tab.is-active {
    background: #1a3349;
}

.filters {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.6rem;
    margin-bottom: 0.8rem;
}

.filter-search-group {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    min-width: 0;
}

.filter-search-group #filter-search {
    flex: 1 1 auto;
    min-width: 0;
}

.filters select,
.filters input,
#toggle-expand-all {
    min-height: 44px;
    padding: 0.5rem 0.7rem;
    font-size: 0.92rem;
}

#toggle-expand-all {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    width: 44px;
    min-width: 44px;
    font-size: 1.45rem;
    line-height: 1;
    font-weight: 700;
}

.user-row {
    cursor: pointer;
    user-select: none;
}
.user-row:hover td {
    background: rgba(255,255,255,0.04);
}
.user-row .user-cell-main {
    display: flex;
    align-items: flex-start;
    gap: 0.35rem;
}
.user-row .expand-arrow {
    display: inline-block;
    font-size: 0.6rem;
    margin-top: 0.28rem;
    transition: transform 0.15s;
    opacity: 0.55;
    flex: 0 0 auto;
}
.user-row .user-name {
    display: block;
    min-width: 0;
}
.user-row.expanded .expand-arrow {
    transform: rotate(90deg);
    opacity: 1;
}
.lines-row td {
    padding: 0 !important;
    border-bottom: 2px solid var(--accent);
}
.lines-content {
    padding: 0.5rem 0 0.75rem 1.4rem;
}
.lines-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
    table-layout: fixed;
}
.lines-table th,
.lines-table td {
    border-bottom: 1px solid rgba(255,255,255,0.07);
    padding: 0.4rem 0.65rem;
    text-align: left;
    vertical-align: middle;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.lines-table th:nth-child(2),
.lines-table td:nth-child(2) {
    text-align: left;
}
.lines-table th:nth-child(3),
.lines-table td:nth-child(3) {
    text-align: left;
}
.lines-table th:nth-child(4),
.lines-table td:nth-child(4) {
    text-align: right;
}
.lines-table th:nth-child(5),
.lines-table td:nth-child(5) {
    text-align: center;
}
.lines-table thead th {
    color: var(--muted);
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-bottom: 1px solid var(--line);
}
.lines-table tr.order-group-header td {
    background: rgba(11, 177, 125, 0.08);
    color: var(--accent);
    font-weight: 600;
    font-size: 0.8rem;
    padding: 0.7rem 0.9rem;
    border-bottom: none;
    border-top: 1px solid rgba(11, 177, 125, 0.2);
    letter-spacing: 0.02em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.lines-table .order-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.6rem;
}
.lines-table .order-header-delivered {
    color: var(--muted);
    font-weight: 500;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.lines-table tr.order-gap td {
    border: none;
    padding: 0;
    height: 12px;
    background: transparent;
}

.table {
    width: 100%;
    border-collapse: collapse;
    overflow: hidden;
}

#users-table {
    table-layout: fixed;
}
#users-table > thead > tr > th:nth-child(2),
#users-table > tbody > tr > td:nth-child(2) {
    width: 80px;
    text-align: right;
}
#users-table > thead > tr > th:nth-child(3),
#users-table > tbody > tr > td:nth-child(3) {
    width: 110px;
    text-align: right;
}

.table th,
.table td {
    border-bottom: 1px solid var(--line);
    padding: 0.65rem;
    text-align: left;
    vertical-align: top;
}

.badge {
    padding: 0.2rem 0.45rem;
    border-radius: 999px;
    font-size: 0.75rem;
    background: #33485a;
}

.badge.deleted {
    background: var(--danger);
    color: white;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.8rem;
}

.stat-card {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.8rem;
    background: #13283b;
}

.stat {
    font-size: 2rem;
    margin: 0.3rem 0 0;
}

.card-inline {
    margin-top: 1rem;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.8rem;
}

.plain-list {
    margin: 0;
    padding-left: 1rem;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: grid;
    place-items: center;
    padding: 1rem;
}

.modal-dialog {
    width: min(960px, 96vw);
    max-height: 90vh;
    overflow: auto;
    background: #102131;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.8rem;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.8rem;
}

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

    .lines-content {
        padding: 0.45rem 0 0.65rem 0.7rem;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .lines-table {
        min-width: 640px;
    }

    .lines-table th,
    .lines-table td {
        padding: 0.38rem 0.55rem;
    }

    .stats-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 560px) {
    .filters {
        grid-template-columns: 1fr;
        gap: 0.45rem;
    }

    .filter-search-group {
        gap: 0.35rem;
    }

    #app-view.card {
        background: transparent;
        border: none;
        border-radius: 0;
        padding: 0;
    }

    .page {
        margin: 0.7rem auto 0;
        padding: 0 0.45rem 1rem;
    }

    .card {
        padding: 0.7rem;
    }

    .toolbar {
        gap: 0.55rem;
        margin-bottom: 0.35rem;
    }

    .header-logo {
        max-width: 132px;
        max-height: 42px;
    }

    #welcome-title {
        margin: 0;
    }

    #welcome-subtitle:empty {
        display: none;
    }

    .tabs {
        gap: 0.25rem;
        margin-bottom: 0.5rem;
    }

    .tab {
        padding: 0.36rem 0.5rem;
        font-size: 0.82rem;
        white-space: nowrap;
        line-height: 1.15;
    }

    .filters select,
    .filters input,
    #toggle-expand-all {
        padding: 0.42rem 0.58rem;
        min-height: 38px;
        font-size: 0.9rem;
    }

    #toggle-expand-all {
        padding: 0;
        width: 38px;
        min-width: 38px;
        font-size: 1.3rem;
    }

    .lines-content {
        padding: 0.35rem 0 0.5rem 0.35rem;
        overflow-x: hidden;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .lines-table {
        min-width: 0;
        width: 100%;
        table-layout: auto;
        font-size: 0.84rem;
    }

    .lines-table,
    .lines-table tbody {
        display: block;
        width: 100%;
    }

    .lines-table colgroup,
    .lines-table thead {
        display: none;
    }

    .lines-table tr.order-group-header,
    .lines-table tr.order-gap {
        display: block;
        width: 100%;
    }

    .lines-table tr.order-group-header td,
    .lines-table tr.order-gap td {
        display: block;
        width: 100%;
    }

    .lines-table tbody tr.line-item {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 2.2rem 2.2rem;
        grid-template-areas:
            "product product qty check"
            "variant variant code code";
        column-gap: 0.65rem;
        row-gap: 0.32rem;
        padding: 0.42rem 0.45rem 0.52rem;
        border-bottom: 1px solid rgba(255,255,255,0.08);
        width: 100%;
    }

    .lines-table tbody tr.line-item td {
        border-bottom: none;
        padding: 0;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        min-width: 0;
        overflow-wrap: normal;
        word-break: normal;
        display: block;
    }

    .lines-table tbody tr.line-item td.line-code {
        grid-area: code;
        color: var(--muted);
        font-weight: 400;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        overflow-wrap: break-word;
        word-break: normal;
    }

    .lines-table tbody tr.line-item td.line-product {
        grid-area: product;
        font-weight: 600;
    }

    .lines-table tbody tr.line-item td.line-variant {
        grid-area: variant;
        color: var(--muted);
        text-align: left;
    }

    .lines-table tbody tr.line-item td.line-qty {
        grid-area: qty;
        align-self: start;
        justify-self: center;
        text-align: center;
        min-width: 2.2rem;
    }

    .lines-table tbody tr.line-item td.line-check {
        grid-area: check;
        align-self: start;
        justify-self: end;
        text-align: right;
    }

    .lines-table tr.order-group-header td {
        padding: 0.62rem 0.78rem;
        font-size: 0.8rem;
    }

    #products-table,
    #products-table tbody {
        display: block;
        width: 100%;
    }

    #products-table thead {
        display: none;
    }

    #products-table tr.product-row {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "name total"
            "variation code"
            "users users";
        column-gap: 0.8rem;
        row-gap: 0.28rem;
        padding: 0.72rem 0;
        border-bottom: 1px solid var(--line);
    }

    #products-table tr.product-row td {
        display: block;
        border-bottom: none;
        padding: 0;
        min-width: 0;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
    }

    #products-table td.product-name {
        grid-area: name;
        font-weight: 600;
    }

    #products-table td.product-total {
        grid-area: total;
        text-align: right;
        font-weight: 600;
        white-space: nowrap;
    }

    #products-table td.product-variation {
        grid-area: variation;
        color: var(--muted);
    }

    #products-table td.product-code {
        grid-area: code;
        color: var(--muted);
        text-align: right;
    }

    #products-table td.product-users {
        grid-area: users;
        color: var(--muted);
        font-size: 0.88rem;
        line-height: 1.35;
    }
}
