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

:root {
    --primary: #4F46E5;
    --primary-light: #6366F1;
    --primary-bg: #EEF2FF;
    --success: #059669;
    --success-bg: #D1FAE5;
    --danger: #DC2626;
    --danger-bg: #FEE2E2;
    --warning: #D97706;
    --warning-bg: #FEF3C7;
    --gray-50: #F9FAFB;
    --gray-100: #F3F4F6;
    --gray-200: #E5E7EB;
    --gray-300: #D1D5DB;
    --gray-400: #9CA3AF;
    --gray-500: #6B7280;
    --gray-600: #4B5563;
    --gray-700: #374151;
    --gray-800: #1F2937;
    --gray-900: #111827;
    --sidebar-width: 240px;
    --topbar-height: 56px;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--gray-800);
    background: var(--gray-50);
    overflow-x: hidden;
}

html { overflow-x: hidden; }

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-light); }

/* ===== Auth Layout ===== */
.auth-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #EEF2FF 0%, #E0E7FF 100%);
}

.auth-container { width: 100%; max-width: 400px; padding: 20px; }

.auth-card {
    background: white;
    border-radius: var(--radius-lg);
    padding: 32px;
    box-shadow: var(--shadow-md);
}

.auth-logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 24px;
}

.auth-logo h1 {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
}

.auth-card h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--gray-900);
    margin-bottom: 4px;
}

.auth-subtitle {
    color: var(--gray-500);
    font-size: 13px;
    margin-bottom: 20px;
}

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

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-width);
    background: white;
    border-right: 1px solid var(--gray-200);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 40;
    display: flex;
    flex-direction: column;
    transition: transform 0.2s;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--gray-200);
}

.sidebar-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--gray-900);
}

.sidebar-nav {
    padding: 12px 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: var(--radius);
    color: var(--gray-600);
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
}

.sidebar-link:hover {
    background: var(--gray-100);
    color: var(--gray-900);
}

.sidebar-link.active {
    background: var(--primary-bg);
    color: var(--primary);
}

.sidebar-toggle { display: none; background: none; border: none; cursor: pointer; color: var(--gray-600); }

/* ===== Main Content ===== */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    min-height: 100vh;
}

.topbar {
    height: var(--topbar-height);
    background: white;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 30;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.topbar-user { color: var(--gray-600); font-size: 13px; }
.topbar-logout { color: var(--gray-400); font-size: 13px; }
.topbar-logout:hover { color: var(--danger); }

.content-wrapper { padding: 24px; max-width: 1200px; }

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

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 4px;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: var(--font);
    color: var(--gray-900);
    background: white;
    transition: border-color 0.15s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.form-hint { font-size: 12px; color: var(--gray-400); margin-top: 4px; }

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

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-light); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #B91C1C; color: white; }
.btn-outline {
    background: white;
    color: var(--gray-700);
    border: 1px solid var(--gray-300);
}
.btn-outline:hover { background: var(--gray-50); color: var(--gray-900); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 12px; }

/* ===== Alerts ===== */
.alert {
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13px;
    margin-bottom: 16px;
}

.alert-success { background: var(--success-bg); color: #065F46; }
.alert-error { background: var(--danger-bg); color: #991B1B; }
.alert-warning { background: var(--warning-bg); color: #92400E; }

/* ===== Cards ===== */
.card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow-sm);
}

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

.card-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--gray-900);
}

/* ===== KPI Cards ===== */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    margin-bottom: 24px;
}

.kpi-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 16px 20px;
}

.kpi-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.kpi-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--gray-900);
    margin-top: 4px;
    line-height: 1.2;
}

.kpi-change {
    font-size: 12px;
    font-weight: 500;
    margin-top: 4px;
}

.kpi-change.positive { color: var(--success); }
.kpi-change.negative { color: var(--danger); }

/* ===== Tables ===== */
.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th {
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 8px 12px;
    border-bottom: 1px solid var(--gray-200);
}

.data-table td {
    padding: 10px 12px;
    font-size: 13px;
    border-bottom: 1px solid var(--gray-100);
    color: var(--gray-700);
}

.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--gray-50); }

.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ===== Progress Bars (for browser/os breakdown) ===== */
.progress-bar {
    display: flex;
    align-items: center;
    gap: 8px;
}

.progress-bar-track {
    flex: 1;
    height: 6px;
    background: var(--gray-100);
    border-radius: 3px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    transition: width 0.3s;
}

.progress-bar-label {
    font-size: 12px;
    color: var(--gray-500);
    min-width: 36px;
    text-align: right;
}

/* ===== Analytics Layout ===== */
.analytics-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 12px;
}

.analytics-site-info h2 {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
}

.analytics-site-info .domain {
    font-size: 13px;
    color: var(--gray-500);
}

.analytics-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.realtime-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--success);
    font-weight: 500;
}

.realtime-dot {
    width: 8px;
    height: 8px;
    background: var(--success);
    border-radius: 50%;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* Date Picker */
.date-picker {
    display: flex;
    gap: 4px;
    background: var(--gray-100);
    padding: 3px;
    border-radius: var(--radius);
}

.date-picker button {
    padding: 6px 12px;
    border: none;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    font-family: var(--font);
    color: var(--gray-600);
    background: transparent;
    cursor: pointer;
    transition: all 0.15s;
}

.date-picker button.active {
    background: white;
    color: var(--gray-900);
    box-shadow: var(--shadow-sm);
}

.date-picker button:hover:not(.active) { color: var(--gray-900); }

.date-picker-custom {
    display: flex;
    align-items: center;
    gap: 6px;
}

.date-picker-custom input[type="date"] {
    padding: 4px 8px;
    border: 1px solid var(--gray-300);
    border-radius: 6px;
    font-size: 12px;
    font-family: var(--font);
}

/* Chart */
.chart-container {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 24px;
}

.chart-canvas { width: 100%; height: 280px; }

/* Analytics Grid */
.analytics-grid {
    display: grid;
    grid-template-columns: 1.5fr 1fr;
    gap: 24px;
    margin-bottom: 24px;
}

.analytics-grid .card { overflow: hidden; }

/* ===== Dashboard Site Cards ===== */
.sites-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.site-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 20px;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
    color: inherit;
    display: block;
}

.site-card:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow-md);
    color: inherit;
}

.site-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.site-card-name {
    font-size: 15px;
    font-weight: 600;
    color: var(--gray-900);
}

.site-card-domain {
    font-size: 12px;
    color: var(--gray-400);
}

.site-card-stats {
    display: flex;
    gap: 20px;
    margin-top: 12px;
}

.site-card-stat { text-align: center; }

.site-card-stat-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--gray-900);
}

.site-card-stat-label {
    font-size: 11px;
    color: var(--gray-500);
    text-transform: uppercase;
}

.site-card-settings {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    color: var(--gray-400);
    transition: all 0.15s;
}

.site-card-settings:hover {
    background: var(--gray-100);
    color: var(--gray-700);
}

.site-card-sparkline {
    height: 40px;
    margin-top: 8px;
}

/* ===== View Toggle ===== */
.view-toggle {
    display: flex;
    gap: 2px;
    background: var(--gray-100);
    padding: 3px;
    border-radius: var(--radius);
}

.view-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 28px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--gray-400);
    cursor: pointer;
    transition: all 0.15s;
}

.view-toggle-btn.active {
    background: white;
    color: var(--gray-700);
    box-shadow: var(--shadow-sm);
}

.view-toggle-btn:hover:not(.active) { color: var(--gray-600); }

/* ===== Sites List View ===== */
.sites-list-row:hover td { background: var(--gray-50); }

.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.sortable:hover { color: var(--gray-900); }
.sortable.active { color: var(--gray-900); }
.sort-arrow { font-size: 10px; margin-left: 4px; color: var(--gray-400); }

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
}

.empty-state h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: 8px;
}

.empty-state p {
    color: var(--gray-500);
    font-size: 13px;
    margin-bottom: 20px;
}

/* ===== Tracking Code ===== */
.tracking-code {
    background: var(--gray-900);
    color: #E5E7EB;
    padding: 16px;
    border-radius: var(--radius);
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 12px;
    line-height: 1.6;
    overflow-x: auto;
    position: relative;
}

.tracking-code .copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 4px 10px;
    background: var(--gray-700);
    color: var(--gray-300);
    border: none;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    font-family: var(--font);
}

.tracking-code .copy-btn:hover { background: var(--gray-600); }

/* ===== Error Page ===== */
.error-page {
    text-align: center;
    padding: 80px 20px;
}

.error-page h1 {
    font-size: 64px;
    font-weight: 800;
    color: var(--gray-300);
}

.error-page p {
    color: var(--gray-500);
    margin: 12px 0 24px;
}

/* ===== UTM Section ===== */
.collapsible-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    padding: 12px 0;
    user-select: none;
}

.collapsible-header .arrow {
    transition: transform 0.2s;
    color: var(--gray-400);
}

.collapsible-header.open .arrow { transform: rotate(180deg); }
.collapsible-body { display: none; }
.collapsible-body.open { display: block; }

/* ===== Settings Page ===== */
.settings-section {
    margin-bottom: 32px;
}

.settings-section h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--gray-900);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--gray-200);
}

.danger-zone {
    border: 1px solid var(--danger);
    border-radius: var(--radius-lg);
    padding: 20px;
}

.danger-zone h4 {
    color: var(--danger);
    font-size: 14px;
    margin-bottom: 8px;
}

.danger-zone p {
    color: var(--gray-600);
    font-size: 13px;
    margin-bottom: 12px;
}

/* ===== Device Breakdown ===== */
.device-bar {
    display: flex;
    height: 28px;
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 8px;
}

.device-bar-segment {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 500;
    color: white;
    min-width: 30px;
    transition: width 0.3s;
}

.device-bar-segment.desktop { background: var(--primary); }
.device-bar-segment.mobile { background: #8B5CF6; }
.device-bar-segment.tablet { background: #06B6D4; }

.device-labels {
    display: flex;
    gap: 16px;
    font-size: 12px;
    color: var(--gray-600);
}

.device-label {
    display: flex;
    align-items: center;
    gap: 6px;
}

.device-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

/* ===== Tooltip (for chart) ===== */
.chart-tooltip {
    position: absolute;
    background: var(--gray-900);
    color: white;
    padding: 8px 12px;
    border-radius: var(--radius);
    font-size: 12px;
    pointer-events: none;
    z-index: 50;
    white-space: nowrap;
    box-shadow: var(--shadow-md);
}

.chart-tooltip-label { color: var(--gray-400); margin-bottom: 4px; }
.chart-tooltip-value { font-weight: 600; }

/* ===== Page Header ===== */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}

.page-header h2 {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .sidebar-toggle { display: block; }
    .main-content { margin-left: 0; }
    .topbar { justify-content: space-between; }
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .analytics-grid { grid-template-columns: 1fr; }
    .sites-grid { grid-template-columns: 1fr; }
    .analytics-header { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 480px) {
    .kpi-grid { grid-template-columns: 1fr; }
    .content-wrapper { padding: 16px; }
}
