/* Dictée – "Cahier Seyès": trang vở ô ly kiểu Pháp là điểm nhấn, phần còn lại gọn và yên. */
:root {
  --desk: #eef0fa;
  --paper: #ffffff;
  --text: #171c4a;
  --muted: #5f668f;
  --line: #d9ddf2;
  --ink: #2438c5;
  --ink-press: #1a2a9e;
  --ink-soft: #e4e8ff;
  --on-ink: #ffffff;
  --seyes: #e3e7f8;
  --seyes-main: #bcc5ef;
  --marge: #ef4f78;
  --fluo: #ffe84a;
  --ok: #10935f;
  --ok-soft: #dcf5e9;
  --err: #d92d57;
  --err-soft: #ffe3ea;
  --warn: #9a6200;
  --lv-mine: #2438c5;
  --lv-A1: #ff9f2e;
  --lv-A2: #17b087;
  --lv-B1: #7a5cf0;
  --lv-B2: #ef4f78;
  --shadow: 0 1px 0 rgba(23, 28, 74, .06), 0 22px 44px -28px rgba(36, 56, 197, .45);
  --ui: "Bricolage Grotesque", "Segoe UI", system-ui, sans-serif;
  --hand: "Playwrite FR Moderne", "Segoe Print", cursive;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --desk: #0e1230;
    --paper: #171c45;
    --text: #edefff;
    --muted: #a5abd8;
    --line: #2c3368;
    --ink: #8c9dff;
    --ink-press: #aab6ff;
    --ink-soft: #262e68;
    --on-ink: #0e1230;
    --seyes: #20275a;
    --seyes-main: #343f84;
    --marge: #ff6f93;
    --ok: #46d79a;
    --ok-soft: #13392f;
    --err: #ff7393;
    --err-soft: #3f1a2d;
    --warn: #f2c056;
    --lv-mine: #8c9dff;
    --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 22px 44px -26px rgba(0, 0, 0, .7);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--desk);
  color: var(--text);
  font: 15px/1.5 var(--ui);
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
:focus-visible { outline: 2.5px solid var(--ink); outline-offset: 2px; }
button, input, select, textarea { font: inherit; color: inherit; }

.ic {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.app { display: grid; grid-template-columns: 300px 1fr; height: 100vh; }

/* ---------- Thư viện ---------- */
.library {
  display: flex; flex-direction: column; min-height: 0;
  border-right: 1.5px solid var(--line);
  padding: 22px 14px 10px;
}
.brand { padding: 0 8px 18px; }
.brand-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.logo { font: 400 27px/1.4 var(--hand); color: var(--ink); }
.tagline { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

.lang-switch { display: inline-flex; border: 1.5px solid var(--line); border-radius: 999px; padding: 2px; }
.lang-switch button {
  border: 0; background: none; cursor: pointer;
  font-size: 12px; font-weight: 700; padding: 3px 10px; border-radius: 999px; color: var(--muted);
}
.lang-switch button[aria-pressed="true"] { background: var(--text); color: var(--paper); }

.add-btn { width: 100%; justify-content: center; margin-bottom: 14px; }

.lesson-list { scrollbar-width: thin; scrollbar-color: var(--line) transparent; list-style: none; margin: 0; padding: 0 2px; overflow-y: auto; flex: 1; }
.lesson-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 10px; cursor: pointer;
}
.lesson-list li:hover { background: var(--paper); }
.lesson-list .meta { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.lesson-list .meta.done { color: var(--ok); font-weight: 600; }
.lesson-list .empty { color: var(--muted); cursor: default; }

.lesson-list li.group { margin-top: 6px; font-weight: 700; font-size: 14px; user-select: none; }
.g-name { display: flex; align-items: center; gap: 8px; }
.g-name .ic { width: 14px; height: 14px; color: var(--muted); transition: transform .15s; }
.group.open .g-name .ic { transform: rotate(90deg); }
.dot { width: 10px; height: 10px; border-radius: 3px; background: var(--lv, var(--lv-mine)); transform: rotate(45deg); }
[data-level="mine"] { --lv: var(--lv-mine); }
[data-level="A1"] { --lv: var(--lv-A1); }
[data-level="A2"] { --lv: var(--lv-A2); }
[data-level="B1"] { --lv: var(--lv-B1); }
[data-level="B2"] { --lv: var(--lv-B2); }

.lesson-list li.item { padding-left: 32px; font-size: 14px; position: relative; }
.i-name { display: flex; align-items: center; gap: 6px; min-width: 0; }
.i-name .ic { width: 15px; height: 15px; color: var(--ink); }
.lesson-list li.item.active { background: var(--paper); font-weight: 600; box-shadow: var(--shadow); }
.lesson-list li.item.active::before {
  content: ""; position: absolute; left: 14px; top: 50%; width: 6px; height: 6px; margin-top: -3px;
  border-radius: 50%; background: var(--lv);
}
.library-foot { display: flex; flex-wrap: wrap; gap: 4px 16px; padding: 12px 10px 4px; border-top: 1.5px solid var(--line); }

/* ---------- Luyện tập ---------- */
.practice { scrollbar-width: thin; scrollbar-color: var(--line) transparent; overflow-y: auto; padding: 30px 40px 60px; }
#practice-view, .empty-state { max-width: 900px; margin: 0 auto; }

.empty-state { text-align: center; padding-top: 14vh; }
.empty-state h2 { font-size: 28px; font-weight: 800; letter-spacing: -.02em; margin: 26px 0 6px; }
.empty-state p { color: var(--muted); max-width: 46ch; margin: 0 auto; }
.empty-sheet {
  width: 230px; height: 140px; margin: 0 auto; transform: rotate(-3deg);
  border-radius: 6px; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  font: 400 30px var(--hand); color: var(--ink);
  background:
    linear-gradient(90deg, transparent 39px, var(--marge) 39px, var(--marge) 41px, transparent 41px),
    repeating-linear-gradient(180deg, var(--seyes-main) 0 1.5px, transparent 1.5px 28px),
    repeating-linear-gradient(180deg, var(--seyes) 0 1px, transparent 1px 7px),
    var(--paper);
}

.title-row { display: flex; align-items: center; gap: 14px; }
.title-row h2 { margin: 0; font-size: 34px; line-height: 1.15; font-weight: 800; letter-spacing: -.025em; flex: 1; min-width: 0; }
.title-actions { display: flex; gap: 6px; }
.sticker {
  flex: none; background: var(--lv); color: #fff; font-weight: 800; font-size: 14px;
  padding: 4px 10px; border-radius: 8px; transform: rotate(-4deg);
}

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 22px; margin-top: 18px; }
.ctrl { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.ctrl output { min-width: 46px; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.ctrl select { max-width: 200px; }
input[type=range] { accent-color: var(--ink); width: 100px; }
select {
  background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px; padding: 6px 10px;
}

.segmented { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--paper); border: 1.5px solid var(--line); }
.seg { border: 0; background: none; cursor: pointer; padding: 7px 15px; border-radius: 999px; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg.active { background: var(--ink); color: var(--on-ink); }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 600; padding: 4px 12px; border-radius: 999px;
  background: var(--ink-soft); color: var(--ink);
}
.badge .ic { width: 15px; height: 15px; }
.badge.missing { background: var(--err-soft); color: var(--err); }
.warning { color: var(--warn); font-size: 14px; margin: 12px 0 0; }

/* Trình phát */
.player {
  margin-top: 24px; padding: 18px 20px; border-radius: 20px;
  background: var(--paper); box-shadow: var(--shadow);
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 18px;
}
.play-btn {
  display: inline-flex; align-items: center; gap: 10px; cursor: pointer;
  height: 54px; padding: 0 24px 0 20px; border: 0; border-radius: 999px;
  background: var(--ink); color: var(--on-ink); font-size: 16px; font-weight: 700;
  transition: background .15s, transform .1s;
}
.play-btn .ic { width: 22px; height: 22px; }
.play-btn:hover:not(:disabled) { background: var(--ink-press); }
.play-btn:active:not(:disabled) { transform: scale(.97); }
.play-btn:disabled { opacity: .5; cursor: default; }
.player-main { min-width: 0; }
.status { display: flex; align-items: center; gap: 12px; min-height: 30px; }
#status-text b { font-size: 20px; font-weight: 700; letter-spacing: -.01em; }
#status-text .sub { color: var(--muted); font-size: 14px; margin-left: 6px; }
.player-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.wave { display: inline-flex; align-items: center; gap: 3px; height: 18px; }
.wave i { width: 3px; height: 100%; border-radius: 2px; background: var(--ink); animation: wave .9s ease-in-out infinite; transform-origin: 50% 50%; }
.wave i:nth-child(2) { animation-delay: -.6s; }
.wave i:nth-child(3) { animation-delay: -.3s; }
.wave i:nth-child(4) { animation-delay: -.75s; }
@keyframes wave { 0%, 100% { transform: scaleY(.3); } 50% { transform: scaleY(1); } }

/* Tiến độ: mỗi câu một vạch */
.progress { display: flex; gap: 4px; margin-top: 10px; }
.progress i { flex: 1; height: 8px; border-radius: 4px; background: var(--seyes); min-width: 3px; }
.progress i.done { background: var(--ink); }
.progress i.help { background: var(--fluo); box-shadow: inset 0 0 0 1.5px rgba(23, 28, 74, .15); }
.progress i.skip { background: var(--marge); }
.progress i.cur { background: var(--paper); box-shadow: inset 0 0 0 2px var(--ink); }

/* ---------- Trang vở Seyès ---------- */
.sheet {
  --row: 32px;
  --margin: 76px;
  position: relative; margin-top: 22px;
  padding: 33px 30px 30px calc(var(--margin) + 22px);
  border-radius: 4px; box-shadow: var(--shadow);
  background:
    linear-gradient(90deg, transparent calc(var(--margin) - 1px), var(--marge) calc(var(--margin) - 1px), var(--marge) calc(var(--margin) + 1px), transparent calc(var(--margin) + 1px)),
    repeating-linear-gradient(180deg, var(--seyes-main) 0 1.5px, transparent 1.5px var(--row)),
    repeating-linear-gradient(180deg, var(--seyes) 0 1px, transparent 1px calc(var(--row) / 4)),
    repeating-linear-gradient(90deg, var(--seyes) 0 1px, transparent 1px var(--row)),
    var(--paper);
  background-position: 0 0, 0 24px, 0 24px, var(--margin) 0, 0 0;
  transition: box-shadow .2s;
}
.sheet:focus-within { box-shadow: var(--shadow), 0 0 0 3px var(--ink-soft); }

#input {
  display: block; width: 100%; resize: none; overflow: hidden;
  min-height: 64px; padding: 0; margin: 0;
  border: 0; outline: 0; background: transparent;
  font: 500 21px/var(--row) var(--ui); color: var(--text);
  transition: background .3s;
}
#input::placeholder { color: var(--muted); opacity: .75; font-weight: 400; }
#input:disabled { opacity: .5; }
#input.flash-ok { background: color-mix(in srgb, var(--ok-soft) 70%, transparent); }

/* Gợi ý: tô dạ quang */
.hint-box { margin-bottom: var(--row); }
.hint-label { font-size: 13px; font-weight: 600; color: var(--muted); line-height: var(--row); }
.hint-text {
  display: inline; padding: 0 2px;
  font: 500 21px/var(--row) var(--ui); letter-spacing: .5px; white-space: pre-wrap;
  background: linear-gradient(180deg, transparent 52%, var(--fluo) 52%, var(--fluo) 88%, transparent 88%);
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
}
@media (prefers-color-scheme: dark) {
  .hint-text { color: #171c4a; background: linear-gradient(180deg, transparent 18%, var(--fluo) 18%, var(--fluo) 92%, transparent 92%); }
}
.hint-box.answer .hint-label { color: var(--warn); }

/* Chấm bài: như bút đỏ của cô giáo */
.feedback { margin-top: 0; }
.feedback .summary { font-weight: 700; font-size: 15px; line-height: var(--row); }
.feedback.ok .summary { color: var(--ok); }
.feedback.bad .summary { color: var(--err); }
.feedback .marked { font: 500 21px/var(--row) var(--ui); }
.tok-wrong { color: var(--err); text-decoration: underline wavy var(--err); text-underline-offset: 5px; text-decoration-thickness: 1.5px; }
.tok-case { color: var(--err); text-decoration: underline dotted var(--err); text-underline-offset: 5px; text-decoration-thickness: 2px; }
.tok-extra { color: var(--err); text-decoration: line-through 2px var(--err); }
.tok-missing {
  display: inline-block; min-width: 2.2em; height: 1em; vertical-align: -3px; margin: 0 3px;
  border: 2px solid var(--err); border-top: 0; border-radius: 0 0 4px 4px; background: var(--err-soft);
}
.tok-missing.punct { min-width: .8em; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 13px; color: var(--muted); line-height: var(--row); }
.legend i { font-style: normal; font-weight: 600; margin-right: 2px; }
.legend i.tok-missing { min-width: 1.6em; height: .9em; }
.legend.warn { color: var(--warn); }

/* Các câu đã chép: viết tay vào vở, số thứ tự nằm ở lề */
.done { margin-top: var(--row); }
.done h3 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); line-height: var(--row); }
.done ol { list-style: none; margin: 0; padding: 0; counter-reset: s; }
.done li {
  position: relative; counter-increment: s;
  font: 300 17px/var(--row) var(--hand); color: var(--ink);
}
.done li::before {
  content: counter(s); position: absolute; right: calc(100% + 34px);
  font: 600 13px/var(--row) var(--ui); color: var(--muted);
}
.tag {
  font: 600 11px var(--ui); margin-left: 10px; padding: 2px 8px; border-radius: 999px; vertical-align: 3px; white-space: nowrap;
}
.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.skip { background: var(--err-soft); color: var(--err); }
.tag.hint { background: var(--fluo); color: #3d3500; }
.tag.answer { background: var(--err-soft); color: var(--warn); }

.writing-actions { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-top: 14px; }
.kbd-list { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; color: var(--muted); padding-top: 8px; }
kbd {
  font: 600 11.5px var(--ui); color: var(--text); background: var(--paper);
  border: 1.5px solid var(--line); border-bottom-width: 2.5px; border-radius: 6px; padding: 1px 6px; margin-right: 5px;
}
.check-buttons { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; flex: none; }
.restart-row { margin-top: 26px; }

/* ---------- Nút ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer; white-space: nowrap;
  height: 40px; padding: 0 16px; border-radius: 999px;
  border: 1.5px solid var(--line); background: var(--paper); color: var(--text);
  font-size: 14px; font-weight: 600;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover:not(:disabled) { border-color: var(--ink); color: var(--ink); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--on-ink); }
.btn.primary:hover:not(:disabled) { background: var(--ink-press); border-color: var(--ink-press); color: var(--on-ink); }
.btn.ghost { background: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--ink-soft); border-color: var(--ink-soft); }
.btn.small { height: 32px; padding: 0 12px; font-size: 13px; }
.btn.link { height: auto; padding: 4px 2px; border: 0; background: none; color: var(--ink); font-weight: 600; }
.btn.link:hover:not(:disabled) { text-decoration: underline; text-underline-offset: 3px; }
.btn.link .ic { width: 16px; height: 16px; }
.icon-btn { width: 40px; padding: 0; justify-content: center; }
.btn.danger:hover:not(:disabled) { border-color: var(--err); color: var(--err); background: var(--err-soft); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.disabled { opacity: .45; pointer-events: none; }

/* ---------- Hộp thoại ---------- */
dialog {
  border: 0; border-radius: 22px; padding: 0; box-shadow: 0 40px 80px -30px rgba(10, 14, 50, .6);
  background: var(--paper); color: var(--text); width: min(700px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(14, 18, 48, .45); backdrop-filter: blur(3px); }
.editor-form { padding: 26px 28px; display: flex; flex-direction: column; gap: 14px; }
.editor-form h2 { margin: 0; font-size: 24px; font-weight: 800; letter-spacing: -.02em; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.field input, .field textarea {
  font-weight: 400; color: var(--text); background: var(--desk);
  border: 1.5px solid transparent; border-radius: 12px; padding: 10px 12px; font-size: 15px;
}
.field textarea { resize: vertical; line-height: 1.6; }
.field input:focus, .field textarea:focus { outline: 0; border-color: var(--ink); background: var(--paper); }
.audio-box { border: 1.5px dashed var(--seyes-main); border-radius: 14px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.audio-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.audio-row select { font-size: 13px; }
.audio-status { font-size: 13px; color: var(--muted); }
.audio-status:empty { display: none; }
.audio-status.err { color: var(--err); }
.audio-status.ok { color: var(--ok); }
.progress-line { height: 6px; border-radius: 3px; background: var(--seyes); overflow: hidden; }
#ed-audio-bar { height: 100%; width: 0; background: var(--ink); border-radius: 3px; transition: width .3s; }
.preview summary { cursor: pointer; color: var(--ink); font-size: 14px; font-weight: 600; }
.preview ol { max-height: 200px; overflow-y: auto; font-size: 14px; color: var(--muted); }
.note { margin: 0; font-size: 13px; color: var(--muted); }
.editor-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Màn hình nhỏ ---------- */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: auto; min-height: 100vh; }
  .library { border-right: 0; border-bottom: 1.5px solid var(--line); max-height: 46vh; }
  .practice { padding: 20px 16px 40px; }
  .player { grid-template-columns: 1fr; }
  .player-actions { justify-content: flex-start; }
  .sheet { --margin: 44px; padding-right: 16px; }
  .title-row h2 { font-size: 26px; }
  .writing-actions { flex-direction: column-reverse; align-items: stretch; }
  .check-buttons { justify-content: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ---------- Điện thoại / máy tính bảng ---------- */
.lib-toggle { display: none; }
@media (max-width: 860px) {
  .library { max-height: none; padding-top: calc(16px + env(safe-area-inset-top)); }
  .lib-toggle {
    display: flex; align-items: center; justify-content: space-between; width: 100%;
    margin: 0 0 12px; padding: 10px 14px; border: 1.5px solid var(--line); border-radius: 12px;
    background: var(--paper); cursor: pointer; font-weight: 600; font-size: 14px; color: var(--ink);
  }
  .lib-toggle .ic { transform: rotate(-90deg); transition: transform .15s; }
  .lib-collapsed .lib-toggle .ic { transform: rotate(90deg); }
  .lesson-list { max-height: 52vh; }
  .lib-collapsed .lesson-list, .lib-collapsed .library-foot, .lib-collapsed .add-btn { display: none; }
  .lib-collapsed .library { padding-bottom: 4px; }
  .lib-collapsed .brand { padding-bottom: 12px; }
  .practice { padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
}
@media (hover: none) and (pointer: coarse) {
  .kbd-list { display: none; }
  .btn { height: 44px; }
  .icon-btn { width: 44px; }
  .lesson-list li { padding-top: 11px; padding-bottom: 11px; }
  input[type=range] { width: 120px; }
}

/* ---------- Gói nghe ---------- */
[data-level="pack"] { --lv: #0e9fb3; }
.pack-actions { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 0 8px 12px; }
.lesson-list li.group.pack .g-name .ic:not(:first-child) { width: 15px; height: 15px; color: var(--lv); transform: none; }
.g-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lesson-list li.group.pack { flex-wrap: wrap; }
.lesson-list li.group.pack .g-name { flex: 1; min-width: 0; }
.g-actions { display: flex; gap: 2px; width: 100%; padding-left: 22px; }
.g-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 28px;
  border: 0; border-radius: 8px; background: none; color: var(--muted); cursor: pointer;
}
.g-btn .ic { width: 16px; height: 16px; }
.g-btn:hover { background: var(--ink-soft); color: var(--ink); }
.pk-list { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; font-size: 13px; }
.pk-list:empty { display: none; }
.pk-list li { display: flex; justify-content: space-between; gap: 12px; padding: 4px 2px; border-bottom: 1px solid var(--line); }
.pk-list .pk-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pk-list .pk-st { color: var(--muted); white-space: nowrap; flex: none; max-width: 55%; overflow: hidden; text-overflow: ellipsis; }
.pk-list li.running .pk-st { color: var(--ink); font-weight: 600; }
.pk-list li.done .pk-st { color: var(--ok); }
.pk-list li.err .pk-st { color: var(--err); }
#pk-bar { height: 100%; width: 0; background: var(--ink); border-radius: 3px; transition: width .3s; }
.lib-collapsed .pack-actions { display: none; }
@media (hover: none) and (pointer: coarse) { .g-btn { width: 40px; height: 36px; } }
