:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f1eee7;
  --border: #e3ded3;
  --text: #1f1d1a;
  --muted: #6b665d;
  --accent: #3f5bd9;
  --accent-soft: #e6eafc;
  --good: #1f8a4c;
  --good-soft: #e1f3e8;
  --bad: #c2372f;
  --bad-soft: #fbe5e3;
  --warn: #a86a00;
  --shadow: 0 1px 2px rgba(30, 25, 15, .06), 0 6px 24px rgba(30, 25, 15, .07);
  --radius: 14px;
  --font: "Inter", system-ui, -apple-system, sans-serif;
  --display: "Fraunces", Georgia, serif;
  color-scheme: light;
}
body[data-lang="fr"] { --accent: #2f55d4; --accent-soft: #e5ebfd; }
body[data-lang="es"] { --accent: #c4402b; --accent-soft: #fbe7e2; }
body[data-lang="tl"] { --accent: #0d8a7a; --accent-soft: #dcf3ef; }
body[data-lang="en"] { --accent: #6d3fc4; --accent-soft: #eee6fb; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15140f; --surface: #1f1e19; --surface-2: #2a2822; --border: #3a372f;
    --text: #f1ede4; --muted: #a8a294; --good: #4cc27c; --good-soft: #1d3326;
    --bad: #f0776f; --bad-soft: #3c2120; --warn: #e3a948;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) body[data-lang="fr"] { --accent: #8aa4ff; --accent-soft: #232c4a; }
  :root:not([data-theme="light"]) body[data-lang="es"] { --accent: #ff8a73; --accent-soft: #452720; }
  :root:not([data-theme="light"]) body[data-lang="tl"] { --accent: #4fd1bd; --accent-soft: #173a35; }
  :root:not([data-theme="light"]) body[data-lang="en"] { --accent: #b598f5; --accent-soft: #2f2448; }
}
:root[data-theme="dark"] {
  --bg: #15140f; --surface: #1f1e19; --surface-2: #2a2822; --border: #3a372f;
  --text: #f1ede4; --muted: #a8a294; --good: #4cc27c; --good-soft: #1d3326;
  --bad: #f0776f; --bad-soft: #3c2120; --warn: #e3a948;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}
:root[data-theme="dark"] body[data-lang="fr"] { --accent: #8aa4ff; --accent-soft: #232c4a; }
:root[data-theme="dark"] body[data-lang="es"] { --accent: #ff8a73; --accent-soft: #452720; }
:root[data-theme="dark"] body[data-lang="tl"] { --accent: #4fd1bd; --accent-soft: #173a35; }
:root[data-theme="dark"] body[data-lang="en"] { --accent: #b598f5; --accent-soft: #2f2448; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.6 var(--font); min-height: 100vh;
}
h1, h2, h3 { font-family: var(--display); line-height: 1.2; margin: 0 0 .5em; letter-spacing: -.01em; }
h1 { font-size: clamp(1.6rem, 3vw, 2.2rem); }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; font-family: var(--font); font-weight: 600; }
p { margin: 0 0 .9em; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
code { background: var(--surface-2); padding: .1em .35em; border-radius: 5px; font-size: .9em; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.15rem; text-decoration: none; color: var(--text); font-family: var(--display); }
.brand-mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 1rem; }
.lang-switch { display: flex; gap: 6px; margin-left: auto; }
.lang-switch button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 12px; border-radius: 999px; cursor: pointer; font: 500 .9rem var(--font);
  display: flex; gap: 6px; align-items: center;
}
.lang-switch button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn { background: none; border: 0; font-size: 1.2rem; cursor: pointer; color: var(--text); padding: 6px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }
#menuBtn { display: none; }
.account-btn { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text); border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 10px 4px 4px; font: 500 .85rem var(--font); white-space: nowrap; }
.account-btn.signed-out { padding: 6px 12px; }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: .8rem; overflow: hidden; position: relative; flex: none; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync-dot.synced { background: var(--good); } .sync-dot.syncing { background: var(--warn); } .sync-dot.error { background: var(--bad); }

/* Layout */
.layout { display: grid; grid-template-columns: 230px 1fr; max-width: 1180px; margin: 0 auto; }
.sidenav { position: sticky; top: 57px; align-self: start; padding: 20px 14px; display: flex; flex-direction: column; gap: 2px; height: calc(100vh - 57px); }
.sidenav a { display: flex; gap: 10px; align-items: center; padding: 9px 12px; border-radius: 10px; text-decoration: none; color: var(--text); font-weight: 500; }
.sidenav a:hover { background: var(--surface-2); }
.sidenav a.active { background: var(--accent-soft); color: var(--accent); }
.level-box { margin-top: auto; padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.level-box label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 4px; }
main { padding: 28px 28px 120px; min-width: 0; outline: none; }

/* Components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 18px; }
.card.flat { box-shadow: none; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); padding: 9px 16px; border-radius: 10px; font: 600 .92rem var(--font); cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.sm { padding: 5px 10px; font-size: .82rem; }
select, input[type="text"], input[type="password"], textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px; width: 100%;
}
select { width: auto; }
.level-box select { width: 100%; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: .78rem; font-weight: 600; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.gray { background: var(--surface-2); color: var(--muted); }
.hero { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.hero .flag { font-size: 2.6rem; line-height: 1; }
.stat { font-family: var(--display); font-size: 2rem; font-weight: 700; line-height: 1; }
.stat-label { color: var(--muted); font-size: .85rem; margin-top: 6px; }
.progress-bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }

/* Today checklist */
.task { display: flex; gap: 14px; align-items: center; padding: 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); text-decoration: none; color: var(--text); }
.task:hover { border-color: var(--accent); }
.task .check { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--border); display: grid; place-items: center; flex: none; font-size: .9rem; }
.task.done .check { background: var(--good); border-color: var(--good); color: #fff; }
.task .t-title { font-weight: 600; }

/* Lesson */
.lesson-list a { display: flex; gap: 12px; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); text-decoration: none; color: var(--text); }
.lesson-list a:last-child { border-bottom: 0; }
.lesson-list a:hover { background: var(--surface-2); }
.lesson-num { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; background: var(--surface-2); font-weight: 700; flex: none; }
.lesson-list a.done .lesson-num { background: var(--good); color: #fff; }
.lesson-body h3 { margin-top: 1.4em; }
.lesson-body ul { padding-left: 1.2em; }
.lesson-body table.conj { display: block; overflow-x: auto; max-width: 100%; }
.examples { list-style: none; padding: 0; margin: 10px 0 0; }
.examples li { padding: 10px 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 10px 10px 0; margin-bottom: 8px; }
.examples .en { color: var(--muted); font-size: .9rem; display: block; }
table.conj { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: .93rem; }
table.conj th, table.conj td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); }
table.conj th { background: var(--surface-2); font-weight: 600; }
.table-wrap { overflow-x: auto; }

/* Clickable target-language words */
.w { cursor: pointer; border-radius: 4px; transition: background .12s; }
.w:hover, .w:focus { background: var(--accent-soft); outline: none; }
.w.active { background: var(--accent); color: #fff; }
.target { font-size: 1.05rem; }
.gloss { color: var(--muted); font-size: .9em; font-weight: 400; }
.term { border-bottom: 2px dotted var(--accent); cursor: help; font-weight: 600; }
.term:hover, .term:focus { background: var(--accent-soft); outline: none; }
.interlinear { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 0 4px; }
.il { display: inline-flex; flex-direction: column; line-height: 1.25; }
.il-w { font-weight: 600; }
.il-m { font-size: .76rem; color: var(--muted); }
.callout { border-left: 4px solid var(--accent); background: var(--accent-soft); padding: 12px 14px; border-radius: 0 10px 10px 0; margin: 14px 0; }
.callout.warn { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, var(--surface)); }
.terms-box dl { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; }
.terms-box dt { font-weight: 600; }
.terms-box dd { margin: 0; color: var(--muted); }
@media (max-width: 560px) { .terms-box dl { grid-template-columns: 1fr; } .terms-box dd { margin-bottom: 6px; } }
.passage { font-size: 1.12rem; line-height: 1.85; }
.passage.literary { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; }
.passage.literary p { margin-bottom: 1em; }
.word-card .wc-word { font-family: var(--display); font-size: 1.7rem; font-weight: 700; }

/* Exercises */
.ex { padding: 16px; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px; background: var(--surface); }
.ex-q { font-weight: 500; margin-bottom: 10px; }
.opts { display: grid; gap: 8px; }
.opt { text-align: left; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 10px 14px; border-radius: 10px; cursor: pointer; font: inherit; }
.opt:hover:not(:disabled) { border-color: var(--accent); }
.opt.correct { background: var(--good-soft); border-color: var(--good); }
.opt.wrong { background: var(--bad-soft); border-color: var(--bad); }
.opt:disabled { cursor: default; }
.feedback { margin-top: 10px; font-size: .92rem; padding: 10px 12px; border-radius: 10px; }
.feedback.good { background: var(--good-soft); color: var(--good); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.fill-row { display: flex; gap: 8px; }
.fill-row input { flex: 1; }

/* Flashcards */
.flash-wrap { perspective: 1200px; max-width: 560px; margin: 0 auto; }
.flash { position: relative; height: 280px; cursor: pointer; transform-style: preserve-3d; transition: transform .45s; }
.flash.flipped { transform: rotateY(180deg); }
.flash-face { position: absolute; inset: 0; backface-visibility: hidden; border-radius: 18px; border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; text-align: center; }
.flash-face.back { transform: rotateY(180deg); background: var(--accent-soft); }
.flash-front-text { font-family: var(--display); font-size: 2rem; font-weight: 700; }
.flash-back-text { font-size: 1.3rem; font-weight: 600; }
.rate-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; max-width: 560px; margin: 18px auto 0; }
.rate-row .btn { justify-content: center; flex-direction: column; gap: 0; }
.rate-row .btn small { font-weight: 400; color: var(--muted); font-size: .72rem; }

/* Heatmap */
.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 13px); grid-auto-columns: 13px; justify-content: start; gap: 3px; overflow-x: auto; padding-bottom: 4px; }
.heatmap span { width: 13px; height: 13px; border-radius: 3px; background: var(--surface-2); }
.heatmap span[data-l="1"] { background: color-mix(in srgb, var(--accent) 30%, var(--surface-2)); }
.heatmap span[data-l="2"] { background: color-mix(in srgb, var(--accent) 55%, var(--surface-2)); }
.heatmap span[data-l="3"] { background: color-mix(in srgb, var(--accent) 80%, var(--surface-2)); }
.heatmap span[data-l="4"] { background: var(--accent); }

/* Word popup */
.word-popup {
  position: fixed; z-index: 60; width: min(400px, calc(100vw - 32px)); max-height: min(560px, 75vh); overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 12px 48px rgba(0,0,0,.22);
  padding: 18px;
}
.wp-head { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.wp-word { font-family: var(--display); font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.wp-sec { margin-top: 14px; }
.wp-sec h4 { margin: 0 0 6px; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.wp-sec ul { margin: 0; padding-left: 1.1em; }
.mnemonic { background: color-mix(in srgb, var(--warn) 14%, var(--surface)); border-radius: 10px; padding: 10px 12px; font-size: .93rem; }
.speak { border: 1px solid var(--border); background: var(--surface-2); border-radius: 50%; width: 34px; height: 34px; cursor: pointer; flex: none; font-size: 1rem; color: var(--text); }
.speak:hover { background: var(--accent-soft); }
.speak.inline { width: 26px; height: 26px; font-size: .8rem; vertical-align: middle; margin-left: 4px; }

/* Tutor */
.tutor-fab {
  position: fixed; right: 22px; bottom: 22px; z-index: 50; border: 0; border-radius: 999px; padding: 13px 20px;
  background: var(--accent); color: #fff; font: 600 .95rem var(--font); cursor: pointer; box-shadow: 0 8px 28px rgba(0,0,0,.22);
}
.tutor-panel {
  position: fixed; right: 22px; bottom: 22px; z-index: 55; width: min(420px, calc(100vw - 32px)); height: min(620px, calc(100vh - 100px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: 0 16px 60px rgba(0,0,0,.25);
  display: flex; flex-direction: column; overflow: hidden;
}
.tutor-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.tutor-msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { padding: 10px 13px; border-radius: 14px; max-width: 92%; font-size: .94rem; overflow-wrap: anywhere; }
.msg p:last-child { margin-bottom: 0; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.2em; margin: 0 0 .6em; }
.msg.err { background: var(--bad-soft); color: var(--bad); }
.tutor-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.tutor-form textarea { flex: 1; }
.suggestions { display: flex; flex-wrap: wrap; gap: 6px; }
.suggestions button { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 5px 11px; font-size: .82rem; cursor: pointer; }

.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 70; background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-weight: 500; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.notice { padding: 12px 14px; border-radius: 10px; background: var(--surface-2); font-size: .9rem; }
.dict-diff { font-family: ui-monospace, monospace; font-size: .95rem; }
.dict-diff .ok { color: var(--good); }
.dict-diff .miss { color: var(--bad); text-decoration: underline; }

/* Mobile */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  #menuBtn { display: block; }
  .sidenav { position: fixed; top: 57px; left: 0; bottom: 0; width: 260px; height: auto; z-index: 40; background: var(--bg); border-right: 1px solid var(--border); transform: translateX(-100%); transition: transform .2s; }
  .sidenav.open { transform: none; }
  main { padding: 20px 16px 120px; }
  .topbar { padding: 10px 16px; gap: 8px; }
  .lang-switch button .ln { display: none; }
  .account-btn .acct-name { display: none; }
  .account-btn { padding: 3px; }
  .account-btn.signed-out { padding: 5px 9px; }
  .brand { font-size: 1rem; }
  .tutor-fab span { display: none; }
  .tutor-panel { right: 8px; left: 8px; bottom: 8px; width: auto; height: calc(100vh - 80px); }
  .rate-row { grid-template-columns: repeat(2, 1fr); }
}
