:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e3ded6;
  --accent: #2f6fc0;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  /* quiet controls (‹ › ✕ speaker, flash-card menu), shared by levels 2-4. iPhones report a wide
     safe area on both sides in landscape; the corners are usable, so controls sit ~36px inside it. */
  --cfs: 15px; --pad-x: 8px;
  --sl: max(16px, calc(env(safe-area-inset-left) - 36px)); --sr: max(16px, calc(env(safe-area-inset-right) - 36px));
  --hz: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #17181a; --card: #222326; --ink: #f2f2f3; --muted: #a0a0a6; --line: #34353a; --accent: #5b9cf0; --shadow: none; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 0 8px; }

/* layout per level */
main { max-width: 640px; margin: 0 auto; padding: max(20px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom)); }
main.level2 { height: 100dvh; display: flex; flex-direction: column; }
main.level3 { max-width: none; height: 100dvh; display: flex; flex-direction: column; padding-left: 0; padding-right: 0; overflow: hidden; }

.iconbtn {
  border: 1px solid var(--line); background: var(--card); border-radius: 12px; min-width: 44px; height: 44px; padding: 0 12px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 20px; color: var(--ink);
}
.iconbtn.sm { min-width: 36px; height: 36px; font-size: 16px; padding: 0 10px; }
.iconbtn:focus-visible { outline: none; }

/* level 1 */
.convs { display: flex; flex-direction: column; gap: 12px; }
.conv {
  text-align: left; border: 1px solid var(--line); background: var(--card); border-radius: 16px; padding: 20px 18px;
  display: grid; gap: 4px; box-shadow: var(--shadow);
}
.conv:focus-visible { outline: none; }
.ct { font-weight: 700; font-size: 19px; }
.cc { color: var(--muted); font-size: 15px; }
.foot { display: flex; justify-content: center; margin-top: 28px; }
.foot .iconbtn { color: var(--muted); }
.foot .save { color: var(--ink); font-size: 16px; font-weight: 700; padding: 0 18px; }

/* arrange mode */
.fgroup { border-radius: 16px; }
.fhead { display: flex; align-items: stretch; border: 1px solid var(--line); background: var(--card); border-radius: 16px; box-shadow: var(--shadow); }
.ftoggle { flex: 1; position: relative; text-align: left; border: 0; background: none; padding: 16px 44px 16px 4px; display: grid; gap: 2px; border-radius: 0 16px 16px 0; }
.ftoggle:focus-visible { outline: none; }
.ftoggle .cc { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
.chev { position: absolute; right: 18px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 22px; transition: transform .15s; }
.fgroup.open .chev { transform: translateY(-50%) rotate(90deg); }
.fgroup.drop .fhead { border-color: var(--accent); border-style: dashed; }
.fhead .handle { position: static; align-self: center; margin-left: 4px; }
.flist { padding: 10px 0 2px 12px; border-left: 2px solid var(--line); margin: 10px 0 0 10px; }
.flist .phrase { padding-right: 56px; }
.flist .empty { color: var(--muted); font-size: 14px; border: 1px dashed var(--line); border-radius: 12px; padding: 12px; text-align: center; display: none; }
.flist .empty:only-child, .dragging-on .flist .empty { display: block; }
.handle {
  position: absolute; top: 6px; right: 6px; width: 44px; height: 44px; display: grid; place-items: center; flex: none;
  color: var(--muted); font-size: 22px; cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; border-radius: 10px;
}
.handle:hover { background: var(--line); }
.dragging { opacity: .35; }
.dragging-on { user-select: none; -webkit-user-select: none; }
.ghost { position: fixed; z-index: 40; pointer-events: none; margin: 0; opacity: .95; box-shadow: 0 10px 30px rgba(0,0,0,.25); }

/* phrase card */
.phrase {
  position: relative; display: block; width: 100%; text-align: left; background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px;
}
.phrase[role="button"] { cursor: pointer; }
.phrase[role="button"]:focus-visible { outline: none; }
.phrase > span { display: block; }
.hzt { font-family: var(--hz); font-size: 26px; line-height: 1.35; }
.pyt { font-size: 17px; margin-top: 2px; }
.ent { font-size: 15px; color: var(--muted); margin-top: 2px; }
.note { font-size: 14px; color: var(--muted); margin-top: 8px; }
.status { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-top: 8px; }
.status.draft::before { content: "● "; color: #c9952b; }
.status.checked::before { content: "● "; color: #3a8f5b; }
.status.used::before { content: "● "; color: #2f6fc0; }

/* level 2 */
.topbar { display: flex; align-items: center; gap: 2px; margin-bottom: 16px; }
.topbar .ctl { margin: 0 0 0 -13px; }
.topbar h1 { font-size: 20px; margin: 0; line-height: 1.2; }
.track {
  display: flex; overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory; scrollbar-width: none;
  overscroll-behavior-x: contain; touch-action: pan-x pan-y;
}
.track::-webkit-scrollbar { display: none; }
.track.grabbing { scroll-snap-type: none; cursor: grabbing; }
.slide { flex: 0 0 100%; scroll-snap-align: center; scroll-snap-stop: always; }
.level2 .track { flex: 1; margin: 0 -16px; }
.level2 .slide { padding: 0 16px; display: flex; flex-direction: column; overflow-y: auto; overscroll-behavior-y: contain; }
.level2 .phrase { flex: 1 0 auto; margin: 0; padding: 8px 8px; display: flex; flex-direction: column; justify-content: center; gap: 14px;
  background: none; border: 0; border-radius: 0; box-shadow: none; -webkit-tap-highlight-color: transparent; }
.level2 .phrase[role="button"]:hover { border: 0; }
.level2 .phrase[role="button"]:focus-visible { outline: none; }
.level2 .phrase > span { margin: 0; }
.level2 .hzt { font-size: clamp(34px, 10vmin, 56px); line-height: 1.3; }
.level2 .pyt { font-size: clamp(28px, 8vmin, 46px); line-height: 1.3; }
.level2 .ent { font-size: clamp(22px, 6vmin, 36px); line-height: 1.35; }
.level2 .note { font-size: 16px; line-height: 1.45; }
.count { text-align: center; color: var(--muted); font-size: 14px; margin: 12px 0 0; font-variant-numeric: tabular-nums; }

/* level 3 */
/* Flash-card chrome, the same upright and sideways: quiet menu top left, ✕ top right,
   counter bottom left, speaker bottom right. ✕ is the height of the menu's capitals and the
   speaker matches it; both sit centred in same-width buttons so they line up vertically.
   iPhones report a wide safe area on both sides in landscape; the corners are usable, so the
   controls sit ~36px inside it. */
main.level3 { padding: 0; }
.fbar, .fbot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding-left: var(--sl); padding-right: var(--sr); }
.fbar { padding-top: max(10px, env(safe-area-inset-top)); }
.fbot { padding-bottom: max(10px, calc(env(safe-area-inset-bottom) - 14px)); }
.seg { display: inline-flex; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 4px var(--pad-x); border-radius: 7px; font-size: var(--cfs); font-weight: 300; line-height: 1.3; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--line); color: var(--ink); }
.ctl { width: 36px; height: 36px; margin-right: -10px; display: grid; place-items: center; border: 0; padding: 0; background: transparent; color: var(--muted); font-size: var(--cfs); }
.ctl > span { display: flex; }
.ctl svg { height: 1em; width: auto; overflow: visible; stroke-width: .9; }
.ctl[data-speak] svg, .ctl[data-say] svg { height: calc(var(--cfs) + 3px); stroke-width: 1.4; }
.ctl:hover, .ctl:focus-visible { color: var(--ink); outline: none; }
.ctl:active { color: var(--ink); }
.ctl:disabled { opacity: .3; cursor: default; color: var(--muted); }
.level3 .count { margin: 0; padding-left: var(--pad-x); font-size: var(--cfs); font-weight: 300; line-height: 1.3; text-align: left; }
.level3 .track { flex: 1; min-height: 0; }
.level3 .slide { overflow-y: auto; }
.fbody { min-height: 100%; display: grid; grid-template-columns: minmax(0, 1fr); place-items: center; text-align: center; padding: 16px 20px; }
.fbody .h { font-family: var(--hz); font-size: clamp(40px, 12vmin, 90px); line-height: 1.3; }
.fbody .p { font-size: clamp(40px, 12vmin, 90px); line-height: 1.3; }
.fbody .e { font-size: clamp(40px, 12vmin, 90px); line-height: 1.3; }
.wlist { width: 100%; list-style: none; padding: 0; margin: 0; display: grid; gap: .35em; text-align: left; font-size: clamp(26px, 8vmin, 54px); line-height: 1.25; }
.wrow { display: block; width: 100%; text-align: left; font-size: inherit; border: 0; background: none; padding: .15em .3em; margin: 0; border-radius: 8px; }
.wrow:hover, .wrow:focus-visible { background: var(--card); outline: none; }
.wdt { font-weight: 600; }
.wh { font-family: var(--hz); font-weight: 400; margin-right: .15em; }
.wdd { margin-left: .3em; color: var(--muted); }
.eq { opacity: .7; }

/* level 4: drill-down page (full screen, plain text; component characters stay as cards) */
.sheet { position: fixed; inset: 0; z-index: 30; background: var(--bg); overflow-y: auto; overscroll-behavior: contain; }
.sheet[hidden] { display: none; }
.panel { max-width: 640px; margin: 0 auto; padding: 0 16px max(24px, env(safe-area-inset-bottom)); }
.bar {
  position: sticky; top: 0; z-index: 1; background: var(--bg); display: flex; justify-content: space-between; align-items: center;
  padding: max(6px, calc(env(safe-area-inset-top) - 4px)) var(--sr) 2px var(--sl);
}
.nav { display: flex; gap: 8px; margin-left: -18px; }
.nav .ctl { width: 44px; height: 44px; margin: 0; }
.dhead { display: flex; align-items: center; gap: 12px; padding: 12px 0 4px; }
.dtext { flex: 1; min-width: 0; }
.big { font-family: var(--hz); font-size: clamp(30px, 10vmin, 60px); line-height: 1.25; }
.bigp { font-size: clamp(30px, 10vmin, 60px); line-height: 1.25; margin-top: 4px; }
.bige { font-size: clamp(24px, 7vmin, 44px); line-height: 1.3; color: var(--muted); margin-top: 6px; }
.dnote { font-size: clamp(18px, 5vmin, 24px); line-height: 1.45; color: var(--muted); margin: 18px 0 0; }
.dnote strong { color: var(--ink); }
.rows { margin-top: 28px; }
.charrow {
  display: grid; grid-template-columns: 64px 1fr auto; align-items: center; gap: 12px; width: 100%; text-align: left;
  border: 1px solid var(--line); background: var(--card); border-radius: 14px; padding: 10px 14px; margin-bottom: 10px;
  font-size: clamp(18px, 5vmin, 22px); line-height: 1.3;
}
.charrow .c { font-family: var(--hz); font-size: 44px; text-align: center; }
.charrow .m { color: var(--muted); font-size: .85em; }
.charrow .go { color: var(--muted); font-size: 1.3em; }
.charrow.static { cursor: default; }
.uses { display: grid; gap: 18px; margin-top: 32px; }
.use { display: grid; gap: 2px; width: 100%; text-align: left; border: 0; background: none; padding: 6px 8px; margin: 0 -8px; border-radius: 10px; }
.use:hover, .use:focus-visible { background: var(--card); outline: none; }
.uh { font-family: var(--hz); font-size: clamp(28px, 8vmin, 40px); line-height: 1.3; }
.up { font-size: clamp(20px, 5.5vmin, 28px); line-height: 1.3; }
.ue { font-size: clamp(17px, 4.6vmin, 22px); line-height: 1.35; color: var(--muted); }
.role { font-size: 12px; font-weight: 700; color: var(--muted); }
.disclaim { font-size: 13px; color: var(--muted); margin-top: 14px; }

/* flash cards: vertical swipes change version, so stop the browser's pull-to-refresh */
html.flash, html.flash body { overscroll-behavior: none; }

/* phone on its side: no title bar, quiet controls tucked into the corners, counter under the text.
   iPhones report a wide safe area on both sides in landscape; the corner itself is usable, so the
   ✕ sits ~36px inside it rather than at its edge. */
.ctl > .ico-close { display: none; }
@media (orientation: landscape) and (max-height: 540px) {
  main.level2 {
    max-width: none;
    padding: 10px max(16px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  }
  .level2 .topbar .ctl { position: fixed; z-index: 2; top: max(10px, env(safe-area-inset-top)); right: var(--sr); margin: 0 -10px 0 0; }
  .level2 .topbar { margin: 0; }
  .level2 .topbar h1, .level2 .ctl > .ico-back { display: none; }
  .level2 .ctl > .ico-close { display: flex; }
  .level2 .track { margin: 0; min-height: 0; }
  .level2 .slide { padding: 0 24px 0 8px; }

  .level2 .count { margin: 4px 0 0; font-size: 12px; }
}
