/* ═══════════════════════════════════════════
   Components – Cards, Tables, Forms, Modals, Badges, Toast
   ═══════════════════════════════════════════ */

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-5);
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: all var(--transition-fast);
    white-space: nowrap;
    line-height: 1.5;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    box-shadow: 0 2px 8px rgba(232, 89, 12, 0.25);
}
.btn-primary:hover { background: linear-gradient(135deg, var(--primary-dark), var(--primary-darker)); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(232, 89, 12, 0.35); }

.btn-secondary {
    background: var(--bg-muted);
    color: var(--text-primary);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg-hover); border-color: var(--border-strong); }

.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: var(--success-dark); }

.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: var(--danger-dark); }

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

.btn-sm { padding: var(--sp-1) var(--sp-3); font-size: var(--text-xs); }
.btn-lg { padding: var(--sp-3) var(--sp-8); font-size: var(--text-base); }
.btn-icon-only { padding: var(--sp-2); }

/* ── Cards ── */
.card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--transition-base), transform var(--transition-base);
}

.card:hover { box-shadow: var(--shadow-md); }

.card-header {
    padding: var(--sp-5) var(--sp-6);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-header h3 {
    font-size: var(--text-base);
    font-weight: 600;
}

.card-body { padding: var(--sp-6); }
.card-footer { padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--border); }

/* KPI Card */
.kpi-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--sp-5) var(--sp-6);
    box-shadow: var(--shadow-sm);
    transition: all var(--transition-base);
    position: relative;
    overflow: hidden;
}

.kpi-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), var(--accent));
}

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

.kpi-card .kpi-icon {
    font-size: 1.75rem;
    margin-bottom: var(--sp-3);
}

.kpi-card .kpi-value {
    font-family: var(--font-mono);
    font-size: var(--text-2xl);
    font-weight: 700;
    color: var(--text-primary);
    animation: countUp 0.5s ease-out;
}

.kpi-card .kpi-label {
    font-size: var(--text-sm);
    color: var(--text-secondary);
    margin-top: var(--sp-1);
}

.kpi-card .kpi-trend {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    font-size: var(--text-xs);
    font-weight: 600;
    margin-top: var(--sp-2);
    padding: 2px 8px;
    border-radius: var(--radius-full);
}

.kpi-trend.up { color: var(--success); background: var(--success-light); }
.kpi-trend.down { color: var(--danger); background: var(--danger-light); }

/* ── Tables ── */
.table-container {
    overflow-x: auto;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    background: var(--bg-surface);
}

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

.data-table thead th {
    padding: var(--sp-3) var(--sp-4);
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    background: var(--bg-muted);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.data-table tbody td {
    padding: var(--sp-3) var(--sp-4);
    font-size: var(--text-sm);
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.data-table tbody tr { transition: background var(--transition-fast); }
.data-table tbody tr:hover { background: var(--bg-hover); }
.data-table tbody tr:last-child td { border-bottom: none; }

/* ── Badges ── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 2px 10px;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 600;
    white-space: nowrap;
}

.badge-primary { background: var(--primary-lightest); color: var(--primary-dark); }
.badge-success { background: var(--success-light); color: var(--success-dark); }
.badge-warning { background: var(--warning-light); color: var(--warning-dark); }
.badge-danger { background: var(--danger-light); color: var(--danger-dark); }
.badge-info { background: var(--info-light); color: var(--info); }
.badge-neutral { background: var(--bg-muted); color: var(--text-secondary); }

/* ── Forms ── */
.form-group {
    margin-bottom: var(--sp-4);
}

.form-label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--sp-1);
}

.form-input, .form-select, .form-textarea {
    width: 100%;
    padding: var(--sp-2) var(--sp-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    color: var(--text-primary);
    background: var(--bg-surface);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    outline: none;
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(232, 89, 12, 0.1);
}

.form-textarea { resize: vertical; min-height: 80px; }

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--sp-4);
}

/* ── Modal ── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(28, 25, 23, 0.4);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
}

.modal-container {
    background: var(--bg-surface);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    max-width: 640px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: scaleIn var(--transition-base) ease-out;
}

.modal-container.modal-lg { max-width: 900px; }
.modal-container.modal-xl { max-width: 1100px; }

.modal-header {
    padding: var(--sp-5) var(--sp-6);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h2 { font-size: var(--text-lg); font-weight: 700; }

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-tertiary);
    padding: var(--sp-1);
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    line-height: 1;
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }

.modal-body { padding: var(--sp-6); }
.modal-footer {
    padding: var(--sp-4) var(--sp-6);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: var(--sp-3);
}

/* ── Toast ── */
.toast-container {
    position: fixed;
    top: var(--sp-4);
    right: var(--sp-4);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: var(--sp-3);
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-5);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    font-weight: 500;
    pointer-events: auto;
    animation: toastSlideIn var(--transition-base) ease-out;
    min-width: 280px;
    max-width: 420px;
}

.toast.toast-exit { animation: toastSlideOut var(--transition-base) ease-in forwards; }
.toast-icon { font-size: 1.25rem; flex-shrink: 0; }
.toast-success { border-left: 4px solid var(--success); }
.toast-danger { border-left: 4px solid var(--danger); }
.toast-warning { border-left: 4px solid var(--warning); }
.toast-info { border-left: 4px solid var(--info); }

/* ── Tabs ── */
.tabs {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: var(--sp-6);
    overflow-x: auto;
}

.tab-btn {
    padding: var(--sp-3) var(--sp-5);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
}

.tab-btn:hover { color: var(--text-primary); }
.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}

/* ── Status Dots ── */
.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: var(--radius-full);
    margin-right: var(--sp-2);
}

.status-dot.green { background: var(--success); }
.status-dot.yellow { background: var(--warning); }
.status-dot.red { background: var(--danger); }
.status-dot.blue { background: var(--info); }
.status-dot.gray { background: var(--text-tertiary); }

/* ── Progress Bar ── */
.progress-bar {
    height: 8px;
    background: var(--bg-muted);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--accent));
    border-radius: var(--radius-full);
    transition: width 0.6s ease;
}

/* ── Empty State ── */
.empty-state {
    text-align: center;
    padding: var(--sp-12);
    color: var(--text-secondary);
}

.empty-state .empty-icon { font-size: 3rem; margin-bottom: var(--sp-4); }
.empty-state h3 { font-size: var(--text-lg); margin-bottom: var(--sp-2); color: var(--text-primary); }
.empty-state p { font-size: var(--text-sm); }

/* ── Cluster Color Chips ── */
.cluster-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 3px 12px;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 600;
}

/* ── Toolbar ── */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--sp-4);
    margin-bottom: var(--sp-6);
}

.toolbar-left { display: flex; align-items: center; gap: var(--sp-3); }
.toolbar-right { display: flex; align-items: center; gap: var(--sp-3); }

.search-input {
    padding: var(--sp-2) var(--sp-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    width: 240px;
    background: var(--bg-muted);
    transition: all var(--transition-fast);
    outline: none;
}

.search-input:focus {
    background: var(--bg-surface);
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(232, 89, 12, 0.1);
    width: 300px;
}

/* ── Phase 2 Badge ── */
.phase2-overlay {
    position: relative;
}

.phase2-badge {
    position: absolute;
    top: var(--sp-3);
    right: var(--sp-3);
    background: linear-gradient(135deg, var(--accent), var(--primary-light));
    color: white;
    padding: 2px 10px;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.05em;
    z-index: 2;
}

.phase2-overlay::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 251, 245, 0.6);
    border-radius: inherit;
    z-index: 1;
    pointer-events: none;
}

/* ── Grid Layouts ── */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--sp-6); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--sp-6); }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--sp-6); }

@media (max-width: 1024px) {
    .grid-4 { grid-template-columns: repeat(2, 1fr); }
    .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
    .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }

    /* Buttons touch-friendly */
    .btn { padding: var(--sp-3) var(--sp-5); font-size: var(--text-sm); min-height: 44px; }
    .btn-sm { padding: var(--sp-2) var(--sp-4); min-height: 38px; }
    
    /* Cards compact */
    .card-header { padding: var(--sp-3) var(--sp-4); flex-wrap: wrap; gap: var(--sp-2); }
    .card-body { padding: var(--sp-4); }
    .card-header h3 { font-size: var(--text-sm); }

    /* KPI Cards compact */
    .kpi-card { padding: var(--sp-3) var(--sp-4); }
    .kpi-card .kpi-icon { font-size: 1.3rem; margin-bottom: var(--sp-2); }
    .kpi-card .kpi-value { font-size: var(--text-lg); }
    .kpi-card .kpi-label { font-size: var(--text-xs); }

    /* Tables: horizontal scroll with sticky first column */
    .table-container { border-radius: var(--radius-md); }
    .data-table thead th { padding: var(--sp-2) var(--sp-3); font-size: 0.65rem; }
    .data-table tbody td { padding: var(--sp-2) var(--sp-3); font-size: var(--text-xs); }

    /* Toolbar stacks vertically */
    .toolbar { flex-direction: column; align-items: stretch; gap: var(--sp-3); }
    .toolbar-left, .toolbar-right { width: 100%; justify-content: space-between; }
    .toolbar-left { flex-wrap: wrap; }

    /* Search full width */
    .search-input { width: 100%; }
    .search-input:focus { width: 100%; }

    /* Modal fullscreen on mobile */
    .modal-overlay { padding: 0; align-items: flex-end; }
    .modal-container, .modal-container.modal-lg, .modal-container.modal-xl {
        max-width: 100%;
        max-height: 95vh;
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
        animation: slideUp 0.3s ease-out;
    }
    .modal-header { padding: var(--sp-4); }
    .modal-header h2 { font-size: var(--text-base); }
    .modal-body { padding: var(--sp-4); }
    .modal-footer { padding: var(--sp-3) var(--sp-4); flex-wrap: wrap; }
    .modal-footer .btn { flex: 1; }

    /* Toast at bottom on mobile */
    .toast-container {
        top: auto;
        bottom: var(--sp-4);
        right: var(--sp-3);
        left: var(--sp-3);
    }
    .toast { min-width: auto; max-width: 100%; }

    /* Tabs scrollable */
    .tabs { gap: 0; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .tab-btn { padding: var(--sp-2) var(--sp-3); font-size: var(--text-xs); }

    /* Form fields larger touch targets */
    .form-input, .form-select { padding: var(--sp-3); font-size: var(--text-base); min-height: 44px; }

    /* Badges */
    .badge { font-size: 0.65rem; padding: 2px 6px; }

    /* Accordion */
    .accordion-header { padding: var(--sp-3); font-size: var(--text-xs); }
    .accordion-body { padding: var(--sp-3); }

    /* Phase2 badge */
    .phase2-badge { font-size: 0.6rem; padding: 1px 6px; }
}

/* ── Accordion ── */
.accordion-item {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: var(--sp-2);
    overflow: hidden;
}

.accordion-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-3) var(--sp-4);
    background: var(--bg-muted);
    cursor: pointer;
    font-weight: 600;
    font-size: var(--text-sm);
    transition: background var(--transition-fast);
    border: none;
    width: 100%;
    text-align: left;
    font-family: var(--font-body);
    color: var(--text-primary);
}

.accordion-header:hover { background: var(--bg-hover); }

.accordion-body {
    padding: var(--sp-4);
    display: none;
}

.accordion-item.open .accordion-body { display: block; animation: fadeIn var(--transition-fast) ease-out; }
.accordion-item.open .accordion-header { background: var(--primary-lightest); color: var(--primary); }
