:root {
    --bg: #0e1012;
    --surface: #161819;
    --surface2: #0b0d0e;
    --border: #1e2225;
    --border2: #252a2d;
    --accent: #128c7e; /* Darker WhatsApp Green */
    --accent-glow: rgba(18, 140, 126, 0.4);
    --green: #128c7e;
    --green-dim: #075e54;
    --green-glow: rgba(18, 140, 126, 0.15);
    --text: #ffffff;
    --text-dim: #999999;
    --text-muted: #555555;
    --danger: #ff4444;
    --glass-blur: 20px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'DM Sans', sans-serif;
    background: var(--bg);
    background-image: 
        radial-gradient(circle at 0% 0%, rgba(99, 102, 241, 0.05) 0%, transparent 40%),
        radial-gradient(circle at 100% 100%, rgba(61, 158, 104, 0.05) 0%, transparent 40%);
    min-height: 100vh;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 20px 12px;
    color: var(--text);
    overflow-x: hidden;
}

.wrap {
    width: 100%;
    max-width: 900px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin: 0 auto;
}

/* Header */
.header {
    text-align: center;
    margin-bottom: 8px;
    padding: 10px 0;
}

.header h1 {
    font-size: 2rem;
    font-weight: 600;
    color: #fff;
    letter-spacing: -0.03em;
}

.header h1 em {
    font-style: normal;
    color: var(--accent);
}

/* Auth Card & Main Card */
#main-search-card {
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
    position: relative;
}

#searchForm {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

#searchForm .field { flex: 1; }

@media (max-width: 600px) {
    #searchForm { flex-direction: column; align-items: stretch; }
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 24px;
    position: relative;
    backdrop-filter: blur(var(--glass-blur));
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
    transition: transform 0.2s;
}

/* Form Styling Enhancements */
.field label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: 10px;
}

.max-control {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 30px;
    padding: 3px 8px;
    background: linear-gradient(180deg, rgba(255,255,255,0.06) 0%, rgba(255,255,255,0.02) 100%);
    border: 1px solid var(--border2);
    border-radius: 999px;
}

.field .max-label {
    margin-bottom: 0;
    font-size: 0.63rem;
    letter-spacing: 0.11em;
    color: var(--text-dim);
    white-space: nowrap;
}

#maxResultsInput {
    width: 46px;
    height: 22px;
    padding: 0 6px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.35);
    color: var(--text);
    font-family: 'DM Mono', monospace;
    font-size: 0.78rem;
    font-weight: 500;
    text-align: center;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s, background-color 0.2s;
}

#maxResultsInput::-webkit-outer-spin-button,
#maxResultsInput::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

#maxResultsInput[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}

#maxResultsInput:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(18, 140, 126, 0.2);
    background: rgba(0, 0, 0, 0.5);
}

#config-banner p {
    font-size: 0.8rem;
    color: var(--text-dim);
    margin-bottom: 20px;
}

.config-label {
    display: block;
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 14px;
    margin-bottom: 6px;
}

#config-banner input {
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 0.85rem;
    padding: 12px;
    border-radius: 10px;
    margin-bottom: 4px;
    outline: none;
    transition: border-color 0.2s;
}

#config-banner input:focus {
    border-color: var(--accent);
}

.config-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
}

.config-actions button {
    flex: 1;
    padding: 10px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: 10px;
    border: none;
    transition: all 0.2s;
}

.btn-save { background: var(--accent); color: #000; }
.btn-save:hover { opacity: 0.9; }
.btn-clear { background: transparent; color: var(--danger); border: 1px solid rgba(255, 68, 68, 0.2) !important; }
.btn-clear:hover { background: rgba(255, 68, 68, 0.05); }

/* Ultra-Minimalist Corner Auth */
#authCard {
    position: absolute;
    top: -78px;
    right: 0;
    z-index: 50;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

#authBtn {
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.05);
    padding: 8px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s;
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#authBtn:hover { 
    background: rgba(255,255,255,0.1);
    transform: scale(1.1);
}

#authBtn svg { width: 18px; height: 18px; }

#authBtn.has-avatar {
    padding: 0;
    overflow: hidden;
}

#authBtn .auth-avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    display: block;
}

.auth-status {
    position: absolute;
    right: 48px;
    top: 0px;
    font-size: 0.65rem;
    color: var(--text-muted);
    opacity: 0;
    transition: all 0.2s;
    background: var(--surface);
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 10px;
}

#authCard:hover .auth-status { opacity: 1; right: 44px; }

.disconnect-btn {
    background: rgba(255, 68, 68, 0.1);
    color: var(--danger);
    border: 1px solid rgba(255, 68, 68, 0.2);
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.6rem;
    cursor: pointer;
}

.disconnect-btn:hover { background: var(--danger); color: #fff; }

#authCard.connected #authBtn::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 10px;
    height: 10px;
    background: var(--green);
    border-radius: 50%;
    border: 1.5px solid var(--bg);
}

.auth-config-link {
    font-size: 0.65rem;
    color: var(--text-muted);
    background: none;
    border: none;
    cursor: pointer;
    margin-top: 6px;
    opacity: 0.8;
    transition: opacity 0.2s;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.auth-config-link:hover {
    opacity: 1;
    color: var(--accent);
}

/* Form */
.field label {
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 8px;
}

.input-row {
    position: relative;
    display: flex;
    align-items: center;
}

input[type="email"] {
    width: 100%;
    height: 52px;
    padding: 0 78px 0 16px;
    font-size: 0.95rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    outline: none;
    font-family: 'DM Sans', sans-serif;
    transition: all 0.2s;
}

input[type="email"]:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.paste-btn {
    position: absolute;
    right: 12px;
    cursor: pointer;
    color: var(--text-muted);
    width: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    user-select: none;
}

.paste-btn:hover { color: var(--accent); }

.paste-btn svg {
    width: 70px;
    height: 70px;
}

.clear-btn {
    position: absolute;
    right: 44px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid var(--border2);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1;
    color: var(--text-dim);
    background: rgba(255, 255, 255, 0.06);
    cursor: pointer;
    user-select: none;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, background-color 0.2s, color 0.2s;
}

.clear-btn.show {
    opacity: 1;
    pointer-events: auto;
}

.clear-btn:hover {
    color: #fff;
    background: rgba(239, 68, 68, 0.35);
}

#searchForm .btn-submit {
    margin-top: 0;
    width: auto;
    min-width: 180px;
    height: 52px;
    padding: 0 24px;
}

.btn-submit {
    width: 100%;
    margin-top: 14px;
    padding: 16px;
    font-size: 1rem;
    font-weight: 700;
    color: #000;
    background: var(--green);
    border: none;
    border-radius: 14px;
    cursor: pointer;
    box-shadow: 0 4px 20px var(--green-glow);
    transition: all 0.2s;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.btn-submit:hover:not(:disabled) { 
    transform: translateY(-2px); 
    box-shadow: 0 8px 25px var(--green-glow);
    filter: brightness(1.1);
}

.btn-submit:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* Results */
.results-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 15px;
}

.max-control-results {
    flex-shrink: 0;
}

#resultsContainer {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.email-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 16px;
    position: relative;
    cursor: pointer;
    animation: none;
    transition: box-shadow 0.2s;
}

.email-item:hover {
    box-shadow: 0 5px 20px rgba(0,0,0,0.4);
}

.card-indicator {
    position: absolute;
    bottom: 12px;
    right: 16px;
    font-size: 0.7rem;
    color: var(--text-muted);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0.6;
}

.msg-body iframe {
    width: 100%;
    min-height: 500px;
    border: none;
    border-radius: 8px;
}

/* Metadata summary shown before expanding */
.card-summary {
    display: block;
}

.email-subject {
    font-size: 0.95rem;
    font-weight: 600;
    color: #fff;
    margin: 4px 0 6px;
    line-height: 1.3;
}

.email-top-row {
    font-size: 0.8rem;
    color: var(--text);
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.email-from {
    font-weight: 700;
    color: var(--green);
    letter-spacing: 0.01em;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.email-top-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.email-date {
    font-weight: 600;
    background: rgba(255, 255, 255, 0.06);
    padding: 4px 10px;
    border-radius: 10px;
    font-size: 0.75rem;
    color: var(--text-dim);
    border: 1px solid transparent;
}

.email-item-new-live {
    border-color: rgba(18, 140, 126, 0.75);
    background: linear-gradient(180deg, rgba(18, 140, 126, 0.2) 0%, var(--surface) 72%);
    box-shadow: 0 0 18px rgba(18, 140, 126, 0.24);
    animation: newCardPulse 2.2s ease-in-out infinite;
}

@keyframes newCardPulse {
    0% {
        box-shadow: 0 0 10px rgba(18, 140, 126, 0.2);
        border-color: rgba(18, 140, 126, 0.6);
    }
    50% {
        box-shadow: 0 0 28px rgba(18, 140, 126, 0.38), 0 0 54px rgba(18, 140, 126, 0.14);
        border-color: rgba(18, 140, 126, 0.9);
    }
    100% {
        box-shadow: 0 0 10px rgba(18, 140, 126, 0.2);
        border-color: rgba(18, 140, 126, 0.6);
    }
}

.email-snippet {
    font-size: 0.8rem;
    color: var(--text-dim);
    line-height: 1.5;
    margin-bottom: 10px;
}

.email-meta {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 12px;
}

.btn-view, .btn-link-action {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.8rem;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 0;
}

.btn-view:hover { opacity: 0.8; }

/* Message Body */
.msg-body {
    display: none;
    margin-top: 14px;
    background: #fff;
    border-radius: 12px;
    border: 1px solid var(--border);
    font-size: 0.85rem;
    color: #333;
    line-height: 1.6;
    white-space: pre-wrap;
    overflow: hidden; /* Iframe will determine size */
    word-break: break-word;
}

/* Formatted Links in Body */
.msg-body a {
    color: var(--accent);
    text-decoration: underline;
    text-underline-offset: 3px;
}

/* Code Display */
.code-box {
    margin-top: 0;
    display: inline-flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    background: rgba(18, 140, 126, 0.08); /* Deep Whatsapp Green dim */
    border-radius: 10px;
    border: 1px solid rgba(18, 140, 126, 0.2);
    flex-shrink: 0;
}

.code-value {
    font-family: 'DM Mono', monospace;
    font-size: 1.15rem;
    color: var(--green);
    letter-spacing: 2px;
    font-weight: 700;
}

.code-label {
    display: none;
}

.copy-mini {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.1rem;
    transition: color 0.2s;
}
.copy-mini:hover { color: var(--green); }

/* Loader */
#loader {
    display: none;
    justify-content: center;
    padding: 15px 0;
    gap: 8px;
}

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: none; }
.dot:nth-child(2) { animation-delay: 0.18s; }
.dot:nth-child(3) { animation-delay: 0.36s; }

/* Toast */
#toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(30px);
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 12px 24px;
    border-radius: 30px;
    font-size: 0.85rem;
    font-weight: 600;
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
    z-index: 2000;
    pointer-events: none;
    box-shadow: 0 15px 35px rgba(0,0,0,0.5);
}

#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: rgba(239, 68, 68, 0.4); color: var(--danger); background: #1a1010; }
#toast.success { border-color: rgba(34, 197, 94, 0.4); color: var(--green); background: #0e1a12; }

.back-to-top {
    position: fixed;
    right: 18px;
    bottom: 20px;
    z-index: 1500;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: 1px solid var(--border2);
    background: rgba(11, 13, 14, 0.92);
    color: var(--text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-weight: 800;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
    transition: opacity 0.2s, transform 0.2s, background-color 0.2s;
}

.back-to-top svg {
    width: 18px;
    height: 18px;
}

.back-to-top.show {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.back-to-top:hover {
    background: rgba(18, 140, 126, 0.22);
    border-color: rgba(18, 140, 126, 0.65);
}

/* Logout */
.disconnect {
    position: absolute;
    top: 15px;
    right: 15px;
    font-size: 0.7rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px 12px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: rgba(255,255,255,0.03);
    font-weight: 600;
}
.disconnect:hover { color: var(--danger); background: rgba(239, 68, 68, 0.08); border-color: rgba(239, 68, 68, 0.2); }

/* RESPONSIVE */
@media (max-width: 480px) {
    body { padding: 12px 10px; }
    .header h1 { font-size: 1.2rem; }
    .card { padding: 16px; border-radius: 16px; }
    .email-item { padding: 14px; border-radius: 14px; }
    .email-subject { font-size: 0.9rem; }
    .email-snippet { font-size: 0.75rem; }
    .btn-submit { padding: 12px; font-size: 0.9rem; }
    input[type="email"] { padding: 11px 72px 11px 12px; font-size: 0.85rem; }
    .max-control { height: 28px; padding: 3px 7px; }
    #maxResultsInput { width: 42px; font-size: 0.75rem; }
    .results-head { margin-bottom: 12px; }
    #authCard { top: -64px; right: 0; }
}

/* Small screens like 350x480 */
@media (max-width: 360px) {
    .wrap { gap: 10px; }
    .header h1 { font-size: 1.1rem; }
    .card { padding: 12px; }
    .email-meta { flex-wrap: wrap; gap: 8px; }
    .msg-body { font-size: 0.8rem; padding: 10px; }
}
