:root {
  --bg: #faf8f4;
  --surface: #ffffff;
  --surface-2: #f2efe8;
  --text: #1f2421;
  --muted: #6b716d;
  --line: #e4e0d6;
  --accent: #1f6f6b;
  --accent-ink: #ffffff;
  --accent-soft: #dcefec;
  --hl: #fff1c2;
  --known: #c2410c;
  --danger: #b42318;
  --shadow: 0 8px 30px rgba(31, 36, 33, .12);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", "Noto Sans SC", sans-serif;
  --font-ko: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", "Nanum Gothic", var(--font);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151816; --surface: #1d211f; --surface-2: #262b28; --text: #e8ebe8; --muted: #9aa29c;
    --line: #333a36; --accent: #5fbfb7; --accent-ink: #0f1513; --accent-soft: #223a37; --hl: #4a4020;
    --known: #fb923c; --danger: #f97066; --shadow: 0 8px 30px rgba(0, 0, 0, .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #151816; --surface: #1d211f; --surface-2: #262b28; --text: #e8ebe8; --muted: #9aa29c;
  --line: #333a36; --accent: #5fbfb7; --accent-ink: #0f1513; --accent-soft: #223a37; --hl: #4a4020;
  --known: #fb923c; --danger: #f97066; --shadow: 0 8px 30px rgba(0, 0, 0, .5); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.6; }
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--text); font-size: 1.05rem; }
.brand-ko { color: var(--accent); font-family: var(--font-ko); }
.brand.big { font-size: 1.6rem; margin-bottom: 4px; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { padding: 6px 12px; border-radius: 999px; color: var(--muted); font-weight: 500; }
.topbar nav a.on { background: var(--accent-soft); color: var(--accent); }

main { max-width: 760px; margin: 0 auto; padding: 20px 16px 160px; }
h1 { font-size: 1.5rem; margin: 4px 0 16px; line-height: 1.3; }
h2 { font-size: 1.15rem; margin: 24px 0 10px; }
h3 { font-size: .95rem; margin: 20px 0 8px; color: var(--muted); font-weight: 600; letter-spacing: .02em; }

/* ---------- 按钮 / 表单 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: .85rem; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: none; background: none; cursor: pointer; padding: 4px 6px; border-radius: 8px; font-size: 1rem; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); }
input[type=text], input[type=url], input[type=password], input[type=search], textarea, select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { min-height: 220px; resize: vertical; font-family: var(--font-ko); line-height: 1.8; }
label.field { display: block; margin: 14px 0; }
label.field > span { display: block; font-size: .9rem; color: var(--muted); margin-bottom: 6px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

/* ---------- 登录 ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); padding: 28px; border-radius: var(--radius); box-shadow: var(--shadow); display: grid; gap: 12px; }
.err { color: var(--danger); margin: 0; }

/* ---------- 文章列表 ---------- */
.lib-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.lib-head h1 { margin: 4px 0; }
.alist { display: grid; gap: 10px; margin-top: 16px; }
.acard {
  display: flex; align-items: center; gap: 14px; padding: 14px 16px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--text);
}
.acard:hover { border-color: var(--accent); }
.acard .ico { width: 40px; height: 40px; flex: none; display: grid; place-items: center; border-radius: 10px; background: var(--accent-soft); font-size: 1.1rem; }
.acard .t { font-weight: 600; font-family: var(--font-ko); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acard .m { font-size: .85rem; color: var(--muted); }
.acard .info { min-width: 0; flex: 1; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.notice { background: var(--surface-2); border-radius: 10px; padding: 10px 14px; font-size: .9rem; margin: 12px 0; }
.notice.warn { border-left: 3px solid var(--known); }

/* ---------- 新建 ---------- */
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 4px; }
.tabs button { flex: none; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.tabs button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.log { margin-top: 14px; font-size: .9rem; background: var(--surface-2); border-radius: 10px; padding: 10px 14px; white-space: pre-wrap; }
.log .e { color: var(--danger); }

/* ---------- 阅读 ---------- */
.reader-head { display: flex; align-items: flex-start; gap: 8px; }
.reader-head h1 { flex: 1; font-family: var(--font-ko); }
.hint { font-size: .85rem; color: var(--muted); margin: -6px 0 18px; }
#pre { margin-top: -12px; color: var(--accent); }
#pre:empty { display: none; }
.ko-text { font-family: var(--font-ko); font-size: 1.2rem; line-height: 2.05; word-break: keep-all; overflow-wrap: anywhere; }
.ko-text p { margin: 0 0 1.1em; }
.sent { border-radius: 6px; padding: 2px 1px; cursor: pointer; transition: background .15s; -webkit-tap-highlight-color: transparent; }
.sent.cur { background: var(--hl); box-shadow: 0 0 0 3px var(--hl); }
.sent:hover { background: color-mix(in srgb, var(--hl) 45%, transparent); }
.sent.cur:hover { background: var(--hl); }
.spk { font-size: .78em; font-weight: 700; padding: 1px 7px; border-radius: 999px; vertical-align: 2px; color: #fff; user-select: none; }
.spk0 { background: #1f6f6b; } .spk1 { background: #b4532a; } .spk2 { background: #5b4fa8; } .spk3 { background: #2f6db0; } .spk4 { background: #8a6d1f; }
mark.known { background: none; color: inherit; border-bottom: 2px dotted var(--known); cursor: pointer; }

/* 划词工具条 */
.seltool {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 40;
  display: flex; gap: 4px; padding: 6px; background: var(--text); border-radius: 999px; box-shadow: var(--shadow);
}
body.has-player .seltool { bottom: calc(108px + env(safe-area-inset-bottom)); }
body.has-player.player-min .seltool { bottom: calc(84px + env(safe-area-inset-bottom)); }
.seltool button { border: none; background: transparent; color: var(--bg); padding: 8px 14px; border-radius: 999px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.seltool button:hover { background: color-mix(in srgb, var(--bg) 18%, transparent); }

/* 播放器 */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px); border-top: 1px solid var(--line); padding: 8px 16px calc(10px + env(safe-area-inset-bottom));
}
.player .inner { max-width: 760px; margin: 0 auto; }
.player .bar { display: flex; align-items: center; gap: 6px; }
.player .pbtn { border: none; background: none; cursor: pointer; width: 40px; height: 40px; border-radius: 50%; font-size: 1.05rem; display: grid; place-items: center; flex: none; }
.player .pbtn:hover { background: var(--surface-2); }
.player .pbtn.main { background: var(--accent); color: var(--accent-ink); width: 46px; height: 46px; font-size: 1.15rem; }
.player .pbtn.on { background: var(--accent-soft); color: var(--accent); }
.player .tog { font-size: .8rem; font-weight: 600; width: auto; padding: 0 10px; border-radius: 999px; height: 32px; }
.player select { width: auto; padding: 4px 6px; font-size: .85rem; border-radius: 8px; }
.player .time { font-size: .75rem; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 78px; text-align: right; }
.player input[type=range] { width: 100%; margin: 2px 0 4px; accent-color: var(--accent); }
.player .label { font-size: .75rem; color: var(--muted); }
.player.min { left: auto; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); border: 1px solid var(--line); border-radius: 999px; padding: 6px; box-shadow: var(--shadow); }
.player.min .full { display: none; }
.player:not(.min) .mini { display: none; }
@media (max-width: 520px) {
  .player .hide-sm { display: none; }
}

/* ---------- 底部面板 ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .25); z-index: 50; }
.sheet {
  position: fixed; z-index: 60; left: 0; right: 0; bottom: 0; max-height: 82vh; overflow-y: auto; background: var(--surface);
  border-radius: 18px 18px 0 0; box-shadow: var(--shadow); padding: 20px 18px calc(24px + env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (min-width: 900px) {
  .sheet { left: auto; top: 0; width: 440px; max-height: none; border-radius: 0; animation: none; border-left: 1px solid var(--line); }
  .sheet-backdrop { background: transparent; }
}
.sheet-close { position: sticky; top: 0; float: right; border: none; background: var(--surface-2); width: 32px; height: 32px; border-radius: 50%; cursor: pointer; z-index: 1; }
.sheet .lemma { font-family: var(--font-ko); font-size: 2rem; font-weight: 700; line-height: 1.2; }
.tag { display: inline-block; font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; vertical-align: middle; }
.tag.gray { background: var(--surface-2); color: var(--muted); }
.meaning { font-size: 1.05rem; margin: 8px 0; }
.box { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin: 8px 0; font-size: .95rem; }
.ex { padding: 8px 0; border-bottom: 1px solid var(--line); }
.ex:last-child { border-bottom: none; }
.ex .ko { font-family: var(--font-ko); font-size: 1.02rem; }
.ex .ko b { color: var(--known); font-weight: 600; }
.ex .zh { color: var(--muted); font-size: .9rem; }
.ex .src { font-size: .75rem; color: var(--muted); }
.gram { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin: 8px 0; }
.gram .pat { font-family: var(--font-ko); font-weight: 700; color: var(--accent); }
.wrow { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.wrow .w { font-family: var(--font-ko); font-weight: 600; cursor: pointer; }
.wrow .w:hover { color: var(--accent); }
.wrow .mm { flex: 1; color: var(--muted); font-size: .9rem; }
.sheet-actions { position: sticky; bottom: calc(-24px - env(safe-area-inset-bottom)); background: var(--surface); padding: 12px 0 4px; margin-top: 12px; display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; }
.loading { padding: 30px 0; text-align: center; color: var(--muted); }
.loading::before { content: ""; display: block; width: 26px; height: 26px; margin: 0 auto 10px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 单词本 ---------- */
.vtools { display: grid; gap: 10px; margin-bottom: 14px; }
.wcard { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.wcard .head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wcard .lemma { font-family: var(--font-ko); font-size: 1.4rem; font-weight: 700; }
.wcard .note { font-size: .9rem; color: var(--muted); margin: 4px 0; }
.wcard .forms { font-size: .82rem; color: var(--muted); font-family: var(--font-ko); }
.wcard details summary { cursor: pointer; font-size: .85rem; color: var(--accent); margin-top: 6px; }
.wcard .foot { display: flex; gap: 6px; justify-content: flex-end; margin-top: 6px; }
.hide-meaning .wcard:not(.reveal) .meaning,
.hide-meaning .wcard:not(.reveal) .note,
.hide-meaning .wcard:not(.reveal) .zh { filter: blur(6px); user-select: none; }
.hide-meaning .wcard:not(.reveal) { cursor: pointer; }

.toast {
  position: fixed; top: calc(64px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 80; background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 999px; font-size: .9rem; box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}
.toast.err { background: var(--danger); color: #fff; }
