/* ==========================================================================
   Economics Mcq & Private Chatroom - Modern Layered Stylesheet
   ========================================================================== */
@layer reset, base, components, utilities;

@layer reset {
    html {
        line-height: 1.5;
        -webkit-text-size-adjust: 100%;
    }
    body {
        margin: 0;
        min-block-size: 100dvh;
    }
    h1, h2, h3, p {
        margin: 0;
    }
    button, input {
        font: inherit;
        margin: 0;
    }
    img, video {
        display: block;
        max-inline-size: 100%;
    }
}

@layer base {
    :root {
        /* Brand & Accent */
        --color-primary: #2563eb;
        --color-primary-hover: #1d4ed8;
        --color-primary-soft: #eff6ff;
        --color-primary-border: #bfdbfe;
        --color-accent-gradient: linear-gradient(135deg, #2563eb 0%, #4f46e5 100%);

        /* Neutrals */
        --surface-page: #f8fafc;
        --surface-card: #ffffff;
        --surface-subtle: #f1f5f9;
        --surface-chat: #eef2f6;
        --border-subtle: #e2e8f0;
        --border-strong: #cbd5e1;

        /* Text */
        --text-primary: #0f172a;
        --text-secondary: #475569;
        --text-muted: #64748b;

        /* Semantics */
        --color-success: #10b981;
        --color-success-bg: #ecfdf5;
        --color-success-text: #065f46;
        --color-danger: #ef4444;
        --color-danger-solid: #dc2626;
        --color-danger-bg: #fef2f2;
        --color-danger-text: #991b1b;
        --color-warning-bg: #fffbeb;
        --color-warning-border: #fde68a;
        --color-warning-text: #92400e;

        /* Shape & Elevation */
        --radius-sm: 8px;
        --radius-md: 12px;
        --radius-lg: 16px;
        --radius-pill: 9999px;
        --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
        --shadow-md: 0 4px 12px -2px rgba(15, 23, 42, 0.08), 0 2px 4px -2px rgba(15, 23, 42, 0.04);
        --shadow-lg: 0 20px 30px -10px rgba(15, 23, 42, 0.25);
        --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    }

    body {
        font-family: var(--font-sans);
        color: var(--text-primary);
        background-color: var(--surface-page);
        -webkit-font-smoothing: antialiased;
        display: flex;
        flex-direction: column;
    }

    body.is-chat-mode {
        block-size: 100dvh;
        overflow: hidden;
    }

    a {
        color: inherit;
        text-decoration: none;
    }
}

@layer components {
    /* ======================================================================
       Header & Navigation
       ====================================================================== */
    .site-header {
        background-color: rgba(255, 255, 255, 0.9);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        border-bottom: 1px solid var(--border-subtle);
        position: sticky;
        top: 0;
        z-index: 40;
        flex-shrink: 0;
    }

    .header-inner {
        max-inline-size: 860px;
        margin-inline: auto;
        padding: 12px 16px;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 10px 14px;
    }

    .brand-group {
        display: flex;
        align-items: center;
        gap: 10px;
        min-inline-size: 0;
    }

    .brand-icon {
        font-size: 1.35rem;
        line-height: 1;
    }

    .site-title {
        font-size: 1.2rem;
        font-weight: 800;
        color: var(--text-primary);
        letter-spacing: -0.02em;
    }

    .quiz-badge {
        background: var(--color-primary-soft);
        color: var(--color-primary);
        font-size: 0.75rem;
        font-weight: 700;
        padding: 3px 10px;
        border-radius: var(--radius-pill);
        border: 1px solid var(--color-primary-border);
        white-space: nowrap;
    }

    .header-nav {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
    }

    .user-pill {
        display: inline-flex;
        align-items: center;
        background-color: var(--surface-subtle);
        color: var(--text-primary);
        font-size: 0.8125rem;
        font-weight: 600;
        padding: 5px 11px;
        border-radius: var(--radius-pill);
        border: 1px solid var(--border-subtle);
    }

    /* ======================================================================
       Main Layout Shell
       ====================================================================== */
    .main-wrapper {
        flex: 1 1 auto;
        min-block-size: 0;
        display: flex;
        flex-direction: column;
    }

    .content-view {
        inline-size: 100%;
        flex: 1 1 auto;
        min-block-size: 0;
    }

    .container {
        max-inline-size: 820px;
        margin-inline: auto;
        padding: 24px 16px 72px;
    }

    /* ======================================================================
       Buttons
       ====================================================================== */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        padding: 9px 18px;
        font-size: 0.9375rem;
        font-weight: 600;
        border-radius: var(--radius-sm);
        border: 1px solid transparent;
        cursor: pointer;
        transition: background-color 0.16s ease, transform 0.12s ease, box-shadow 0.16s ease;
        white-space: nowrap;
    }

    .btn:active:not(:disabled) {
        transform: translateY(1px);
    }

    .btn:disabled {
        opacity: 0.6;
        cursor: not-allowed;
    }

    .btn-primary {
        background: var(--color-accent-gradient);
        color: #ffffff;
        box-shadow: 0 2px 6px rgba(37, 99, 235, 0.25);
    }
    .btn-primary:hover:not(:disabled) {
        filter: brightness(1.06);
        box-shadow: 0 4px 10px rgba(37, 99, 235, 0.35);
    }

    .btn-large {
        padding: 12px 32px;
        font-size: 1rem;
        border-radius: var(--radius-md);
    }

    .btn-outline {
        background-color: var(--surface-card);
        border-color: var(--border-strong);
        color: var(--text-primary);
    }
    .btn-outline:hover:not(:disabled) {
        background-color: var(--surface-subtle);
    }

    .btn-danger {
        background-color: var(--color-danger-solid);
        color: #ffffff;
        box-shadow: 0 2px 5px rgba(220, 38, 38, 0.22);
    }
    .btn-danger:hover:not(:disabled) {
        background-color: #b91c1c;
    }

    .btn-sm {
        padding: 6px 12px;
        font-size: 0.8125rem;
    }

    .btn-block {
        inline-size: 100%;
    }

    /* ======================================================================
       VIEW 1: Economics MCQ Quiz (Modern Academic UI)
       ====================================================================== */
    .intro-card {
        background: linear-gradient(135deg, #ffffff 0%, #f1f5f9 100%);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-lg);
        padding: 20px 22px;
        margin-block-end: 24px;
        box-shadow: var(--shadow-sm);
    }

    .intro-top {
        display: flex;
        flex-wrap: wrap;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px 16px;
        margin-block-end: 16px;
    }

    .intro-card h2 {
        font-size: 1.25rem;
        font-weight: 700;
        margin-block-end: 6px;
        color: var(--text-primary);
        letter-spacing: -0.01em;
    }

    .intro-card p {
        font-size: 0.9375rem;
        color: var(--text-secondary);
        margin-block-end: 16px;
    }

    .intro-top p {
        margin-block-end: 0;
    }

    .lang-switch {
        display: inline-flex;
        align-items: center;
        gap: 2px;
        padding: 3px;
        background: var(--surface-subtle);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-pill);
        flex-shrink: 0;
    }

    .lang-switch-intro {
        background: var(--surface-card);
        box-shadow: var(--shadow-sm);
    }

    .lang-btn {
        appearance: none;
        border: none;
        background: transparent;
        color: var(--text-secondary);
        font: inherit;
        font-size: 0.78rem;
        font-weight: 600;
        line-height: 1.2;
        padding: 6px 11px;
        border-radius: var(--radius-pill);
        cursor: pointer;
        white-space: nowrap;
        transition: background-color 0.16s ease, color 0.16s ease, box-shadow 0.16s ease;
    }

    .lang-btn:hover:not(:disabled):not(.is-active) {
        color: var(--text-primary);
        background: rgba(15, 23, 42, 0.05);
    }

    .lang-btn.is-active {
        background: var(--color-primary);
        color: #ffffff;
        box-shadow: 0 1px 3px rgba(37, 99, 235, 0.25);
    }

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

    .quiz-tracker {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 12px 20px;
        padding-block-start: 14px;
        border-top: 1px solid var(--border-subtle);
        font-size: 0.875rem;
    }

    .tracker-item {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        background: var(--surface-card);
        padding: 5px 12px;
        border-radius: var(--radius-pill);
        border: 1px solid var(--border-subtle);
    }

    .tracker-label {
        color: var(--text-muted);
        font-weight: 500;
    }

    .tracker-val {
        font-weight: 700;
        color: var(--color-primary);
    }

    .btn-text {
        background: none;
        border: none;
        color: var(--color-primary);
        font-size: 0.875rem;
        font-weight: 600;
        cursor: pointer;
        margin-inline-start: auto;
        padding: 5px 10px;
        border-radius: var(--radius-sm);
    }
    .btn-text:hover:not(:disabled) {
        background-color: var(--color-primary-soft);
    }

    .mcq-list {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .card {
        background-color: var(--surface-card);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-md);
        padding: 20px 22px;
        box-shadow: var(--shadow-sm);
        transition: box-shadow 0.2s ease, border-color 0.2s ease;
    }
    .card:hover {
        box-shadow: var(--shadow-md);
    }

    .mcq-head {
        display: flex;
        align-items: flex-start;
        gap: 12px;
        margin-block-end: 14px;
    }

    .mcq-num {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background-color: var(--surface-subtle);
        color: var(--text-secondary);
        font-weight: 700;
        font-size: 0.8125rem;
        padding: 4px 10px;
        border-radius: var(--radius-sm);
        flex-shrink: 0;
    }

    .mcq-question {
        font-size: 1.02rem;
        font-weight: 600;
        color: var(--text-primary);
        line-height: 1.45;
    }

    .mcq-options {
        display: flex;
        flex-direction: column;
        gap: 9px;
    }

    .option-btn {
        display: flex;
        align-items: center;
        gap: 12px;
        inline-size: 100%;
        text-align: start;
        background: var(--surface-card);
        border: 1px solid var(--border-strong);
        border-radius: var(--radius-sm);
        padding: 11px 14px;
        cursor: pointer;
        transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
    }

    .option-btn:hover:not(:disabled) {
        background-color: var(--color-primary-soft);
        border-color: var(--color-primary);
        transform: translateX(2px);
    }

    .opt-prefix {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        inline-size: 28px;
        block-size: 28px;
        background: var(--surface-subtle);
        border-radius: 6px;
        font-size: 0.8125rem;
        font-weight: 700;
        color: var(--text-secondary);
        flex-shrink: 0;
    }

    .opt-text {
        font-size: 0.9375rem;
        color: var(--text-primary);
        line-height: 1.4;
    }

    .option-btn.opt-correct {
        background-color: var(--color-success-bg);
        border-color: var(--color-success);
    }
    .option-btn.opt-correct .opt-prefix {
        background-color: var(--color-success);
        color: #ffffff;
    }
    .option-btn.opt-correct .opt-text {
        color: var(--color-success-text);
        font-weight: 600;
    }

    .option-btn.opt-wrong {
        background-color: var(--color-danger-bg);
        border-color: var(--color-danger);
    }
    .option-btn.opt-wrong .opt-prefix {
        background-color: var(--color-danger);
        color: #ffffff;
    }
    .option-btn.opt-wrong .opt-text {
        color: var(--color-danger-text);
    }

    .mcq-result {
        margin-block-start: 14px;
        padding: 12px 14px;
        border-radius: var(--radius-sm);
        font-size: 0.875rem;
    }
    .mcq-result.correct {
        background-color: var(--color-success-bg);
        border: 1px solid #a7f3d0;
        color: var(--color-success-text);
    }
    .mcq-result.wrong {
        background-color: var(--color-danger-bg);
        border: 1px solid #fecaca;
        color: var(--color-danger-text);
    }
    .result-message {
        font-weight: 700;
        margin-block-end: 4px;
    }
    .result-explanation {
        color: var(--text-secondary);
        line-height: 1.45;
    }

    .home-footer {
        margin-block-start: 36px;
        padding: 26px 20px;
        background-color: var(--surface-card);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-lg);
        text-align: center;
        box-shadow: var(--shadow-sm);
    }

    .login-prompt p {
        font-size: 0.9375rem;
        color: var(--text-secondary);
        margin-block-end: 16px;
    }

    /* ======================================================================
       VIEW 2: Chat Room (Full-Height Modern Flex Messenger)
       ====================================================================== */
    #view-chat {
        background-color: var(--surface-chat);
        display: flex;
        flex-direction: column;
        min-block-size: 0;
    }

    .chat-container {
        max-inline-size: 860px;
        inline-size: 100%;
        flex: 1 1 auto;
        min-block-size: 0;
        margin-inline: auto;
        display: flex;
        flex-direction: column;
        background-color: var(--surface-chat);
        border-inline: 1px solid var(--border-subtle);
        box-shadow: var(--shadow-md);
    }

    .chat-inner-header {
        background-color: var(--surface-card);
        border-bottom: 1px solid var(--border-subtle);
        padding: 10px 16px;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        flex-shrink: 0;
    }

    .chat-header-info {
        display: flex;
        align-items: center;
        gap: 10px;
        min-inline-size: 0;
    }

    .chat-title-block {
        min-inline-size: 0;
    }

    .chat-back-btn {
        background: var(--surface-subtle);
        border: 1px solid var(--border-subtle);
        font-size: 0.8125rem;
        font-weight: 600;
        color: var(--text-secondary);
        padding: 6px 11px;
        border-radius: var(--radius-sm);
        cursor: pointer;
        line-height: 1;
        transition: background-color 0.15s ease, color 0.15s ease;
        white-space: nowrap;
    }
    .chat-back-btn:hover:not(:disabled) {
        background-color: var(--color-danger-bg);
        border-color: #fecaca;
        color: var(--color-danger-solid);
    }

    .chat-title {
        font-size: 1.05rem;
        font-weight: 700;
        color: var(--text-primary);
        line-height: 1.2;
    }

    .chat-status-row {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
    }

    .chat-status {
        font-size: 0.72rem;
        color: #059669;
        display: inline-flex;
        align-items: center;
        gap: 5px;
        font-weight: 600;
    }

    .typing-status-pill {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 2px 9px;
        font-size: 0.6875rem;
        font-weight: 600;
        border-radius: var(--radius-pill);
        background-color: var(--color-primary-soft);
        color: var(--color-primary);
        border: 1px solid var(--color-primary-border);
        animation: typingPulse 1.4s ease-in-out infinite;
    }

    @keyframes typingPulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.68; }
    }

    .webhook-pill-btn {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 2px 8px;
        font-size: 0.6875rem;
        font-weight: 600;
        border-radius: var(--radius-pill);
        border: 1px solid var(--border-strong);
        background-color: var(--surface-subtle);
        color: var(--text-secondary);
        cursor: pointer;
        transition: background-color 0.15s ease, border-color 0.15s ease;
    }
    .webhook-pill-btn.is-active {
        background-color: var(--color-success-bg);
        border-color: #6ee7b7;
        color: var(--color-success-text);
    }
    .webhook-pill-btn:hover:not(:disabled) {
        filter: brightness(0.96);
    }

    .status-dot {
        inline-size: 7px;
        block-size: 7px;
        border-radius: 50%;
        background-color: var(--color-success);
        box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
    }

    .chat-header-user {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
    }

    /* ======================================================================
       Pinned Message Bar
       ====================================================================== */
    .pinned-bar {
        background-color: var(--color-warning-bg);
        border-bottom: 1px solid var(--color-warning-border);
        border-inline-start: 4px solid #f59e0b;
        padding: 10px 16px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        flex-shrink: 0;
        z-index: 10;
    }

    .pinned-content {
        display: flex;
        align-items: flex-start;
        gap: 10px;
        flex: 1;
        min-inline-size: 0;
    }

    .pinned-icon {
        font-size: 1.05rem;
        line-height: 1.2;
        flex-shrink: 0;
    }

    .pinned-body {
        flex: 1;
        min-inline-size: 0;
    }

    .pinned-label {
        display: block;
        font-size: 0.6875rem;
        font-weight: 700;
        text-transform: uppercase;
        color: #b45309;
        letter-spacing: 0.04em;
        margin-block-end: 2px;
    }

    .pinned-text {
        font-size: 0.875rem;
        color: var(--color-warning-text);
        font-weight: 500;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: 1.35;
    }

    .btn-unpin {
        background: none;
        border: none;
        color: #b45309;
        font-size: 1rem;
        font-weight: 700;
        cursor: pointer;
        padding: 4px 8px;
        border-radius: 6px;
        flex-shrink: 0;
    }
    .btn-unpin:hover:not(:disabled) {
        background-color: #fef3c7;
        color: #78350f;
    }

    /* ======================================================================
       One-Time Permission Prompt Bar
       ====================================================================== */
    .permission-prompt-bar {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 8px 12px;
        padding: 9px 16px;
        background-color: var(--color-primary-soft);
        border-bottom: 1px solid var(--color-primary-border);
        font-size: 0.8125rem;
        color: var(--text-primary);
        flex-shrink: 0;
    }

    .perm-info-text {
        font-weight: 500;
        color: var(--text-secondary);
    }

    /* ======================================================================
       Messages Stream
       ====================================================================== */
    .chat-box {
        flex: 1 1 auto;
        min-block-size: 0;
        overflow-y: auto;
        scrollbar-gutter: stable;
        overscroll-behavior: contain;
        padding: 18px 16px;
        background: radial-gradient(circle at top left, #f8fafc 0%, #eef2f6 100%);
        scroll-behavior: smooth;
    }

    .messages-container {
        display: flex;
        flex-direction: column;
        gap: 14px;
        min-block-size: 100%;
        justify-content: flex-end;
    }

    .msg-row {
        display: flex;
        flex-direction: column;
        max-inline-size: 82%;
        animation: msgSlideIn 0.18s ease-out;
    }

    @keyframes msgSlideIn {
        from { opacity: 0; transform: translateY(6px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .msg-row.msg-me {
        align-self: flex-end;
        align-items: flex-end;
    }

    .msg-row.msg-other {
        align-self: flex-start;
        align-items: flex-start;
    }

    .msg-meta {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        font-size: 0.72rem;
        color: var(--text-muted);
        margin-block-end: 4px;
        padding-inline: 6px;
        font-weight: 500;
    }

    .msg-tick {
        font-size: 0.74rem;
        font-weight: 700;
        color: #94a3b8;
        letter-spacing: -1px;
    }
    .msg-edited-tag {
        font-size: 0.6875rem;
        font-style: italic;
        color: var(--text-muted);
        opacity: 0.9;
    }
    .msg-tick.is-delivered {
        color: #64748b;
    }
    .msg-tick.is-seen {
        color: #10b981;
    }

    .msg-row.msg-highlight-pulse .msg-bubble {
        box-shadow: 0 0 0 3px #f59e0b, var(--shadow-md);
    }

    .reply-quote-box {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
        inline-size: 100%;
        text-align: start;
        padding: 6px 10px;
        margin-block-end: 6px;
        border: none;
        border-inline-start: 3px solid rgba(255, 255, 255, 0.75);
        background-color: rgba(255, 255, 255, 0.16);
        border-radius: 8px;
        color: inherit;
        cursor: pointer;
        font-size: 0.8rem;
        line-height: 1.3;
    }
    .msg-row.msg-other .reply-quote-box {
        border-inline-start-color: var(--color-primary);
        background-color: var(--surface-subtle);
    }
    .reply-quote-sender {
        font-weight: 700;
        font-size: 0.72rem;
        opacity: 0.9;
    }
    .reply-quote-snippet {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-inline-size: 240px;
        opacity: 0.92;
    }

    .msg-bubble-wrap {
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .msg-row.msg-me .msg-bubble-wrap {
        flex-direction: row-reverse;
    }

    .msg-bubble {
        padding: 10px 15px;
        font-size: 0.9375rem;
        line-height: 1.45;
        word-break: break-word;
        white-space: pre-wrap;
        box-shadow: var(--shadow-sm);
    }

    .msg-row.msg-me .msg-bubble {
        background: var(--color-accent-gradient);
        color: #ffffff;
        border-radius: 18px 18px 4px 18px;
    }

    .msg-row.msg-other .msg-bubble {
        background-color: var(--surface-card);
        color: var(--text-primary);
        border: 1px solid var(--border-subtle);
        border-radius: 18px 18px 18px 4px;
    }

    .msg-actions {
        display: flex;
        align-items: center;
        gap: 2px;
        flex-shrink: 0;
    }

    .btn-pin-msg,
    .btn-reply-msg,
    .btn-edit-msg,
    .btn-delete-msg {
        opacity: 0;
        background: none;
        border: none;
        font-size: 0.85rem;
        cursor: pointer;
        padding: 5px;
        border-radius: 6px;
        color: var(--text-muted);
        transition: opacity 0.15s ease, background-color 0.15s ease;
        line-height: 1;
    }

    .msg-row:hover .btn-pin-msg,
    .msg-row:hover .btn-reply-msg,
    .msg-row:hover .btn-edit-msg,
    .msg-row:hover .btn-delete-msg,
    .btn-pin-msg:focus-visible,
    .btn-reply-msg:focus-visible,
    .btn-edit-msg:focus-visible,
    .btn-delete-msg:focus-visible {
        opacity: 1;
    }

    .btn-pin-msg:hover:not(:disabled),
    .btn-reply-msg:hover:not(:disabled),
    .btn-edit-msg:hover:not(:disabled),
    .btn-delete-msg:hover:not(:disabled) {
        color: var(--color-primary);
        background-color: rgba(255, 255, 255, 0.9);
    }

    .btn-pin-msg:hover:not(:disabled) {
        color: #f59e0b;
    }

    .btn-delete-msg:hover:not(:disabled) {
        color: #dc2626;
        background-color: var(--color-danger-bg);
    }

    .btn-pin-msg.is-pinned {
        opacity: 1;
        color: #f59e0b;
    }

    .msg-edit-form {
        display: flex;
        flex-direction: column;
        gap: 8px;
        min-inline-size: min(260px, 68vw);
        margin-block-start: 4px;
    }

    .msg-edit-input {
        inline-size: 100%;
        padding: 8px 10px;
        border-radius: 10px;
        border: 1px solid rgba(15, 23, 42, 0.18);
        background-color: #ffffff;
        color: var(--text-primary);
        font: inherit;
        font-size: 0.875rem;
        line-height: 1.4;
        resize: vertical;
        min-block-size: 56px;
    }

    .msg-edit-input:focus {
        outline: 2px solid #93c5fd;
        border-color: var(--color-primary);
    }

    .msg-edit-controls {
        display: flex;
        justify-content: flex-end;
        gap: 6px;
    }

    .btn-edit-cancel,
    .btn-edit-save {
        appearance: none;
        border-radius: 8px;
        padding: 4px 11px;
        font: inherit;
        font-size: 0.78rem;
        font-weight: 600;
        cursor: pointer;
    }

    .btn-edit-cancel {
        background-color: rgba(255, 255, 255, 0.85);
        color: var(--text-secondary);
        border: 1px solid var(--border-subtle);
    }

    .btn-edit-save {
        background-color: #0f172a;
        color: #ffffff;
        border: 1px solid #0f172a;
    }

    .msg-row.msg-other .btn-edit-save {
        background-color: var(--color-primary);
        border-color: var(--color-primary);
    }

    .btn-edit-cancel:hover:not(:disabled),
    .btn-edit-save:hover:not(:disabled) {
        filter: brightness(0.94);
    }

    /* User 1 View-Once Media Tag */
    .media-tag {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        font-weight: 600;
        font-size: 0.9375rem;
        padding: 2px 6px;
        background-color: rgba(255, 255, 255, 0.16);
        border-radius: var(--radius-sm);
    }

    .media-caption {
        margin-block-start: 6px;
        font-size: 0.875rem;
        line-height: 1.4;
        border-top: 1px solid rgba(255, 255, 255, 0.22);
        padding-block-start: 5px;
    }

    .msg-row.msg-other .media-caption {
        border-top-color: var(--border-subtle);
        color: var(--text-secondary);
    }

    .chat-media-img {
        max-inline-size: min(260px, 65vw);
        max-block-size: 280px;
        inline-size: auto;
        block-size: auto;
        border-radius: 10px;
        cursor: pointer;
        object-fit: contain;
    }

    .chat-media-video {
        max-inline-size: min(280px, 70vw);
        max-block-size: 280px;
        border-radius: 10px;
        background: #000000;
    }

    .chat-media-gif {
        border-radius: 10px;
    }

    /* ======================================================================
       Bottom Composer, Inline Error Banner & Progress Bar
       ====================================================================== */
    .chat-footer {
        background-color: var(--surface-card);
        border-top: 1px solid var(--border-subtle);
        padding: 12px 16px max(12px, env(safe-area-inset-bottom));
        flex-shrink: 0;
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .typing-indicator-bar {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 4px 10px;
        font-size: 0.78rem;
        font-weight: 600;
        color: var(--color-primary);
        background-color: var(--color-primary-soft);
        border: 1px solid var(--color-primary-border);
        border-radius: var(--radius-pill);
        align-self: flex-start;
    }

    .typing-dots {
        display: inline-flex;
        align-items: center;
        gap: 3px;
    }

    .typing-dots span {
        inline-size: 5px;
        block-size: 5px;
        border-radius: 50%;
        background-color: var(--color-primary);
        animation: typingBounce 1.1s infinite ease-in-out;
    }

    .typing-dots span:nth-child(2) {
        animation-delay: 0.16s;
    }

    .typing-dots span:nth-child(3) {
        animation-delay: 0.32s;
    }

    @keyframes typingBounce {
        0%, 80%, 100% {
            transform: translateY(0);
            opacity: 0.45;
        }
        40% {
            transform: translateY(-3px);
            opacity: 1;
        }
    }

    .reply-preview-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        padding: 8px 12px;
        background-color: var(--surface-subtle);
        border-inline-start: 3px solid var(--color-primary);
        border-radius: var(--radius-sm);
    }

    .reply-preview-content {
        min-inline-size: 0;
        flex: 1;
    }

    .reply-preview-label {
        display: block;
        font-size: 0.72rem;
        font-weight: 700;
        color: var(--color-primary);
    }

    .reply-preview-text {
        font-size: 0.8125rem;
        color: var(--text-secondary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .reply-cancel-btn {
        background: transparent;
        border: none;
        color: var(--text-muted);
        font-size: 0.95rem;
        font-weight: 700;
        cursor: pointer;
        padding: 4px 8px;
        border-radius: 6px;
    }
    .reply-cancel-btn:hover:not(:disabled) {
        background-color: var(--border-subtle);
        color: var(--text-primary);
    }

    .media-error-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        padding: 8px 12px;
        background-color: var(--color-danger-bg);
        border: 1px solid #fecaca;
        color: var(--color-danger-text);
        border-radius: var(--radius-sm);
        font-size: 0.8125rem;
        font-weight: 500;
    }

    .media-err-close {
        background: transparent;
        border: none;
        color: var(--color-danger-text);
        font-weight: 700;
        cursor: pointer;
        padding: 2px 6px;
    }

    .media-upload-bar {
        display: flex;
        flex-direction: column;
        gap: 6px;
        background-color: var(--color-primary-soft);
        border: 1px solid var(--color-primary-border);
        padding: 8px 12px;
        border-radius: var(--radius-md);
    }

    .upload-bar-header {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 0.8125rem;
        color: var(--color-primary);
        font-weight: 600;
    }

    .upload-percent {
        margin-inline-start: auto;
        font-variant-numeric: tabular-nums;
    }

    .upload-progress-track {
        inline-size: 100%;
        block-size: 6px;
        background-color: #dbeafe;
        border-radius: var(--radius-pill);
        overflow: hidden;
    }

    .upload-progress-fill {
        block-size: 100%;
        background: var(--color-accent-gradient);
        border-radius: var(--radius-pill);
        transition: width 0.15s ease;
    }

    .chat-input-bar {
        display: flex;
        gap: 8px;
        align-items: center;
    }

    .chat-media-actions {
        display: flex;
        align-items: center;
        gap: 6px;
        flex-shrink: 0;
    }

    .chat-icon-btn {
        background: var(--surface-subtle);
        border: 1px solid var(--border-subtle);
        border-radius: 50%;
        inline-size: 38px;
        block-size: 38px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.15rem;
        cursor: pointer;
        transition: background-color 0.15s ease, transform 0.1s ease;
        line-height: 1;
    }
    .chat-icon-btn:hover:not(:disabled) {
        background-color: var(--border-subtle);
        transform: scale(1.05);
    }

    .chat-text-input {
        flex: 1 1 auto;
        min-inline-size: 0;
        padding: 10px 16px;
        border: 1px solid var(--border-strong);
        border-radius: var(--radius-pill);
        outline: none;
        font-size: 0.9375rem;
        background-color: var(--surface-page);
        transition: border-color 0.15s ease, background-color 0.15s ease;
    }
    .chat-text-input:focus {
        border-color: var(--color-primary);
        background-color: var(--surface-card);
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    }

    .chat-send-btn {
        background: var(--color-accent-gradient);
        color: #ffffff;
        border: none;
        border-radius: var(--radius-pill);
        padding: 10px 20px;
        font-weight: 600;
        font-size: 0.9375rem;
        cursor: pointer;
        flex-shrink: 0;
        transition: filter 0.15s ease, transform 0.1s ease;
    }
    .chat-send-btn:hover:not(:disabled) {
        filter: brightness(1.08);
    }
    .chat-send-btn:disabled {
        background: #94a3b8;
        cursor: not-allowed;
    }

    /* ======================================================================
       Camera Choice Popover
       ====================================================================== */
    .camera-menu-wrapper {
        position: relative;
        display: inline-block;
    }

    .camera-choice-popover {
        position: absolute;
        bottom: calc(100% + 10px);
        left: 0;
        background-color: #1e293b;
        border: 1px solid #334155;
        border-radius: 14px;
        box-shadow: 0 18px 34px rgba(0, 0, 0, 0.4);
        padding: 6px;
        display: flex;
        flex-direction: column;
        gap: 3px;
        min-inline-size: 250px;
        z-index: 60;
        animation: popoverFadeIn 0.15s ease-out;
    }

    @keyframes popoverFadeIn {
        from { opacity: 0; transform: translateY(6px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .cam-choice-item {
        display: flex;
        align-items: center;
        gap: 10px;
        background: transparent;
        border: none;
        color: #f8fafc;
        padding: 10px 14px;
        border-radius: 10px;
        cursor: pointer;
        font-size: 0.875rem;
        font-weight: 500;
        text-align: start;
        transition: background-color 0.15s ease;
        inline-size: 100%;
    }
    .cam-choice-item:hover:not(:disabled) {
        background-color: #334155;
    }
    .cam-choice-item .choice-icon {
        font-size: 1.2rem;
        line-height: 1;
    }
    .cam-choice-item .choice-text {
        flex: 1;
        white-space: nowrap;
    }

    /* ======================================================================
       Native <dialog> Modals (Login & Live Camera)
       ====================================================================== */
    dialog.app-dialog {
        margin: auto;
        padding: 0;
        border: none;
        background: transparent;
        max-inline-size: min(460px, calc(100vw - 24px));
        inline-size: 100%;
        overflow: visible;
    }

    dialog.login-dialog,
    dialog.delete-dialog {
        max-inline-size: min(400px, calc(100vw - 24px));
    }

    .delete-card {
        padding: 22px 22px;
    }

    .delete-header {
        margin-block-end: 18px;
    }

    .delete-title {
        font-size: 1.125rem;
        font-weight: 700;
        color: var(--text-primary);
        margin-block-end: 6px;
    }

    .delete-desc {
        font-size: 0.875rem;
        color: var(--text-secondary);
        line-height: 1.45;
    }

    .delete-preview-box {
        margin-block-start: 12px;
        padding: 9px 12px;
        border-radius: var(--radius-sm);
        background-color: var(--surface-subtle);
        border-inline-start: 3px solid var(--color-danger-solid);
        font-size: 0.825rem;
        color: var(--text-primary);
        word-break: break-word;
    }

    .delete-actions {
        display: flex;
        justify-content: flex-end;
        gap: 8px;
    }

    dialog.app-dialog[open] {
        display: flex;
        flex-direction: column;
    }

    dialog.app-dialog:modal {
        overscroll-behavior: contain;
    }

    dialog.app-dialog::backdrop {
        background-color: rgba(15, 23, 42, 0.55);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
        overflow: hidden;
        overscroll-behavior: contain;
    }

    .login-card {
        background: var(--surface-card);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-lg);
        padding: 28px 24px;
        inline-size: 100%;
        box-shadow: var(--shadow-lg);
    }

    .login-header {
        text-align: center;
        margin-block-end: 20px;
    }

    .sub-badge {
        display: inline-block;
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        color: var(--color-primary);
        background: var(--color-primary-soft);
        padding: 3px 10px;
        border-radius: var(--radius-pill);
        letter-spacing: 0.05em;
        margin-block-end: 8px;
    }

    .login-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: var(--text-primary);
        margin-block-end: 6px;
    }

    .login-desc {
        font-size: 0.875rem;
        color: var(--text-muted);
    }

    .form-group {
        margin-block-end: 16px;
    }

    .form-label {
        display: block;
        font-size: 0.875rem;
        font-weight: 600;
        color: var(--text-secondary);
        margin-block-end: 6px;
    }

    .form-input {
        inline-size: 100%;
        padding: 10px 14px;
        border: 1px solid var(--border-strong);
        border-radius: var(--radius-sm);
        background-color: var(--surface-page);
        color: var(--text-primary);
        outline: none;
        transition: border-color 0.15s ease;
    }
    .form-input:focus {
        border-color: var(--color-primary);
        background-color: var(--surface-card);
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    }

    .alert {
        padding: 10px 12px;
        border-radius: var(--radius-sm);
        font-size: 0.875rem;
        margin-block-end: 16px;
    }

    .alert-error {
        background-color: var(--color-danger-bg);
        border: 1px solid #fecaca;
        color: var(--color-danger-text);
    }

    /* Live Camera Card inside <dialog> */
    .camera-card {
        background-color: #0f172a;
        color: #ffffff;
        border-radius: var(--radius-lg);
        inline-size: 100%;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        box-shadow: var(--shadow-lg);
        border: 1px solid #334155;
    }

    .camera-top-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 12px 16px;
        background-color: #1e293b;
        border-bottom: 1px solid #334155;
    }

    .camera-title {
        font-size: 0.95rem;
        font-weight: 600;
    }

    .camera-timer {
        font-size: 0.875rem;
        font-weight: 700;
        color: #f87171;
        background-color: rgba(239, 68, 68, 0.18);
        padding: 3px 10px;
        border-radius: 12px;
    }

    .camera-close-btn {
        background: transparent;
        border: none;
        color: #94a3b8;
        font-size: 1.25rem;
        cursor: pointer;
        line-height: 1;
        padding: 4px 8px;
        border-radius: 6px;
    }
    .camera-close-btn:hover:not(:disabled) {
        color: #ffffff;
        background: rgba(255, 255, 255, 0.1);
    }

    .camera-viewfinder-wrap {
        position: relative;
        inline-size: 100%;
        aspect-ratio: 4 / 3;
        max-block-size: 380px;
        background-color: #000000;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #camera-video-preview {
        inline-size: 100%;
        block-size: 100%;
        object-fit: cover;
    }

    .camera-fallback-msg {
        padding: 20px;
        text-align: center;
        color: #e2e8f0;
        font-size: 0.875rem;
    }

    .camera-fallback-actions {
        display: flex;
        gap: 8px;
        justify-content: center;
        margin-block-start: 14px;
        flex-wrap: wrap;
    }

    .camera-fallback-actions #btn-fallback-photo { background: #2563eb; color: #fff; }
    .camera-fallback-actions #btn-fallback-selfie { background: #d97706; color: #fff; }
    .camera-fallback-actions #btn-fallback-video { background: #dc2626; color: #fff; }
    .camera-fallback-actions #btn-fallback-video-selfie { background: #7c3aed; color: #fff; }

    .camera-controls {
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding: 14px 16px;
        background-color: #1e293b;
        border-top: 1px solid #334155;
    }

    .cam-action-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 6px;
        background: transparent;
        border: none;
        color: #e2e8f0;
        font-size: 0.75rem;
        font-weight: 500;
        cursor: pointer;
    }

    .cam-icon-circle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        inline-size: 46px;
        block-size: 46px;
        border-radius: 50%;
        background-color: #334155;
        font-size: 1.35rem;
        line-height: 1;
    }

    .cam-shutter-inner {
        inline-size: 48px;
        block-size: 48px;
        border-radius: 50%;
        background-color: #ffffff;
        border: 3px solid #cbd5e1;
        box-shadow: 0 0 0 2px #334155;
        display: block;
    }

    .cam-rec-inner {
        inline-size: 48px;
        block-size: 48px;
        border-radius: 50%;
        background-color: #ef4444;
        border: 3px solid #ffffff;
        box-shadow: 0 0 0 2px #334155;
        display: block;
        transition: border-radius 0.2s ease;
    }

    .cam-rec-inner.is-recording {
        border-radius: 8px;
        inline-size: 38px;
        block-size: 38px;
        background-color: #dc2626;
        animation: cam-pulse-red 1s infinite;
    }

    @keyframes cam-pulse-red {
        0%, 100% { opacity: 1; transform: scale(1); }
        50% { opacity: 0.72; transform: scale(0.95); }
    }
}

@layer utilities {
    @media (max-width: 640px) {
        .container {
            padding: 16px 12px 60px;
        }
        .header-inner {
            padding: 10px 12px;
        }
        .site-title {
            font-size: 1.05rem;
        }
        .quiz-badge {
            font-size: 0.7rem;
            padding: 2px 8px;
        }
        .lang-btn {
            padding: 5px 8px;
            font-size: 0.72rem;
        }
        .card {
            padding: 16px 14px;
        }
        .chat-container {
            border-inline: none;
        }
        .chat-inner-header {
            padding: 8px 12px;
        }
        .chat-header-user {
            margin-inline-start: auto;
        }
        .msg-row {
            max-inline-size: 90%;
        }
        .btn-pin-msg,
        .btn-reply-msg,
        .btn-edit-msg,
        .btn-delete-msg {
            opacity: 0.78;
        }
        .chat-footer {
            padding: 10px 10px max(10px, env(safe-area-inset-bottom));
        }
        .chat-text-input {
            padding: 9px 12px;
            font-size: 0.875rem;
        }
        .chat-send-btn {
            padding: 9px 15px;
            font-size: 0.875rem;
        }
    }

    @media (prefers-reduced-motion: reduce) {
        .btn,
        .card,
        .option-btn,
        .chat-back-btn,
        .btn-pin-msg,
        .chat-icon-btn,
        .chat-text-input,
        .chat-send-btn,
        .form-input,
        .msg-row,
        .camera-choice-popover,
        .cam-rec-inner,
        .upload-progress-fill,
        .typing-status-pill,
        .typing-dots span {
            animation-duration: 0.01ms !important;
            transition-duration: 0.01ms !important;
        }
    }
}
