/* ── Reset & base ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --purple: #534AB7;
    --purple-light: #EEEDFE;
    --purple-dark: #3C3489;
    --teal: #1D9E75;
    --teal-light: #E1F5EE;
    --teal-dark: #085041;
    --amber: #BA7517;
    --amber-light: #FAEEDA;
    --red: #E24B4A;
    --gray-50: #F8F8F6;
    --gray-100: #EFEFED;
    --gray-200: #D3D1C7;
    --gray-400: #888780;
    --gray-700: #444441;
    --gray-900: #1C1C1A;
    --text: #1C1C1A;
    --text-muted: #5F5E5A;
    --text-hint: #888780;
    --border: rgba(28,28,26,0.12);
    --border-strong: rgba(28,28,26,0.25);
    --bg: #ffffff;
    --bg-2: #F8F8F6;
    --radius: 12px;
    --radius-sm: 8px;
    --radius-lg: 16px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1C1C1A;
        --bg-2: #242422;
        --text: #E8E6DF;
        --text-muted: #9C9A92;
        --text-hint: #666460;
        --border: rgba(255,255,255,0.1);
        --border-strong: rgba(255,255,255,0.2);
        --gray-50: #242422;
        --gray-100: #2C2C2A;
        --purple-light: #2A2660;
        --teal-light: #0A3D2E;
        --amber-light: #3D2E0A;
    }
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg-2);
    color: var(--text);
    line-height: 1.5;
    min-height: 100dvh;
}

a { color: var(--purple); text-decoration: none; }
button { cursor: pointer; font-family: inherit; }
input { font-family: inherit; }

/* ── HOME PAGE ─────────────────────────────────────────────────────────── */
.qs-home {
    max-width: 680px;
    margin: 0 auto;
    padding: 24px 16px 40px;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.qs-hero { text-align: center; padding: 16px 0 8px; }

.qs-logo {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 10px;
}

.qs-tagline {
    font-size: 0.95rem;
    color: var(--text-muted);
    max-width: 380px;
    margin: 0 auto;
    line-height: 1.6;
}

.qs-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 16px;
}

.qs-card {
    background: var(--bg);
    border: 0.5px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow);
}

.qs-card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.qs-card-icon {
    width: 40px; height: 40px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.1rem;
    flex-shrink: 0;
}

.host-icon { background: var(--purple-light); color: var(--purple); }
.viewer-icon { background: var(--teal-light); color: var(--teal-dark); }

.qs-card-title { font-size: 1rem; font-weight: 600; color: var(--text); }
.qs-card-sub { font-size: 0.8rem; color: var(--text-muted); }

/* Form fields */
.qs-field { margin-bottom: 16px; }
.qs-field label {
    display: block;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}
.qs-field label .optional { font-weight: 400; text-transform: none; letter-spacing: 0; }

.qs-field input[type="text"] {
    width: 100%;
    padding: 9px 12px;
    border: 0.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
    color: var(--text);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.15s;
}
.qs-field input[type="text"]:focus { border-color: var(--purple); }

/* Subject grid */
.subject-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 6px;
    margin-bottom: 8px;
}

.subj-chip {
    padding: 7px 8px;
    border: 0.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
    font-size: 0.78rem;
    color: var(--text-muted);
    cursor: pointer;
    text-align: center;
    transition: all 0.15s;
    user-select: none;
}
.subj-chip:hover { border-color: var(--purple); color: var(--purple); }
.subj-chip.active {
    border-color: var(--purple);
    background: var(--purple-light);
    color: var(--purple-dark);
    font-weight: 500;
}

.custom-subj-row {
    display: flex;
    gap: 6px;
    margin-bottom: 0;
}
.custom-subj-row input {
    flex: 1;
    padding: 7px 10px;
    border: 0.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
    color: var(--text);
    font-size: 0.85rem;
}
.custom-subj-row button {
    padding: 7px 12px;
    border: 0.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
    color: var(--text-muted);
    font-size: 0.82rem;
    white-space: nowrap;
}

/* Type toggle */
.type-toggle { display: flex; gap: 8px; }
.type-btn {
    flex: 1;
    padding: 9px 8px;
    border: 0.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 500;
    text-align: center;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
}
.type-btn:hover { border-color: var(--purple); }
.type-btn.active {
    border-color: var(--purple);
    background: var(--purple-light);
    color: var(--purple-dark);
}

/* Buttons */
.qs-btn {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    width: 100%;
    padding: 11px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    border: none;
    transition: all 0.15s;
}
.qs-btn.primary {
    background: var(--purple);
    color: white;
}
.qs-btn.primary:hover { background: var(--purple-dark); }
.qs-btn.secondary {
    background: var(--bg-2);
    color: var(--text);
    border: 0.5px solid var(--border-strong);
}
.qs-btn.secondary:hover { background: var(--gray-100); }
.qs-btn.full-width { width: 100%; }

.qs-divider {
    text-align: center;
    font-size: 0.78rem;
    color: var(--text-hint);
    margin-top: 12px;
    padding-top: 12px;
    border-top: 0.5px solid var(--border);
}

.qs-footer {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-hint);
    padding-bottom: 16px;
}

/* ── ROOM PAGE ─────────────────────────────────────────────────────────── */
.qs-room {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    max-width: 600px;
    margin: 0 auto;
    background: var(--bg);
}

/* Top bar */
.room-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 0.5px solid var(--border);
    background: var(--bg);
    position: sticky;
    top: 0;
    z-index: 10;
}

.room-info { display: flex; align-items: center; gap: 8px; overflow: hidden; }

.back-btn {
    font-size: 1.1rem;
    color: var(--text-muted);
    padding: 2px 4px;
    flex-shrink: 0;
}

.subject-badge {
    background: var(--purple-light);
    color: var(--purple-dark);
    font-size: 0.72rem;
    font-weight: 600;
    padding: 3px 9px;
    border-radius: 20px;
    flex-shrink: 0;
}

.room-name-lbl {
    font-size: 0.82rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.live-badge {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--teal);
    letter-spacing: 0.04em;
}

.viewer-badge {
    background: var(--purple-light);
    color: var(--purple-dark);
    font-size: 0.7rem;
    font-weight: 500;
    padding: 2px 8px;
    border-radius: 12px;
}

/* Viewfinder */
.viewfinder-wrap {
    position: relative;
    width: 100%;
    background: #111;
    aspect-ratio: 4/3;
    overflow: hidden;
    flex-shrink: 0;
}

.viewfinder-wrap video,
.viewfinder-wrap canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

#feedCanvas { background: #111; }

.vf-corners { position: absolute; inset: 0; pointer-events: none; }
.corner {
    position: absolute;
    width: 22px; height: 22px;
    border-color: rgba(255,255,255,0.7);
    border-style: solid;
}
.corner.tl { top: 10px; left: 10px; border-width: 2px 0 0 2px; }
.corner.tr { top: 10px; right: 10px; border-width: 2px 2px 0 0; }
.corner.bl { bottom: 10px; left: 10px; border-width: 0 0 2px 2px; }
.corner.br { bottom: 10px; right: 10px; border-width: 0 2px 2px 0; }

.vf-label {
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.65rem;
    color: rgba(255,255,255,0.55);
    letter-spacing: 0.05em;
    white-space: nowrap;
}

.thinking-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: white;
    font-size: 0.85rem;
}

.waiting-overlay {
    position: absolute;
    inset: 0;
    background: #111;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: rgba(255,255,255,0.4);
    font-size: 0.85rem;
}
.waiting-icon { font-size: 2rem; opacity: 0.4; }

/* Spinner */
.spinner {
    width: 32px; height: 32px;
    border: 3px solid rgba(255,255,255,0.2);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Type selector in room */
.type-selector {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 0.5px solid var(--border);
}

.type-label {
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--text-hint);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    flex-shrink: 0;
}

.type-row { display: flex; gap: 6px; flex: 1; }

.type-selector .type-btn {
    flex: 1;
    padding: 7px 6px;
    font-size: 0.8rem;
}

/* Controls */
.controls-row {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding: 14px 24px;
    border-bottom: 0.5px solid var(--border);
}

.ctrl-btn {
    width: 44px; height: 44px;
    border-radius: 50%;
    border: 0.5px solid var(--border-strong);
    background: var(--bg-2);
    color: var(--text-muted);
    font-size: 1.1rem;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
}
.ctrl-btn:hover { background: var(--gray-100); }
.ctrl-btn.disabled {
    opacity: 0.3;
    cursor: not-allowed;
    pointer-events: none;
}

.capture-btn {
    width: 64px; height: 64px;
    border-radius: 50%;
    background: var(--purple);
    border: 4px solid var(--purple-light);
    color: white;
    font-size: 1.5rem;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
    box-shadow: 0 2px 8px rgba(83,74,183,0.3);
}
.capture-btn:hover { background: var(--purple-dark); transform: scale(1.05); }
.capture-btn:active { transform: scale(0.96); }
.capture-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; pointer-events: none; }

/* Answer panel */
.answer-panel {
    padding: 14px;
    border-bottom: 0.5px solid var(--border);
    background: var(--bg);
}

.answer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.answer-label {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-hint);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.answer-type-badge {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 500;
    background: var(--purple-light);
    color: var(--purple-dark);
}

.answer-body {
    margin-bottom: 12px;
    background: var(--bg-2);
    border-radius: var(--radius-sm);
    padding: 12px;
    border: 0.5px solid var(--border);
}

.mcq-answer { display: flex; flex-direction: column; gap: 6px; }

.correct-line {
    font-size: 1rem;
    font-weight: 700;
    color: var(--teal);
    background: var(--teal-light);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--teal);
}

.reason-line {
    font-size: 0.85rem;
    color: var(--text-muted);
    border-left: 2px solid var(--purple);
    padding-left: 10px;
    line-height: 1.5;
}

.options-line, .answer-line {
    font-size: 0.8rem;
    color: var(--text-hint);
    line-height: 1.5;
}

.long-answer {
    font-size: 0.88rem;
    color: var(--text);
    line-height: 1.7;
}

.answer-error {
    color: var(--red);
    font-size: 0.85rem;
    padding: 8px;
}

/* Share bar */
.share-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px 10px;
    border-top: 0.5px solid var(--border);
    background: var(--bg);
}

.share-icon { font-size: 1rem; color: var(--text-hint); flex-shrink: 0; }

.share-link {
    flex: 1;
    font-size: 0.72rem;
    color: var(--text-hint);
    background: var(--bg-2);
    border-radius: 6px;
    padding: 5px 9px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.share-copy-btn {
    width: 32px; height: 32px;
    border-radius: 6px;
    border: 0.5px solid var(--border-strong);
    background: var(--bg-2);
    color: var(--text-muted);
    font-size: 1rem;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: all 0.15s;
}
.share-copy-btn:hover { background: var(--gray-100); }

/* History drawer */
.history-drawer {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 600px;
    background: var(--bg);
    border-top: 0.5px solid var(--border);
    border-radius: 16px 16px 0 0;
    max-height: 60dvh;
    overflow-y: auto;
    z-index: 100;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.1);
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 0.5px solid var(--border);
    font-weight: 500;
    font-size: 0.9rem;
    position: sticky;
    top: 0;
    background: var(--bg);
}

.history-header button {
    background: none; border: none;
    font-size: 1.1rem; color: var(--text-muted);
}

.history-list { padding: 8px; }

.history-item {
    display: flex;
    gap: 10px;
    padding: 10px;
    border-radius: var(--radius-sm);
    border-bottom: 0.5px solid var(--border);
}
.history-item:last-child { border-bottom: none; }

.history-thumb {
    width: 72px; height: 54px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
    background: #111;
}

.history-content { flex: 1; overflow: hidden; }
.history-meta { font-size: 0.7rem; color: var(--text-hint); margin-bottom: 3px; }
.history-ans { font-size: 0.8rem; color: var(--text-muted); line-height: 1.4; }

/* Error state */
.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 40px 20px;
    text-align: center;
}
.error-icon { font-size: 2.5rem; }
.error-msg { font-size: 0.95rem; color: var(--text-muted); }

/* ── Responsive tweaks ─────────────────────────────────────────────────── */
@media (min-width: 600px) {
    .qs-room { border-left: 0.5px solid var(--border); border-right: 0.5px solid var(--border); }
    .viewfinder-wrap { aspect-ratio: 16/9; }
}

@media (max-width: 360px) {
    .type-label { display: none; }
    .subject-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ── Safe area insets (notch/home bar) ─────────────────────────────────── */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .share-bar { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
    .qs-home { padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
}

/* ── Additional overlays (bug fix additions) ───────────────────────────── */
.host-left-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.72);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    color: rgba(255,255,255,0.85);
    font-size: 0.9rem;
    text-align: center;
    padding: 16px;
}
.host-left-overlay small { color: rgba(255,255,255,0.55); font-size: 0.78rem; }

.camera-error-overlay {
    position: absolute;
    inset: 0;
    background: #111;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    color: rgba(255,255,255,0.6);
    font-size: 0.88rem;
    text-align: center;
    padding: 16px;
}
.camera-error-overlay small { font-size: 0.75rem; color: rgba(255,255,255,0.35); }

/* ── Fullscreen mode ───────────────────────────────────────────────────── */
.viewfinder-wrap {
    cursor: zoom-in;
}

.viewfinder-wrap:-webkit-full-screen {
    width: 100vw;
    height: 100vh;
    background: #000;
    border-radius: 0;
    aspect-ratio: unset;
}

.viewfinder-wrap:-webkit-full-screen video,
.viewfinder-wrap:-webkit-full-screen canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.viewfinder-wrap:fullscreen {
    width: 100vw;
    height: 100vh;
    background: #000;
    border-radius: 0;
    aspect-ratio: unset;
}

.viewfinder-wrap:fullscreen video,
.viewfinder-wrap:fullscreen canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Keep overlays visible in fullscreen */
.viewfinder-wrap:fullscreen .vf-label,
.viewfinder-wrap:fullscreen .vf-corners,
.viewfinder-wrap:fullscreen .thinking-overlay,
.viewfinder-wrap:fullscreen .waiting-overlay,
.viewfinder-wrap:fullscreen .host-left-overlay,
.viewfinder-wrap:fullscreen .camera-error-overlay {
    z-index: 9999;
}

.viewfinder-wrap:fullscreen .vf-label {
    font-size: 0.85rem;
    bottom: 16px;
}

/* ── Room code bar ─────────────────────────────────────────────────────── */
.room-code-bar {
    padding: 12px 14px;
    border-bottom: 0.5px solid var(--border);
    background: var(--purple-light);
    text-align: center;
}

.room-code-label {
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--purple-dark);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 4px;
}

.room-code-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--purple-dark);
    letter-spacing: 0.2em;
    font-family: var(--font-mono);
    line-height: 1.2;
}

.room-code-hint {
    font-size: 0.72rem;
    color: var(--purple-dark);
    opacity: 0.7;
    margin-top: 3px;
}

/* ── Room code banner (replaces old room-code-bar) ─────────────────────── */
.room-code-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--purple-light);
    border-bottom: 0.5px solid var(--border);
    gap: 12px;
}

.rcb-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.rcb-label {
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--purple-dark);
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.rcb-code {
    font-size: 1.6rem;
    font-weight: 800;
    color: var(--purple-dark);
    letter-spacing: 0.25em;
    font-family: var(--font-mono);
    line-height: 1;
}

.rcb-right {
    text-align: right;
    flex-shrink: 0;
}

.rcb-hint {
    font-size: 0.68rem;
    color: var(--purple-dark);
    opacity: 0.65;
    margin-bottom: 2px;
}

.rcb-url {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--purple-dark);
    font-family: var(--font-mono);
}

.room-code-chip {
    background: var(--purple);
    color: #EEEDFE;
    font-size: 0.72rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 10px;
    letter-spacing: 0.1em;
    font-family: var(--font-mono);
}
