/* ── Reset & Base ───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #0f1117;
    color: #e4e6eb;
    min-height: 100vh;
}
.hidden { display: none !important; }

/* ── Login ──────────────────────────────────────────────────── */
.screen { width: 100%; min-height: 100vh; }
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0f1117 0%, #1a1d29 100%);
    overflow-y: auto;
    padding: 2rem 1rem 40vh 1rem;
}
.login-box {
    text-align: center;
    padding: 3rem 2.5rem;
    background: #1c1f2e;
    border-radius: 16px;
    border: 1px solid #2a2d3e;
    width: 380px;
    max-width: 90vw;
}
.login-box h1 {
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 0.3rem;
    color: #fff;
}
.login-subtitle {
    color: #8b8fa3;
    margin-bottom: 2rem;
    font-size: 0.9rem;
}
#login-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}
#password-input {
    padding: 0.8rem 1rem;
    border-radius: 8px;
    border: 1px solid #2a2d3e;
    background: #13151f;
    color: #e4e6eb;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}
#password-input:focus { border-color: #5b6ef5; }
#login-btn {
    padding: 0.8rem;
    border-radius: 8px;
    border: none;
    background: #5b6ef5;
    color: #fff;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}
#login-btn:hover { background: #4a5de0; }
.error-text { color: #ef4444; font-size: 0.85rem; margin-top: 0.5rem; }

/* ── Nav ────────────────────────────────────────────────────── */
.nav-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    height: 56px;
    background: #1c1f2e;
    border-bottom: 1px solid #2a2d3e;
}
.nav-brand {
    font-size: 1.1rem;
    font-weight: 700;
    color: #fff;
}
.nav-tabs { display: flex; gap: 0.25rem; }
.nav-tab {
    padding: 0.5rem 1.25rem;
    border: none;
    background: transparent;
    color: #8b8fa3;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s;
}
.nav-tab:hover { color: #e4e6eb; }
.nav-tab.active { background: #2a2d3e; color: #fff; }

/* ── Tabs ───────────────────────────────────────────────────── */
.tab-content { display: none; padding: 1.5rem; max-width: 1200px; margin: 0 auto; }
.tab-content.active { display: block; }

/* ── Controls ───────────────────────────────────────────────── */
.controls {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}
#niche-select {
    flex: 1;
    max-width: 300px;
    padding: 0.7rem 1rem;
    border-radius: 8px;
    border: 1px solid #2a2d3e;
    background: #13151f;
    color: #e4e6eb;
    font-size: 0.95rem;
    cursor: pointer;
}
#generate-btn {
    padding: 0.7rem 1.5rem;
    border-radius: 8px;
    border: none;
    background: #5b6ef5;
    color: #fff;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}
#generate-btn:hover { background: #4a5de0; }
#generate-btn:disabled {
    background: #3a3d4e;
    color: #666;
    cursor: not-allowed;
}

/* ── Loading ────────────────────────────────────────────────── */
#gen-loading {
    padding: 1.5rem;
    max-width: 640px;
    margin: 0 auto;
}
.loading-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
}
.pulse-ring {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 3px solid #5b6ef5;
    flex-shrink: 0;
    animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
    0% { transform: scale(0.9); opacity: 0.5; }
    50% { transform: scale(1.1); opacity: 1; }
    100% { transform: scale(0.9); opacity: 0.5; }
}
.loading-info { display: flex; flex-direction: column; }
.loading-text { font-size: 1rem; font-weight: 600; color: #fff; margin: 0; }
.loading-elapsed { font-size: 1.4rem; font-weight: 700; color: #5b6ef5; margin: 0; font-variant-numeric: tabular-nums; }

/* ── Activity Log ──────────────────────────────────────────── */
.activity-log {
    background: #13151f;
    border: 1px solid #2a2d3e;
    border-radius: 10px;
    padding: 1rem;
    max-height: 280px;
    overflow-y: auto;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.8rem;
    line-height: 1.7;
}
.activity-log::-webkit-scrollbar { width: 6px; }
.activity-log::-webkit-scrollbar-track { background: transparent; }
.activity-log::-webkit-scrollbar-thumb { background: #2a2d3e; border-radius: 3px; }
.log-line {
    color: #8b8fa3;
    animation: logFadeIn 0.3s ease-out;
}
.log-line .log-time {
    color: #555a6e;
    margin-right: 0.25rem;
}
.log-line.log-success { color: #22c55e; }
.log-line.log-fail { color: #ef4444; }
.log-line.log-info { color: #c9ccd5; }
.log-line.log-indent { padding-left: 1.5rem; color: #6b7080; }
@keyframes logFadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ── Ad Grid ────────────────────────────────────────────────── */
.ad-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1.5rem;
}
.library-grid { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

/* ── Ad Card (Facebook-style) ──────────────────────────────── */
.ad-card {
    background: #242736;
    border-radius: 8px;
    border: 1px solid #2a2d3e;
    overflow: hidden;
    position: relative;
    transition: transform 0.15s;
}
.ad-card:hover { transform: translateY(-2px); }
.ad-card.library-card { cursor: pointer; }

/* Page header */
.ad-page-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 12px 16px 0;
}
.ad-page-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #5b6ef5;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 700;
    color: #fff;
    flex-shrink: 0;
}
.ad-page-info {
    display: flex;
    flex-direction: column;
    line-height: 1.3;
}
.ad-page-name {
    font-size: 0.85rem;
    font-weight: 600;
    color: #e4e6eb;
}
.ad-page-sponsored {
    font-size: 0.75rem;
    color: #8b8fa3;
}

/* Body text (above image) */
.ad-copy {
    font-size: 0.9rem;
    line-height: 1.5;
    color: #c9ccd5;
    padding: 8px 16px 12px;
    white-space: pre-wrap;
    word-wrap: break-word;
}

/* Image area wrapper (for score badge positioning) */
.ad-image-wrapper {
    position: relative;
    width: 100%;
}
.ad-card-image {
    width: 100%;
    display: block;
    background: #13151f;
}
.score-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 700;
    color: #fff;
    backdrop-filter: blur(4px);
    z-index: 2;
}
.score-green { background: rgba(34, 197, 94, 0.85); }
.score-yellow { background: rgba(234, 179, 8, 0.85); }
.score-red { background: rgba(239, 68, 68, 0.85); }

/* Below-image bar: display URL + headline + CTA */
.ad-link-bar {
    padding: 10px 16px;
    background: #1c1f2e;
    border-top: 1px solid #2a2d3e;
}
.ad-display-url {
    font-size: 0.75rem;
    color: #8b8fa3;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-bottom: 4px;
}
.ad-headline-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}
.ad-headline {
    font-size: 0.95rem;
    font-weight: 700;
    color: #e4e6eb;
    flex: 1;
    min-width: 0;
}
.ad-description {
    font-size: 0.8rem;
    color: #8b8fa3;
    margin-top: 2px;
}
.ad-cta {
    display: inline-block;
    padding: 0.4rem 1rem;
    background: #e4e6eb;
    color: #1c1f2e;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 700;
    flex-shrink: 0;
    white-space: nowrap;
    text-transform: capitalize;
}

/* ── Image Placeholder ─────────────────────────────────────── */
.image-placeholder {
    width: 100%;
    aspect-ratio: 1.91 / 1;
    background: #13151f;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}
.image-placeholder.no-image {
    background: #1a1c28;
}
.placeholder-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid #2a2d3e;
    border-top-color: #5b6ef5;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}
.placeholder-text {
    font-size: 0.8rem;
    color: #555a6e;
    font-weight: 500;
}

/* ── Fade-in animation for images ──────────────────────────── */
.fade-in {
    animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ── Feedback Buttons ───────────────────────────────────────── */
.ad-actions {
    display: flex;
    gap: 0.5rem;
    padding: 0 1rem 1rem;
    clear: both;
}
.action-btn {
    flex: 1;
    padding: 0.55rem;
    border-radius: 8px;
    border: 1px solid #2a2d3e;
    background: transparent;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}
.approve-btn { color: #22c55e; border-color: #22c55e33; }
.approve-btn:hover { background: #22c55e22; }
.reject-btn { color: #ef4444; border-color: #ef444433; }
.reject-btn:hover { background: #ef444422; }
.action-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Feedback Input ─────────────────────────────────────────── */
.feedback-input-row {
    display: flex;
    gap: 0.5rem;
    padding: 0 1rem 1rem;
}
.feedback-input {
    flex: 1;
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    border: 1px solid #2a2d3e;
    background: #13151f;
    color: #e4e6eb;
    font-size: 0.85rem;
}
.feedback-submit {
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: none;
    background: #5b6ef5;
    color: #fff;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
}

/* ── Modal ──────────────────────────────────────────────────── */
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.7);
}
.modal-content {
    position: relative;
    background: #1c1f2e;
    border-radius: 16px;
    border: 1px solid #2a2d3e;
    width: 640px;
    max-width: 95vw;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    color: #8b8fa3;
    font-size: 1.5rem;
    cursor: pointer;
    z-index: 1;
}
.modal-close:hover { color: #fff; }
#modal-image {
    width: 100%;
    border-radius: 16px 16px 0 0;
}
.modal-fields { padding: 1.25rem; }
.field-group {
    margin-bottom: 1rem;
    border-bottom: 1px solid #2a2d3e;
    padding-bottom: 1rem;
}
.field-group:last-of-type { border-bottom: none; }
.field-group label {
    font-size: 0.75rem;
    font-weight: 600;
    color: #8b8fa3;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.35rem;
    display: block;
}
.field-row {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
}
.field-row p {
    flex: 1;
    font-size: 0.9rem;
    line-height: 1.5;
    color: #e4e6eb;
    white-space: pre-wrap;
}
.copy-btn {
    padding: 0.3rem 0.75rem;
    border-radius: 6px;
    border: 1px solid #2a2d3e;
    background: transparent;
    color: #8b8fa3;
    font-size: 0.75rem;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
}
.copy-btn:hover { background: #2a2d3e; color: #fff; }
.copy-btn.copied { background: #22c55e33; color: #22c55e; border-color: #22c55e55; }

.download-btn {
    display: inline-block;
    padding: 0.6rem 1.25rem;
    border-radius: 8px;
    background: #5b6ef5;
    color: #fff;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 600;
    margin-top: 0.5rem;
}
.download-btn:hover { background: #4a5de0; }

/* ── Stats Bar ──────────────────────────────────────────────── */
.stats-bar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0.75rem 1.25rem;
    margin-bottom: 1rem;
    background: #1c1f2e;
    border: 1px solid #2a2d3e;
    border-radius: 10px;
    flex-wrap: wrap;
}
.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.15rem;
}
.stat-value {
    font-size: 1.2rem;
    font-weight: 700;
    color: #fff;
}
.stat-label {
    font-size: 0.7rem;
    color: #8b8fa3;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    font-weight: 500;
}
.stat-rejections {
    flex: 1;
    min-width: 120px;
    align-items: flex-start;
}
.stat-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-top: 0.2rem;
}
.stat-tag {
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    background: #ef444422;
    color: #ef4444;
    font-size: 0.7rem;
    font-weight: 500;
}

/* ── Library Filters ───────────────────────────────────────── */
.library-filters {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
    align-items: center;
}
.library-filters select {
    padding: 0.55rem 0.85rem;
    border-radius: 8px;
    border: 1px solid #2a2d3e;
    background: #13151f;
    color: #e4e6eb;
    font-size: 0.85rem;
    cursor: pointer;
    min-width: 140px;
}
.export-btn {
    padding: 0.55rem 1rem;
    border-radius: 8px;
    border: 1px solid #5b6ef5;
    background: transparent;
    color: #5b6ef5;
    font-size: 0.85rem;
    cursor: pointer;
    margin-left: auto;
    white-space: nowrap;
    transition: background 0.2s, color 0.2s;
}
.export-btn:hover { background: #5b6ef5; color: #fff; }
.export-btn:disabled { opacity: 0.5; cursor: default; }
.meta-export-btn { margin-left: 0.5rem; border-color: #1877f2; color: #1877f2; }
.meta-export-btn:hover { background: #1877f2; color: #fff; }

.copy-all-btn { margin-right: 0.5rem; }

/* ── Empty state ────────────────────────────────────────────── */
.empty-text { text-align: center; color: #8b8fa3; padding: 3rem 1rem; }

/* ── Regenerate Button ─────────────────────────────────────── */
.ad-select-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.4rem 1rem;
}
.ad-select-label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
    font-size: 0.8rem;
    color: #8b8fa3;
}
.ad-select-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #5b6ef5;
    cursor: pointer;
}
.regenerate-btn {
    background: none;
    border: 1px solid #2a2d3e;
    border-radius: 6px;
    padding: 0.3rem 0.6rem;
    font-size: 1rem;
    cursor: pointer;
    color: #8b8fa3;
    transition: all 0.2s;
}
.regenerate-btn:hover { background: #2a2d3e; color: #e4e6eb; }
.regenerate-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Loading overlay on card during regeneration */
.ad-card.card-loading {
    opacity: 0.5;
    pointer-events: none;
}

/* ── Batch Action Bar ──────────────────────────────────────── */
.batch-bar {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.6rem 1rem;
    background: #1c1f2e;
    border: 1px solid #2a2d3e;
    border-radius: 8px;
}
.batch-select-all-label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
    font-size: 0.85rem;
    color: #e4e6eb;
    font-weight: 500;
    white-space: nowrap;
}
.batch-select-all-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #5b6ef5;
    cursor: pointer;
}
.batch-actions {
    display: flex;
    gap: 0.5rem;
    margin-left: auto;
}
.batch-actions .action-btn {
    padding: 0.4rem 1rem;
    font-size: 0.8rem;
    white-space: nowrap;
}
.batch-feedback-row {
    grid-column: 1 / -1;
    margin-bottom: 0.5rem;
}

/* ── Responsive ─────────────────────────────────────────────── */
@media (max-width: 768px) {

    /* ── Font sizes: prevent iOS auto-zoom on input focus ────── */
    body { font-size: 16px; }
    #password-input,
    #niche-select,
    .feedback-input { font-size: 16px; }

    /* ── Nav bar: stack brand + tabs, prevent overflow ────────── */
    .nav-bar {
        flex-wrap: wrap;
        height: auto;
        padding: 0.6rem 1rem;
        gap: 0.4rem;
    }
    .nav-brand { font-size: 1rem; }
    .nav-tabs {
        width: 100%;
        justify-content: center;
        gap: 0.15rem;
    }
    .nav-tab {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* ── Tab content: tighter padding on mobile ──────────────── */
    .tab-content { padding: 1rem 0.75rem; }

    /* ── Controls bar: stack niche dropdown + generate button ── */
    .controls {
        flex-direction: column;
        gap: 0.6rem;
    }
    #niche-select {
        max-width: 100%;
        min-height: 44px;
    }
    #generate-btn {
        min-height: 44px;
        width: 100%;
    }

    /* ── Ad grid: single column ──────────────────────────────── */
    .ad-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    .library-grid {
        grid-template-columns: 1fr;
    }

    /* ── Ad card images: full-width on mobile ────────────────── */
    .ad-card-image {
        width: 100%;
        height: auto;
    }

    /* ── Touch targets: all buttons at least 44px ────────────── */
    .action-btn {
        min-height: 44px;
        font-size: 0.9rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    #login-btn {
        min-height: 44px;
    }
    .feedback-submit {
        min-height: 44px;
        padding: 0.5rem 1.25rem;
    }
    .copy-btn {
        min-height: 44px;
        padding: 0.5rem 0.75rem;
        display: flex;
        align-items: center;
    }
    .download-btn {
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
    }

    /* ── Modal: full-screen on mobile ────────────────────────── */
    .modal {
        align-items: stretch;
        justify-content: stretch;
    }
    .modal-content {
        width: 100%;
        max-width: 100%;
        max-height: 100%;
        height: 100%;
        border-radius: 0;
        border: none;
    }
    #modal-image {
        border-radius: 0;
    }
    .modal-close {
        top: 8px;
        right: 12px;
        font-size: 1.75rem;
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .modal-fields { padding: 1rem; }

    /* ── Login box: fill width on small screens ──────────────── */
    .login-box {
        width: 100%;
        padding: 2rem 1.5rem;
    }

    /* ── Stats bar: wrap on mobile ────────────────────────────── */
    .stats-bar {
        gap: 1rem;
        padding: 0.75rem 1rem;
    }
    .stat-item { min-width: 60px; }

    /* ── Library filters: stack on mobile ──────────────────────── */
    .library-filters {
        flex-direction: column;
        gap: 0.5rem;
    }
    .library-filters select {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
    }
    .export-btn {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
        margin-left: 0;
    }

    /* ── Ad card hover: disable on touch devices ─────────────── */
    .ad-card:hover { transform: none; }

    /* ── Feedback input row: keep usable on mobile ───────────── */
    .feedback-input-row {
        padding: 0 0.75rem 0.75rem;
    }
    .feedback-input {
        min-height: 44px;
    }
}

/* ── Format Tabs ──────────────────────────────────────────── */
.format-tabs {
    display: flex;
    gap: 4px;
    padding: 6px 8px;
    background: rgba(0,0,0,0.3);
}
.format-tab {
    background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.12);
    color: #b0b3b8;
    font-size: 11px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s;
}
.format-tab:hover {
    background: rgba(255,255,255,0.14);
    color: #e4e6eb;
}
.format-tab.active {
    background: #3578e5;
    border-color: #3578e5;
    color: #fff;
}
.modal-format-tabs {
    justify-content: center;
    padding: 10px 0;
    background: transparent;
}
.modal-format-tabs .format-tab {
    font-size: 13px;
    padding: 5px 16px;
}
