:root {
  --bg: #f5f6fb;
  --surface: #ffffff;
  --surface-2: #f0f1f8;
  --text: #1c1f2e;
  --muted: #646a82;
  --border: #e1e4ef;
  --primary: #4f46e5;
  --primary-soft: #eceafe;
  --ok: #0f9d58;
  --ok-soft: #e3f6ec;
  --bad: #d93025;
  --bad-soft: #fde8e6;
  --warn: #b26a00;
  --warn-soft: #fff3dc;
  --shadow: 0 1px 2px rgba(20, 24, 50, .06), 0 4px 16px rgba(20, 24, 50, .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11131c;
    --surface: #1a1d29;
    --surface-2: #232736;
    --text: #e8eaf3;
    --muted: #9aa0b8;
    --border: #2e3346;
    --primary: #8b84ff;
    --primary-soft: #2a2852;
    --ok: #3ccf87;
    --ok-soft: #173326;
    --bad: #ff6b60;
    --bad-soft: #3a1c1a;
    --warn: #f1b14b;
    --warn-soft: #3a2c12;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Rubik", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  font-size: 16px;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.7rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
[hidden] { display: none !important; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.topnav { display: flex; gap: 14px; flex: 1; }
.topnav a { color: var(--muted); font-weight: 500; }
.user-box { display: flex; gap: 10px; align-items: center; color: var(--muted); }
main { max-width: 1100px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.crumbs { font-size: .9rem; color: var(--muted); margin-bottom: 6px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.two-col { display: grid; gap: 20px; grid-template-columns: 1fr 1fr; }
@media (max-width: 800px) { .two-col { grid-template-columns: 1fr; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card h2 { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* Buttons & forms */
.btn {
  font: inherit; font-weight: 500;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 16px;
  background: var(--primary); color: #fff;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  transition: filter .15s, background .15s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: var(--primary-soft); color: var(--primary); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--border); }
.btn.small { padding: 5px 10px; font-size: .85rem; border-radius: 8px; }
.btn.big { padding: 12px 22px; font-size: 1.05rem; }
input, textarea, select {
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
label { display: block; font-weight: 500; margin: 12px 0 4px; }
.field-row { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }

/* Auth */
.auth-wrap { max-width: 420px; margin: 8vh auto 0; }
.auth-wrap .hero { text-align: center; margin-bottom: 20px; }
.auth-wrap .hero .logo { font-size: 3rem; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 18px; overflow-x: auto; }
.tabs button {
  flex: 1; font: inherit; font-weight: 500;
  background: transparent; color: var(--muted);
  border: 0; border-radius: 9px; padding: 8px 12px; cursor: pointer; white-space: nowrap;
}
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.error-msg { color: var(--bad); margin-top: 10px; min-height: 1.4em; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 24px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .num { font-size: 1.7rem; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: .9rem; }

/* Room cards */
.room-card { position: relative; display: block; color: var(--text); overflow: hidden; padding-top: 22px; }
.room-card:hover { text-decoration: none; border-color: var(--primary); }
.room-card .stripe { position: absolute; inset: 0 0 auto 0; height: 6px; }
.room-card h3 { margin-bottom: 4px; }
.room-card .meta { display: flex; gap: 12px; color: var(--muted); font-size: .85rem; margin-top: 10px; flex-wrap: wrap; }
.new-room { display: flex; align-items: center; justify-content: center; min-height: 140px; border-style: dashed; color: var(--muted); cursor: pointer; font-weight: 500; background: transparent; box-shadow: none; font: inherit; }
.new-room:hover { color: var(--primary); border-color: var(--primary); }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; }

.badge { display: inline-block; font-size: .78rem; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.primary { background: var(--primary-soft); color: var(--primary); }
.score-pill { font-weight: 700; min-width: 54px; text-align: center; }

/* Upload */
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 26px 16px; text-align: center; cursor: pointer;
  background: var(--surface-2); transition: border-color .15s, background .15s;
}
.dropzone.drag, .dropzone:hover { border-color: var(--primary); background: var(--primary-soft); }
.dropzone .icon { font-size: 2rem; }
.upload-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.progress-line { font-size: .9rem; color: var(--muted); margin-top: 8px; }
.kind-icon { font-size: 1.3rem; width: 30px; text-align: center; flex-shrink: 0; }

/* Plan */
.plan-topic { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; }
.plan-topic .head { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.plan-topic .num { background: var(--primary-soft); color: var(--primary); border-radius: 8px; padding: 0 8px; font-weight: 700; }
.plan-topic ul { margin: 6px 0 0; padding-inline-start: 22px; color: var(--muted); font-size: .92rem; }

/* Chat */
.chat-page { display: flex; flex-direction: column; height: calc(100vh - 140px); min-height: 420px; }
.chat-log { flex: 1; overflow-y: auto; padding: 8px 4px 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 85%; padding: 12px 16px; border-radius: 16px; box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.user { align-self: flex-start; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-end; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant .who { font-size: .8rem; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.msg.error { align-self: center; background: var(--bad-soft); color: var(--bad); box-shadow: none; }
.chat-input { display: flex; gap: 8px; align-items: flex-end; padding-top: 10px; border-top: 1px solid var(--border); }
.chat-input textarea { resize: none; min-height: 48px; max-height: 180px; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; padding: 8px 0 0; }
.quick button { font: inherit; font-size: .85rem; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.quick button:hover { border-color: var(--primary); color: var(--primary); }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; margin-inline-start: 2px; }
@keyframes blink { 50% { opacity: 0; } }

/* Markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: .5em 0; }
.md ul, .md ol { margin: .4em 0; padding-inline-start: 1.4em; }
.md table { border-collapse: collapse; margin: .6em 0; width: 100%; font-size: .93rem; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: start; }
.md th { background: var(--surface-2); }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; direction: ltr; unicode-bidi: embed; }
.md pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; direction: ltr; }
.md blockquote { border-inline-start: 3px solid var(--primary); margin: .5em 0; padding: 2px 12px; color: var(--muted); }
.md h1, .md h2, .md h3 { margin: .8em 0 .3em; }
.md h1 { font-size: 1.3rem; } .md h2 { font-size: 1.15rem; } .md h3 { font-size: 1.02rem; }

/* Exam */
.question { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; background: var(--surface); }
.question .qhead { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: .85rem; margin-bottom: 6px; }
.question .qtext { font-weight: 500; font-size: 1.05rem; margin-bottom: 10px; unicode-bidi: plaintext; }
.option { display: flex; align-items: center; gap: 10px; border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; margin: 6px 0; cursor: pointer; unicode-bidi: plaintext; font-weight: 400; }
.option:hover { border-color: var(--primary); }
.option input { width: auto; margin: 0; }
.option.selected { border-color: var(--primary); background: var(--primary-soft); }
.option.correct { border-color: var(--ok); background: var(--ok-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.question.res-ok { border-inline-start: 5px solid var(--ok); }
.question.res-bad { border-inline-start: 5px solid var(--bad); }
.question.res-part { border-inline-start: 5px solid var(--warn); }
.answer-box { margin-top: 8px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: .95rem; }
.answer-box div + div { margin-top: 4px; }
.answer-box .lbl { font-weight: 600; }
.dir-auto { unicode-bidi: plaintext; }
.exam-bar { position: sticky; bottom: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; display: flex; gap: 12px; align-items: center; justify-content: space-between; box-shadow: var(--shadow); flex-wrap: wrap; }

.score-hero { text-align: center; padding: 26px 16px; }
.score-ring { --p: 0; --c: var(--primary); width: 150px; height: 150px; border-radius: 50%; margin: 0 auto 12px; display: grid; place-items: center;
  background: conic-gradient(var(--c) calc(var(--p) * 1%), var(--surface-2) 0); }
.score-ring > div { width: 118px; height: 118px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-size: 2rem; font-weight: 700; }
.filter-row { display: flex; gap: 8px; margin: 18px 0 12px; flex-wrap: wrap; }

/* Chart */
.chart { display: flex; align-items: flex-end; gap: 6px; height: 120px; padding-top: 10px; border-bottom: 1px solid var(--border); }
.chart .bar { flex: 1; max-width: 38px; border-radius: 6px 6px 0 0; background: var(--primary); position: relative; min-height: 3px; }
.chart .bar span { position: absolute; top: -20px; left: 50%; transform: translateX(-50%); font-size: .72rem; color: var(--muted); }

/* Modal, toast, banner */
.modal-back { position: fixed; inset: 0; background: rgba(10, 12, 25, .5); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius); padding: 22px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow); }
.modal.wide { max-width: 860px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 18px; flex-wrap: wrap; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; z-index: 60; box-shadow: var(--shadow); max-width: 90vw; }
.banner { background: var(--warn-soft); color: var(--warn); padding: 10px 20px; text-align: center; font-size: .92rem; }
.colors { display: flex; gap: 8px; flex-wrap: wrap; }
.colors button { width: 28px; height: 28px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.colors button.active { border-color: var(--text); }
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.thumbs img { width: 120px; height: 90px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-center { display: grid; place-items: center; gap: 10px; padding: 60px 0; color: var(--muted); }

@media (max-width: 600px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  .topnav { display: none; }
  .brand span { display: none; }
  .brand { flex: 1; }
  .msg { max-width: 95%; }
  .field-row { grid-template-columns: 1fr; }
  h1 { font-size: 1.4rem; }
}
