/* M1racle Study v11
   Radix Colors: Sage (grey) + Blue 11 (accent), official light and dark scales.
   Type: 12 / 14 / 18 / 28 only. Spacing on a 4px grid. Hairlines 0.5px Sage 6.
   Radius: cards 24 (16 padding) around 8px controls, concentric. */

@layer reset, tokens, base, layout, components, states, enhance;

@layer tokens {
  :root {
    color-scheme: light dark;
    --sage-1: light-dark(#fbfdfc, #101211);
    --sage-2: light-dark(#f7f9f8, #171918);
    --sage-3: light-dark(#eef1f0, #202221);
    --sage-4: light-dark(#e6e9e8, #272a29);
    --sage-6: light-dark(#d7dad9, #373b39);
    --sage-11: light-dark(#5f6563, #adb5b2);
    --sage-12: light-dark(#1a211e, #eceeed);
    --blue-11: light-dark(#0d74ce, #70b8ff);
    --red-11: light-dark(#ce2c31, #ff9592);

    --bg: var(--sage-1);
    --sidebar: var(--sage-2);
    --hover: var(--sage-3);
    --line: var(--sage-6);
    --muted: var(--sage-11);
    --text: var(--sage-12);
    --accent: var(--blue-11);
    --error: var(--red-11);
    --surface: light-dark(#fff, var(--sage-2));
    --raised: light-dark(#fff, var(--sage-4));
    --under: light-dark(var(--sage-2), var(--sage-3));
    --btn-hover: light-dark(#2a322e, #fff);
    --toast-shadow: light-dark(0 4px 16px rgb(26 33 30 / .12), 0 4px 16px rgb(0 0 0 / .45));
    --shade-1: light-dark(rgb(26 33 30 / .11), rgb(0 0 0 / .28));
    --shade-2: light-dark(rgb(26 33 30 / .06), rgb(0 0 0 / .16));
    --page-shade: light-dark(rgb(26 33 30 / .16), rgb(0 0 0 / .32));

    --fs-12: 0.75rem;
    --fs-14: 0.875rem;
    --fs-18: 1.125rem;
    --fs-28: 1.75rem;
    --r-ctl: 8px;
    --r-card: 24px;
    --r-page: 16px;
    --hair: .5px solid var(--line);
    --font: system-ui, -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    --ease: 250ms ease-out;
    --ease-slow: 400ms ease-out;
  }
  :root[data-theme="light"] { color-scheme: light; }
  :root[data-theme="dark"] { color-scheme: dark; }
}

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  [hidden]:not([hidden="until-found"]) { display: none !important; }
  [hidden="until-found"] { content-visibility: hidden; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  body { margin: 0; }
  h1, h2, h3, p { margin: 0; }
  button, input, select, textarea { font: inherit; color: inherit; }
  button { cursor: pointer; border: 0; background: none; padding: 0; }
  a { color: inherit; text-decoration: none; }
  img { display: block; max-width: 100%; }
  /* Without scripting the loading state never clears, so the regions must show
     themselves. This replaces the old <noscript><style> blocks, which the CSP
     can no longer allow. */
  @media (scripting: none) {
    :root.is-loading [data-loading-content] { visibility: visible; pointer-events: auto; }
    :root.is-loading .startup-region { min-block-size: 0; }
  }
}

@layer base {
  html, body { background: var(--bg); color: var(--text); }
  html { scrollbar-color: var(--sage-6) transparent; scrollbar-width: thin; }
  body { font: var(--fs-14)/1.55 var(--font); -webkit-font-smoothing: antialiased; overflow-wrap: anywhere; text-wrap: pretty; }
  h1 { font-size: var(--fs-28); font-weight: 600; letter-spacing: -.03em; line-height: 1.2; text-wrap: balance; }
  h2 { font-size: var(--fs-18); font-weight: 600; letter-spacing: -.02em; line-height: 1.3; text-wrap: balance; }
  :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .tabular { font-variant-numeric: tabular-nums; }
  .visually-hidden {
    position: absolute; width: 1px; height: 1px; padding: 0;
    overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
  }
  .skip-link {
    position: fixed; inset: 8px auto auto 8px; z-index: 50; padding: 8px 12px;
    border-radius: var(--r-ctl); background: var(--text); color: var(--bg);
    translate: 0 -200%;
  }
  .skip-link:focus-visible { translate: none; }
}

@layer layout {
  .app { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 100dvh; }
  .sidebar {
    position: sticky; top: 0; height: 100dvh;
    display: flex; flex-direction: column;
    padding: 28px 16px 24px;
    background: var(--sidebar); border-right: var(--hair);
  }
  .brand { display: flex; align-items: center; gap: 12px; min-height: 32px; padding: 0 8px; margin-bottom: 32px; width: max-content; border-radius: var(--r-ctl); }
  .brand img { height: 28px; width: auto; }
  .brand span { font-size: var(--fs-18); font-weight: 600; letter-spacing: -.03em; line-height: 1; translate: 0 -1px; }
  .side-nav { display: flex; flex-direction: column; gap: 4px; }
  .side-nav a {
    display: flex; align-items: center; min-height: 2.25rem; padding: 8px 12px;
    border-radius: var(--r-ctl); color: var(--muted);
  }
  .side-nav a:hover { background: var(--hover); color: var(--text); }
  .side-nav a[aria-current="page"] { background: var(--hover); color: var(--text); font-weight: 600; }

  .top-mobile, .mobile-bar { display: none; }
  .main { min-width: 0; padding: 32px 48px 64px; }
  .page { max-width: 720px; }
  .head { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 4px 16px; margin: 0 0 28px; }
  .head .meta { color: var(--muted); }
  .section { margin-top: 36px; }
  .section-h { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 4px 16px; margin: 0 0 12px; }
  .section-h .meta { color: var(--muted); font-variant-numeric: tabular-nums; }

  @media (max-width: 850px) {
    .app { display: block; }
    .sidebar { display: none; }
    .top-mobile {
      display: flex; align-items: center; height: 56px; padding: 0 16px;
      background: var(--bg); border-bottom: var(--hair);
    }
    .top-mobile .brand { margin: 0; padding: 0; }
    .main { padding: 24px 16px max(96px, 7rem); }
    .page { max-width: none; }
    .head { margin-bottom: 20px; }
    .mobile-bar {
      position: fixed; inset: auto 0 0 0; z-index: 20;
      display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
      padding: 8px 8px max(8px, env(safe-area-inset-bottom));
      background: var(--bg); border-top: var(--hair);
    }
    .mobile-bar a {
      display: grid; place-items: center; min-height: max(44px, 2.75rem);
      border-radius: var(--r-ctl); color: var(--muted); font-size: var(--fs-12);
    }
    .mobile-bar a[aria-current="page"] { background: var(--hover); color: var(--text); font-weight: 600; }
    :root.keyboard-open .mobile-bar { display: none; }
    :root.keyboard-open .main { padding-bottom: 24px; }
  }
  @media (max-width: 360px) {
    .main { padding-inline: 12px; }
  }
}

@layer components {
  /* Buttons */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 2.625rem; padding: 8px 16px; border-radius: var(--r-ctl);
    background: var(--text); color: var(--bg); font-weight: 500; white-space: nowrap;
    transition: background-color var(--ease);
  }
  .btn:hover { background: var(--btn-hover); }
  .btn-quiet { background: var(--sage-3); color: var(--text); }
  .btn-quiet:hover { background: var(--sage-4); }
  .btn-quiet[aria-pressed="true"] { background: var(--text); color: var(--bg); }
  .btn:disabled { opacity: .5; cursor: not-allowed; }
  .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 28px; }

  /* Rows */
  .list { border-top: var(--hair); }
  .row {
    display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 4px 16px;
    width: 100%; min-height: max(48px, 3rem); padding: 12px 0;
    border-bottom: var(--hair); text-align: start; color: var(--text);
  }
  :is(a, button).row:hover { background: var(--hover); margin-inline: -12px; padding-inline: 12px; width: calc(100% + 24px); }
  .row .name { font-weight: 500; }
  .row-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .sub { color: var(--muted); font-size: var(--fs-12); }
  .counts { display: flex; flex-wrap: wrap; gap: 4px 12px; color: var(--muted); font-size: var(--fs-12); font-variant-numeric: tabular-nums; }
  .counts b { color: var(--text); font-size: var(--fs-14); font-weight: 500; }
  .review-head { align-items: center; }
  .review-head .btn { min-block-size: 44px; }
  .review-head .meta { margin-top: 8px; }
  .import.actions:has(> :not([hidden]).import-progress) { margin-bottom: 28px; }
  .import.actions:not(:has(> :not([hidden]).import-progress, > .is-invalid)) { margin-bottom: 0; }
  .deck-columns { display: flex; align-items: center; justify-content: space-between; padding: 12px 0 12px 44px; color: var(--muted); }
  .deck-columns > span:first-child, .deck-columns .deck-counts > span { font-size: var(--fs-12); }
  .deck-tree { list-style: none; margin: 0; padding: 0; }
  .deck-line { display: flex; align-items: stretch; padding-inline-start: var(--deck-indent, 0px); border-bottom: var(--hair); }
  .deck-line:hover { background: var(--hover); }
  .deck-toggle, .deck-toggle-space { flex: 0 0 44px; inline-size: 44px; min-block-size: 44px; }
  .deck-toggle { display: grid; place-items: center; border-radius: var(--r-ctl); color: var(--muted); align-self: flex-start; margin-top: 4px; }
  .deck-toggle::before { content: ''; inline-size: 16px; block-size: 16px; background: currentColor; mask: url('/assets/chevron-right.svg') center / contain no-repeat; }
  .deck-toggle[aria-expanded="true"]::before { rotate: 90deg; }
  .deck-row { flex: 1; min-width: 0; width: auto; gap: 4px 8px; border-bottom: 0; padding: 16px 0; }
  .deck-row .row-main { flex: 1 1 8em; }
  .deck-row .deck-counts { margin-inline-start: auto; }
  .deck-line .deck-row:hover { margin-inline: 0; padding-inline: 0; width: auto; background: none; }
  .deck-counts { display: grid; grid-template-columns: repeat(3, var(--deck-count-width, 32px)); gap: 4px; justify-items: end; flex-shrink: 0; font-size: var(--fs-14); }
  .deck-number b { font-weight: 500; }
  .deck-number[data-count="new"] { color: var(--accent); }
  .deck-number[data-zero="true"] { color: var(--muted); }
  .note { color: var(--muted); font-size: var(--fs-12); margin-top: 8px; }
  .cloze-meta { margin: 0 0 12px; }

  /* Card surface shared by block, lesson, anki, empty, status, stats */
  .card {
    border: var(--hair); border-radius: var(--r-card);
    background: var(--surface);
  }

  /* 今天要做的事 */
  .block { overflow: clip; }
  .block .row { padding-inline: 16px; min-height: max(52px, 3.25rem); }
  .block :is(a, button).row:hover { margin-inline: 0; padding-inline: 16px; width: 100%; }
  .continue {
    display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px 16px;
    padding: 16px;
  }
  .block-empty { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px 16px; padding: 16px; min-height: 72px; }
  /* 汇总行：不参与 .row 的计数，避免和牌组行混在一起 */
  .block-note { padding: 12px 16px; border-top: var(--hair); }
  /* 上课记录：正文占满，删除按钮留在同一行右侧 */
  .session-row .row-main { flex: 1 1 12em; }
  .session-row > .btn { flex: none; }

  /* 背书滚动 */
  .plan-today {
    display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px 16px;
    padding: 16px; border-bottom: var(--hair);
  }
  .plan-phase { padding: 0 16px 12px; }
  .plan-phase ul { margin: 0; padding: 0 0 0 16px; color: var(--muted); font-size: var(--fs-12); }
  .plan-phase li { margin: 4px 0 0; }
  .plan-today > .btn, .plan-foot > .btn { margin-inline-start: auto; }
  .plan-task + .plan-task { margin-top: 12px; padding-top: 12px; border-top: var(--hair); }
  .plan-task .name { font-weight: 500; }
  .plan-links { margin: 8px 0 0; }
  .plan-foot {
    display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px 16px;
    padding: 12px 16px;
  }
  .plan-settings { padding: 16px; border-top: var(--hair); }
  .plan-settings .field { margin: 0 0 16px; }
  .plan-settings .actions { margin: 0; }
  .plan-settings select.input { width: 100%; max-width: 360px; }

  /* Segmented subject buttons */
  .seg { display: flex; flex-wrap: wrap; gap: 4px; margin: 0 0 16px; }
  .seg button { min-height: 2.25rem; padding: 8px 12px; border-radius: var(--r-ctl); color: var(--muted); }
  .seg button:hover { color: var(--text); }
  .seg button[aria-pressed="true"] { background: var(--hover); color: var(--text); font-weight: 600; }

  /* Lesson card */
  .lesson-card { padding: 16px; }
  .card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 0 0 16px; }
  .card-top > div { flex: 1; min-width: 0; }
  .card-top .num { padding-top: 4px; color: var(--muted); font-size: var(--fs-12); font-variant-numeric: tabular-nums; white-space: nowrap; }
  .card-fields { display: flex; flex-wrap: wrap; gap: 16px 24px; margin: 0 0 16px; border: 0; padding: 0; min-inline-size: 0; }
  .card-field { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
  .card-field > .label { color: var(--muted); font-size: var(--fs-12); }
  .pair { display: flex; align-items: center; gap: 8px; }
  .pair .slash { color: var(--muted); }
  .pair input { width: 4.5rem; min-width: 0; }
  .card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

  /* Inputs */
  .input, .field input, .pair input {
    height: 2.625rem; padding: 0 12px; border: var(--hair); border-radius: var(--r-ctl);
    background: var(--surface); font-size: var(--fs-14); font-variant-numeric: tabular-nums;
  }
  .field { display: flex; flex-direction: column; gap: 8px; max-width: 360px; margin: 0 0 16px; }
  .field label { font-weight: 500; }
  .error-msg { display: none; margin: 0; color: var(--error); font-size: var(--fs-12); }
  /* Autofill keeps the Sage surface instead of Chrome's yellow. */
  :is(input, textarea):autofill {
    box-shadow: inset 0 0 0 100px var(--surface);
    -webkit-text-fill-color: var(--text);
    caret-color: var(--text);
  }

  /* Anki */
  .deck-row .counts { justify-content: flex-end; }
  .back { display: inline-flex; align-items: center; min-height: 2.25rem; margin: 0 0 8px; color: var(--muted); border-radius: var(--r-ctl); }
  .back:hover { color: var(--text); }
  .card-context { margin: -16px 0 16px; }
  .anki-card {
    min-height: 200px; padding: 16px;
    font-size: var(--fs-18); line-height: 1.7; letter-spacing: -.02em;
  }
  .anki-card img { display: inline-block; height: auto; border-radius: var(--r-ctl); }
  .anki-card .answer { margin-top: 16px; padding-top: 16px; border-top: var(--hair); }
  .cloze {
    display: inline; padding: 0 8px; border-radius: var(--r-ctl);
    background: var(--sage-3); font: inherit; line-height: 1.4;
    -webkit-box-decoration-break: clone; box-decoration-break: clone;
  }
  button.cloze { display: inline-block; min-width: 56px; height: 1.5rem; vertical-align: baseline; background: var(--sage-4); color: transparent; cursor: pointer; user-select: none; overflow: hidden; }
  button.cloze .hint { color: var(--muted); font-size: var(--fs-14); }
  button.block-answer { display: block; width: 100%; height: 3rem; margin-top: 16px; }
  .image-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-block: 8px 12px; }
  .image-tools .btn { min-inline-size: 44px; min-block-size: 44px; }
  .image-tools [aria-disabled="true"] { opacity: .45; cursor: default; }
  .image-scale { margin-inline-start: auto; color: var(--muted); font-size: var(--fs-14); }
  .image-viewport {
    inline-size: 100%; min-block-size: 0; max-block-size: 70dvh;
    aspect-ratio: var(--image-ratio, auto); overflow: auto;
    overscroll-behavior-inline: contain;
  }
  .image-cloze { position: relative; inline-size: calc(100% * var(--image-scale, 1)); }
  .image-cloze > img { display: block; inline-size: 100%; block-size: auto; border-radius: 0; }
  .image-mask {
    position: absolute; inset-inline-start: var(--mask-x); top: var(--mask-y);
    inline-size: var(--mask-w); block-size: var(--mask-h);
    min-inline-size: 0; min-block-size: 0; margin: 0; padding: 0; border: 0;
    border-radius: 0; background: var(--accent); cursor: pointer;
  }
  .image-mask:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
  .cloze-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0 0; }
  .rate { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 28px; }
  .rate button { min-width: 72px; }

  /* Tab Pill: shared Design Bookmark tokens, styled with Study's Sage palette. */
  .pills {
    --spring-lead: 620 36;
    --spring-trail: 190 19;
    position: relative; isolation: isolate;
    display: inline-flex; flex-wrap: nowrap; gap: 4px; width: max-content; max-width: 100%;
    padding: 0 4px; margin: 0 0 24px; overflow-x: auto; scrollbar-width: none;
    border-radius: 999px; background: var(--sage-3); box-shadow: inset 0 0 0 1px var(--sage-4);
  }
  .pills [role="tab"] {
    position: relative; z-index: 1; flex: 0 0 auto;
    display: inline-flex; align-items: center; justify-content: center;
    min-height: max(36px, 2.25rem); padding: 4px 8px; border-radius: 999px;
    color: var(--muted); font-weight: 500; white-space: nowrap;
    transition: color var(--ease);
  }
  .pills [role="tab"]:hover { color: var(--text); }
  .pills [role="tab"][aria-selected="true"] { color: var(--text); }
  .pills:not(.has-tab-pill) [aria-selected="true"] { background: var(--raised); }
  .tab-pill {
    position: absolute; inset: 4px auto 4px 0; pointer-events: none;
    border-radius: 999px; background: var(--raised);
    box-shadow: 0 0 0 1px var(--sage-6), 0 1px 2px rgb(26 33 30 / .10);
  }
  .practice-tabs .pills { margin-bottom: 16px; }
  @media (pointer: coarse) {
    .pills [role="tab"] { min-height: max(44px, 2.75rem); }
  }

  /* Me: one summary, followed by aligned account and backup sections. */
  .page.me-page { max-width: 960px; }
  .me-overview { margin-bottom: 24px; }
  .me-summary { padding: 16px; container-type: inline-size; }
  .stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stat { min-width: 0; padding: 0 16px; border-inline-end: var(--hair); }
  .stat:first-child { padding-inline-start: 0; }
  .stat:last-child { padding-inline-end: 0; border-inline-end: 0; }
  .stat .n { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px; font-size: var(--fs-28); font-weight: 600; letter-spacing: -.03em; line-height: 1.2; font-variant-numeric: tabular-nums; }
  .stat .stat-unit { color: var(--muted); font-size: var(--fs-12); font-weight: 400; letter-spacing: normal; }
  .stat .l { margin-top: 8px; color: var(--muted); font-size: var(--fs-12); }
  .me-stats-empty { margin-top: 12px; }
  .me-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 24rem), 1fr)); gap: 24px; align-items: start; }
  .me-card { min-width: 0; padding: 16px; }
  .me-card .section-h { margin-bottom: 16px; }
  .me-card [data-login] { display: grid; gap: 12px; }
  .me-card .field { width: 100%; max-width: none; margin: 0; }
  .me-card .field input { width: 100%; min-width: 0; height: max(44px, 2.75rem); }
  .me-card .btn { min-height: max(44px, 2.75rem); padding-inline: 12px; white-space: normal; text-align: center; }
  .me-card .actions { margin: 0; }
  .login-actions > .btn { width: 100%; }
  .login-actions > .error-msg { flex-basis: 100%; }
  .me-card .account-profile { border-top: 0; }
  .me-card .row { flex-wrap: nowrap; gap: 12px; min-height: 44px; padding: 0; border-bottom: 0; }
  .me-card .row-main { flex: 1; }
  .me-card .row > .btn { flex: none; }
  .me-card .account-sync { margin-top: 16px; padding-top: 16px; }
  .me-card:has([data-no-cloud]:not([hidden])) .account-sync { display: none; }
  .me-backup-size { margin-bottom: 8px; }
  .me-card .backup-note { margin-bottom: 16px; }
  .me-card .backup-actions > .btn { flex: 1 1 8rem; min-width: 0; }
  .me-card #restore-error.is-shown { margin-top: 12px; }
  @media (max-width: 360px) {
    .me-summary { padding-inline: 12px; }
    .stat { padding-inline: 8px; }
  }
  @container (max-width: 16rem) {
    .me-summary .stats { grid-template-columns: 1fr; }
    .me-summary .stat { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px 16px; padding: 12px 0; border-inline-end: 0; border-bottom: var(--hair); }
    .me-summary .stat:first-child { padding-top: 0; }
    .me-summary .stat:last-child { padding-bottom: 0; border-bottom: 0; }
    .me-summary .stat .l { order: -1; margin-top: 0; }
  }

  /* Empty + status */
  .empty { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding: 24px 16px; }
  .empty p { color: var(--muted); }
  .chapter .empty { padding: 4px 0 16px; border: 0; background: none; }
  .status-fail {
    display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px;
    padding: 16px; margin: 0 0 20px;
  }
  .status-fail > div { flex: 1; min-width: 12em; }
  .status-fail .sub { margin-top: 4px; }

  /* Toasts: popover="manual" region, bottom right */
  #toasts {
    position: fixed; inset: auto 24px 24px auto; margin: 0; padding: 0; border: 0;
    width: min(360px, calc(100vw - 32px)); overflow: visible; background: transparent;
    display: none; flex-direction: column; gap: 8px; z-index: 40;
  }
  #toasts:is(:popover-open, .is-open) { display: flex; }
  .toast {
    display: flex; align-items: center; gap: 12px;
    padding: 8px 8px 8px 16px; min-height: 3.25rem;
    border: var(--hair); border-radius: var(--r-page);
    background: var(--surface); color: var(--text); box-shadow: var(--toast-shadow);
    line-height: 1.45;
  }
  .toast p { flex: 1; min-width: 0; }
  .backup-note { margin: 0 0 12px; }
  .backup-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .backup-progress { margin-top: 8px; }
  .restore-option { display: flex; align-items: flex-start; gap: 8px; min-height: 44px; margin-top: 12px; cursor: pointer; }
  .restore-option[hidden] { display: none; }
  .restore-option input { flex: none; min-width: 20px; min-height: 20px; margin: 0; accent-color: var(--accent); }
  .restore-option span { flex: 1; font-size: var(--fs-14); line-height: 1.5; }
  .dialog {
    width: min(28rem, calc(100vw - 32px)); max-block-size: calc(100dvh - 32px);
    overflow: auto; padding: 16px; border: .5px solid var(--line);
    border-radius: var(--r-card); background: var(--surface); color: var(--text);
    & h2 { margin: 0 0 8px; font-size: var(--fs-18); }
    & p { margin: 0 0 8px; }
    & .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
    &::backdrop { background: rgb(0 0 0 / .32); }
  }
  /* Modal dialogs must not chain scroll to the page behind them. */
  .dialog:modal { overscroll-behavior: contain; }
  .lesson-video-dialog { width: min(52rem, calc(100vw - 32px)); }
  .lesson-video-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; }
  .lesson-video-dialog video { display: block; width: 100%; max-height: 55dvh; background: #000; border-radius: var(--r-ctl); }
  .lesson-video-form { display: grid; gap: 8px; }
  .lesson-video-form input { width: 100%; }
  .lesson-video-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
  .lesson-video-dialog [data-video-message] { margin: 8px 0 0; }
  .dialog::backdrop { overflow: hidden; overscroll-behavior: contain; }
  .import-progress, .backup-progress { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 12px; flex-basis: 100%; }
  .import-progress[hidden], .backup-progress[hidden] { display: none; }
  .import-progress p, .backup-progress p { grid-column: 1 / -1; }
  progress {
    appearance: none; width: 100%; height: 4px; border: 0; border-radius: 999px;
    background: var(--sage-4, var(--hover)); color: var(--accent); overflow: hidden;
    &::-webkit-progress-bar { background: transparent; }
    &::-webkit-progress-value { background: var(--accent); border-radius: 999px; }
    &::-moz-progress-bar { background: var(--accent); border-radius: 999px; }
  }
  .toast.is-error p { color: var(--error); }
  .toast button { flex-shrink: 0; min-height: 2.25rem; padding: 0 12px; border-radius: var(--r-ctl); font-weight: 500; }
  .toast button:hover { background: var(--hover); }
  @media (max-width: 850px) {
    #toasts { inset: auto 16px calc(80px + env(safe-area-inset-bottom, 0px)) 16px; width: auto; }
  }

  /* Local placeholders leave page titles and filters visible. */
  .startup-region { position: relative; }
  :root.is-loading .startup-region { min-block-size: var(--startup-height, 96px); }
  [data-loading-region="today"] { --startup-height: 76px; }
  [data-loading-region="lesson"] { --startup-height: 296px; }
  [data-loading-region="practice"] { --startup-height: 304px; }
  [data-loading-region="review"] { --startup-height: 232px; }
  [data-loading-region="account"] { --startup-height: 252px; }
  [data-loading-region="card"] { --startup-height: 320px; }
  :root.is-loading [data-loading-content] { visibility: hidden; pointer-events: none; }
  .startup-placeholder { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-around; padding: 16px; opacity: 0; visibility: hidden; pointer-events: none; }
  :root.is-loading-visible .startup-placeholder { opacity: 1; visibility: visible; }
  .skel { display: block; border-radius: var(--r-ctl); background-color: var(--sage-3); }
  .startup-row { block-size: 12px; flex-shrink: 0; }
  .loading-recovery { display: flex; align-items: center; gap: 12px; padding: 16px; }
  .busy-target { position: relative; padding-inline-start: 36px; }
  .busy-target::before { content: ''; position: absolute; inset-inline-start: 12px; top: calc(50% - 6px); width: 12px; height: 12px; border: 1.5px solid currentColor; border-inline-end-color: transparent; border-radius: 50%; opacity: 0; }
  .busy-target.is-busy::before { opacity: 1; }
  .btn[aria-disabled="true"] { cursor: progress; }
  .backup-retry { margin-top: 8px; }
  .import-progress p, .backup-progress p { min-block-size: 3em; line-height: 1.5; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; }
  @media (prefers-reduced-motion: no-preference) {
    .startup-placeholder .skel { background-image: linear-gradient(90deg, transparent, var(--sage-2), transparent); background-size: 320px 100%; background-repeat: no-repeat; background-attachment: fixed; animation: skel-shimmer 1.2s linear infinite; }
    .busy-target.is-busy::before { animation: busy-spin .9s linear infinite; }
  }
  @keyframes busy-spin { to { rotate: 360deg; } }
  @keyframes skel-shimmer { from { background-position: -320px 0; } to { background-position: calc(100vw + 320px) 0; } }

}

/* Swipe Deck springs and geometry follow the supplied motion study. */
@layer components {
  .main:has([data-stack]) { overflow: clip; }
  .swipe-deck { --deck-drag: 900 50; --deck-return: 280 16; --deck-threshold: 110; position: relative; isolation: isolate; min-height: 272px; }
  .swipe-card { position: absolute; inset: 0 0 auto; touch-action: pan-y; cursor: grab; user-select: none; will-change: transform; transform-origin: center; }
  .swipe-card.is-dragging { cursor: grabbing; }
  .swipe-card:not([data-card]) { block-size: var(--swipe-card-height, 224px); pointer-events: none; background: color-mix(in srgb, var(--surface), var(--sage-6) calc(var(--swipe-depth, 0) * 22%)); overflow: clip; }
  .swipe-card input { user-select: text; cursor: text; touch-action: manipulation; }
  .swipe-card .card-actions { justify-content: space-between; flex-wrap: wrap; }
  .lesson-primary-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .swipe-stamp { position: absolute; top: 20px; padding: 8px 12px; border-radius: var(--r-ctl); font-size: var(--fs-12); font-weight: 600; opacity: 0; pointer-events: none; }
  .swipe-save { left: 20px; background: var(--accent); color: var(--bg); rotate: -10deg; }
  .swipe-skip { right: 20px; background: var(--text); color: var(--bg); rotate: 10deg; }
  @media (prefers-reduced-motion: reduce) {
    .swipe-card { will-change: auto; }
    .swipe-stamp { rotate: 0deg; }
    progress:indeterminate { animation: none; }
  }
}

@layer states {
  /* Inline validation, only after interaction */
  :is(input, select, textarea):user-invalid,
  .was-submitted :is(input, select, textarea):invalid { border-color: var(--error); }
  .field:has(:user-invalid) > .error-msg,
  .was-submitted .field:has(:invalid) > .error-msg,
  .card-field:has([data-field="time"]:user-invalid) > .err-time,
  .card-field:has([data-field="duration"]:user-invalid) > .err-duration,
  .card-field:has([data-field="progress"]:user-invalid) > .err-progress,
  .import:has(input:user-invalid, input.is-invalid) > .error-msg { display: block; }
  .card-field .error-msg { max-width: 22em; }
  .error-msg.is-shown { display: block; }
  /* Login failures toggle `hidden`; no inline style is allowed under the CSP. */
  [data-login-error]:not([hidden]) { display: block; }

  /* Empty-state switch driven by data */
  main.no-decks .needs-decks { display: none; }
  main:not(.no-decks) .when-no-decks { display: none; }
}

@layer enhance {
  /* Height/width keywords can interpolate, so future disclosure work stays in CSS. */
  :root { interpolate-size: allow-keywords; }

  /* Chinese/Latin mixed text gets platform spacing instead of hand-tuned margins. */
  @supports (text-autospace: normal) {
    body { text-autospace: normal; }
  }

  /* Continuous-curvature corners where supported; pills and circles stay round. */
  @supports (corner-shape: superellipse(1.25)) {
    :where(*) { corner-shape: superellipse(1.25); }
    .pills, .pills [role="tab"], .tab-pill, .skel-pill { corner-shape: round; }
  }
  @media (prefers-reduced-motion: no-preference) {
    @view-transition { navigation: auto; }
    ::view-transition-group(root) { animation-duration: 250ms; animation-timing-function: ease-out; }
  }
  @media (forced-colors: active) {
    .pills [role="tab"][aria-selected="true"] { outline: 2px solid CanvasText; }
    .btn-quiet[aria-pressed="true"], .seg button[aria-pressed="true"], .side-nav a[aria-current="page"] { outline: 1px solid CanvasText; }
  }
  @media (max-width: 360px) {
    .pair input { width: 4.25em; }
    .continue, .block-empty { flex-direction: column; align-items: flex-start; }
  }
  @media (prefers-reduced-motion: no-preference) {
    html { scroll-behavior: smooth; }
  }
  @media (prefers-contrast: more) {
    :root { --line: var(--sage-11); }
    .card { border-color: var(--sage-11); }
    :is(.input, .field input, .pair input) { border-color: var(--sage-11); }
  }
  @media (forced-colors: active) {
    html { scrollbar-color: auto; }
  }
}

/* The mother-brand mark is black line art; invert it on dark backgrounds. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .brand img { filter: invert(1) brightness(1.08); }
}
:root[data-theme="dark"] .brand img { filter: invert(1) brightness(1.08); }

/* Top-layer entry: dialogs and toasts fade and lift in. Exits stay instant on
   browsers without discrete display/overlay transitions; entry works everywhere
   that supports @starting-style. */
@layer enhance {
  .dialog,
  #toasts {
    opacity: 0;
    pointer-events: none;
    transition-property: opacity, transform, display, overlay;
    transition-duration: 180ms;
    transition-timing-function: ease-out;
    transition-behavior: allow-discrete;
  }
  .dialog[open],
  #toasts:is(:popover-open, .is-open) {
    opacity: 1;
    pointer-events: auto;
    transform: none;
    @starting-style { opacity: 0; }
  }
  @media (prefers-reduced-motion: no-preference) {
    .dialog[open],
    #toasts:is(:popover-open, .is-open) {
      @starting-style { transform: translateY(8px) scale(.98); }
    }
  }
  @media (prefers-reduced-motion: reduce) {
    .dialog,
    #toasts { transition: none; }
  }
}
