/* TextOptimus: "Bút dạ" style (black outlines, hard shadows, highlighter yellow) */
:root {
  color-scheme: light;
  --ground: #FFFFFF;
  --dots: #D5D5CF;
  --surface: #FFFFFF;
  --s2: #F4F4F1;
  --ink: #0B0B0F;
  --muted: #454552;
  --line: #0B0B0F;
  --hard: #0B0B0F;
  --accent: #2B3CFF;
  --on-accent: #FFFFFF;
  --link: #2B3CFF;
  --tab-on: #FFE600;
  --tab-on-ink: #0B0B0F;
  --mark: #FFE600;
  --mark-ink: #0B0B0F;
  --mark-soft: #FFF59E;
  --mark-soft-ink: #0B0B0F;
  --warn-bg: #FFE3C2;
  --warn-ink: #5C2C00;
  --ok-bg: #C9F7D5;
  --ok-ink: #07481F;
  --bad-bg: #FFD6D0;
  --bad-ink: #7A140A;
  --off: #E7E7E2;
  --off-ink: #454552;
  --font-ui: 'Be Vietnam Pro', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-head: 'Space Grotesk', 'Be Vietnam Pro', system-ui, sans-serif;
  --font-read: 'Literata', Georgia, 'Times New Roman', serif;
  --r: 12px;
  --r-sm: 8px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground: #121216;
  --dots: #2C2C34;
  --surface: #1C1C22;
  --s2: #26262E;
  --ink: #F6F6F0;
  --muted: #BDBDC8;
  --line: #F6F6F0;
  --hard: #8E9BFF;
  --accent: #8E9BFF;
  --on-accent: #0B0B0F;
  --link: #B4BDFF;
  --tab-on: #FFE600;
  --tab-on-ink: #0B0B0F;
  --mark: #FFE600;
  --mark-ink: #0B0B0F;
  --mark-soft: #4A4400;
  --mark-soft-ink: #FFF59E;
  --warn-bg: #3A2408;
  --warn-ink: #FFD29A;
  --ok-bg: #0F3A1E;
  --ok-ink: #9BF0B6;
  --bad-bg: #421512;
  --bad-ink: #FFB8AE;
  --off: #33333C;
  --off-ink: #BDBDC8;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: var(--ground);
  background-image: radial-gradient(var(--dots) 1px, transparent 1px);
  background-size: 22px 22px;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p, dl, dd, ul { margin: 0; }
ul { padding: 0; list-style: none; }
a { color: var(--link); }
[hidden] { display: none !important; }
/* Text is filled in by js/i18n.js; keep the empty shell from flashing until then */
:root[data-lang-pending] body { visibility: hidden; }
.lang-btn { font-size: 13px; font-weight: 700; letter-spacing: .02em; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

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

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 2px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, background-color .12s ease;
}
.btn:hover { background: var(--s2); }
.btn:active { transform: translate(1px, 1px); }
.btn--primary { background: var(--accent); color: var(--on-accent); box-shadow: 3px 3px 0 var(--hard); }
.btn--primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn--primary:active { transform: translate(3px, 3px); box-shadow: 0 0 0 var(--hard); }
.btn--quiet { border-color: transparent; background: transparent; color: var(--link); font-weight: 500; }
.btn--quiet:hover { background: var(--s2); }
.btn--icon { width: 44px; padding: 0; border-color: transparent; background: transparent; color: var(--ink); }
.btn--icon:hover { background: var(--s2); }
.btn--block { width: 100%; }
.btn--tiny { width: 36px; min-height: 36px; }
.btn--small { font-size: 14px; }
.btn[disabled] {
  border-color: transparent;
  background: var(--off);
  color: var(--off-ink);
  box-shadow: none;
  filter: none;
  transform: none;
  cursor: not-allowed;
}
.btn .dot { width: 9px; height: 9px; border-radius: 50%; border: 2px solid currentColor; }
.btn .dot[data-on="true"] { background: currentColor; }

/* App bar */
.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  padding: 8px 18px;
  background: var(--surface);
  border-bottom: 2px solid var(--line);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-family: var(--font-head);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -.03em;
}
.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: var(--tab-on);
  color: var(--tab-on-ink);
  box-shadow: 2px 2px 0 var(--hard);
  font-size: 15px;
}
.tabs {
  flex: 1 1 340px;
  display: flex;
  justify-content: center;
}
.tabs__inner {
  display: flex;
  gap: 4px;
  width: 100%;
  max-width: 440px;
  padding: 3px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--s2);
}
.tab {
  flex: 1 1 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.tab:hover { color: var(--ink); }
.tab[aria-current="page"], .tab[aria-pressed="true"] {
  background: var(--tab-on);
  color: var(--tab-on-ink);
  box-shadow: inset 0 0 0 2px var(--line);
}
:root[data-theme="dark"] .tab[aria-current="page"], :root[data-theme="dark"] .tab[aria-pressed="true"] {
  box-shadow: none;
}
.bar__tools { display: flex; align-items: center; gap: 2px; margin-left: auto; }

/* Layout */
.page {
  flex: 1 1 auto;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 22px 18px;
}
.card {
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--r);
  box-shadow: 5px 5px 0 var(--hard);
}
.title {
  font-family: var(--font-head);
  font-weight: 700;
  letter-spacing: -.03em;
  line-height: 1.2;
  text-wrap: balance;
}
.muted { color: var(--muted); }
.small { font-size: 14px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.row--between { justify-content: space-between; }

/* Start screen */
.start { max-width: 760px; margin: 12px auto 40px; }
.start .card { padding: 26px; }
.start__title { display: block; margin-bottom: 16px; font-size: clamp(28px, 5vw, 40px); }
.input {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--s2);
  color: var(--ink);
  font: inherit;
  resize: vertical;
}
.input:focus { background: var(--surface); }
.input--read { font-family: var(--font-read); font-size: 18px; line-height: 1.65; }
.input[aria-invalid="true"] { border-color: var(--bad-ink); background: var(--bad-bg); }
.count[data-over="true"] { color: var(--bad-ink); font-weight: 600; }
.label { display: block; margin-bottom: 7px; font-weight: 600; }

/* Workspace */
.work { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 22px; }
.reader-panel { flex: 999 1 560px; min-width: 0; padding: 28px 36px; }
.rail { flex: 1 1 400px; min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.work[data-view="practice"] .reader-panel,
.work[data-view="export"] .reader-panel { flex: 1 1 480px; }
.work[data-view="practice"] .rail,
.work[data-view="export"] .rail { flex: 1 1 480px; }

.reader-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 18px; }
.reader {
  max-width: 68ch;
  font-family: var(--font-read);
  font-size: 20px;
  line-height: 1.85;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.w {
  display: inline;
  margin: 0;
  padding: 1px 1px;
  border: 0;
  border-radius: 2px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
.w:hover { background: var(--mark-soft); color: var(--mark-soft-ink); }
.w[data-state="marked"], .w[data-state="marked"]:hover {
  background: linear-gradient(transparent 12%, var(--mark) 12%, var(--mark) 92%, transparent 92%);
  color: var(--mark-ink);
}
.w[data-state="done"], .w[data-state="done"]:hover { background: var(--mark-soft); color: var(--mark-soft-ink); }
.w[data-active="true"] { outline: 2px solid var(--ink); outline-offset: 1px; }
.w[data-hard="true"] { text-decoration: underline dotted var(--muted); text-decoration-thickness: 2px; text-underline-offset: 4px; }
.gloss { font-family: var(--font-ui); font-size: 14.5px; font-weight: 500; color: var(--link); }
.gloss .w { color: inherit; }

.level-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-bottom: 16px; padding-bottom: 14px; border-bottom: 2px solid var(--line); font-size: 14px; font-weight: 600; }
.level-bar .muted { flex: 1 1 260px; font-weight: 400; }
.select { min-height: 40px; padding: 0 10px; border: 2px solid var(--line); border-radius: var(--r-sm); background: var(--s2); color: var(--ink); font: inherit; font-weight: 600; }

/* Rail blocks */
.block { padding: 20px; }
.word-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 6px; }
.word-title { font-family: var(--font-read); font-size: 27px; font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; }
.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border: 2px solid var(--line);
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 600;
}
.pill--ai { min-height: 20px; padding: 0 5px; font-size: 11px; }
.quote {
  padding: 11px 13px;
  border-radius: var(--r-sm);
  background: var(--s2);
  font-family: var(--font-read);
  font-size: 16px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: var(--mark-soft);
  color: var(--mark-soft-ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.chip[data-pending="true"] { background: var(--mark); color: var(--mark-ink); }
.chip[aria-pressed="true"] { box-shadow: 2px 2px 0 var(--hard); }

.alert {
  padding: 13px 15px;
  border: 2px solid var(--warn-ink);
  border-radius: var(--r-sm);
  background: var(--warn-bg);
  color: var(--warn-ink);
}
.alert .row { margin-top: 10px; }
.alert .btn { border-color: var(--warn-ink); background: transparent; color: var(--warn-ink); }

.working { display: flex; flex-direction: column; gap: 8px; }
.loader { position: relative; height: 8px; overflow: hidden; background: var(--s2); box-shadow: inset 0 0 0 2px var(--line); }
.loader::after {
  content: '';
  position: absolute;
  inset: 0 auto 0 0;
  width: 38%;
  background: var(--accent);
  animation: slide 1.3s ease-in-out infinite;
}
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(280%); } }

.facts { display: grid; gap: 12px; margin-top: 14px; }
.facts dt { margin-bottom: 2px; font-size: 12.5px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.facts ul { display: grid; gap: 4px; }

/* Practice */
.seg { display: flex; gap: 4px; padding: 3px; margin-bottom: 18px; border: 2px solid var(--line); border-radius: 10px; background: var(--s2); }
.score { font-family: var(--font-head); font-size: 30px; font-weight: 700; letter-spacing: -.03em; line-height: 1.1; }
.tile { padding: 11px 13px; border-radius: var(--r-sm); background: var(--s2); }
.tile__label { font-size: 12.5px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.meter { height: 8px; margin-top: 8px; overflow: hidden; background: var(--off); }
.meter > i { display: block; height: 100%; background: var(--accent); }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.bullets { display: grid; gap: 6px; padding-left: 18px; list-style: disc; }
.question { padding: 16px 0; border-top: 2px solid var(--line); }
.question:first-child { border-top: 0; padding-top: 0; }
.ex { font-family: var(--font-read); font-size: 18px; line-height: 2.25; }
.gap {
  width: 9em;
  max-width: 100%;
  padding: 1px 8px;
  border: 0;
  border-bottom: 3px solid var(--line);
  background: var(--s2);
  color: var(--ink);
  font: inherit;
  text-align: center;
}
.gap[data-result="ok"] { border-bottom-color: var(--ok-ink); background: var(--ok-bg); color: var(--ok-ink); }
.gap[data-result="bad"] { border-bottom-color: var(--bad-ink); background: var(--bad-bg); color: var(--bad-ink); }
.note { margin-top: 2px; font-family: var(--font-ui); font-size: 14px; line-height: 1.5; }
.note[data-result="bad"] { color: var(--bad-ink); }
.note[data-result="ok"] { color: var(--ok-ink); }

/* Export */
.vocab > li { padding: 11px 0; border-top: 2px solid var(--line); }
.vocab > li:first-child { border-top: 0; padding-top: 0; }
.vocab__row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* Dialogs */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 2px solid var(--line);
  border-radius: var(--r);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 6px 6px 0 var(--hard);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg { padding: 22px; }
.dlg__head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.dlg__title { font-size: 21px; }
.steps { display: grid; gap: 14px; counter-reset: step; }
.steps > li { padding-left: 34px; position: relative; counter-increment: step; }
.steps > li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 1px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: var(--tab-on);
  color: var(--tab-on-ink);
  font-size: 13px;
  font-weight: 700;
}
.status[data-kind="ok"] { color: var(--ok-ink); }
.status[data-kind="bad"] { color: var(--bad-ink); }

.foot { padding: 14px 18px 20px; text-align: center; font-size: 13.5px; color: var(--muted); }

/* Phones: the rail becomes a sheet over the bottom of the text */
.sheet-toggle { display: none; }
@media (max-width: 900px) {
  .bar { padding: 6px 10px; }
  .back-label, .tool-label { display: none; }
  .bar .tabs { order: 5; flex-basis: 100%; }
  .tabs__inner { max-width: none; }
  .page { padding: 14px 12px; }
  .work { gap: 14px; }
  .reader-panel { flex-basis: 100%; padding: 18px 16px; }
  .reader { font-size: 19px; }
  .rail { flex-basis: 100%; }
  .work[data-view="read"] { padding-bottom: var(--sheet-space, 120px); }
  .work[data-view="read"] .rail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 4;
    gap: 0;
    max-height: 62vh;
    overflow-y: auto;
    padding: 12px 14px 14px;
    background: var(--surface);
    border-top: 2px solid var(--line);
    border-radius: 16px 16px 0 0;
  }
  .work[data-view="read"] .rail .card { border: 0; box-shadow: none; background: transparent; padding: 0 0 10px; border-radius: 0; }
  .work[data-view="read"] .word-empty,
  .work[data-view="read"] .rail .quote { display: none; }
  /* The main action stays reachable when the word card fills the sheet */
  .work[data-view="read"] .rail > section:last-child { position: sticky; bottom: -14px; padding-bottom: 14px; margin-bottom: -14px; background: var(--surface); }
  .work[data-view="read"] .marked-chips { flex-wrap: nowrap; overflow-x: auto; margin-bottom: 10px !important; padding-bottom: 4px; }
  .work[data-view="read"] .chip { flex: 0 0 auto; }
  .w { scroll-margin-top: 130px; }
  #new-text-btn { white-space: nowrap; }
  .work[data-view="practice"] .reader-panel[data-open="false"],
  .work[data-view="export"] .reader-panel[data-open="false"] { display: none; }
  .sheet-toggle { display: inline-flex; }
  .foot { padding-bottom: 20px; }
}

/* Four tools and the name do not fit one row on a phone: the logo stands in for the name */
@media (max-width: 420px) {
  .brand__name { display: none; }
}

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

@media print {
  .bar, .rail, .foot { display: none; }
  .card { box-shadow: none; border: 0; }
}
