/* =========================================================
   Abwaab Cohorts Viewer
   Restructured, responsive stylesheet
   ========================================================= */

:root {
    /* Brand */
    --primary: #1a73e8;
    --primary-dark: #1557b0;
    --primary-light: #e8f0fe;
    --accent: #0d47a1;

    /* Filled primary buttons — kept AA-legible in both themes */
    --primary-btn: #1a73e8;
    --primary-btn-hover: #1557b0;
    --on-primary: #ffffff;

    /* Neutrals */
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --bg: #ffffff;
    --bg-soft: #f8fafc;

    /* Success (full view state) */
    --success: #16a34a;
    --success-bg: #dcfce7;

    /* Status-badge text — AA contrast on its tinted background */
    --badge-fg: #1557b0;
    --badge-full-fg: #166534;

    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.1);

    /* Layout metrics — referenced by every fixed region so they stay in sync */
    --topbar-h: 58px;
    --statusbar-h: 32px;
    --crop-offset: 150px; /* how much of the embedded page's header is cropped */

    --overlay-bg: rgba(255, 255, 255, 0.75);
}

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

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    color-scheme: light; /* always light — never follow the OS dark preference */
    font-family: 'Tajawal', system-ui, -apple-system, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

/* Visually hidden, still announced by screen readers */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ===== LOADER ===== */
#loader {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 99999;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    transition: opacity 0.4s ease, visibility 0.4s ease;
}

#loader.hidden {
    opacity: 0;
    visibility: hidden;
}

.loader-logo {
    font-size: 28px;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: -0.5px;
}

.loader-logo span {
    color: var(--text);
}

.loader-bar {
    width: 200px;
    max-width: 60vw;
    height: 3px;
    background: var(--border);
    border-radius: 99px;
    overflow: hidden;
}

.loader-bar-fill {
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--primary), #4f9eff);
    border-radius: 99px;
    animation: loadFill 1.8s ease forwards;
}

@keyframes loadFill {
    0% { width: 0%; }
    60% { width: 70%; }
    100% { width: 100%; }
}

.loader-text {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 400;
}

/* ===== TOPBAR ===== */
#topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--topbar-h);
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    z-index: 9990;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 20px;
    animation: slideDown 0.5s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes slideDown {
    from { transform: translateY(-100%); opacity: 0; }
    to   { transform: translateY(0);     opacity: 1; }
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0; /* allow the page label to truncate instead of overflowing */
}

.logo-mark {
    display: flex;
    align-items: center;
    gap: 9px;
    flex-shrink: 0;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--primary), #4f9eff);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.logo-icon svg {
    width: 18px;
    height: 18px;
    fill: #fff;
}

.logo-text {
    font-size: 17px;
    font-weight: 800;
    color: var(--text);
    letter-spacing: -0.3px;
    white-space: nowrap;
}

.logo-text span {
    color: var(--primary);
}

.topbar-divider {
    width: 1px;
    height: 22px;
    background: var(--border);
    margin: 0 4px;
    flex-shrink: 0;
}

.page-label {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 400;
    padding: 3px 10px;
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.tb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 7px 14px;
    min-height: 38px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease, transform 0.1s ease;
    white-space: nowrap;
}

.tb-btn:hover {
    background: var(--bg-soft);
    border-color: var(--border-strong);
}

.tb-btn:active {
    transform: scale(0.97);
}

.tb-btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.tb-btn svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.tb-btn.primary {
    background: var(--primary-btn);
    color: var(--on-primary);
    border-color: var(--primary-btn);
    box-shadow: 0 2px 8px rgba(26, 115, 232, 0.3);
}

.tb-btn.primary:hover {
    background: var(--primary-btn-hover);
    border-color: var(--primary-btn-hover);
}

.tb-btn.active {
    background: var(--primary-light);
    color: var(--primary);
    border-color: #b3cdf8; /* fallback for browsers without color-mix() */
    border-color: color-mix(in srgb, var(--primary) 45%, transparent);
}

/* Spinning refresh icon while reloading */
.tb-btn.loading svg {
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* ===== STATUS BAR ===== */
#statusbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--statusbar-h);
    background: var(--bg-soft);
    border-top: 1px solid var(--border);
    z-index: 9990;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 16px;
    animation: slideUp 0.5s 0.15s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes slideUp {
    from { transform: translateY(100%); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

.status-url {
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 400;
    direction: ltr;
    letter-spacing: 0.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status-badge {
    font-size: 11px;
    font-weight: 500;
    padding: 2px 8px;
    border-radius: 4px;
    background: var(--primary-light);
    color: var(--badge-fg);
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.2s ease, color 0.2s ease;
}

/* "Full view" state — driven by a class instead of inline styles */
.status-badge.is-full {
    background: var(--success-bg);
    color: var(--badge-full-fg);
}

/* ===== IFRAME WRAPPER ===== */
#frame-wrapper {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    right: 0;
    bottom: var(--statusbar-h);
    overflow: hidden;
    background: var(--bg-soft);
}

#frame-wrapper iframe {
    position: absolute;
    left: 0;
    width: 100%;
    height: calc(100% + var(--crop-offset));
    border: none;
    top: calc(-1 * var(--crop-offset));
    transition: top 0.45s cubic-bezier(0.25, 1, 0.5, 1);
}

/* Header revealed: show the embedded page from the top */
#frame-wrapper.show-header iframe {
    top: 0;
    height: 100%;
}

/* ===== REFRESH OVERLAY ===== */
#refresh-overlay {
    position: absolute;
    inset: 0;
    background: var(--overlay-bg);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
}

#refresh-overlay.active {
    opacity: 1;
    pointer-events: all;
}

.spinner-ring {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

/* =========================================================
   RESPONSIVE
   ========================================================= */

/* Tablet: drop the contextual page label to save room */
@media (max-width: 720px) {
    #topbar { padding: 0 14px; }
    .page-label { display: none; }
    .topbar-divider { display: none; }
}

/* Phone: collapse toolbar buttons to icon-only */
@media (max-width: 540px) {
    :root { --topbar-h: 54px; }

    #topbar { padding: 0 12px; gap: 8px; }
    #statusbar { padding: 0 12px; }

    .tb-btn {
        padding: 8px;
        min-width: 44px;
        min-height: 44px;
    }

    /* Hide text labels; the aria-label keeps the buttons accessible */
    .tb-btn .tb-label {
        display: none;
    }

    .tb-btn svg {
        width: 16px;
        height: 16px;
    }
}

/* Very small phones: keep only the logo glyph */
@media (max-width: 360px) {
    .logo-text { display: none; }
    .status-url { font-size: 10px; }
}

/* Respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }

    .loader-bar-fill { width: 100%; }
    .spinner-ring { border-top-color: var(--primary); }
}
