:root {
    --sidebar: #14213d;
    --sidebar-strong: #0b1220;
    --sidebar-link: #dbeafe;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --line: #e2e8f0;
    --page: #f3f6fa;
    --text: #0f172a;
    --muted: #64748b;
    --primary: #0f766e;
    --accent: #2563eb;
    --danger: #dc2626;
    --shadow: 0 12px 32px rgba(15, 23, 42, .08);
    --radius: 8px;
    --mobile-appbar-height: 60px;
    --mobile-bottom-height: 68px;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    min-height: 100%;
    overflow-x: hidden;
}

body {
    margin: 0;
    background: var(--page);
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

img,
svg,
canvas,
video {
    max-width: 100%;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    min-height: 38px;
    white-space: nowrap;
}

.btn-sm {
    min-height: 34px;
}

.form-control,
.form-select {
    min-height: 42px;
    border-color: #d7dee8;
    font-size: 14px;
}

textarea.form-control {
    min-height: auto;
}

.form-control:focus,
.form-select:focus {
    border-color: rgba(15, 118, 110, .55);
    box-shadow: 0 0 0 .2rem rgba(15, 118, 110, .14);
}

.app-shell {
    display: grid;
    grid-template-columns: 268px minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    background: linear-gradient(180deg, var(--sidebar) 0%, var(--sidebar-strong) 100%);
    color: #fff;
    padding: 16px 12px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
}

.brand {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 6px 8px 16px;
}

.brand-link,
.mobile-brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    color: inherit;
    font-weight: 800;
    letter-spacing: 0;
    text-decoration: none;
}

.brand-link {
    font-size: 18px;
}

.brand-link:hover,
.mobile-brand:hover {
    color: inherit;
}

.brand-link i,
.mobile-brand i {
    color: #5eead4;
}

.sidebar .nav {
    gap: 3px;
}

.sidebar .nav-link {
    color: var(--sidebar-link);
    display: flex;
    align-items: center;
    gap: 11px;
    border-radius: 6px;
    padding: 10px;
    min-height: 40px;
    line-height: 1.2;
}

.sidebar .nav-link i {
    width: 20px;
    text-align: center;
    color: #93c5fd;
}

.sidebar .nav-link:hover,
.sidebar .nav-link.active {
    background: rgba(255, 255, 255, .1);
    color: #fff;
}

.sidebar .nav-link.active i {
    color: #5eead4;
}

.main {
    min-width: 0;
    min-height: 100vh;
    padding: 22px;
    display: flex;
    flex-direction: column;
}

.desktop-navbar {
    position: sticky;
    top: 0;
    z-index: 1015;
    display: grid;
    grid-template-columns: minmax(220px, 1fr) auto minmax(280px, 1fr);
    align-items: center;
    gap: 18px;
    margin: -22px -22px 20px;
    padding: 12px 22px;
    background: rgba(255, 255, 255, .96);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 8px 24px rgba(15, 23, 42, .05);
    backdrop-filter: blur(14px);
}

.navbar-company {
    min-width: 0;
    display: inline-flex;
    align-items: center;
    gap: 11px;
    color: var(--text);
    text-decoration: none;
}

.navbar-company:hover {
    color: var(--text);
}

.navbar-company-icon {
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: #0f766e;
    color: #fff;
    box-shadow: 0 10px 20px rgba(15, 118, 110, .22);
}

.navbar-company strong,
.navbar-company small {
    display: block;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.navbar-company strong {
    font-size: 15px;
    font-weight: 800;
}

.navbar-company small {
    color: var(--muted);
    font-size: 12px;
}

.desktop-action-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-width: 0;
}

.desktop-action-link {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 36px;
    padding: 0 11px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: #334155;
    font-weight: 700;
    text-decoration: none;
    white-space: nowrap;
}

.desktop-action-link i {
    color: var(--primary);
}

.desktop-action-link:hover {
    border-color: rgba(15, 118, 110, .35);
    color: var(--primary);
    background: #f0fdfa;
}

.navbar-tools {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.navbar-tools form {
    margin: 0;
}

.system-chip {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 34px;
    padding: 0 10px;
    border: 1px solid #bbf7d0;
    border-radius: 999px;
    background: #f0fdf4;
    color: #166534;
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
}

.system-chip i {
    color: #16a34a;
    font-size: 8px;
}

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

.topbar-title {
    min-width: 0;
}

.topbar h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 800;
    letter-spacing: 0;
    overflow-wrap: anywhere;
}

.topbar p {
    color: var(--muted);
    margin: 3px 0 0;
    max-width: 820px;
}

.user-menu {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: flex-end;
    min-width: 0;
}

.user-menu form {
    margin: 0;
}

.profile-button {
    max-width: min(280px, 32vw);
}

.profile-button .user-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-user-panel {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    margin-top: 14px;
    padding: 12px 8px 4px;
    border-top: 1px solid rgba(255, 255, 255, .12);
}

.sidebar-user-panel form {
    margin: 0;
}

.sidebar-user-panel .icon-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
    background: rgba(255, 255, 255, .1);
    color: #fff;
    border-color: rgba(255, 255, 255, .14);
}

.sidebar-user-link {
    min-width: 0;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: #fff;
    text-decoration: none;
    font-weight: 700;
}

.sidebar-user-link span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-user-link i {
    color: #5eead4;
    font-size: 18px;
}

.panel {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.panel + .panel {
    margin-top: 14px;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    font-weight: 800;
    margin-bottom: 14px;
}

.panel-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.panel-toolbar > * {
    min-width: 0;
}

.panel-toolbar .btn-group {
    flex-wrap: wrap;
    gap: 6px;
}

.panel-toolbar .btn-group > .btn {
    border-radius: 6px !important;
    margin-left: 0 !important;
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
}

.metric-card {
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 14px;
    display: grid;
    gap: 5px;
    box-shadow: 0 8px 20px rgba(15, 23, 42, .04);
}

.metric-card i {
    color: var(--primary);
    font-size: 20px;
}

.metric-card span {
    color: var(--muted);
}

.metric-card strong {
    min-width: 0;
    font-size: 21px;
    line-height: 1.15;
    overflow-wrap: anywhere;
}

.table-responsive,
.dataTables_wrapper,
.dataTables_scroll,
.dataTables_scrollBody {
    max-width: 100%;
}

.table-responsive,
.dataTables_wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table {
    margin-bottom: 0;
    vertical-align: middle;
}

.table th {
    color: #334155;
    font-weight: 800;
    white-space: nowrap;
}

.table td {
    overflow-wrap: anywhere;
}

table.dataTable,
#itemsTable,
#documentTable,
#productTable,
#stockTable,
#paymentTable,
#expenseTable,
#crudTable {
    min-width: 760px;
}

.dataTables_wrapper .row {
    align-items: center;
    row-gap: 10px;
}

.dataTables_wrapper .dataTables_filter input {
    max-width: 100%;
    min-height: 38px;
}

.dataTables_wrapper .pagination {
    flex-wrap: wrap;
    gap: 4px;
    margin-bottom: 0;
}

.modal-content {
    border: 0;
    border-radius: var(--radius);
    overflow: hidden;
}

.modal-header,
.modal-footer {
    background: var(--surface-soft);
}

.modal-title {
    font-weight: 800;
}

.auth-body {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: 18px;
    background: radial-gradient(circle at top left, rgba(45, 212, 191, .18), transparent 34%), linear-gradient(135deg, #eef7f6 0%, #f8fafc 48%, #eef2ff 100%);
}

.auth-panel {
    width: min(430px, 100%);
    background: rgba(255, 255, 255, .96);
    border: 1px solid rgba(226, 232, 240, .9);
    border-radius: var(--radius);
    padding: 28px;
    box-shadow: 0 24px 56px rgba(15, 23, 42, .14);
}

.auth-brand {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    color: var(--text);
    font-size: 22px;
    font-weight: 800;
    margin-bottom: 22px;
}

.auth-brand i {
    color: var(--primary);
}

.permission-group {
    border-top: 1px solid var(--line);
    padding-top: 16px;
    padding-bottom: 8px;
}

.permission-group h6 {
    font-size: 0.85rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
}

.permission-group .form-check {
    min-width: 0;
    margin: 0;
    padding: 0;
}

.permission-group .form-check-input {
    margin-top: 0.35rem;
}

.permission-group .form-check-label {
    font-size: 0.95rem;
    color: var(--text-primary);
}

.role-card .role-card-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    align-items: center;
    margin-bottom: 12px;
}

.role-card .form-control.w-auto {
    width: 100% !important;
}

.accounting-tabs {
    justify-content: flex-start;
}

.accounting-tabs .btn {
    min-width: 132px;
}

.barcode-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 12px;
}

.barcode-card {
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px;
    text-align: center;
    background: #fff;
}

.qr-box {
    display: inline-block;
    max-width: 100%;
    padding: 16px;
    border: 1px solid var(--line);
    background: #fff;
}

.accounting-output {
    background: #0f172a;
    color: #e2e8f0;
    border-radius: var(--radius);
    padding: 16px;
    min-height: 220px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    white-space: pre-wrap;
    word-break: break-word;
}

.print-body {
    background: #fff;
}

.invoice-print {
    width: min(920px, calc(100vw - 24px));
    margin: 24px auto;
}

.print-actions {
    margin-top: 24px;
}

.app-footer {
    display: grid;
    grid-template-columns: minmax(220px, 1fr) auto minmax(180px, 1fr);
    align-items: center;
    gap: 18px;
    margin-top: auto;
    padding: 22px 2px 0;
    color: var(--muted);
}

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

.footer-brand strong {
    color: var(--text);
    font-weight: 800;
}

.footer-brand span {
    overflow-wrap: anywhere;
}

.footer-links {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}

.footer-links a {
    color: #475569;
    font-weight: 700;
    text-decoration: none;
}

.footer-links a:hover {
    color: var(--primary);
}

.footer-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    flex-wrap: wrap;
    font-size: 12px;
}

.footer-meta span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.mobile-appbar,
.bottom-nav,
.sidebar-backdrop,
.mobile-sidebar-close {
    display: none;
}

.icon-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border: 1px solid var(--line);
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    color: var(--text);
    cursor: pointer;
}

.icon-btn:hover {
    color: var(--primary);
    border-color: rgba(15, 118, 110, .35);
}

.pwa-install-button[hidden] {
    display: none !important;
}

@media (max-width: 1200px) {
    .desktop-navbar {
        grid-template-columns: minmax(220px, 1fr) minmax(0, 1.4fr);
    }

    .desktop-action-nav {
        order: 3;
        grid-column: 1 / -1;
        justify-content: flex-start;
        overflow-x: auto;
        padding-bottom: 2px;
    }

    .navbar-tools {
        min-width: 0;
    }

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

@media (max-width: 992px) {
    body.erp-app-body {
        padding-top: calc(var(--mobile-appbar-height) + env(safe-area-inset-top));
        padding-bottom: calc(var(--mobile-bottom-height) + env(safe-area-inset-bottom));
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .mobile-appbar {
        position: fixed;
        inset: 0 0 auto 0;
        z-index: 1045;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        min-height: calc(var(--mobile-appbar-height) + env(safe-area-inset-top));
        padding: env(safe-area-inset-top) 12px 0;
        background: rgba(255, 255, 255, .96);
        border-bottom: 1px solid var(--line);
        backdrop-filter: blur(12px);
    }

    .mobile-app-actions {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .desktop-navbar,
    .app-footer {
        display: none;
    }

    .mobile-brand {
        min-width: 0;
        flex: 1;
        justify-content: center;
        color: var(--text);
        font-size: 16px;
    }

    .app-shell {
        display: block;
        min-height: auto;
    }

    .sidebar {
        position: fixed;
        z-index: 1050;
        inset: 0 auto 0 0;
        width: min(304px, 86vw);
        height: 100dvh;
        padding-top: calc(14px + env(safe-area-inset-top));
        transform: translateX(-105%);
        transition: transform .22s ease;
        box-shadow: 24px 0 48px rgba(15, 23, 42, .26);
    }

    .sidebar .nav-link {
        min-height: 44px;
    }

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

    .mobile-sidebar-close {
        display: inline-flex;
        background: rgba(255, 255, 255, .1);
        color: #fff;
        border-color: rgba(255, 255, 255, .14);
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 1040;
        background: rgba(15, 23, 42, .48);
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease;
    }

    body.sidebar-open .sidebar-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    .main {
        padding: 14px;
        min-height: auto;
    }

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

    .bottom-nav {
        position: fixed;
        inset: auto 0 0 0;
        z-index: 1035;
        display: grid;
        grid-template-columns: repeat(5, minmax(0, 1fr));
        min-height: calc(var(--mobile-bottom-height) + env(safe-area-inset-bottom));
        padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
        background: rgba(255, 255, 255, .98);
        border-top: 1px solid var(--line);
        box-shadow: 0 -10px 28px rgba(15, 23, 42, .08);
        backdrop-filter: blur(12px);
    }

    .bottom-nav-link {
        min-width: 0;
        border: 0;
        background: transparent;
        color: var(--muted);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        border-radius: 8px;
        padding: 6px 4px;
        font-size: 11px;
        font-weight: 700;
        line-height: 1.1;
        text-decoration: none;
        cursor: pointer;
    }

    .bottom-nav-link i {
        width: 34px;
        height: 34px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        font-size: 17px;
    }

    .bottom-nav-link.active,
    .bottom-nav-link:hover {
        color: var(--primary);
        background: rgba(15, 118, 110, .1);
    }

    .bottom-nav-link.active i {
        background: var(--primary);
        color: #fff;
        box-shadow: 0 8px 18px rgba(15, 118, 110, .24);
    }
}

@media (max-width: 576px) {
    body {
        font-size: 13px;
    }

    .main {
        padding: 12px;
    }

    .topbar {
        flex-direction: column;
        gap: 12px;
        margin-bottom: 12px;
    }

    .topbar h1 {
        font-size: 20px;
    }

    .topbar p {
        font-size: 13px;
    }

    .mobile-appbar .icon-btn {
        width: 42px;
        height: 42px;
        min-width: 42px;
    }

    .user-menu {
        width: 100%;
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        justify-content: stretch;
    }

    .user-menu .pwa-install-button {
        grid-column: 1 / -1;
        width: 100%;
    }

    .profile-button {
        max-width: none;
        width: 100%;
        justify-content: flex-start;
    }

    .panel {
        padding: 12px;
        box-shadow: 0 8px 22px rgba(15, 23, 42, .06);
    }

    .panel-toolbar {
        align-items: stretch;
        gap: 8px;
    }

    .panel-toolbar > .btn,
    .panel-toolbar > a.btn,
    .panel-toolbar > button.btn {
        width: 100%;
    }

    .panel-toolbar .btn-group,
    .accounting-tabs {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .panel-toolbar .btn-group .btn,
    .accounting-tabs .btn {
        min-width: 0;
        width: 100%;
        padding-left: 8px;
        padding-right: 8px;
        white-space: normal;
    }

    .metric-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .metric-card {
        grid-template-columns: 28px minmax(0, 1fr);
        align-items: center;
        padding: 12px;
    }

    .metric-card strong {
        grid-column: 1 / -1;
        font-size: 20px;
    }

    .dataTables_wrapper .row > [class*="col-"] {
        width: 100%;
        flex: 0 0 100%;
        max-width: 100%;
    }

    .dataTables_wrapper .dataTables_length,
    .dataTables_wrapper .dataTables_filter,
    .dataTables_wrapper .dataTables_info,
    .dataTables_wrapper .dataTables_paginate {
        text-align: left !important;
    }

    .dataTables_wrapper .dataTables_filter label,
    .dataTables_wrapper .dataTables_filter input,
    .dataTables_wrapper .dataTables_length label,
    .dataTables_wrapper .dataTables_length select {
        width: 100%;
        margin-left: 0;
    }

    .dataTables_wrapper .pagination {
        justify-content: flex-start !important;
    }

    .modal-dialog,
    .modal-dialog.modal-xl,
    .modal-dialog.modal-lg {
        width: calc(100vw - 16px);
        max-width: calc(100vw - 16px);
        margin: 8px auto;
    }

    .modal-content {
        max-height: calc(100dvh - 16px);
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 12px;
    }

    .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 2;
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .modal-footer > * {
        width: 100%;
        margin: 0 !important;
    }

    .auth-body {
        padding: 12px;
        align-items: stretch;
    }

    .auth-panel {
        align-self: center;
        padding: 22px 18px;
    }

    .auth-panel .d-flex.justify-content-between {
        align-items: flex-start !important;
        gap: 8px;
        flex-direction: column;
    }

    .role-card .role-card-header {
        grid-template-columns: 1fr;
    }

    .role-card .role-card-header .btn {
        width: 100%;
    }

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

    .invoice-print {
        width: calc(100vw - 16px);
        margin: 12px auto;
    }
}

@media (max-width: 380px) {
    .bottom-nav-link span {
        display: none;
    }

    .bottom-nav {
        min-height: calc(58px + env(safe-area-inset-bottom));
    }
}

@media print {
    body.erp-app-body {
        padding: 0;
    }

    .sidebar,
    .topbar,
    .desktop-navbar,
    .app-footer,
    .mobile-appbar,
    .bottom-nav,
    .sidebar-backdrop,
    .print-actions {
        display: none !important;
    }

    .app-shell,
    .main {
        display: block;
        min-height: auto;
        padding: 0;
    }

    .panel {
        box-shadow: none;
        border: 0;
    }
}
