/* ================================================================
   Tesla Personal Assistant — Shared Design System
   ================================================================ */

/* ---------- Design Tokens ---------- */
:root {
    /* Brand */
    --primary: #e82127;
    --primary-soft: rgba(232, 33, 39, 0.08);
    --primary-medium: rgba(232, 33, 39, 0.15);

    /* Surfaces */
    --bg: #f0f2f5;
    --surface: #ffffff;
    --dark: #1c1c1e;

    /* Text */
    --text: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-tertiary: #aeaeb2;

    /* Borders */
    --border: #e5e5ea;
    --border-light: #f0f0f2;

    /* Status */
    --success: #34c759;
    --success-soft: rgba(52, 199, 89, 0.12);
    --warning: #ff9500;
    --warning-soft: rgba(255, 149, 0, 0.12);
    --info: #007aff;
    --info-soft: rgba(0, 122, 255, 0.10);

    /* Spacing */
    --sp-1: 4px;
    --sp-2: 8px;
    --sp-3: 12px;
    --sp-4: 16px;
    --sp-5: 20px;
    --sp-6: 24px;
    --sp-8: 32px;
    --sp-10: 40px;

    /* Radius */
    --r-sm: 8px;
    --r-md: 12px;
    --r-lg: 16px;
    --r-xl: 20px;
    --r-pill: 999px;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.08), 0 2px 8px rgba(0,0,0,0.04);

    /* Layout */
    --container-max: 960px;
    --header-height: 60px;
}

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    font-size: 15px;
    line-height: 1.5;
}
a { color: inherit; text-decoration: none; }

/* ---------- Header / Navigation ---------- */
.app-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 var(--sp-6);
    height: var(--header-height);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(12px);
    background: rgba(255,255,255,0.92);
}
.app-brand {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.3px;
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    white-space: nowrap;
}
.app-brand .logo { font-size: 20px; }
.app-nav {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    justify-self: center;
}
.app-nav a {
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--r-pill);
    transition: background 0.15s ease, color 0.15s ease;
    min-width: 56px;
    text-align: center;
}
.app-nav a:hover {
    background: var(--border-light);
    color: var(--text);
}
.app-nav a.active {
    color: var(--primary);
    background: var(--primary-soft);
}
.app-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    justify-self: end;
    min-width: 0;
}
.app-actions .auth-text {
    font-size: 13px;
    color: var(--text-tertiary);
}

/* ---------- Container ---------- */
.app-container {
    max-width: var(--container-max);
    margin: 0 auto;
    padding: var(--sp-6) var(--sp-4);
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--r-pill);
    padding: 8px 18px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s ease, transform 0.1s ease;
    white-space: nowrap;
}
.btn:hover { opacity: 0.88; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: var(--dark); }
.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--border-light); }
.btn-sm { padding: 6px 14px; font-size: 13px; }

/* ---------- Card ---------- */
.card {
    background: var(--surface);
    border-radius: var(--r-lg);
    padding: var(--sp-5);
    margin-bottom: var(--sp-4);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-light);
}
.card-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 0 0 var(--sp-4);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}
.card-title .tag {
    font-size: 11px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: var(--r-pill);
    background: var(--success-soft);
    color: var(--success);
}

/* ---------- Metric Grid ---------- */
.metric-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: var(--sp-3);
}
.metric-item {
    background: var(--bg);
    border-radius: var(--r-md);
    padding: var(--sp-3) var(--sp-4);
}
.metric-item .metric-label {
    font-size: 12px;
    color: var(--text-tertiary);
    margin-bottom: var(--sp-1);
}
.metric-item .metric-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    line-height: 1.3;
}
.metric-item .metric-value .unit {
    font-size: 13px;
    font-weight: 400;
    color: var(--text-secondary);
    margin-left: 2px;
}
.metric-item.highlight .metric-value { color: var(--primary); }
.metric-item.success .metric-value { color: var(--success); }

/* ---------- Status Badges ---------- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 10px;
    border-radius: var(--r-pill);
    font-size: 11px;
    font-weight: 600;
}
.badge-ongoing { background: var(--warning-soft); color: var(--warning); }
.badge-completed { background: var(--success-soft); color: var(--success); }
.badge-info { background: var(--info-soft); color: var(--info); }

/* ---------- Toolbar / Filters ---------- */
.toolbar {
    display: flex;
    gap: var(--sp-3);
    align-items: center;
    flex-wrap: wrap;
}
.toolbar input[type="date"],
.toolbar select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-size: 14px;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s;
}
.toolbar input:focus,
.toolbar select:focus {
    outline: none;
    border-color: var(--primary);
}
.toolbar .toolbar-label {
    font-size: 13px;
    color: var(--text-tertiary);
    font-weight: 500;
}

/* ---------- List Items ---------- */
.list-item {
    border-bottom: 1px solid var(--border-light);
    padding: var(--sp-4) 0;
    cursor: pointer;
    transition: background 0.1s;
}
.list-item:last-child { border-bottom: none; }
.list-item:hover { background: var(--border-light); margin: 0 calc(-1 * var(--sp-5)); padding-left: var(--sp-5); padding-right: var(--sp-5); border-radius: var(--r-md); }

/* ---------- Empty / Error ---------- */
.empty-state {
    text-align: center;
    padding: var(--sp-10) var(--sp-5);
    color: var(--text-tertiary);
}
.empty-state .icon { font-size: 40px; margin-bottom: var(--sp-3); }
.error-box {
    color: var(--primary);
    background: var(--primary-soft);
    padding: var(--sp-3) var(--sp-4);
    border-radius: var(--r-md);
    font-size: 14px;
}

/* ---------- Pagination ---------- */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: var(--sp-2);
    margin-top: var(--sp-4);
}
.pagination button {
    padding: 6px 14px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--r-sm);
    cursor: pointer;
    font-size: 14px;
    transition: all 0.15s;
}
.pagination button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination .page-info { font-size: 14px; color: var(--text-secondary); padding: 0 var(--sp-2); }

/* ---------- Chart Wrapper ---------- */
.chart-wrapper {
    position: relative;
    height: 280px;
    margin-top: var(--sp-2);
}

/* ---------- Back Link ---------- */
.back-link {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    color: var(--primary);
    font-size: 14px;
    font-weight: 500;
    margin-bottom: var(--sp-4);
}
.back-link:hover { opacity: 0.8; }

/* ---------- Toggle Switch ---------- */
.switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-3) 0;
}
.switch-row + .switch-row {
    border-top: 1px solid var(--border-light);
}
.switch-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.switch-label {
    font-size: 14px;
    font-weight: 600;
}
.switch-sub {
    font-size: 12px;
    color: var(--text-tertiary);
    word-break: break-all;
}
.switch-sub a {
    color: var(--primary);
}
.toggle {
    position: relative;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
    cursor: pointer;
}
.toggle input { display: none; }
.toggle .track {
    position: absolute;
    inset: 0;
    background: var(--border);
    border-radius: 14px;
    transition: background 0.2s;
}
.toggle input:checked + .track {
    background: var(--success);
}
.toggle .thumb {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.toggle input:checked ~ .thumb {
    transform: translateX(20px);
}
.toggle.loading { opacity: 0.6; pointer-events: none; }
.toggle.loading .thumb { animation: pulse 0.8s infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.5; } }

.system-panel-title {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}
.system-panel-title .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
}
.system-panel-title .status-dot.off { background: var(--text-tertiary); }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
    .app-header {
        padding: 0 var(--sp-2);
        grid-template-columns: auto 1fr auto;
        gap: var(--sp-2);
    }
    .app-brand .brand-text { display: none; }
    .app-brand .logo { font-size: 22px; }
    .app-nav {
        gap: 0;
        justify-self: center;
    }
    .app-nav a {
        padding: var(--sp-2) var(--sp-3);
        font-size: 13px;
        min-width: 44px;
    }
    .app-actions {
        gap: var(--sp-1);
        justify-self: end;
    }
    .app-actions .auth-text { display: none; }
    .app-actions .btn {
        padding: 6px 10px;
        font-size: 12px;
    }
    .app-actions .btn .btn-full-text { display: none; }
    .app-actions .btn .btn-icon { display: inline; }
    .app-container { padding: var(--sp-3) var(--sp-2); }
    .metric-grid { grid-template-columns: repeat(2, 1fr); }
    .card { padding: var(--sp-3); }
    .toolbar { gap: var(--sp-2); }
    .toolbar input[type="date"] { font-size: 13px; padding: 6px 8px; }
}

/* Desktop: show full text, hide icon-only */
@media (min-width: 641px) {
    .app-actions .btn .btn-icon { display: none; }
    .app-actions .btn .btn-full-text { display: inline; }
}

/* ---------- Confirm Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.2s;
}
.modal-overlay.show { opacity: 1; }
.modal-box {
    background: var(--surface);
    border-radius: var(--r-lg);
    padding: var(--sp-6);
    max-width: 340px;
    width: calc(100% - 48px);
    box-shadow: var(--shadow-lg);
    transform: scale(0.92);
    transition: transform 0.2s;
}
.modal-overlay.show .modal-box { transform: scale(1); }
.modal-title {
    font-size: 17px;
    font-weight: 700;
    margin-bottom: var(--sp-2);
}
.modal-body {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: var(--sp-5);
}
.modal-actions {
    display: flex;
    gap: var(--sp-3);
    justify-content: flex-end;
}
.modal-actions .btn-cancel {
    background: var(--bg);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}
.modal-actions .btn-danger {
    background: var(--primary);
}
