:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #1d2033;
  --muted: #74788f;
  --line: #e8e9f1;
  --primary: #6657e8;
  --primary-soft: #efedff;
  --green: #1f9d72;
  --green-soft: #e9f8f2;
  --red: #dc5b69;
  --red-soft: #fff0f1;
  --shadow: 0 14px 40px rgba(32, 35, 68, 0.07);
  --radius: 20px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; min-width: 320px; background: var(--bg); color: var(--text); font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.app-shell { min-height: 100vh; }

.sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; display: flex; width: 245px; flex-direction: column; padding: 28px 20px; border-right: 1px solid #ebecf3; background: rgba(255, 255, 255, 0.96); }
.brand { display: flex; align-items: center; gap: 11px; padding: 0 10px; color: var(--text); font-size: 20px; font-weight: 760; letter-spacing: -0.4px; text-decoration: none; }
.brand-mark { display: grid; width: 38px; height: 38px; place-items: center; border-radius: 12px; background: linear-gradient(145deg, #7869f3, #5848d9); color: white; box-shadow: 0 7px 18px rgba(102, 87, 232, 0.28); }
.brand-mark svg { width: 23px; height: 23px; }
.side-nav { display: grid; gap: 7px; margin-top: 42px; }
.nav-item { display: flex; align-items: center; gap: 13px; padding: 12px 14px; border-radius: 12px; color: #75798f; font-size: 14px; font-weight: 650; text-decoration: none; transition: 160ms ease; }
.nav-item:hover { background: #f6f5ff; color: var(--primary); transform: translateX(2px); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); }
.nav-item svg { width: 19px; height: 19px; }
.sidebar-tip { display: flex; gap: 10px; margin-top: auto; padding: 16px; border: 1px solid #e5e1ff; border-radius: 16px; background: linear-gradient(145deg, #f9f8ff, #f1efff); }
.tip-icon { color: var(--primary); font-size: 18px; }
.sidebar-tip strong { font-size: 13px; }
.sidebar-tip p { margin: 5px 0 0; color: var(--muted); font-size: 11px; line-height: 1.5; }

.main-content { width: calc(100% - 245px); max-width: 1480px; margin-left: 245px; padding: 38px 48px 64px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 24px; margin-bottom: 30px; }
.eyebrow { margin: 0 0 6px; color: var(--primary); font-size: 11px; font-weight: 750; letter-spacing: 1.15px; text-transform: uppercase; }
.topbar h1 { margin: 0; font-size: clamp(24px, 3vw, 34px); line-height: 1.18; letter-spacing: -1.1px; }
.profile-chip { display: flex; align-items: center; gap: 11px; padding: 8px 13px 8px 8px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.avatar { display: grid; width: 36px; height: 36px; place-items: center; border-radius: 11px; background: #1f2337; color: white; font-size: 13px; font-weight: 750; }
.profile-copy { display: grid; gap: 2px; }
.profile-copy strong { font-size: 12px; }
.profile-copy small { color: var(--muted); font-size: 10px; }
.workspace-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(290px, 0.75fr); gap: 22px; align-items: start; }
.panel { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.composer-panel { padding: 30px; }
.panel-heading { display: flex; gap: 14px; margin-bottom: 27px; }
.heading-icon { display: grid; flex: 0 0 44px; width: 44px; height: 44px; place-items: center; border-radius: 13px; }
.heading-icon.purple { background: var(--primary-soft); color: var(--primary); }
.heading-icon svg { width: 23px; height: 23px; }
.panel h2 { margin: 0; font-size: 20px; letter-spacing: -0.35px; }
.panel-description { margin: 7px 0 0; color: var(--muted); font-size: 13px; line-height: 1.55; }

.form-group { display: grid; gap: 9px; margin-top: 20px; }
.form-group label { color: #3d4158; font-size: 12px; font-weight: 700; }
.label-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.field-hint { color: #a0a3b3; font-size: 10px; }
input, textarea { width: 100%; border: 1px solid #dedfe9; outline: 0; background: #fcfcfe; color: var(--text); transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease; }
input { height: 48px; padding: 0 15px; border-radius: 12px; }
textarea { min-height: 220px; padding: 15px; border-radius: 14px; line-height: 1.6; resize: vertical; }
input::placeholder, textarea::placeholder { color: #a7a9b8; }
input:hover, textarea:hover { border-color: #c8c9d8; }
input:focus, textarea:focus { border-color: var(--primary); background: white; box-shadow: 0 0 0 4px rgba(102, 87, 232, 0.1); }
.primary-button { display: flex; width: 100%; min-height: 50px; align-items: center; justify-content: center; gap: 10px; margin-top: 22px; padding: 12px 18px; border: 0; border-radius: 13px; background: linear-gradient(135deg, #7464ee, #5b4bdd); color: white; box-shadow: 0 10px 24px rgba(91, 75, 221, 0.23); cursor: pointer; font-size: 13px; font-weight: 720; transition: transform 160ms ease, box-shadow 160ms ease, filter 160ms ease; }
.primary-button:hover { transform: translateY(-2px); box-shadow: 0 14px 28px rgba(91, 75, 221, 0.3); filter: saturate(1.08); }
.primary-button:active { transform: translateY(0); }
.primary-button:disabled { cursor: wait; opacity: 0.7; transform: none; }
.primary-button svg { width: 18px; height: 18px; }
.button-arrow { margin-left: auto; }

.library-panel { overflow: hidden; }
.library-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 25px 24px 19px; border-bottom: 1px solid var(--line); }
.live-badge { display: flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 20px; background: var(--green-soft); color: #267e61; font-size: 9px; font-weight: 750; text-transform: uppercase; }
.live-badge span { width: 6px; height: 6px; border-radius: 50%; background: var(--green); }
.study-sets-list { display: grid; gap: 10px; max-height: 560px; padding: 14px; overflow-y: auto; }
.study-set-card { position: relative; padding: 15px 42px 15px 15px; border: 1px solid transparent; border-radius: 13px; background: #fafafe; cursor: pointer; transition: 160ms ease; }
.study-set-card::after { content: "›"; position: absolute; top: 50%; right: 16px; color: #b0b2c0; font-size: 22px; transform: translateY(-52%); transition: 160ms ease; }
.study-set-card:hover { border-color: #dedaff; background: #f7f5ff; transform: translateY(-1px); }
.study-set-card:hover::after { right: 13px; color: var(--primary); }
.study-set-card h3 { margin: 0 0 7px; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.study-set-card p { margin: 0; color: var(--muted); font-size: 10px; }
.empty-state { padding: 38px 20px; color: var(--muted); text-align: center; }
.empty-state-icon { display: grid; width: 44px; height: 44px; margin: 0 auto 12px; place-items: center; border-radius: 14px; background: var(--primary-soft); color: var(--primary); font-size: 18px; }
.empty-state strong { display: block; margin-bottom: 5px; color: var(--text); font-size: 13px; }
.empty-state p { margin: 0; font-size: 11px; line-height: 1.5; }
.skeleton-card { display: grid; gap: 10px; padding: 17px 15px; border-radius: 13px; background: #fafafe; }
.skeleton-card span { height: 9px; border-radius: 8px; background: linear-gradient(90deg, #ececf3 25%, #f6f6fa 50%, #ececf3 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite; }
.skeleton-card span:last-child { width: 48%; height: 7px; }
@keyframes shimmer { to { background-position: -200% 0; } }

.quiz-output { margin-top: 22px; }
.quiz-output:empty { display: none; }
.quiz-header { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 16px; }
.quiz-header h2 { margin: 0; font-size: 22px; }
.quiz-count { padding: 7px 10px; border-radius: 20px; background: var(--primary-soft); color: var(--primary); font-size: 10px; font-weight: 750; }
.question-list { display: grid; gap: 14px; }
.question-card { padding: 24px; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); box-shadow: 0 8px 28px rgba(32, 35, 68, 0.045); }
.question-number { margin: 0 0 8px; color: var(--primary); font-size: 10px; font-weight: 760; letter-spacing: 1px; text-transform: uppercase; }
.question-text { margin: 0 0 18px; font-size: 15px; font-weight: 650; line-height: 1.55; }
.choices { display: grid; gap: 9px; }
.choice-option { display: flex; align-items: center; gap: 11px; padding: 12px 13px; border: 1px solid var(--line); border-radius: 11px; background: #fcfcfe; cursor: pointer; font-size: 12px; line-height: 1.4; transition: 150ms ease; }
.choice-option:hover { border-color: #cec9ff; background: #f9f8ff; }
.choice-option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: #4238ad; box-shadow: 0 0 0 2px rgba(102, 87, 232, 0.08); }
.choice-option input { width: 16px; height: 16px; margin: 0; accent-color: var(--primary); }
.submit-button { width: auto; min-width: 170px; margin: 18px 0 0 auto; }
.loading-state { display: flex; align-items: center; gap: 12px; padding: 20px; border: 1px solid #e3dfff; border-radius: 15px; background: var(--primary-soft); color: #5144c1; font-size: 13px; font-weight: 650; }
.spinner { width: 19px; height: 19px; border: 2px solid rgba(102, 87, 232, 0.2); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-message { padding: 13px 15px; border: 1px solid #ffd7da; border-radius: 12px; background: var(--red-soft); color: #b53f4a; font-size: 12px; }
.score-card { padding: 25px; border: 1px solid #d8f0e7; border-radius: 17px; background: linear-gradient(145deg, #f4fcf8, #ebf9f3); text-align: center; }
.score-card p { margin: 0 0 6px; color: var(--green); font-size: 10px; font-weight: 760; letter-spacing: 1px; text-transform: uppercase; }
.score-card h2 { margin: 0; font-size: 28px; }
.result-list { display: grid; gap: 12px; margin-top: 14px; }
.result-card { padding: 20px; border: 1px solid var(--line); border-left: 4px solid var(--red); border-radius: 14px; background: var(--surface); }
.result-card.correct { border-left-color: var(--green); }
.result-card h4 { margin: 0 0 12px; font-size: 13px; }
.result-card p { margin: 6px 0; color: #5e6278; font-size: 11px; line-height: 1.5; }
.result-status { display: inline-flex; margin-top: 7px; padding: 5px 8px; border-radius: 8px; background: var(--red-soft); color: var(--red); font-weight: 750; }
.correct .result-status { background: var(--green-soft); color: var(--green); }

@media (max-width: 980px) {
  .sidebar { width: 82px; padding: 26px 14px; }
  .brand { justify-content: center; padding: 0; }
  .brand > span:last-child { display: none; }
  .nav-item { justify-content: center; padding: 13px; font-size: 0; }
  .nav-item svg { width: 21px; height: 21px; }
  .sidebar-tip { display: none; }
  .main-content { width: calc(100% - 82px); margin-left: 82px; padding: 32px 28px 56px; }
}

@media (max-width: 760px) {
  .sidebar { position: static; width: 100%; height: 70px; flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 18px; border-right: 0; border-bottom: 1px solid var(--line); }
  .brand > span:last-child { display: inline; }
  .side-nav { display: flex; margin: 0; }
  .nav-item { padding: 10px; }
  .main-content { width: 100%; margin: 0; padding: 25px 18px 48px; }
  .workspace-grid { grid-template-columns: 1fr; }
  .library-panel { order: -1; }
  .study-sets-list { grid-template-columns: repeat(2, minmax(0, 1fr)); max-height: 230px; }
  .topbar { align-items: flex-start; }
  .profile-copy { display: none; }
  .profile-chip { padding: 5px; border: 0; background: transparent; }
}

@media (max-width: 520px) {
  .topbar h1 { font-size: 24px; }
  .composer-panel { padding: 22px 18px; }
  .study-sets-list { grid-template-columns: 1fr; }
  .field-hint { display: none; }
  .question-card { padding: 19px 16px; }
  .submit-button { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}

/* Authentication */
.auth-body { min-height: 100vh; overflow-x: hidden; }
.auth-page { position: relative; display: grid; min-height: 100vh; place-items: center; padding: 32px 20px; overflow: hidden; background: radial-gradient(circle at 50% 0%, #fbfaff 0, var(--bg) 45%); }
.auth-glow { position: absolute; border-radius: 50%; filter: blur(2px); pointer-events: none; }
.auth-glow-one { top: -170px; right: -120px; width: 430px; height: 430px; background: rgba(102, 87, 232, 0.1); }
.auth-glow-two { bottom: -210px; left: -160px; width: 480px; height: 480px; background: rgba(77, 198, 159, 0.08); }
.auth-card { position: relative; z-index: 1; width: min(100%, 440px); padding: 34px; border: 1px solid rgba(226, 226, 239, 0.9); border-radius: 24px; background: rgba(255, 255, 255, 0.96); box-shadow: 0 24px 70px rgba(35, 35, 70, 0.11); }
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 34px; font-size: 18px; font-weight: 760; letter-spacing: -0.35px; }
.auth-brand .brand-mark { width: 36px; height: 36px; }
.auth-brand .brand-mark svg { width: 21px; height: 21px; }
.auth-heading { margin-bottom: 27px; }
.auth-heading h1 { margin: 0; font-size: 27px; line-height: 1.2; letter-spacing: -0.8px; }
.auth-heading > p:last-child { min-height: 21px; margin: 9px 0 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.auth-form > .form-group:first-child { margin-top: 0; }
.auth-signup-fields { display: grid; max-height: 0; overflow: hidden; opacity: 0; transform: translateY(-8px); transition: max-height 320ms ease, opacity 220ms ease, transform 320ms ease; }
.auth-card.is-signup .auth-signup-fields { max-height: 105px; opacity: 1; transform: translateY(0); }
.auth-actions { display: grid; margin-top: 22px; }
.auth-submit { grid-area: 1 / 1; margin: 0; opacity: 0; pointer-events: none; transform: translateY(7px); transition: opacity 180ms ease, transform 220ms ease, box-shadow 160ms ease; }
.auth-submit.is-active { opacity: 1; pointer-events: auto; transform: translateY(0); }
.auth-submit:hover { transform: translateY(-2px); }
.auth-message { min-height: 18px; margin: 14px 0 0; color: var(--red); font-size: 12px; line-height: 1.5; text-align: center; }
.auth-message:empty { margin-top: 6px; }
.auth-switch { display: flex; align-items: center; justify-content: center; gap: 5px; margin-top: 17px; padding-top: 20px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.auth-switch button { padding: 3px 2px; border: 0; background: transparent; color: var(--primary); cursor: pointer; font-weight: 720; }
.auth-switch button:hover { color: #4f40cf; text-decoration: underline; text-underline-offset: 3px; }
.auth-switch button:focus-visible { border-radius: 4px; outline: 3px solid rgba(102, 87, 232, 0.18); }

@media (max-width: 480px) {
  .auth-page { align-items: start; padding-top: 22px; }
  .auth-card { padding: 27px 22px; border-radius: 20px; }
  .auth-brand { margin-bottom: 28px; }
  .auth-heading h1 { font-size: 24px; }
  .auth-switch { flex-direction: column; gap: 2px; }
}

/* App views and study-set library */
[hidden] { display: none !important; }
.app-view { animation: view-in 240ms ease both; }
@keyframes view-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.workspace-focus { width: min(100%, 900px); margin: 0 auto; }
.workspace-focus .quiz-output { scroll-margin-top: 28px; }
.composer-actions { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 11px; margin-top: 22px; }
.composer-actions .primary-button { margin-top: 0; }
.clear-button { display: inline-flex; min-height: 50px; align-items: center; justify-content: center; gap: 8px; padding: 12px 18px; border: 1px solid #dedfe9; border-radius: 13px; background: white; color: var(--muted); cursor: pointer; font-size: 12px; font-weight: 720; transition: border-color 160ms ease, background 160ms ease, color 160ms ease, transform 160ms ease; }
.clear-button svg { width: 17px; height: 17px; }
.clear-button:hover { border-color: #cfcafc; background: var(--primary-soft); color: var(--primary); transform: translateY(-1px); }
.clear-button:active { transform: translateY(0); }
.logout-button { padding: 7px 10px; border: 0; border-left: 1px solid var(--line); background: transparent; color: var(--muted); cursor: pointer; font-size: 10px; font-weight: 700; transition: color 150ms ease; }
.logout-button:hover { color: var(--red); }

.view-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 23px; }
.view-heading h2 { margin: 0; font-size: 25px; letter-spacing: -0.6px; }
.view-heading > div > p:last-child { margin: 8px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.collection-count { flex: 0 0 auto; padding: 7px 11px; border: 1px solid #dfdcff; border-radius: 20px; background: var(--primary-soft); color: var(--primary); font-size: 10px; font-weight: 750; }
.study-sets-page-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.study-sets-page-grid .skeleton-card { min-height: 172px; align-content: end; padding: 22px; border: 1px solid var(--line); background: var(--surface); }
.study-set-tile { display: flex; min-height: 180px; flex-direction: column; align-items: flex-start; padding: 22px; border: 1px solid var(--line); border-radius: 18px; background: var(--surface); box-shadow: 0 8px 28px rgba(32, 35, 68, 0.045); color: var(--text); cursor: pointer; text-align: left; transition: border-color 180ms ease, box-shadow 180ms ease, transform 180ms ease; }
.study-set-tile:hover { border-color: #d3ceff; box-shadow: 0 16px 36px rgba(70, 59, 158, 0.11); transform: translateY(-3px); }
.study-set-tile:focus-visible { outline: 4px solid rgba(102, 87, 232, 0.14); border-color: var(--primary); }
.study-set-tile-icon { display: grid; width: 42px; height: 42px; place-items: center; border-radius: 12px; background: var(--primary-soft); color: var(--primary); }
.study-set-tile-icon svg { width: 21px; height: 21px; }
.study-set-tile-copy { display: grid; gap: 7px; width: 100%; margin-top: 18px; }
.study-set-tile-copy strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.study-set-tile-copy small { color: var(--muted); font-size: 10px; }
.study-set-open { display: flex; width: 100%; align-items: center; justify-content: space-between; margin-top: auto; padding-top: 17px; color: var(--primary); font-size: 11px; font-weight: 720; }
.study-set-open span { font-size: 17px; transition: transform 160ms ease; }
.study-set-tile:hover .study-set-open span { transform: translateX(3px); }
.library-empty-state { grid-column: 1 / -1; padding: 60px 20px; border: 1px dashed #dcd9ef; border-radius: 18px; background: rgba(255,255,255,0.6); }
.secondary-button { margin-top: 17px; padding: 9px 13px; border: 1px solid #d7d2ff; border-radius: 10px; background: white; color: var(--primary); cursor: pointer; font-size: 11px; font-weight: 720; }
.secondary-button:hover { background: var(--primary-soft); }

.back-button { display: inline-flex; align-items: center; gap: 7px; margin-bottom: 18px; padding: 8px 0; border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 11px; font-weight: 700; }
.back-button:hover { color: var(--primary); }
.back-button svg { width: 17px; height: 17px; }
.study-set-detail-panel { padding: 28px; }
.detail-heading { display: flex; align-items: center; gap: 14px; }
.detail-heading h2 { margin: 0; font-size: 23px; letter-spacing: -0.5px; }
.saved-notes { margin-top: 24px; padding: 18px; border: 1px solid var(--line); border-radius: 14px; background: #fafafe; }
.saved-notes-label { margin: 0 0 9px; color: var(--primary); font-size: 10px; font-weight: 760; letter-spacing: 0.9px; text-transform: uppercase; }
.saved-notes > p:last-child { max-height: 160px; margin: 0; overflow-y: auto; color: #5e6278; font-size: 12px; line-height: 1.7; white-space: pre-wrap; }
.detail-quiz-output { margin-top: 28px; }
.quiz-result:not(:empty) { margin-top: 18px; }

@media (max-width: 1100px) {
  .study-sets-page-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 680px) {
  .view-heading { align-items: flex-start; flex-direction: column; gap: 13px; }
  .study-sets-page-grid { grid-template-columns: 1fr; }
  .study-set-tile { min-height: 165px; }
  .study-set-detail-panel { padding: 22px 18px; }
  .detail-heading { align-items: flex-start; }
  .logout-button { display: none; }
}

@media (max-width: 430px) {
  .composer-actions { grid-template-columns: 1fr; }
}

.chat-messages {
  display: grid;
  gap: 12px;
  max-height: 520px;
  min-height: 280px;
  overflow-y: auto;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fafafe;
}

.chat-message {
  max-width: 78%;
  padding: 12px 14px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.user-message {
  justify-self: end;
  background: var(--primary);
  color: white;
  border-bottom-right-radius: 4px;
}

.ai-message {
  justify-self: start;
  background: white;
  border: 1px solid var(--line);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.chat-input-row {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 12px;
  margin-top: 16px;
}

.chat-input-row textarea {
  min-height: 70px;
}

.chat-input-row .primary-button {
  margin-top: 0;
}

@media (max-width: 620px) {
  .chat-input-row {
    grid-template-columns: 1fr;
  }

  .chat-message {
    max-width: 92%;
  }
}

.ai-message p {
  margin: 0 0 8px;
}

.ai-message p:last-child {
  margin-bottom: 0;
}

.ai-message ul,
.ai-message ol {
  margin: 8px 0;
  padding-left: 20px;
}

.ai-message strong {
  font-weight: 750;
}

.ai-message h1,
.ai-message h2,
.ai-message h3 {
  margin: 10px 0 6px;
  font-size: 15px;
}