/* ============================================
 CSS Variables & Theme
 ============================================ */
:root {
    --pad: 14px;
    --gap: 14px;
    /* Dark Theme Colors */
    --bg-primary: #0b1020;
    --bg-secondary: #0f1530;
    --bg-tertiary: #0f1733;
    --bg-card: #0f1530;
    --card-bg: #0f1530;
    --bg-gradient: linear-gradient(0deg, #0b1020, #0f1530);
    --bg-card-gradient:
        radial-gradient(1200px 400px at -10% -40%, #182045 0%, transparent 40%),
        #0f1530;
    --text-primary: #e7e9ee;
    --text-secondary: rgba(231, 233, 238, 0.7);
    --border-primary: #20263f;
    --border-secondary: #1c2342;
    --border-tertiary: #243055;
    --border-color: #20263f;
    --border-focus: #4c74ff;
    --badge-border: #2a3a78;
    --ok-bg: #072a1b;
    --ok-border: #144d37;
    --ok-text: #79f2c0;
    --warn-bg: #2a1707;
    --warn-border: #6b3b0a;
    --warn-text: #ffd37a;
    --danger-bg: #2a0707;
    --danger-border: #6b0a0a;
    --danger-text: #ff8f8f;
    --shadow: rgba(0, 0, 0, 0.25);
    --card-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    --primary-color: #4c74ff;
    --primary-hover: #3d5fd9;
    --hover-bg: rgba(76, 116, 255, 0.1);
}

body.light-theme {
    /* Light Theme Colors */
    --bg-primary: #f5f7fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #ffffff;
    --bg-card: #ffffff;
    --card-bg: #ffffff;
    --bg-gradient: linear-gradient(0deg, #f5f7fa, #ffffff);
    --bg-card-gradient:
        radial-gradient(1200px 400px at -10% -40%, #e8ecf5 0%, transparent 40%),
        #ffffff;
    --text-primary: #1a1d2e;
    --text-secondary: rgba(26, 29, 46, 0.7);
    --border-primary: #e1e4e8;
    --border-secondary: #d1d5db;
    --border-tertiary: #c2c7d0;
    --border-color: #e1e4e8;
    --border-focus: #4c74ff;
    --badge-border: #c2c7d0;
    --ok-bg: #d1fae5;
    --ok-border: #6ee7b7;
    --ok-text: #047857;
    --warn-bg: #fef3c7;
    --warn-border: #fbbf24;
    --warn-text: #92400e;
    --danger-bg: #fee2e2;
    --danger-border: #fca5a5;
    --danger-text: #b91c1c;
    --shadow: rgba(0, 0, 0, 0.1);
    --card-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    --primary-color: #4c74ff;
    --primary-hover: #3d5fd9;
    --hover-bg: rgba(76, 116, 255, 0.05);
}

/* ============================================
 Base Styles
 ============================================ */
body {
    font-family:
        ui-sans-serif,
        system-ui,
        -apple-system,
        "Segoe UI",
        Roboto,
        "Noto Sans Thai",
        "Prompt",
        Arial,
        sans-serif;
    margin: 0;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition:
        background-color 0.3s ease,
        color 0.3s ease;
}

/* ============================================
 Header & Navigation
 ============================================ */
header {
    padding: 18px var(--pad);
    border-bottom: 1px solid var(--border-primary);
    background: var(--bg-gradient);
    position: sticky;
    top: 0;
    z-index: 10;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

h1 {
    margin: 0 0 2px;
    font-size: 20px;
    font-weight: 700;
    letter-spacing: 0.2px;
}

.sub {
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 400;
}

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

.filters {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.filters select {
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--border-secondary);
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.filters select:hover {
    border-color: var(--border-focus);
}

.filters select:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 2px rgba(76, 116, 255, 0.15);
}

/* ============================================
 Buttons
 ============================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: linear-gradient(135deg, #4c74ff 0%, #3b5bdb 100%);
    color: #fff;
    box-shadow: 0 2px 8px rgba(76, 116, 255, 0.3);
}

.btn-primary:hover {
    box-shadow: 0 4px 12px rgba(76, 116, 255, 0.4);
    transform: translateY(-1px);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-primary);
    border-color: var(--border-secondary);
}

.btn-secondary:hover {
    border-color: var(--border-focus);
    background: var(--bg-tertiary);
}

#theme-toggle {
    padding: 8px;
    font-size: 18px;
    cursor: pointer;
}

/* ============================================
 KPI Cards
 ============================================ */
.kpi-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: var(--gap);
    padding: var(--pad);
}

.kpi-card {
    background: var(--bg-card-gradient);
    border: 1px solid var(--border-secondary);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 2px 8px var(--shadow);
    transition: all 0.2s;
}

.kpi-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px var(--shadow);
}

.kpi-title {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 6px;
    font-weight: 500;
}

.kpi-val {
    font-size: 30px;
    font-weight: 700;
    margin: 0;
    background: linear-gradient(135deg, #4c74ff 0%, #7b61ff 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* ============================================
 Charts Section
 ============================================ */
.charts-section {
    padding: var(--pad);
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--gap);
}

.chart-card {
    background: var(--bg-card-gradient);
    border: 1px solid var(--border-secondary);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 2px 8px var(--shadow);
}

.chart-title {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--text-primary);
}

.chart-container {
    width: 100%;
    height: 320px;
}

/* ============================================
 Data Table
 ============================================ */
.table-section {
    padding: var(--pad);
}

.table-card {
    background: var(--bg-card-gradient);
    border: 1px solid var(--border-secondary);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px var(--shadow);
}

.table-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-secondary);
}

.table-header h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.table-wrapper {
    overflow-x: auto;
    max-height: 600px;
}

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

thead {
    background: var(--bg-tertiary);
    position: sticky;
    top: 0;
    z-index: 1;
}

thead th {
    padding: 12px 14px;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-secondary);
    white-space: nowrap;
}

tbody td {
    padding: 12px 14px;
    font-size: 13px;
    border-bottom: 1px solid var(--border-primary);
}

tbody tr:hover {
    background: var(--bg-tertiary);
}

.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    border: 1px solid var(--badge-border);
}

.badge.ok {
    background: var(--ok-bg);
    border-color: var(--ok-border);
    color: var(--ok-text);
}

.badge.warn {
    background: var(--warn-bg);
    border-color: var(--warn-border);
    color: var(--warn-text);
}

/* ============================================
 Modal Styles
 ============================================ */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.2s ease;
}

.modal.show {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border-secondary);
    border-radius: 16px;
    width: 90%;
    max-width: 500px;
    padding: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    animation: slideUp 0.3s ease;
    position: relative;
}

.modal-content.large {
    max-width: 800px;
}

@keyframes slideUp {
    from {
        transform: translateY(30px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-content h2 {
    margin: 0 0 20px 0;
    font-size: 18px;
    font-weight: 600;
}

.close {
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 28px;
    font-weight: bold;
    color: var(--text-secondary);
    cursor: pointer;
    transition: color 0.2s;
}

.close:hover {
    color: var(--text-primary);
}

/* ============================================
 Form Styles
 ============================================ */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--border-secondary);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 14px;
    transition: all 0.2s;
    box-sizing: border-box;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px rgba(76, 116, 255, 0.1);
}

/* ============================================
 Loading Spinner
 ============================================ */
.loading {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ============================================
 Responsive Design
 ============================================ */
@media (min-width: 768px) {
    .charts-section {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .charts-section {
        grid-template-columns: 2fr 1fr;
    }

    .chart-container {
        height: 380px;
    }
}

@media (max-width: 640px) {
    h1 {
        font-size: 18px;
    }

    .filters {
        width: 100%;
    }

    .filters select {
        flex: 1;
        min-width: 0;
    }

    .kpi-row {
        grid-template-columns: repeat(2, 1fr);
    }

    .btn {
        font-size: 12px;
        padding: 7px 12px;
    }
}
