/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f0f2f5;
    --surface: #ffffff;
    --border: #e4e7eb;
    --text: #1f2937;
    --text-secondary: #6b7280;
    --primary: #0073ea;
    --primary-hover: #0060c2;
    --won: #00c875;
    --won-bg: #e6f9f0;
    --lost: #df2f4a;
    --lost-bg: #fce8ec;
    --active: #579bfc;
    --active-bg: #eaf1ff;
    --conversion: #9d50dd;
    --conversion-bg: #f3e8ff;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

.hidden { display: none !important; }

/* === Modal === */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal.hidden { display: none; }

.modal-content {
    background: var(--surface);
    border-radius: 12px;
    padding: 32px;
    max-width: 480px;
    width: 90%;
    box-shadow: var(--shadow-lg);
}

.modal-content h2 {
    font-size: 1.25rem;
    margin-bottom: 8px;
}

.modal-content p {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-bottom: 16px;
}

.modal-content input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    outline: none;
    transition: border-color 0.2s;
}

.modal-content input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0,115,234,0.1);
}

.modal-actions {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
}

.hint {
    color: var(--text-secondary);
    font-size: 0.75rem !important;
    margin-top: 12px !important;
    margin-bottom: 0 !important;
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: var(--radius);
    border: none;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}

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

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

.btn-secondary {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
}

.btn-secondary:hover { background: var(--bg); }

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    padding: 8px;
}

.btn-ghost:hover { color: var(--text); background: var(--bg); }

.btn-sm {
    padding: 4px 12px;
    font-size: 0.8rem;
    border-radius: 6px;
    background: var(--bg);
    border: 1px solid transparent;
    color: var(--text-secondary);
}

.btn-sm.active {
    background: var(--primary);
    color: white;
}

.btn-sm:hover:not(.active) {
    border-color: var(--border);
}

/* === Header === */
header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 16px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-logo {
    height: 36px;
    width: auto;
    object-fit: contain;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 700;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.8rem;
    margin-left: 12px;
}

.last-updated {
    color: var(--text-secondary);
    font-size: 0.7rem;
    margin-left: 16px;
    opacity: 0.8;
}

.auto-refresh-control {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text-secondary);
    background: var(--bg);
    padding: 4px 10px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.auto-refresh-control label {
    font-weight: 500;
    white-space: nowrap;
}

.auto-refresh-control select {
    border: none;
    background: transparent;
    font-size: 0.8rem;
    color: var(--text);
    outline: none;
    cursor: pointer;
    padding: 2px 4px;
}

.countdown {
    font-size: 0.7rem;
    font-variant-numeric: tabular-nums;
    min-width: 32px;
    text-align: right;
    color: var(--primary);
    font-weight: 600;
}

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

/* === Filters === */

/* === Loading === */
.loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 24px;
    gap: 16px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.loading p {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.loading-detail {
    font-size: 0.75rem !important;
    color: var(--text-secondary);
}

/* === Dashboard === */
#dashboard {
    padding: 24px;
    max-width: 1400px;
    margin: 0 auto;
}

.chart-filter {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.chart-filter label {
    font-weight: 500;
}

.chart-filter select {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.8rem;
    background: white;
    outline: none;
}

/* === Product Sections === */
.product-section {
    margin-bottom: 8px;
}

.product-section-header {
    margin-bottom: 8px;
}

.product-section-label {
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
    padding: 4px 12px;
    border-radius: 4px;
    background: var(--bg);
    border: 1px solid var(--border);
}

.product-logo .product-section-label {
    border-left: 4px solid var(--primary);
}

.product-nonlogo .product-section-label {
    border-left: 4px solid #fdab3d;
}

/* === KPI Cards === */
.kpi-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    margin-bottom: 12px;
}

.kpi-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    border-left: 4px solid var(--border);
}

.kpi-card.won { border-left-color: var(--won); background: var(--won-bg); }
.kpi-card.lost { border-left-color: var(--lost); background: var(--lost-bg); }
.kpi-card.active { border-left-color: var(--active); background: var(--active-bg); }
.kpi-card.conversion { border-left-color: var(--conversion); background: var(--conversion-bg); }

.kpi-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 4px;
    min-height: 2.4em;
    line-height: 1.2;
    display: flex;
    align-items: flex-end;
}

.kpi-value {
    font-size: 1.5rem;
    font-weight: 700;
}

.kpi-card.won .kpi-value { color: #037f4c; }
.kpi-card.lost .kpi-value { color: var(--lost); }
.kpi-card.active .kpi-value { color: #0060c2; }
.kpi-card.conversion .kpi-value { color: var(--conversion); }

.kpi-today {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--won);
    vertical-align: middle;
    margin-left: 2px;
}

/* === Charts === */
.charts-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 16px;
    margin-bottom: 16px;
}

.chart-card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}

.chart-card.wide {
    grid-column: 1 / -1;
}

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

.chart-header h3 {
    font-size: 0.95rem;
    font-weight: 600;
}

.chart-container {
    position: relative;
    height: 300px;
}

/* === Table === */
.table-card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px 20px;
    margin-bottom: 16px;
}

.table-card h3 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 10px;
}

.table-wrapper {
    overflow-x: auto;
}

.product-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.product-table th {
    text-align: center;
    padding: 6px 8px;
    border-bottom: 2px solid var(--border);
    font-weight: 600;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    white-space: nowrap;
}

.product-table th:first-child {
    text-align: left;
}

.product-table th.month-group {
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text);
    padding: 6px 8px;
}

.product-table th.month-group.current {
    background: var(--active-bg);
    color: var(--primary);
}

.product-table td {
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.product-table td:first-child {
    text-align: left;
    font-weight: 500;
}

.product-table tbody tr:hover {
    background: #f9fafb;
}

.product-table tfoot td {
    font-weight: 700;
    border-top: 2px solid var(--border);
    border-bottom: none;
    background: #f9fafb;
}

.rate-badge {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 0.75rem;
}

.rate-high { background: var(--won-bg); color: #037f4c; }
.rate-medium { background: #fff8e6; color: #b58105; }
.rate-low { background: var(--lost-bg); color: var(--lost); }

.target-cr {
    color: var(--text-secondary);
    font-size: 0.75rem;
}

/* Month group separators — vertical border on first column of each month */
.product-table th.month-group + .month-group,
.product-table td.month-sep {
    border-left: 2px solid var(--border);
}

/* Alternating month-group backgrounds (past months get subtle tint) */
.product-table td.month-past {
    background: #f8f9fa;
}

.product-table tfoot td.month-past {
    background: #f1f3f5;
}

/* === Settings Modal === */
.modal-wide {
    max-width: 560px;
}

.settings-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 12px;
}

.settings-panel {
    min-height: 180px;
}

.settings-panel h4 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 12px 0 6px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.settings-panel h4:first-child {
    margin-top: 0;
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
}

.settings-row label {
    font-size: 0.85rem;
    font-weight: 500;
    white-space: nowrap;
}

.settings-row input {
    width: 120px;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.85rem;
    text-align: right;
    outline: none;
}

.settings-row input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0,115,234,0.1);
}

/* === Responsive - Mobile === */
@media (max-width: 768px) {
    header { flex-direction: column; gap: 12px; align-items: flex-start; }
    .kpi-row { grid-template-columns: repeat(2, 1fr); }
    .charts-row { grid-template-columns: 1fr; }
    #dashboard { padding: 16px; }
    .chart-container { height: 250px; }
    .product-table { font-size: 0.7rem; }
    .product-table th { font-size: 0.6rem; }
    .product-table td { padding: 5px 6px; }
}

/* === Responsive - 4K TV (3840x2160) === */
@media (min-width: 2560px) {
    body { font-size: 1.25rem; }

    header { padding: 20px 40px; }
    header h1 { font-size: 2rem; }
    .header-logo { height: 56px; }
    .last-updated { font-size: 1rem; margin-left: 24px; }

    .auto-refresh-control {
        font-size: 1.1rem;
        padding: 8px 16px;
        gap: 10px;
    }
    .auto-refresh-control select { font-size: 1.1rem; }
    .countdown { font-size: 1rem; min-width: 48px; }

    .btn { padding: 12px 24px; font-size: 1.1rem; gap: 10px; }
    .btn svg { width: 22px; height: 22px; }
    .btn-ghost { padding: 12px; }
    .btn-ghost svg { width: 26px; height: 26px; }

    #dashboard { padding: 32px 40px; max-width: 100%; }

    .product-section { margin-bottom: 12px; }
    .product-section-label { font-size: 1.1rem; padding: 6px 16px; }
    .kpi-row { grid-template-columns: repeat(9, 1fr); gap: 14px; margin-bottom: 14px; }
    .kpi-card { padding: 20px 24px; }
    .kpi-label { font-size: 0.85rem; margin-bottom: 6px; min-height: 2.4em; }
    .kpi-value { font-size: 2rem; }
    .kpi-today { font-size: 1rem; }

    .charts-row { gap: 20px; margin-bottom: 20px; }
    .charts-row { grid-template-columns: repeat(2, 1fr); }
    .chart-card { padding: 24px; }
    .chart-card.wide { grid-column: 1 / -1; }
    .chart-header h3 { font-size: 1.3rem; }
    .chart-container { height: 420px; }

    .chart-filter label { font-size: 1rem; }
    .chart-filter select { font-size: 1rem; padding: 6px 12px; }

    .table-card { padding: 24px 28px; margin-bottom: 20px; }
    .table-card h3 { font-size: 1.5rem; margin-bottom: 14px; }
    .btn-sm { padding: 8px 18px; font-size: 1.1rem; }
    .product-table { font-size: 1.3rem; }
    .product-table th { padding: 12px 14px; font-size: 1.05rem; }
    .product-table th.month-group { font-size: 1.15rem; padding: 10px 14px; }
    .product-table td { padding: 12px 14px; }
    .rate-badge { padding: 5px 12px; font-size: 1.15rem; }
    .target-cr { font-size: 1.05rem; }
}
