:root {
  --bg: #faf7f2;
  --fg: #1f1b16;
  --muted: #6b645c;
  --card: #ffffff;
  --line: #e4ddd3;
  --accent: #b22234;
  --accent-soft: #f6e3e5;
  --link: #8a1626;
  --hl: #fff3b0;
  --fs: 100%;
  --row: 36px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
:root[data-theme="dark"] {
  --bg: #15130f; --fg: #ece6dc; --muted: #a39a8e; --card: #1f1c17; --line: #342f28;
  --accent: #e2566a; --accent-soft: #3a1c22; --link: #f08a9a; --hl: #5a4a00;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15130f; --fg: #ece6dc; --muted: #a39a8e; --card: #1f1c17; --line: #342f28;
    --accent: #e2566a; --accent-soft: #3a1c22; --link: #f08a9a; --hl: #5a4a00;
  }
}
* { box-sizing: border-box; }
html { font-size: var(--fs); height: 100%; }
body {
  margin: 0; height: 100%; display: flex; flex-direction: column;
  background: var(--bg); color: var(--fg);
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.5; -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
a { color: var(--link); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.grow { flex: 1; }

/* header */
.top {
  flex: none; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-top)) 10px 8px;
  background: var(--accent); color: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.iconbtn { width: 40px; height: 40px; border-radius: 10px; font-size: 1.3rem; display: inline-flex; align-items: center; justify-content: center; }
.iconbtn:hover { background: rgba(0,0,0,.12); }
.search { flex: 1; position: relative; display: flex; max-width: 720px; }
.search input {
  width: 100%; height: 42px; border: 0; border-radius: 12px; padding: 0 40px 0 14px;
  font-size: 1.05rem; background: #fff; color: #1f1b16; outline: none;
}
.search input::-webkit-search-cancel-button { display: none; }
.search .clear { position: absolute; right: 0; top: 1px; color: #666; }
.keys { display: flex; gap: 4px; }
.key { width: 36px; height: 40px; border-radius: 10px; background: rgba(255,255,255,.18); font-size: 1.15rem; font-weight: 600; }
.key:active { background: rgba(255,255,255,.4); }

/* drawer */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: min(320px, 85vw); z-index: 40;
  background: var(--card); color: var(--fg); padding: calc(16px + var(--safe-top)) 12px 16px;
  transform: translateX(-105%); transition: transform .22s ease; overflow-y: auto;
  box-shadow: 2px 0 16px rgba(0,0,0,.2);
}
.drawer.open { transform: none; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; opacity: 0; pointer-events: none; transition: opacity .2s; }
.scrim.show { opacity: 1; pointer-events: auto; }
.drawer-head { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.drawer-head img { border-radius: 10px; }
.drawer-item { display: block; width: 100%; text-align: left; padding: 10px 10px; border-radius: 10px; font-size: 1rem; }
.drawer-item:hover { background: var(--accent-soft); }
.drawer-section { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; margin: 8px 10px 4px; }
.drawer-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; }
.drawer-row select, .drawer-row input[type=range] { max-width: 50%; }
.drawer-note { font-size: .85rem; color: var(--muted); padding: 0 10px 6px; }
.drawer hr { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }

/* layout: word list + content */
.layout { flex: 1; display: flex; min-height: 0; }
.side { flex: none; width: 300px; display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--card); }
.letters { flex: none; display: flex; flex-wrap: wrap; gap: 2px; padding: 6px 6px 4px; border-bottom: 1px solid var(--line); }
.letters button { min-width: 26px; height: 24px; padding: 0 4px; border-radius: 6px; font-size: .78rem; font-weight: 600; color: var(--muted); }
.letters button:hover { background: var(--accent-soft); color: var(--fg); }
.wl { flex: 1; overflow-y: auto; overscroll-behavior: contain; position: relative; }
.wl-inner { position: relative; }
.wl-row {
  position: absolute; left: 0; right: 0; height: var(--row); line-height: var(--row);
  padding: 0 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: block; width: 100%; text-align: left; border-bottom: 1px solid var(--line);
}
.wl-row:hover { background: var(--accent-soft); }
.wl-row.active { background: var(--accent); color: #fff; }
.wl-row .hom { font-size: .75em; opacity: .7; margin-left: 4px; }
.wl-row .letter { display: inline-block; width: 2.2em; color: var(--muted); font-size: .8em; }
.wl-note { flex: none; font-size: .8rem; color: var(--muted); padding: 4px 12px; border-top: 1px solid var(--line); min-height: 1.6em; }

main { flex: 1; min-width: 0; overflow-y: auto; padding: 12px 16px calc(32px + var(--safe-bottom)); }
main > .view { max-width: 820px; margin: 0 auto; }
.hero { text-align: center; padding: 34px 0 10px; }
.hero h1 { margin: 0 0 6px; font-size: 1.7rem; }

.results { list-style: none; margin: 0; padding: 0; }
.results li { border-bottom: 1px solid var(--line); }
.results li button { display: block; width: 100%; text-align: left; padding: 10px 6px; line-height: 1.35; }
.results li button:hover { background: var(--accent-soft); }
.results .hw { font-weight: 700; }
.results .hom { font-size: .8em; color: var(--muted); margin-left: 4px; }
.results .snip { display: block; font-size: .9rem; color: var(--muted); margin-top: 2px; }
.results .snip mark { background: var(--hl); color: inherit; padding: 0 1px; border-radius: 2px; }

.entry-nav { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; }
.navbtn { width: 44px; height: 40px; border-radius: 10px; font-size: 1.6rem; background: var(--card); border: 1px solid var(--line); }
.entry-nav .iconbtn { color: var(--fg); background: var(--card); border: 1px solid var(--line); }
.entry-nav .iconbtn.on { color: var(--accent); }

.entry { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px 14px; font-size: 1.05rem; }
.entry h2 { margin: 0 0 4px; font-size: 1.6rem; letter-spacing: .01em; color: var(--accent); }
.entry h2 sup { font-size: .55em; color: var(--muted); margin-left: 3px; }
.entry p { margin: 0 0 .55em; }
.entry p.gram { color: var(--muted); font-style: italic; margin-bottom: .8em; }
.entry p.gram i { font-style: normal; }
.entry p b { color: var(--accent); }
.entry p.idiom b { color: var(--fg); }
.entry a.xref { text-decoration: none; border-bottom: 1px dotted var(--link); font-weight: 600; }
.entry a.xref:hover { background: var(--accent-soft); }

.btn { padding: 9px 16px; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { background: transparent; color: var(--accent); border: 1px solid var(--accent); margin-top: 12px; }
.ftrow { display: flex; gap: 8px; margin: 8px 0 12px; }
.ftrow input { flex: 1; height: 42px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; font-size: 1rem; background: var(--card); color: var(--fg); }
.progress { position: relative; height: 24px; background: var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 10px; font-size: .8rem; }
.progress .bar { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--accent); transition: width .15s; }
.progress span { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.splash { max-width: 100%; border-radius: 10px; border: 1px solid var(--line); }

.fab { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(16px + var(--safe-bottom)); z-index: 25;
  background: var(--accent); color: #fff; padding: 10px 18px; border-radius: 999px; font-weight: 600; box-shadow: 0 4px 14px rgba(0,0,0,.3); }
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; font-size: .95rem; box-shadow: 0 4px 16px rgba(0,0,0,.3); }

/* phones and small tablets: list OR content, never both */
@media (max-width: 899px) {
  .side { width: 100%; border-right: 0; }
  body.show-content .side { display: none; }
  body:not(.show-content) main { display: none; }
  body:not(.show-content) .fab { display: none !important; }
}
@media (min-width: 900px) {
  .fab { display: none !important; }
  main { padding-top: 20px; }
  .entry { padding: 24px 28px 18px; }
}
@media (min-width: 1300px) { .side { width: 340px; } }
