:root {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-card: #1e293b;
    --bg-input: #334155;
    --bg-hover: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --success: #22c55e;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --warning: #f59e0b;
    --border: #334155;
    --shadow: rgba(0, 0, 0, 0.3);
    --radius: 8px;
    --radius-lg: 12px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
}

/* ─── Auth Page ─────────────────────────────────────────── */
.auth-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
}

.auth-box {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 40px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 8px 32px var(--shadow);
}

.auth-box h1 {
    text-align: center;
    margin-bottom: 8px;
    font-size: 1.8rem;
}

.auth-box .subtitle {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 30px;
    font-size: 0.9rem;
}

.auth-tabs {
    display: flex;
    gap: 0;
    margin-bottom: 24px;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
}

.auth-tab {
    flex: 1;
    padding: 10px;
    text-align: center;
    cursor: pointer;
    background: var(--bg-input);
    color: var(--text-secondary);
    border: none;
    font-size: 0.95rem;
    transition: all 0.2s;
}

.auth-tab.active {
    background: var(--accent);
    color: white;
}

/* ─── Forms ─────────────────────────────────────────────── */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.95rem;
    transition: border-color 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.form-group textarea {
    resize: vertical;
    min-height: 80px;
    font-family: inherit;
}

/* ─── Buttons ───────────────────────────────────────────── */
.btn {
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-primary {
    background: var(--accent);
    color: white;
    width: 100%;
    justify-content: center;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--bg-hover);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.8rem;
}

.btn-icon {
    padding: 6px 8px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.9rem;
}

.btn-icon:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-icon.danger:hover {
    background: var(--danger);
    color: white;
    border-color: var(--danger);
}

/* ─── Layout ────────────────────────────────────────────── */
.app-container {
    display: none;
}

.app-container.visible {
    display: block;
}

.navbar {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.navbar .logo {
    font-size: 1.3rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.navbar .nav-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.navbar .user-badge {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.main-content {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
}

/* ─── Add Car Section ───────────────────────────────────── */
.add-car-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 24px;
}

.add-car-section h2 {
    margin-bottom: 16px;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.add-car-form {
    display: flex;
    gap: 12px;
}

.add-car-form input {
    flex: 1;
    padding: 12px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 1rem;
}

.add-car-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.add-car-form .btn {
    white-space: nowrap;
}

/* ─── Loading ───────────────────────────────────────────── */
.loading-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.85);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 16px;
}

.loading-overlay.visible {
    display: flex;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-text {
    color: var(--text-secondary);
    font-size: 1rem;
}

/* ─── Cars Section ──────────────────────────────────────── */
.cars-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.section-header {
    padding: 20px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
}

.section-header h2 {
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.car-count {
    background: var(--accent);
    color: white;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.8rem;
    font-weight: 600;
}

/* ─── Car Cards Grid ────────────────────────────────────── */
.car-cards {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 16px;
}

.car-card {
    display: flex;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    transition: box-shadow 0.2s, border-color 0.2s;
}

.car-card:hover {
    border-color: var(--accent);
    box-shadow: 0 4px 24px rgba(59, 130, 246, 0.12);
}

/* ── Card Image ─────── */
.car-card-image {
    width: 300px;
    min-width: 300px;
    height: 220px;
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
    cursor: pointer;
    background: #111827;
}

.car-card-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.35s ease;
}

.car-card:hover .car-card-image img {
    transform: scale(1.04);
}

.car-card-image .img-count {
    position: absolute;
    bottom: 10px;
    left: 10px;
    background: rgba(0,0,0,0.6);
    color: white;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    backdrop-filter: blur(6px);
    letter-spacing: 0.3px;
}

.car-card-no-image {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 3.5rem;
    background: var(--bg-input);
}

/* ── Card Info ──────── */
.car-card-info {
    flex: 1;
    padding: 20px 24px;
    display: flex;
    flex-wrap: wrap;
    min-width: 0;
    gap: 0;
}

.car-card-left {
    flex: 1;
    min-width: 200px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.car-card-right {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 20px;
    padding-left: 24px;
    border-left: 2px solid var(--border);
    margin-left: 24px;
    align-content: center;
    min-width: 320px;
}

.car-card-title {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.35;
    margin-bottom: 2px;
}

.car-card-price {
    font-size: 1.75rem;
    font-weight: 800;
    color: var(--success);
    letter-spacing: -0.5px;
    margin-bottom: 6px;
}

.car-card-price--empty {
    color: var(--text-muted);
    font-size: 1.1rem;
    font-weight: 500;
}

.car-card-link {
    font-size: 0.82rem;
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    margin-top: auto;
}

.car-card-link:hover {
    text-decoration: underline;
    color: var(--accent-hover);
}

/* ── Spec Chips (right column) ── */
.spec-chip {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
}

.spec-icon {
    width: 26px;
    height: 26px;
    color: var(--accent);
    flex-shrink: 0;
}

.spec-text {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.spec-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}

.spec-value {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-primary);
    white-space: nowrap;
}

.car-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
    width: 100%;
}

.car-card-actions {
    display: flex;
    gap: 4px;
}

.car-card-status {
    font-size: 0.78rem;
    font-weight: 600;
}

.price-up {
    color: var(--danger);
}

.price-down {
    color: var(--success);
}

.price-diff {
    font-size: 0.75rem;
    font-weight: normal;
    display: block;
}

.status-active {
    color: var(--success);
}

.status-inactive {
    color: var(--text-muted);
}

.no-cars {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.no-cars .emoji {
    font-size: 3rem;
    margin-bottom: 12px;
}

.car-title-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
}

.car-title-link:hover {
    text-decoration: underline;
}

/* ─── Modal ─────────────────────────────────────────────── */
.modal-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.9);
    z-index: 500;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.modal-backdrop.visible {
    display: flex;
}

.modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 700px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 16px 64px var(--shadow);
}

.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    font-size: 1.1rem;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 24px;
}

/* ─── AI Debug Panel ────────────────────────────────────── */
.modal-wide {
    max-width: 900px;
}

.debug-section {
    margin-bottom: 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.debug-heading {
    margin: 0;
    padding: 10px 16px;
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    font-size: 0.95rem;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    user-select: none;
}

.debug-heading:hover {
    background: rgba(99, 102, 241, 0.18);
}

.debug-toggle {
    font-size: 0.75rem;
    transition: transform 0.2s;
}

.debug-pre {
    margin: 0;
    padding: 14px 16px;
    background: var(--bg-primary);
    color: var(--text-secondary);
    font-family: 'Fira Code', 'Consolas', 'Monaco', monospace;
    font-size: 0.8rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 400px;
    overflow-y: auto;
    transition: max-height 0.3s ease;
}

.debug-pre.collapsed {
    max-height: 0;
    padding: 0 16px;
    overflow: hidden;
}

.debug-table {
    width: 100%;
    border-collapse: collapse;
}

.debug-table tr {
    border-bottom: 1px solid var(--border);
}

.debug-table tr:last-child {
    border-bottom: none;
}

.debug-table td {
    padding: 8px 14px;
    font-size: 0.88rem;
}

.debug-key {
    color: var(--text-secondary);
    font-weight: 600;
    width: 160px;
    font-family: 'Fira Code', monospace;
    font-size: 0.82rem;
}

.debug-changed {
    background: rgba(16, 185, 129, 0.08);
}

.debug-changed td:last-child {
    color: var(--success);
    font-weight: 600;
}

.debug-errors {
    border-color: var(--error);
}

.debug-errors .debug-heading {
    background: rgba(239, 68, 68, 0.12);
    color: var(--error);
}

.debug-errors ul {
    margin: 0;
    padding: 12px 16px 12px 32px;
    list-style: disc;
}

.debug-errors li {
    padding: 4px 0;
    font-size: 0.88rem;
    color: var(--error);
}

/* ─── Image Gallery ─────────────────────────────────────── */
.image-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 8px;
    margin-top: 12px;
}

.image-gallery-item {
    position: relative;
    border-radius: var(--radius);
    overflow: hidden;
}

.image-gallery-item img {
    width: 100%;
    height: 120px;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.2s;
    display: block;
}

.image-gallery-item:hover img {
    transform: scale(1.05);
}

.image-delete-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 26px;
    height: 26px;
    background: rgba(239, 68, 68, 0.85);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 0.85rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 2;
    line-height: 1;
}

.image-gallery-item:hover .image-delete-btn {
    opacity: 1;
}

.image-delete-btn:hover {
    background: var(--danger);
    transform: scale(1.15);
}

/* ─── Token Usage Badge ─────────────────────────────────── */
.token-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(99, 102, 241, 0.12);
    border: 1px solid rgba(99, 102, 241, 0.3);
    color: #a5b4fc;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    margin-right: 8px;
}

.token-badge .token-label {
    color: var(--text-muted);
    font-weight: 400;
}

.token-badge .token-value {
    font-weight: 700;
    font-family: 'Fira Code', monospace;
}

/* ─── Image Lightbox ────────────────────────────────────── */
.lightbox {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 600;
    justify-content: center;
    align-items: center;
}

.lightbox.visible {
    display: flex;
}

.lightbox img {
    max-width: 85vw;
    max-height: 85vh;
    object-fit: contain;
    border-radius: var(--radius);
    user-select: none;
}

.lightbox-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(255,255,255,0.12);
    border: 2px solid rgba(255,255,255,0.25);
    border-radius: 50%;
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, border-color 0.2s;
    z-index: 10;
    backdrop-filter: blur(8px);
}

.lightbox-arrow:hover {
    background: rgba(255,255,255,0.25);
    border-color: rgba(255,255,255,0.5);
}

.lightbox-prev {
    left: 20px;
}

.lightbox-next {
    right: 20px;
}

.lightbox-counter {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255,255,255,0.7);
    font-size: 0.9rem;
    font-weight: 600;
    background: rgba(0,0,0,0.5);
    padding: 5px 16px;
    border-radius: 20px;
    backdrop-filter: blur(4px);
}

.lightbox-close {
    position: absolute;
    top: 16px;
    right: 20px;
    width: 40px;
    height: 40px;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 50%;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    z-index: 10;
}

.lightbox-close:hover {
    background: rgba(239, 68, 68, 0.6);
}

/* ─── History ───────────────────────────────────────────── */
.history-list {
    max-height: 400px;
    overflow-y: auto;
}

.history-entry {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
    background: var(--bg-primary);
}

.history-date {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.history-price {
    font-weight: 700;
    font-size: 1rem;
}

.history-changes {
    margin-top: 6px;
    font-size: 0.8rem;
    color: var(--warning);
}

/* ─── Config Panel ──────────────────────────────────────── */
.config-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 24px;
    display: none;
}

.config-section.visible {
    display: block;
}

.config-section h2 {
    margin-bottom: 16px;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.config-hint {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-bottom: 12px;
    line-height: 1.5;
}

/* ─── Edit inline ───────────────────────────────────────── */
.editable-cell {
    cursor: pointer;
    position: relative;
}

.editable-cell:hover {
    background: var(--bg-input);
    border-radius: 4px;
}

.editable-cell input {
    width: 100%;
    padding: 4px 8px;
    background: var(--bg-input);
    border: 1px solid var(--accent);
    border-radius: 4px;
    color: var(--text-primary);
    font-size: 0.9rem;
}

/* ─── Toast ─────────────────────────────────────────────── */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 20px;
    box-shadow: 0 4px 16px var(--shadow);
    animation: slideIn 0.3s ease;
    max-width: 350px;
    font-size: 0.9rem;
}

.toast.success {
    border-left: 3px solid var(--success);
}

.toast.error {
    border-left: 3px solid var(--danger);
}

.toast.info {
    border-left: 3px solid var(--accent);
}

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 768px) {
    .add-car-form {
        flex-direction: column;
    }

    .navbar {
        flex-wrap: wrap;
        gap: 8px;
    }

    .main-content {
        padding: 12px;
    }

    .modal {
        max-width: 100%;
        margin: 10px;
    }

    .car-card {
        flex-direction: column;
    }

    .car-card-image {
        width: 100%;
        min-width: 100%;
        height: 200px;
    }

    .car-card-info {
        padding: 16px;
        flex-direction: column;
    }

    .car-card-right {
        border-left: none;
        margin-left: 0;
        padding-left: 0;
        padding-top: 12px;
        border-top: 1px solid var(--border);
        margin-top: 12px;
        min-width: unset;
        grid-template-columns: 1fr 1fr 1fr;
    }

    .lightbox-arrow {
        width: 38px;
        height: 38px;
        font-size: 1.1rem;
    }

    .lightbox-prev { left: 8px; }
    .lightbox-next { right: 8px; }
}
