:root {
    --crm-primary: #2563eb;
    --crm-primary-dark: #1d4ed8;
    --crm-primary-soft: #e8f0ff;
    --crm-sidebar: #0f172a;
    --crm-sidebar-2: #16213e;
    --crm-bg: #f4f7fb;
    --crm-text: #1e293b;
    --crm-muted: #64748b;
    --crm-border: #e2e8f0;
    --crm-success: #059669;
    --crm-warning: #d97706;
    --crm-danger: #dc2626;
    --crm-radius: 14px;
    --crm-shadow: 0 8px 28px rgba(15, 23, 42, .07);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--crm-bg);
    color: var(--crm-text);
    font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", sans-serif;
    font-size: .925rem;
    -webkit-font-smoothing: antialiased;
}

a {
    text-decoration: none;
}

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

/* --------------------------------------------------------------------------
   側邊欄
   -------------------------------------------------------------------------- */
.sidebar {
    --bs-offcanvas-width: 280px;
    flex: 0 0 270px;
    width: 270px;
}

.sidebar .offcanvas-body {
    padding: 0;
    background: var(--crm-sidebar);
}

.sidebar-inner {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100vh;
    background: linear-gradient(180deg, var(--crm-sidebar) 0%, #111c33 60%, #0b1222 100%);
    color: #cbd5e1;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(148, 163, 184, .35) transparent;
}

.sidebar-inner::-webkit-scrollbar {
    width: 6px;
}

.sidebar-inner::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, .35);
    border-radius: 8px;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: 1.35rem 1.25rem;
    color: #fff;
    border-bottom: 1px solid rgba(148, 163, 184, .12);
}

.sidebar-brand .brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 12px;
    background: linear-gradient(135deg, #3b82f6, #06b6d4);
    color: #fff;
    font-size: 1.35rem;
    box-shadow: 0 8px 20px rgba(37, 99, 235, .35);
    flex: 0 0 auto;
}

.sidebar-brand .brand-text {
    line-height: 1.2;
}

.sidebar-brand .brand-title {
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: .02em;
}

.sidebar-brand .brand-subtitle {
    font-size: .68rem;
    color: #94a3b8;
    letter-spacing: .08em;
    text-transform: uppercase;
}

.sidebar-close {
    position: absolute;
    top: 1.35rem;
    right: 1rem;
    z-index: 5;
    filter: invert(1) grayscale(1);
    opacity: .8;
}

.sidebar-nav {
    flex: 1;
    padding: 1rem .8rem 1.5rem;
}

.sidebar-section {
    padding: 1.1rem .75rem .4rem;
    font-size: .66rem;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: #64748b;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .62rem .8rem;
    margin-bottom: .18rem;
    border-radius: 10px;
    color: #b6c2d4;
    font-weight: 500;
    transition: background .18s ease, color .18s ease, transform .18s ease;
}

.sidebar-link i {
    font-size: 1.05rem;
    width: 1.35rem;
    text-align: center;
    color: #7f8ea3;
    transition: color .18s ease;
}

.sidebar-link:hover {
    background: rgba(59, 130, 246, .12);
    color: #fff;
}

.sidebar-link:hover i {
    color: #93c5fd;
}

.sidebar-link.active {
    background: linear-gradient(90deg, rgba(37, 99, 235, .95), rgba(14, 165, 233, .82));
    color: #fff;
    box-shadow: 0 8px 18px rgba(37, 99, 235, .28);
}

.sidebar-link.active i {
    color: #fff;
}

.sidebar-footer {
    margin: .8rem;
    padding: .9rem;
    border-radius: 12px;
    background: rgba(148, 163, 184, .08);
    border: 1px solid rgba(148, 163, 184, .1);
    color: #94a3b8;
    font-size: .75rem;
}

.sidebar-footer .user-name {
    color: #e2e8f0;
    font-weight: 600;
    font-size: .82rem;
}

/* --------------------------------------------------------------------------
   主內容與頂部列
   -------------------------------------------------------------------------- */
.app-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 1020;
    display: flex;
    align-items: center;
    gap: 1rem;
    min-height: 68px;
    padding: .7rem 1.5rem;
    background: rgba(255, 255, 255, .94);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--crm-border);
}

.topbar-title {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
}

.topbar-subtitle {
    margin: .1rem 0 0;
    color: var(--crm-muted);
    font-size: .78rem;
}

.topbar .btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: 1px solid var(--crm-border);
    background: #fff;
    color: #475569;
}

.topbar .btn-icon:hover {
    background: #f8fafc;
    color: var(--crm-primary);
}

.user-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563eb, #06b6d4);
    color: #fff;
    font-weight: 700;
    font-size: .85rem;
    flex: 0 0 auto;
}

.app-content {
    flex: 1;
    padding: 1.6rem 1.5rem 2.2rem;
}

.app-footer {
    padding: 1rem 1.5rem;
    color: #94a3b8;
    font-size: .76rem;
    border-top: 1px solid var(--crm-border);
    background: #fff;
}

.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 1.25rem;
}

.page-title {
    margin: 0;
    font-size: 1.45rem;
    font-weight: 800;
    letter-spacing: -.01em;
}

.page-description {
    margin: .35rem 0 0;
    color: var(--crm-muted);
    font-size: .84rem;
}

/* --------------------------------------------------------------------------
   卡片、表格、表單
   -------------------------------------------------------------------------- */
.card {
    border: 1px solid var(--crm-border);
    border-radius: var(--crm-radius);
    box-shadow: var(--crm-shadow);
}

.card-header {
    background: #fff;
    border-bottom: 1px solid var(--crm-border);
    padding: 1rem 1.15rem;
    font-weight: 700;
    border-radius: var(--crm-radius) var(--crm-radius) 0 0 !important;
}

.card-body {
    padding: 1.15rem;
}

.stat-card {
    position: relative;
    overflow: hidden;
    height: 100%;
    padding: 1.2rem;
    border-radius: var(--crm-radius);
    background: #fff;
    border: 1px solid var(--crm-border);
    box-shadow: var(--crm-shadow);
}

.stat-card::after {
    content: "";
    position: absolute;
    right: -38px;
    bottom: -55px;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    background: var(--stat-soft, #e8f0ff);
    opacity: .7;
}

.stat-card .stat-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 46px;
    border-radius: 13px;
    background: var(--stat-soft, #e8f0ff);
    color: var(--stat-color, #2563eb);
    font-size: 1.25rem;
    margin-bottom: .9rem;
}

.stat-card .stat-label {
    color: var(--crm-muted);
    font-size: .78rem;
    font-weight: 600;
}

.stat-card .stat-value {
    position: relative;
    z-index: 1;
    margin-top: .2rem;
    font-size: 1.55rem;
    font-weight: 800;
    letter-spacing: -.02em;
}

.stat-card .stat-note {
    position: relative;
    z-index: 1;
    margin-top: .35rem;
    font-size: .74rem;
    color: #94a3b8;
}

.stat-blue {
    --stat-color: #2563eb;
    --stat-soft: #e8f0ff;
}

.stat-green {
    --stat-color: #059669;
    --stat-soft: #e7f8f1;
}

.stat-amber {
    --stat-color: #d97706;
    --stat-soft: #fff5e6;
}

.stat-rose {
    --stat-color: #e11d48;
    --stat-soft: #ffeaf0;
}

.stat-cyan {
    --stat-color: #0891b2;
    --stat-soft: #e6f8fb;
}

.table {
    --bs-table-bg: transparent;
    margin-bottom: 0;
    color: var(--crm-text);
    vertical-align: middle;
}

.table > :not(caption) > * > * {
    padding: .78rem .75rem;
    border-bottom-color: #edf2f7;
}

.table thead th {
    background: #f8fafc;
    color: #64748b;
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
    white-space: nowrap;
    border-bottom: 1px solid var(--crm-border);
}

.table-hover > tbody > tr:hover > * {
    background: #f8fbff;
}

.table-responsive {
    border-radius: 0 0 var(--crm-radius) var(--crm-radius);
}

.btn {
    border-radius: 9px;
    font-weight: 600;
    font-size: .86rem;
    padding: .5rem .9rem;
}

.btn-sm {
    padding: .34rem .65rem;
    font-size: .78rem;
    border-radius: 8px;
}

.btn-primary {
    background: var(--crm-primary);
    border-color: var(--crm-primary);
}

.btn-primary:hover,
.btn-primary:focus {
    background: var(--crm-primary-dark);
    border-color: var(--crm-primary-dark);
}

.btn-soft-primary {
    background: var(--crm-primary-soft);
    color: var(--crm-primary);
    border: 1px solid transparent;
}

.btn-soft-primary:hover {
    background: #dbe8ff;
    color: var(--crm-primary-dark);
}

.form-label {
    font-size: .8rem;
    font-weight: 600;
    color: #475569;
    margin-bottom: .35rem;
}

.form-control,
.form-select {
    border-color: #dbe3ee;
    border-radius: 9px;
    padding: .52rem .75rem;
    font-size: .875rem;
    color: var(--crm-text);
}

.form-control:focus,
.form-select:focus {
    border-color: #93b4ff;
    box-shadow: 0 0 0 .2rem rgba(37, 99, 235, .1);
}

.form-text {
    font-size: .74rem;
    color: #94a3b8;
}

.input-group-text {
    background: #f8fafc;
    border-color: #dbe3ee;
    color: #64748b;
}

.badge {
    font-weight: 600;
    letter-spacing: .01em;
    padding: .38em .65em;
    border-radius: 7px;
}

.badge-soft-primary {
    background: #e8f0ff;
    color: #1d4ed8;
}

.badge-soft-success {
    background: #e7f8f1;
    color: #047857;
}

.badge-soft-warning {
    background: #fff5e6;
    color: #b45309;
}

.badge-soft-danger {
    background: #ffeaf0;
    color: #be123c;
}

.badge-soft-info {
    background: #e6f8fb;
    color: #0e7490;
}

.badge-soft-secondary {
    background: #f1f5f9;
    color: #475569;
}

.badge-soft-dark {
    background: #e2e8f0;
    color: #1e293b;
}

.empty-state {
    padding: 3rem 1rem;
    text-align: center;
    color: #94a3b8;
}

.empty-state i {
    display: block;
    font-size: 2.4rem;
    margin-bottom: .6rem;
    color: #cbd5e1;
}

.filter-card .card-body {
    padding: .95rem 1.15rem;
}

.nav-tabs {
    border-bottom-color: var(--crm-border);
    gap: .25rem;
}

.nav-tabs .nav-link {
    border: 0;
    border-bottom: 2px solid transparent;
    color: #64748b;
    font-weight: 600;
    font-size: .85rem;
    padding: .65rem .9rem;
    border-radius: 8px 8px 0 0;
}

.nav-tabs .nav-link:hover {
    color: var(--crm-primary);
    background: #f8fbff;
}

.nav-tabs .nav-link.active {
    color: var(--crm-primary);
    background: transparent;
    border-bottom-color: var(--crm-primary);
}

.detail-list dt {
    color: #94a3b8;
    font-size: .74rem;
    font-weight: 600;
    margin-bottom: .15rem;
}

.detail-list dd {
    margin-bottom: .95rem;
    font-weight: 500;
    color: #334155;
}

.timeline {
    position: relative;
    padding-left: 1.4rem;
}

.timeline::before {
    content: "";
    position: absolute;
    top: .35rem;
    bottom: .35rem;
    left: .35rem;
    width: 2px;
    background: #e2e8f0;
}

.timeline-item {
    position: relative;
    padding-bottom: 1.1rem;
}

.timeline-item::before {
    content: "";
    position: absolute;
    left: -1.28rem;
    top: .28rem;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--crm-primary);
    border: 2px solid #fff;
    box-shadow: 0 0 0 2px #dbeafe;
}

.timeline-title {
    font-size: .82rem;
    font-weight: 700;
    color: #334155;
}

.timeline-meta {
    font-size: .72rem;
    color: #94a3b8;
}

.timeline-content {
    margin-top: .25rem;
    color: #475569;
    font-size: .82rem;
    line-height: 1.6;
}

.alert {
    border: 0;
    border-radius: 11px;
    font-size: .86rem;
}

.alert-success {
    background: #e7f8f1;
    color: #065f46;
}

.alert-danger {
    background: #ffeaf0;
    color: #9f1239;
}

.alert-warning {
    background: #fff5e6;
    color: #92400e;
}

.alert-info {
    background: #e6f8fb;
    color: #155e75;
}

.dropdown-menu {
    border: 1px solid var(--crm-border);
    border-radius: 11px;
    box-shadow: 0 14px 35px rgba(15, 23, 42, .12);
    font-size: .86rem;
    padding: .35rem;
}

.dropdown-item {
    border-radius: 7px;
    padding: .5rem .65rem;
}

.dropdown-item:hover {
    background: #f1f6ff;
    color: var(--crm-primary);
}

.modal-content {
    border: 0;
    border-radius: 15px;
    box-shadow: 0 24px 60px rgba(15, 23, 42, .2);
}

.modal-header,
.modal-footer {
    border-color: var(--crm-border);
}

.pagination {
    margin-bottom: 0;
    gap: .2rem;
}

.page-link {
    border: 0;
    border-radius: 8px !important;
    color: #475569;
    font-size: .8rem;
    padding: .42rem .7rem;
}

.page-item.active .page-link {
    background: var(--crm-primary);
    color: #fff;
}

.page-item.disabled .page-link {
    color: #cbd5e1;
    background: transparent;
}

/* --------------------------------------------------------------------------
   登入頁
   -------------------------------------------------------------------------- */
.auth-page {
    min-height: 100vh;
    display: flex;
    background: #f4f7fb;
}

.auth-visual {
    position: relative;
    width: 46%;
    padding: 3.5rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    color: #fff;
    background: radial-gradient(circle at 15% 15%, #1d4ed8 0%, #0f172a 48%, #08101f 100%);
    overflow: hidden;
}

.auth-visual::before,
.auth-visual::after {
    content: "";
    position: absolute;
    border-radius: 50%;
    filter: blur(2px);
}

.auth-visual::before {
    width: 380px;
    height: 380px;
    right: -120px;
    top: -100px;
    background: radial-gradient(circle, rgba(6, 182, 212, .35), transparent 68%);
}

.auth-visual::after {
    width: 460px;
    height: 460px;
    left: -180px;
    bottom: -190px;
    background: radial-gradient(circle, rgba(37, 99, 235, .3), transparent 70%);
}

.auth-visual .auth-brand {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: center;
    gap: .8rem;
    color: #fff;
    font-weight: 700;
    font-size: 1.05rem;
}

.auth-visual .auth-brand .brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border-radius: 14px;
    background: linear-gradient(135deg, #3b82f6, #06b6d4);
    font-size: 1.5rem;
}

.auth-visual .auth-copy {
    position: relative;
    z-index: 1;
    max-width: 440px;
}

.auth-visual .auth-copy h1 {
    font-size: 2.15rem;
    font-weight: 800;
    line-height: 1.3;
    letter-spacing: -.02em;
}

.auth-visual .auth-copy p {
    color: #aebdd3;
    line-height: 1.8;
    margin-top: 1rem;
}

.auth-feature {
    display: flex;
    align-items: flex-start;
    gap: .7rem;
    margin-top: 1rem;
    color: #cbd5e1;
    font-size: .85rem;
}

.auth-feature i {
    color: #38bdf8;
    font-size: 1rem;
    margin-top: .1rem;
}

.auth-visual .auth-footer {
    position: relative;
    z-index: 1;
    color: #64748b;
    font-size: .75rem;
}

.auth-form-side {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.auth-card {
    width: 100%;
    max-width: 440px;
    padding: 2.1rem;
    background: #fff;
    border: 1px solid var(--crm-border);
    border-radius: 18px;
    box-shadow: 0 20px 50px rgba(15, 23, 42, .08);
}

.auth-card h2 {
    font-size: 1.4rem;
    font-weight: 800;
}

.auth-card .auth-subtitle {
    color: var(--crm-muted);
    font-size: .84rem;
    margin-top: .35rem;
    margin-bottom: 1.4rem;
}

.password-toggle {
    border: 1px solid #dbe3ee;
    border-left: 0;
    border-radius: 0 9px 9px 0;
    color: #94a3b8;
    background: #fff;
}

.demo-accounts {
    border-radius: 11px;
    background: #f8fafc;
    border: 1px dashed #cbd5e1;
    padding: .85rem;
    font-size: .72rem;
    color: #64748b;
    line-height: 1.7;
}

.demo-accounts code {
    color: #1d4ed8;
    background: #e8f0ff;
    padding: .08rem .3rem;
    border-radius: 4px;
}

/* --------------------------------------------------------------------------
   動態明細編輯
   -------------------------------------------------------------------------- */
.items-table th {
    white-space: nowrap;
}

.items-table td {
    vertical-align: top;
}

.items-table .form-control,
.items-table .form-select {
    font-size: .8rem;
    padding: .4rem .55rem;
}

.items-table .line-total {
    font-weight: 700;
    color: #0f172a;
    white-space: nowrap;
    padding-top: .7rem;
}

.totals-box {
    max-width: 340px;
    margin-left: auto;
    border-radius: 11px;
    background: #f8fafc;
    border: 1px solid var(--crm-border);
    padding: 1rem 1.15rem;
}

.totals-box .total-row {
    display: flex;
    justify-content: space-between;
    padding: .3rem 0;
    font-size: .84rem;
    color: #475569;
}

.totals-box .grand-total {
    border-top: 1px dashed #cbd5e1;
    margin-top: .4rem;
    padding-top: .55rem;
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--crm-primary-dark);
}

.stock-badge {
    font-size: .7rem;
    padding: .25rem .5rem;
    border-radius: 6px;
    background: #f1f5f9;
    color: #475569;
    white-space: nowrap;
}

.stock-badge.low {
    background: #ffeaf0;
    color: #be123c;
}

.company-logo-preview {
    max-width: 180px;
    max-height: 90px;
    object-fit: contain;
    border: 1px solid var(--crm-border);
    border-radius: 10px;
    padding: .45rem;
    background: #fff;
}

.security-note {
    border-left: 4px solid var(--crm-primary);
    background: #f8fbff;
    border-radius: 8px;
    padding: .85rem 1rem;
    font-size: .8rem;
    color: #475569;
    line-height: 1.7;
}

.chart-wrap {
    position: relative;
    min-height: 320px;
}

/* --------------------------------------------------------------------------
   響應式
   -------------------------------------------------------------------------- */
@media (max-width: 991.98px) {
    .app-content {
        padding: 1.2rem 1rem 1.8rem;
    }

    .topbar {
        padding: .65rem 1rem;
    }

    .sidebar-inner {
        min-height: 100vh;
    }

    .auth-visual {
        display: none;
    }

    .auth-form-side {
        padding: 1.4rem;
    }
}

@media (max-width: 575.98px) {
    .page-title {
        font-size: 1.25rem;
    }

    .stat-card .stat-value {
        font-size: 1.35rem;
    }

    .auth-card {
        padding: 1.4rem;
    }

    .card-body {
        padding: .95rem;
    }
}

@media (min-width: 992px) {
    .sidebar {
        position: sticky;
        top: 0;
        height: 100vh;
        flex: 0 0 270px;
        width: 270px;
    }

    .sidebar.offcanvas-lg {
        visibility: visible !important;
        transform: none !important;
        background: transparent;
    }

    .sidebar .offcanvas-body {
        height: 100vh;
        overflow-y: auto;
        background: var(--crm-sidebar);
        padding: 0;
    }
}

@media print {
    .sidebar,
    .topbar,
    .app-footer,
    .no-print {
        display: none !important;
    }

    .app-content {
        padding: 0;
    }

    body {
        background: #fff;
    }
}
