:root {
    --primary: #4f46e5;
    --primary-dark: #3730a3;
    --primary-light: #eef0ff;
    --ink: #1e1b2e;
    --muted: #6b7280;
    --bg: #f7f7fb;
    --card: #ffffff;
    --border: #e5e7eb;
    --danger: #dc2626;
    --success: #16a34a;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 4px 20px rgba(31, 27, 61, 0.08);
    --shadow-sm: 0 1px 3px rgba(31, 27, 61, 0.06);
    --shadow-hover: 0 8px 28px rgba(31, 27, 61, 0.12);

    /* Fluid type scale — scales continuously with viewport width instead of
       jumping at fixed breakpoints, so UI chrome text (not paper content,
       which uses --paper-font/--base-font-size from Print & Page Settings)
       genuinely follows screen width. */
    --font-size-sm: clamp(12px, 0.3vw + 11px, 13px);
    --font-size-base: clamp(14px, 0.4vw + 13px, 16px);
    --font-size-lg: clamp(16px, 0.6vw + 14px, 19px);
    --font-size-xl: clamp(19px, 1vw + 16px, 24px);
    --font-size-2xl: clamp(24px, 1.8vw + 18px, 34px);

    /* Spacing scale for consistent rhythm across redesigned pages. */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;

    /* Single breakpoint used consistently by the redesigned dashboard/editor
       (mobile-first: styles apply below this, desktop overrides above it). */
    --breakpoint-desktop: 768px;
}

* { box-sizing: border-box; }

html { font-size: 16px; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    font-size: var(--font-size-base);
    color: var(--ink);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    font-size: 15px;
    font-weight: 600;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    transition: transform .12s ease, box-shadow .12s ease, background .15s ease;
    text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--ink); border: 1px solid var(--border); }
.btn-ghost:hover { background: #f1f1f6; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-lg { padding: 14px 28px; font-size: 17px; }
.btn-block { width: 100%; margin-top: 8px; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--ink);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    text-decoration: none;
    box-shadow: var(--shadow-sm);
    transition: background .15s ease, transform .12s ease, box-shadow .12s ease;
}
.icon-btn:hover { box-shadow: var(--shadow-hover); }
.icon-btn:hover { background: #f1f1f6; }
.icon-btn:active { transform: scale(0.94); }
.icon-btn.danger:hover { background: #fee2e2; }

/* ---------- Landing ---------- */
.nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    max-width: 1100px;
    margin: 0 auto;
}
.nav-brand { font-size: 20px; font-weight: 800; }
.nav-brand span { color: var(--primary); }

.hero {
    text-align: center;
    padding: 60px 20px 40px;
    background: linear-gradient(180deg, #eef0ff 0%, var(--bg) 100%);
}
.hero-content { max-width: 640px; margin: 0 auto; }
.hero h1 { font-size: 32px; line-height: 1.25; margin: 0 0 16px; }
.hero p { color: var(--muted); font-size: 16px; margin-bottom: 28px; }

.features {
    max-width: 1100px;
    margin: 0 auto;
    padding: 30px 20px 60px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 18px;
}
@media (min-width: 720px) {
    .hero h1 { font-size: 42px; }
    .features { grid-template-columns: repeat(4, 1fr); }
}
.feature {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px;
    text-align: center;
}
.feature-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    border-radius: 14px;
    background: var(--primary-light);
    color: var(--primary);
    margin-bottom: 14px;
}
.feature h3 { margin: 0 0 8px; font-size: 16px; }
.feature p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.5; }

.landing-footer { text-align: center; padding: 24px; color: var(--muted); font-size: 13px; }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(20, 18, 40, 0.5);
    display: none;
    align-items: flex-end;
    justify-content: center;
    z-index: 1000;
    padding: 0;
}
.modal-overlay.open { display: flex; }
.modal {
    background: var(--card);
    width: 100%;
    max-width: 420px;
    border-radius: 20px 20px 0 0;
    padding: 28px 24px 32px;
    position: relative;
    box-shadow: var(--shadow);
    max-height: 90vh;
    overflow-y: auto;
}
@media (min-width: 560px) {
    .modal-overlay { align-items: center; }
    .modal { border-radius: var(--radius); }
}
.modal h2 { margin: 0 0 6px; font-size: 20px; }
.modal-sub { color: var(--muted); font-size: 14px; margin: 0 0 18px; }
.modal-close {
    position: absolute;
    top: 14px; right: 16px;
    border: none; background: none;
    font-size: 22px; line-height: 1; cursor: pointer;
    color: var(--muted);
}
.modal-panel label { display: block; font-size: 13px; font-weight: 600; margin: 14px 0 6px; }
.modal-panel input, .modal-panel select, .modal-panel textarea {
    width: 100%;
    padding: 11px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    background: #fff;
}
.modal-panel input:focus, .modal-panel select:focus, .modal-panel textarea:focus {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }
.form-actions { display: flex; gap: 10px; margin-top: 8px; }
.form-actions .btn { flex: 1; }

/* ---------- Shared app chrome (dashboard/setup/editor) ---------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: #ffffff;
    border-bottom: 1px solid #e2e8f0;
    position: sticky;
    top: 0;
    z-index: 50;
    box-shadow: 0 1px 3px rgba(0,0,0,0.02);
}
.topbar-title {
    flex: 1;
    min-width: 0;
    font-weight: 800;
    font-size: 18px;
    letter-spacing: -0.025em;
    color: #0f172a;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.container { max-width: 900px; margin: 0 auto; padding: 24px 20px; }

.card {
    background: var(--card);
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.02), 0 1px 2px rgba(0,0,0,0.03);
    margin-bottom: 20px;
}
.card h3 {
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 16px;
    font-weight: 700;
    color: #0f172a;
    border-bottom: 1px solid #f1f5f9;
    padding-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.card-h-icon { color: var(--primary); flex-shrink: 0; }
.settings-tabs { max-width: 640px; }
.card label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin-bottom: 6px;
    margin-top: 15px;
}
.card label:first-of-type {
    margin-top: 0;
}
.card input[type="text"],
.card input[type="password"],
.card input[type="number"],
.card select,
.card textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-size: 14px;
    background: #ffffff;
    outline: none;
    transition: all 0.2s ease;
}
.card input:focus,
.card select:focus,
.card textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

.search-row { display: flex; gap: 10px; margin-bottom: 18px; align-items: center; flex-wrap: wrap; }
.search-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 160px;
}
.search-row-icon { position: absolute; left: 13px; color: var(--muted); pointer-events: none; display: flex; }
.search-input-wrap input {
    width: 100%;
    padding: 11px 14px 11px 40px;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    font-size: 15px;
    outline: none;
    transition: all 0.2s ease;
    margin: 0;
}
.search-input-wrap input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}
.sort-control {
    position: relative;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}
.sort-control-icon { position: absolute; left: 12px; color: var(--muted); pointer-events: none; display: flex; }
.sort-control select {
    width: auto;
    padding: 10px 14px 10px 36px;
    border-radius: 8px;
    border: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    background: #ffffff;
}
.load-more-row { text-align: center; margin-top: 15px; }
.load-more-row .btn { padding: 10px 20px; }

/* ---------- Dashboard segmented tabs ---------- */
.dash-tabs {
    display: flex;
    background: #e2e8f0;
    padding: 4px;
    border-radius: 10px;
    max-width: 480px;
    margin: 20px auto 0;
    gap: 2px;
}
.dash-tab-btn {
    flex: 1;
    text-align: center;
    justify-content: center;
    border: none;
    background: transparent;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    color: #475569;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}
.dash-tab-btn:hover { color: #0f172a; }
.dash-tab-btn.active { color: #0f172a; background: #ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.dash-tab-panel { }

/* Inner toggle inside the "Shared" panel */
.dash-subtabs {
    display: inline-flex;
    background: #e2e8f0;
    padding: 3px;
    border-radius: 8px;
    margin-bottom: 16px;
    gap: 2px;
}
.dash-subtab-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    border: none;
    background: transparent;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    color: #475569;
    cursor: pointer;
    transition: all 0.2s ease;
}
.dash-subtab-btn:hover { color: #0f172a; }
.dash-subtab-btn.active { color: #0f172a; background: #ffffff; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }

.unread-badge {
    background: var(--danger);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    border-radius: 999px;
    padding: 1px 7px;
    min-width: 18px;
    text-align: center;
    line-height: 1.5;
}

/* ---------- Comments feed (dashboard "Comments" tab) ---------- */
.comments-feed { display: flex; flex-direction: column; gap: 12px; }
.comment-feed-item {
    display: block;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 16px 20px;
    text-decoration: none;
    color: #0f172a;
    transition: all 0.2s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.02);
    position: relative;
    overflow: hidden;
}
.comment-feed-item:hover {
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.05);
    transform: translateY(-1px);
}
.comment-feed-item.unread { border-color: var(--primary); background: #fcfcff; }
.comment-feed-item.unread::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: var(--primary);
}
.comment-feed-meta { font-size: 13px; color: #334155; margin-bottom: 6px; }
.comment-feed-meta strong { color: #0f172a; }
.comment-feed-date { color: #94a3b8; font-weight: 400; margin-left: 6px; font-size: 11px; }
.comment-feed-body { font-size: 13px; color: #475569; overflow-wrap: break-word; line-height: 1.4; }

/* ---------- Paper list (dashboard "My Questions" tab) ---------- */
.paper-list { display: flex; flex-direction: column; gap: 12px; }
.paper-item {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    gap: 15px;
    transition: all 0.2s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.02);
}
.paper-item:hover {
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.05);
    transform: translateY(-1px);
}
.paper-item-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: #f1f5f9;
    color: #4f46e5;
    border-radius: 8px;
    flex-shrink: 0;
}
.paper-item-main { min-width: 0; flex: 1; }
.paper-item-main h4 { margin: 0 0 6px; font-size: 15px; font-weight: 700; color: #0f172a; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.paper-item-main .paper-item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 11px; color: #64748b; }
.meta-tag {
    background: #f1f5f9;
    color: #475569;
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 600;
}
.meta-tag.lang {
    background: #eef0ff;
    color: #4f46e5;
}
.meta-date {
    display: inline-flex;
    align-items: center;
}
.paper-item-actions { display: flex; gap: 6px; flex-shrink: 0; align-items: center; }

.perm-badge { display: inline-flex; align-items: center; font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 999px; text-transform: uppercase; letter-spacing: 0.05em; }
.perm-badge.private { background: #f1f5f9; color: #64748b; }
.perm-badge.view { background: #e0e7ff; color: #4f46e5; }
.perm-badge.copy { background: #dcfce7; color: #15803d; }
.perm-badge.edit { background: #fef3c7; color: #b45309; }
.perm-badge.neutral { background: #f1f5f9; color: #64748b; }

.empty-state { text-align: center; padding: 50px 20px; color: var(--muted); }
.empty-state-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    margin-bottom: 16px;
}
.empty-state p { max-width: 320px; margin: 0 auto; line-height: 1.5; }

.fab {
    position: fixed;
    bottom: 24px; right: 24px;
    width: 56px; height: 56px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: 28px;
    display: flex; align-items: center; justify-content: center;
    box-shadow: var(--shadow);
    border: none; cursor: pointer;
    z-index: 40;
    transition: transform 0.2s ease, background 0.2s ease;
}
.fab:hover {
    background: var(--primary-dark);
    transform: scale(1.05);
}

.toast-stack {
    position: fixed;
    bottom: 20px; left: 50%;
    transform: translateX(-50%);
    z-index: 2000;
    display: flex; flex-direction: column; gap: 8px;
    align-items: center;
}
.toast {
    background: var(--ink);
    color: #fff;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 14px;
    opacity: 0;
    transform: translateY(10px);
    transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }

/* ---------- Math Equation Formatting ---------- */
.math-frac {
    display: inline-flex;
    flex-direction: column;
    vertical-align: middle;
    text-align: center;
    font-size: 0.85em;
    padding: 0 4px;
    line-height: 1.1;
}
.math-num {
    border-bottom: 1px solid currentColor;
    padding-bottom: 1px;
}
.math-den {
    padding-top: 1px;
}
.math-sqrt {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    line-height: 1;
}
.math-radical {
    font-size: 1.1em;
    margin-right: 1px;
}
.math-radicand {
    border-top: 1px solid currentColor;
    padding-top: 1px;
    margin-left: -2px;
}
.math-subsup {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
}
.math-base {
    line-height: 1;
}
.math-scripts {
    display: inline-flex;
    flex-direction: column;
    font-size: 0.7em;
    line-height: 1;
    margin-left: 2px;
    vertical-align: middle;
}
.math-sup {
    margin-bottom: 1px;
}
.math-sub {
    margin-top: 1px;
}

/* Standalone & Inline Watermark Behind Text styling */
.el-image-item.img-behind {
    position: absolute;
    z-index: -1;
    margin: 0;
}
.el-image-item.img-behind.align-center {
    left: 50%;
    transform: translateX(-50%);
}
.el-image-item.img-behind.align-left {
    left: 0;
    transform: none;
}
.el-image-item.img-behind.align-right {
    left: auto;
    right: 0;
    transform: none;
}
.el-image-item.img-behind img {
    height: calc(var(--img-lines, 2) * 1.4 * var(--base-font-size, 13px) * 6) !important;
    width: auto;
    object-fit: contain;
    opacity: 1 !important;
}
.richtext-editable, .compact-preview-text, .el-card, .el-question, .el-mcq, .el-passage, .el-instruction, .el-image {
    position: relative;
    overflow: hidden;
}
.paper-page-inner, .paper-body, .el-card, .el-question, .el-mcq, .el-passage, .el-instruction, .el-image {
    isolation: isolate;
}

/* Clearfix for wrapping floated tables and images */
.richtext-editable::after,
.q-text::after,
.passage-text::after,
.q-main::after,
.el-passage::after,
.el-mcq::after,
.el-question::after {
    content: "";
    display: table;
    clear: both;
}

/* Margin reset for inline/nested paragraph and div nodes */
.paper-body p, .paper-body div, .richtext-editable p, .richtext-editable div {
    margin: 0;
    padding: 0;
}

/* Browser-side Indic and complex script ligatures shaper optimization */
body, select, input, textarea, .richtext-editable, .paper-page-inner {
    text-rendering: optimizeLegibility;
    font-variant-ligatures: normal;
    font-feature-settings: "liga" on, "clig" on, "kern" on;
}
