/* ===== CSS Variables ===== */
:root {
    --primary: #E8792B;
    --primary-dark: #c9621e;
    --primary-light: #f5a66a;
    --sidebar-bg: #2D2D2D;
    --sidebar-hover: #3d3d3d;
    --sidebar-active: #E8792B;
    --bg: #F5F5F5;
    --white: #FFFFFF;
    --text: #333333;
    --text-light: #666666;
    --text-muted: #999999;
    --border: #E0E0E0;
    --success: #198754;
    --danger: #dc3545;
    --warning: #ffc107;
    --info: #0d6efd;
    --shadow: 0 2px 8px rgba(0,0,0,0.08);
    --radius: 8px;
    --sidebar-width: 240px;
    --topbar-height: 60px;
    --font: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
}

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow-x: hidden;
}
a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }
img { max-width: 100%; }
table { border-collapse: collapse; width: 100%; }

/* ===== Layout ===== */
.app-wrapper { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: #ccc;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform 0.3s;
}
.sidebar-header {
    padding: 20px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar-logo { height: 40px; width: auto; }
.sidebar-title { font-size: 13px; font-weight: 600; color: #fff; }
.sidebar-nav { flex: 1; padding: 12px 0; overflow-y: auto; }
.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    color: #aaa;
    font-size: 13px;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active {
    background: rgba(232,121,43,0.15);
    color: var(--primary);
    border-left-color: var(--primary);
}
.nav-item i { width: 20px; text-align: center; font-size: 15px; }
.nav-divider { height: 1px; background: rgba(255,255,255,0.08); margin: 8px 16px; }
.sidebar-footer {
    padding: 12px 0;
    border-top: 1px solid rgba(255,255,255,0.1);
}
.user-info {
    padding: 8px 20px;
    font-size: 12px;
    color: #999;
    display: flex;
    align-items: center;
    gap: 8px;
}
.logout-link { color: #888 !important; font-size: 12px; }

/* Main Content */
.main-content {
    min-width: 0;
    flex: 1;
    margin-left: var(--sidebar-width);
    min-height: 100vh;
}
.topbar {
    height: var(--topbar-height);
    background: var(--white);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 24px;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 50;
}
.sidebar-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text);
    cursor: pointer;
}
.page-title { font-size: 18px; font-weight: 600; color: var(--text); }
.content-area { padding: 24px; overflow-x: auto; }

/* ===== Cards ===== */
.card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 20px;
}
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}
.card-header h2 { font-size: 16px; font-weight: 600; }

/* Stat Cards */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    overflow: hidden;
    min-width: 0;
}
.stat-info { min-width: 0; }
.stat-info h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-icon {
    width: 48px; height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #fff;
}
.stat-icon.orange { background: var(--primary); }
.stat-icon.green { background: var(--success); }
.stat-icon.blue { background: var(--info); }
.stat-icon.red { background: var(--danger); }
.stat-info h3 { font-size: clamp(16px, 2vw, 22px); font-weight: 700; line-height: 1.2; }
.stat-info p { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ===== Tables ===== */
.table-wrapper { overflow-x: auto; }
.data-table { width: 100%; }
.data-table th {
    background: #f8f8f8;
    padding: 10px 12px;
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 2px solid var(--border);
}
.data-table td {
    padding: 10px 12px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 13px;
    vertical-align: middle;
}
.data-table tr:hover td { background: #fafafa; }
.data-table .text-right { text-align: right; }
.data-table .text-center { text-align: center; }
.data-table .nowrap { white-space: nowrap; }

/* ===== Status Badge ===== */
.status-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font);
    text-decoration: none;
}
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { color: #fff; }
.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

/* ===== Forms ===== */
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    margin-bottom: 4px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-light);
}
.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    font-family: var(--font);
    color: var(--text);
    background: var(--white);
    transition: border-color 0.2s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(232,121,43,0.1); }
select.form-control { appearance: auto; }
textarea.form-control { resize: vertical; min-height: 80px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
.form-row-3 { grid-template-columns: repeat(3, 1fr); }
.form-row-4 { grid-template-columns: repeat(4, 1fr); }
.form-actions {
    display: flex;
    gap: 12px;
    padding-top: 16px;
    margin-top: 16px;
    border-top: 1px solid var(--border);
}
.form-section {
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid #f0f0f0;
}
.form-section h3 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 12px;
}

/* ===== Alerts ===== */
.alert {
    padding: 12px 16px;
    border-radius: 6px;
    margin-bottom: 16px;
    font-size: 13px;
}
.alert-success { background: #d1e7dd; color: #0f5132; }
.alert-danger { background: #f8d7da; color: #842029; }
.alert-warning { background: #fff3cd; color: #664d03; }
.alert-info { background: #cff4fc; color: #055160; }

/* ===== Toolbar ===== */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 12px;
}
.search-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0 12px;
}
.search-box input {
    border: none;
    padding: 8px 0;
    font-size: 13px;
    outline: none;
    font-family: var(--font);
    min-width: 200px;
}
.search-box i { color: var(--text-muted); }
.filter-group { display: flex; gap: 8px; align-items: center; }
.filter-group select {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    font-family: var(--font);
    background: var(--white);
}
.radio-btn {
    display: inline-flex;
    align-items: center;
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    font-family: var(--font);
    background: var(--white);
    cursor: pointer;
    transition: all 0.15s;
}
.radio-btn:hover { border-color: var(--primary-light); }
.radio-btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}
.radio-btn input { display: none; }

/* ===== Pagination ===== */
.pagination {
    display: flex;
    justify-content: center;
    gap: 4px;
    margin-top: 20px;
}
.pagination a, .pagination span {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 13px;
    color: var(--text);
}
.pagination a:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
.pagination .active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ===== Dynamic Line Items ===== */
.line-items-table { margin-top: 12px; }
.line-items-table th { font-size: 11px; padding: 8px 6px; }
.line-items-table td { padding: 4px 4px; }
.line-items-table .form-control { padding: 6px 8px; font-size: 12px; }
.line-items-table .row-remove {
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    font-size: 16px;
    padding: 4px;
}
.add-row-btn {
    margin-top: 8px;
    font-size: 12px;
    color: var(--primary);
    background: none;
    border: 1px dashed var(--primary);
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
}
.add-row-btn:hover { background: rgba(232,121,43,0.05); }

/* ===== Section Blocks (Quotation) ===== */
.section-block {
    background: #fafafa;
    border: 1px solid #eee;
    border-radius: 6px;
    padding: 16px;
    margin-bottom: 16px;
}
.section-block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}
.section-block-header h4 { font-size: 14px; font-weight: 600; }

/* ===== Totals Block ===== */
.totals-block {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
}
.totals-table { width: 300px; }
.totals-table td { padding: 6px 12px; font-size: 13px; }
.totals-table .total-row { font-weight: 700; font-size: 15px; border-top: 2px solid var(--text); }
.totals-table .label { text-align: right; color: var(--text-light); }
.totals-table .value { text-align: right; font-weight: 600; }

/* ===== Document View ===== */
.doc-view { max-width: 800px; }
.doc-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}
.doc-meta {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 4px 12px;
    font-size: 13px;
    margin-bottom: 16px;
}
.doc-meta dt { font-weight: 600; color: var(--text-light); }
.doc-meta dd { color: var(--text); }

/* ===== Login Page ===== */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #2D2D2D 0%, #1a1a1a 100%);
}
.login-box {
    background: var(--white);
    border-radius: 12px;
    padding: 40px;
    width: 400px;
    max-width: 90%;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-box .logo-area {
    text-align: center;
    margin-bottom: 32px;
}
.login-box .logo-area img { height: 60px; }
.login-box .logo-area h2 { font-size: 16px; color: var(--text-light); margin-top: 8px; }
.login-box .form-group { margin-bottom: 20px; }
.login-box .form-control { padding: 10px 14px; font-size: 14px; }
.login-box .btn { width: 100%; padding: 12px; font-size: 15px; justify-content: center; }
.login-error { color: var(--danger); font-size: 13px; text-align: center; margin-bottom: 16px; }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 0; border-bottom: 2px solid var(--border); margin-bottom: 20px; }
.tab-link {
    padding: 10px 20px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    transition: all 0.2s;
    background: none;
    border-top: none;
    border-left: none;
    border-right: none;
    font-family: var(--font);
}
.tab-link:hover { color: var(--text); }
.tab-link.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}
.empty-state i { font-size: 48px; margin-bottom: 16px; display: block; }
.empty-state p { font-size: 15px; margin-bottom: 16px; }

/* ===== Sidebar Backdrop ===== */
.sidebar-backdrop {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 99;
}
.sidebar-backdrop.active { display: block; }

/* ===== Table scroll indicator ===== */
.table-wrapper {
    position: relative;
    -webkit-overflow-scrolling: touch;
}
.table-wrapper::after {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 20px;
    background: linear-gradient(to left, rgba(0,0,0,0.06), transparent);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
}
.table-wrapper.has-overflow::after { opacity: 1; }

/* ===== Responsive: Tablet (768px - 1024px) ===== */
@media (max-width: 1024px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0,0,0,0.3);
    }
    .sidebar-toggle { display: block; }
    .main-content {
    min-width: 0; margin-left: 0; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .form-row-4 { grid-template-columns: repeat(2, 1fr); }
}

/* ===== Responsive: Mobile (< 768px) ===== */
@media (max-width: 768px) {
    /* Topbar */
    .topbar { padding: 0 12px; gap: 10px; }
    .page-title {
        font-size: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
    }

    /* Content */
    .content-area { padding: 16px; }
    .card { padding: 16px; }

    /* Forms */
    .form-row, .form-row-3, .form-row-4 { grid-template-columns: 1fr; }
    .form-actions { flex-wrap: wrap; }

    /* Tables */
    .data-table th { padding: 8px 8px; font-size: 11px; }
    .data-table td { padding: 8px 8px; font-size: 12px; }

    /* Toolbar */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .search-box { width: 100%; }
    .search-box input { min-width: 0; width: 100%; }
    .filter-group {
        flex-wrap: wrap;
        width: 100%;
    }
    .filter-group select { flex: 1; min-width: 0; }
    .radio-btn { font-size: 12px; padding: 5px 10px; }

    /* Buttons - touch targets */
    .btn { min-height: 44px; justify-content: center; }
    .btn-sm { min-height: 36px; }

    /* Totals block fluid */
    .totals-table { width: 100%; max-width: 300px; }

    /* Line items */
    .line-items-table th { font-size: 10px; padding: 6px 4px; }
    .line-items-table td { padding: 3px 2px; }
    .line-items-table .form-control { padding: 5px 4px; font-size: 11px; }

    /* Document view */
    .doc-view { padding: 0; }
    .doc-meta { grid-template-columns: 100px 1fr; gap: 4px 8px; font-size: 12px; }
    .doc-actions { flex-wrap: wrap; }

    /* Tabs */
    .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .tab-link { padding: 8px 14px; font-size: 12px; white-space: nowrap; }

    /* Pagination */
    .pagination { flex-wrap: wrap; }
    .pagination a, .pagination span { padding: 8px 12px; min-height: 36px; display: flex; align-items: center; }

    /* Section blocks */
    .section-block { padding: 12px; }
    .section-block-header h4 { font-size: 13px; }
}

/* ===== Responsive: Small Mobile (< 480px) ===== */
@media (max-width: 480px) {
    .stats-grid { grid-template-columns: 1fr; }
    .content-area { padding: 12px; }
    .card { padding: 12px; margin-bottom: 12px; }

    /* Full-width buttons */
    .toolbar .btn { width: 100%; }
    .btn-group { flex-direction: column; }
    .btn-group .btn { width: 100%; }
    .doc-actions .btn { flex: 1; font-size: 12px; }

    /* Stat cards compact */
    .stat-card { padding: 14px; gap: 12px; }
    .stat-icon { width: 40px; height: 40px; font-size: 18px; }
    .stat-info h3 { font-size: 18px; }

    /* Card header stack */
    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    /* Login */
    .login-box { padding: 24px; }
}

/* ===== Responsive Grid Helpers ===== */
.grid-2-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
@media (max-width: 768px) {
    .grid-2-col { grid-template-columns: 1fr; }
}

/* ===== Misc ===== */
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.text-primary { color: var(--primary); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }
.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mb-3 { margin-bottom: 24px; }
.d-flex { display: flex; }
.gap-2 { gap: 16px; }
.flex-1 { flex: 1; }
.hidden { display: none; }

/* ===== Excel-like Grid ===== */
.excel-container { width:100%; overflow-x:auto; border:1px solid #bbb; background:#fff; }
.excel-table { width:100%; border-collapse:collapse; table-layout:fixed; }
.excel-table th { background:#f0f0f0; color:#333; font-weight:600; padding:6px 4px; border:1px solid #bbb; font-size:11px; text-align:center; position:sticky; top:0; z-index:10; text-transform:uppercase; letter-spacing:0.3px; white-space:nowrap; }
.excel-table td { border:1px solid #ccc; padding:0; background:#fff; height:28px; vertical-align:middle; }
.excel-table tr:hover td { background:#f5f9ff; }
.excel-input { width:100%; height:100%; border:none; padding:3px 4px; font-size:12px; font-family:inherit; outline:none; background:transparent; box-sizing:border-box; margin:0; -moz-appearance:textfield; }
.excel-input::-webkit-outer-spin-button, .excel-input::-webkit-inner-spin-button { -webkit-appearance:none; margin:0; }
.excel-input:focus { background:#fff3cd; }
.excel-input.number { text-align:right; }
.excel-input.center { text-align:center; }
.excel-table td.excel-action { background:#f8f8f8; text-align:center; width:28px; }
.excel-delete { background:none; border:none; color:#ccc; cursor:pointer; font-size:12px; padding:2px 6px; line-height:1; }
.excel-delete:hover { color:#dc3545; }
