:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1c1e26;
  --text-2: #5b6070;
  --text-3: #9aa0b0;
  --line: #e6e8f0;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-2: #0d9488;
  --danger: #dc2626;
  --warn: #d97706;
  --ok: #16a34a;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(20, 24, 40, .06), 0 8px 24px rgba(20, 24, 40, .05);
  --sidebar-w: 210px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Segoe UI", Roboto, "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
}
.hidden { display: none !important; }
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; }

/* ---------- 布局 ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  position: fixed; left: 0; top: 0; bottom: 0; width: var(--sidebar-w);
  background: var(--card); border-right: 1px solid var(--line);
  padding: 20px 12px; display: flex; flex-direction: column; gap: 4px; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; }
.brand-logo {
  width: 38px; height: 38px; border-radius: 11px; background: var(--accent);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700;
}
.brand-name { font-size: 17px; font-weight: 700; letter-spacing: 1px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px;
  border-radius: 10px; color: var(--text-2); font-size: 15px; transition: background .15s;
}
.nav-item svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.nav-item:hover { background: var(--bg); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.view {
  flex: 1; margin-left: var(--sidebar-w);
  padding: 28px clamp(16px, 4vw, 48px) 80px;
  max-width: 900px;
}

@media (max-width: 768px) {
  .sidebar {
    top: auto; right: 0; width: auto; height: 62px; flex-direction: row;
    padding: 0 4px; border-right: none; border-top: 1px solid var(--line);
    justify-content: space-around; align-items: stretch;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .brand { display: none; }
  .nav-item { flex-direction: column; gap: 3px; justify-content: center; flex: 1; padding: 6px 2px; font-size: 11px; border-radius: 0; }
  .nav-item.active { background: none; }
  .view { margin-left: 0; padding: 18px 14px calc(86px + env(safe-area-inset-bottom)); }
}

/* ---------- 通用组件 ---------- */
.page-title { font-size: 22px; font-weight: 700; margin-bottom: 18px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.page-title .spacer { flex: 1; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px; margin-bottom: 14px;
}
.card-title { font-size: 13px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.card-title .spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 10px; padding: 8px 16px; font-size: 14px; cursor: pointer;
  transition: all .15s; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #4338ca; color: #fff; }
.btn-danger-ghost { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger-ghost:hover { border-color: var(--danger); color: var(--danger); }
.btn-sm { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn-block { width: 100%; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn-ghost:hover { color: var(--accent); border-color: transparent; background: var(--accent-soft); }

input[type="text"], input[type="password"], input[type="search"], textarea, select {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 14px; font-size: 15px; font-family: inherit; background: var(--card);
  color: var(--text); outline: none; transition: border .15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 72px; }

.seg { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  border: none; background: transparent; padding: 6px 14px; border-radius: 8px;
  font-size: 13px; color: var(--text-2); cursor: pointer;
}
.seg button.active { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 4px 12px;
  background: var(--accent-soft); color: var(--accent); border-radius: 999px;
  font-size: 13px; cursor: pointer; border: none;
}
.badge { display: inline-block; padding: 1px 8px; border-radius: 6px; font-size: 12px; font-weight: 600; }
.badge-en { background: #e0f2fe; color: #0369a1; }
.badge-ja { background: #fce7f3; color: #be185d; }

.fam { display: inline-flex; align-items: center; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; cursor: pointer; border: none; }
.fam-0 { background: #fee2e2; color: #b91c1c; }
.fam-1 { background: #fef3c7; color: #b45309; }
.fam-2 { background: #dcfce7; color: #15803d; }

.icon-btn {
  width: 32px; height: 32px; border-radius: 8px; border: none; background: transparent;
  color: var(--text-2); cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg); color: var(--accent); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.speaking { color: var(--accent); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .4; } }

.empty { text-align: center; color: var(--text-3); padding: 40px 0; font-size: 14px; }
.muted { color: var(--text-3); font-size: 13px; }
.spinner {
  display: inline-block; width: 16px; height: 16px; border: 2px solid var(--line);
  border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite;
  vertical-align: -3px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 列表 ---------- */
.list-row {
  display: flex; align-items: center; gap: 10px; padding: 13px 6px;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.list-row:last-child { border-bottom: none; }
.list-row:hover { background: var(--bg); }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .term { font-size: 16px; font-weight: 600; }
.list-row .sub { font-size: 13px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- markdown 内容 ---------- */
.md { font-size: 15px; }
.md h1, .md h2 { font-size: 15px; color: var(--accent); margin: 14px 0 6px; padding-bottom: 4px; border-bottom: 1px solid var(--line); }
.md h3, .md h4 { font-size: 15px; margin: 12px 0 4px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md li { margin: 3px 0; }
.md code { background: var(--bg); padding: 1px 6px; border-radius: 5px; font-size: 13px; }
.md blockquote { border-left: 3px solid var(--line); padding-left: 12px; color: var(--text-2); margin: 8px 0; }
.md table { border-collapse: collapse; margin: 8px 0; width: 100%; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; font-size: 14px; text-align: left; }

/* ---------- 词典结果 ---------- */
.word-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.word-head .w { font-size: 30px; font-weight: 700; }
.word-head .phonetic { color: var(--text-2); font-size: 15px; }
.dict-translation { white-space: pre-line; font-size: 15px; }
.dict-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.dict-tag { font-size: 12px; background: var(--bg); color: var(--text-2); padding: 2px 9px; border-radius: 6px; }
.mdx-entry { overflow-x: auto; font-size: 14px; }
.mdx-entry img { max-width: 100%; }
.suggest-box { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* ---------- 复习卡片 ---------- */
.review-card {
  background: var(--card); border-radius: 18px; box-shadow: var(--shadow);
  padding: 40px 28px; text-align: center; min-height: 280px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
}
.review-front { font-size: 30px; font-weight: 700; }
.review-front.sentence { font-size: 19px; font-weight: 500; line-height: 1.7; }
.review-back { text-align: left; width: 100%; border-top: 1px dashed var(--line); padding-top: 16px; }
.review-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.review-progress { text-align: center; color: var(--text-3); font-size: 13px; margin-bottom: 12px; }

/* ---------- 聊天 ---------- */
.chat-layout { display: flex; gap: 16px; height: calc(100vh - 140px); }
.chat-list { width: 220px; flex-shrink: 0; overflow-y: auto; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-msgs { flex: 1; overflow-y: auto; padding: 4px 2px 16px; }
.msg { max-width: 92%; margin-bottom: 12px; }
.msg-user { margin-left: auto; }
.msg-user .bubble { background: var(--accent); color: #fff; border-radius: 16px 16px 4px 16px; padding: 10px 16px; }
.msg-ai .bubble { background: var(--card); border-radius: 16px 16px 16px 4px; padding: 12px 16px; box-shadow: var(--shadow); }
.msg-tools { display: flex; gap: 4px; margin-top: 4px; }
.chat-input-row { display: flex; gap: 8px; align-items: flex-end; padding-top: 10px; }
.chat-input-row textarea { min-height: 44px; max-height: 140px; }
.quick-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 0; }
@media (max-width: 768px) {
  .chat-layout { height: auto; flex-direction: column; }
  .chat-list { width: 100%; display: flex; gap: 8px; overflow-x: auto; }
  .chat-list .list-row { flex-shrink: 0; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
  .chat-msgs { max-height: 55vh; }
}

/* ---------- 弹窗 ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 18, 34, .45); z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--card); width: 100%; max-width: 640px; max-height: 88vh;
  border-radius: 20px 20px 0 0; overflow-y: auto; padding: 22px 22px 34px;
  animation: slideUp .22s ease;
}
@media (min-width: 769px) {
  .modal-overlay { align-items: center; }
  .modal { border-radius: 20px; padding-bottom: 22px; }
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } }
.modal-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.modal-head .grow { flex: 1; }
.modal-title { font-size: 19px; font-weight: 700; }

/* ---------- 登录 ---------- */
.login-screen {
  position: fixed; inset: 0; background: linear-gradient(160deg, #eef0ff 0%, #f6f7fb 55%, #e6fbf8 100%);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px;
}
.login-card {
  background: var(--card); border-radius: 22px; box-shadow: var(--shadow);
  padding: 40px 32px; width: 100%; max-width: 360px; text-align: center;
}
.login-logo {
  width: 60px; height: 60px; border-radius: 17px; background: var(--accent); color: #fff;
  font-size: 32px; font-weight: 700; display: flex; align-items: center; justify-content: center;
  margin: 0 auto 14px;
}
.login-card h1 { font-size: 22px; margin-bottom: 4px; }
.login-sub { color: var(--text-3); font-size: 14px; margin-bottom: 22px; }
.login-card input { margin-bottom: 12px; text-align: center; }
.login-error { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------- toast ---------- */
#toast-root { position: fixed; top: 18px; left: 50%; transform: translateX(-50%); z-index: 300; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: #23263a; color: #fff; padding: 9px 20px; border-radius: 999px;
  font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.25); animation: fadeIn .2s ease;
}
.toast.error { background: var(--danger); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-8px); } }

/* ---------- 选词浮动按钮 ---------- */
.sel-pop {
  position: fixed; z-index: 150; border: none; cursor: pointer;
  background: #23263a; color: #fff; padding: 8px 14px; border-radius: 999px;
  font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.3); animation: fadeIn .15s ease;
  max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sel-pop:active { transform: scale(.97); }

/* ---------- 其他 ---------- */
.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 5px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }
.section-gap { height: 10px; }
.detail-block { margin-top: 12px; }
.collapse-head { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 8px 0; font-weight: 600; font-size: 14px; }
.collapse-head .arrow { transition: transform .15s; font-size: 11px; color: var(--text-3); }
.collapse-head.open .arrow { transform: rotate(90deg); }
