:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1f1d1a;
  --muted: #7d766c;
  --line: #e8e2d8;
  --accent: #c8452c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe9e4;
  --chip: #ece7df;
  --shadow: 0 1px 2px rgba(40, 30, 20, .06), 0 4px 14px rgba(40, 30, 20, .05);
  --n5: #2f9e6e; --n4: #1f8a9e; --n3: #3d6fd1; --n2: #8452c9; --n1: #c8452c;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171512; --card: #22201c; --ink: #f1ece4; --muted: #9c958a; --line: #34302a;
    --accent: #e8664a; --accent-soft: #3a2620; --chip: #2d2a25;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171512; --card: #22201c; --ink: #f1ece4; --muted: #9c958a; --line: #34302a;
  --accent: #e8664a; --accent-soft: #3a2620; --chip: #2d2a25;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
}
[lang="ja"] { font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans CJK JP", "Yu Gothic", sans-serif; }
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
input, textarea { font: inherit; color: inherit; font-size: 16px; }
[hidden] { display: none !important; }

/* 顶栏 */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(14px); backdrop-filter: saturate(1.6) blur(14px);
}
.topbar h1 { font-size: 26px; margin: 0; letter-spacing: .5px; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border: 0; background: none; border-radius: 50%; color: var(--ink); }
.icon-btn:active { background: var(--chip); }

main { max-width: 680px; margin: 0 auto; padding: 0 16px calc(84px + var(--bottom)); }

/* 安装提示 */
.install-tip {
  max-width: 680px; margin: 0 auto 8px; padding: 10px 14px; font-size: 14px;
  display: flex; gap: 10px; align-items: center; background: var(--accent-soft); border-radius: 12px;
  margin-left: 16px; margin-right: 16px;
}
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; white-space: nowrap; padding: 4px; }

/* 搜索 */
.search-wrap { position: sticky; top: calc(var(--top) + 56px); z-index: 5; background: var(--bg); padding: 4px 0 8px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.search {
  display: flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 0 6px 0 12px; height: 46px; color: var(--muted); box-shadow: var(--shadow);
}
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: none; color: var(--ink); height: 100%; }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }
.chips { display: flex; gap: 8px; margin-top: 10px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 0; background: var(--chip); padding: 5px 14px; border-radius: 999px; font-size: 14px; white-space: nowrap; }
.chip.on { background: var(--ink); color: var(--bg); }

.count { color: var(--muted); font-size: 13px; margin: 4px 2px 8px; }
.list { display: flex; flex-direction: column; gap: 10px; }

/* 语法卡片 */
.gcard {
  text-align: left; width: 100%; border: 0; background: var(--card); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); display: block;
}
.gcard:active { transform: scale(.985); }
.gcard .top { display: flex; align-items: center; gap: 8px; }
.gcard .pat { font-size: 19px; font-weight: 600; flex: 1; min-width: 0; }
.gcard .mean { color: var(--muted); font-size: 14.5px; margin-top: 2px; }
.lv { font-size: 12px; font-weight: 700; padding: 1px 7px; border-radius: 6px; color: #fff; background: var(--muted); flex: none; }
.lv-N5 { background: var(--n5); } .lv-N4 { background: var(--n4); } .lv-N3 { background: var(--n3); } .lv-N2 { background: var(--n2); } .lv-N1 { background: var(--n1); }
.tag { font-size: 12px; padding: 1px 7px; border-radius: 6px; background: var(--chip); color: var(--muted); flex: none; }
.tag-ai { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.ai-card {
  width: 100%; text-align: left; border: 1.5px dashed var(--accent); background: var(--accent-soft); color: var(--accent);
  border-radius: var(--radius); padding: 14px 16px; font-weight: 600;
}
.ai-card small { display: block; font-weight: 400; color: var(--muted); margin-top: 2px; }

/* 通用卡片 / 按钮 */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px; }
.hint { margin: 0 0 10px; color: var(--muted); font-size: 14px; }
.muted { color: var(--muted); } .small { font-size: 13px; }
textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 10px 12px;
  resize: vertical; outline: 0; line-height: 1.6;
}
textarea:focus, .field input:focus, .field select:focus { border-color: var(--accent); }
.row { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }
.btn { border: 0; border-radius: 10px; padding: 9px 16px; font-size: 15px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: .5; }
.btn.ghost { background: var(--chip); }
.btn.block { width: 100%; }
.btn-small { border: 0; background: var(--accent); color: var(--accent-ink); border-radius: 9px; padding: 6px 14px; font-weight: 600; font-size: 15px; }

/* 详情 */
.d-head { display: flex; gap: 8px; align-items: center; }
.d-pattern { font-size: 28px; line-height: 1.3; margin: 8px 0 4px; }
.d-meaning { font-size: 18px; margin: 0 0 14px; font-weight: 500; }
.sec { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 10px; }
.sec h3 { font-size: 13px; color: var(--accent); margin: 0 0 6px; letter-spacing: 1px; }
.sec p { margin: 0; white-space: pre-wrap; }
.ex { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.ex:first-of-type { border-top: 0; padding-top: 2px; }
.ex-body { flex: 1; min-width: 0; }
.ex-jp { font-size: 17px; }
.ex-kana { font-size: 13px; color: var(--muted); }
.ex-zh { font-size: 14.5px; color: var(--muted); margin-top: 2px; }
.speak { flex: none; width: 34px; height: 34px; border: 0; border-radius: 50%; background: var(--chip); display: grid; place-items: center; color: var(--ink); }
.speak:active { background: var(--accent-soft); color: var(--accent); }
.sim { padding: 6px 0; border-top: 1px solid var(--line); }
.sim:first-of-type { border-top: 0; }
.sim b { font-weight: 600; }
.actions { display: flex; gap: 10px; margin: 14px 0 6px; }
.actions .btn { flex: 1; }
.btn.faved { background: var(--accent-soft); color: var(--accent); }

/* 单词 */
.wcard { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px; }
.w-top { display: flex; align-items: flex-start; gap: 10px; }
.w-word { font-size: 30px; font-weight: 600; line-height: 1.25; }
.w-read { color: var(--muted); font-size: 16px; }
.w-tags { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 4px; }
.w-means { margin: 8px 0 0; padding-left: 22px; font-size: 17px; }
.w-means li { margin: 2px 0; }
.wcard .sub { margin-top: 14px; }
.wcard .sub h3 { font-size: 13px; color: var(--accent); margin: 0 0 4px; letter-spacing: 1px; }
.rel { display: flex; flex-wrap: wrap; gap: 8px; }
.rel button { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 5px 10px; text-align: left; font-size: 14px; }
.rel button small { color: var(--muted); display: block; font-size: 12px; }
.links { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 14px; font-size: 14px; }
.links a { color: var(--accent); text-decoration: none; background: var(--accent-soft); padding: 4px 10px; border-radius: 8px; }
.hist-title { display: flex; align-items: center; justify-content: space-between; font-size: 14px; color: var(--muted); margin: 8px 2px; }
.hist { display: flex; flex-wrap: wrap; gap: 8px; }
.hist button { border: 0; background: var(--card); box-shadow: var(--shadow); border-radius: 10px; padding: 6px 12px; font-size: 15px; }
.hist button small { color: var(--muted); margin-left: 4px; font-size: 12px; }

/* 总结 */
.wrow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.wrow:first-of-type { border-top: 0; padding-top: 2px; }
.wrow .wmain { flex: 1; min-width: 0; }
.wrow .ww { font-size: 17px; font-weight: 600; }
.wrow .wr { color: var(--muted); font-size: 13px; margin-left: 6px; font-weight: 400; }
.wrow .wm { font-size: 14.5px; color: var(--muted); }
.star { flex: none; width: 34px; height: 34px; border: 0; border-radius: 50%; background: none; color: var(--muted); display: grid; place-items: center; }
.star.on { color: var(--accent); }
details.gsum { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 10px; }
details.gsum > summary { list-style: none; padding: 14px 16px; display: flex; gap: 8px; align-items: center; }
details.gsum > summary::-webkit-details-marker { display: none; }
details.gsum > summary .pat { font-weight: 600; font-size: 17px; }
details.gsum > summary .mean { color: var(--muted); font-size: 14px; }
details.gsum .inner { padding: 0 16px 14px; }
details.gsum .inner h4 { font-size: 12.5px; color: var(--accent); margin: 10px 0 2px; letter-spacing: 1px; }
details.gsum .inner p { margin: 0; }
.h2 { font-size: 15px; color: var(--muted); margin: 18px 2px 8px; font-weight: 600; }

/* 收藏 */
.seg { display: flex; background: var(--chip); border-radius: 10px; padding: 3px; margin: 4px 0 12px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 0; font-size: 14.5px; }
.seg button.on { background: var(--card); box-shadow: var(--shadow); font-weight: 600; }
.backup { display: flex; gap: 10px; margin-top: 24px; justify-content: center; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; font-size: 15px; }
.item-row { display: flex; align-items: center; gap: 8px; }
.item-row .gcard { flex: 1; }
.del { border: 0; background: none; color: var(--muted); width: 36px; height: 36px; border-radius: 50%; flex: none; }

/* 加载 / 错误 */
.loading { display: flex; align-items: center; gap: 12px; padding: 22px 16px; color: var(--muted); background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.error { background: var(--accent-soft); color: var(--ink); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; font-size: 15px; }
.error b { color: var(--accent); }
.error .btn { margin-top: 10px; }

/* 底部 Tab */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(6px + var(--bottom));
  background: color-mix(in srgb, var(--card) 90%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(16px); backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line);
}
.tab { flex: 1; max-width: 120px; border: 0; background: none; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: 11px; padding: 4px 0; }
.tab svg { width: 25px; height: 25px; }
.tab.on { color: var(--accent); }

/* 弹出页 */
.sheet {
  position: fixed; inset: 0; z-index: 30; background: var(--bg);
  display: flex; flex-direction: column;
  animation: slidein .22s ease-out;
}
@keyframes slidein { from { transform: translateX(30%); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-bar { display: flex; align-items: center; justify-content: space-between; padding: calc(var(--top) + 6px) 8px 6px; border-bottom: 1px solid var(--line); }
.sheet-bar h2 { font-size: 17px; margin: 0; flex: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 16px calc(30px + var(--bottom)); }
.sheet-body > * { max-width: 680px; margin-left: auto; margin-right: auto; }

/* 设置表单 */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13.5px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select {
  width: 100%; height: 44px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); padding: 0 12px; outline: 0; font-size: 16px;
  -webkit-appearance: none; appearance: none;
}
.note { font-size: 13px; color: var(--muted); background: var(--card); border-radius: 10px; padding: 10px 12px; margin-top: 16px; }
.note p { margin: 4px 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--bottom)); transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--bg); padding: 9px 18px; border-radius: 999px; font-size: 14px;
  max-width: calc(100% - 40px); text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
