:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --border: #e2e8f0;
  --radius: 16px;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
button, input, textarea, select { font: inherit; }
.hidden { display: none !important; }

#app { min-height: 100dvh; display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  min-height: 48px;
  padding: 12px 56px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.app-title { font-size: 18px; margin: 0; font-weight: 700; text-align: center; }
.topbar-right { position: absolute; right: 16px; top: 50%; transform: translateY(-50%); min-width: 40px; z-index: 2; }
.topbar-popover {
  position: absolute; right: 0; top: calc(100% + 8px);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow); min-width: 132px; overflow: hidden; z-index: 30;
}
.topbar-popover button {
  display: block; width: 100%; border: none; background: none;
  padding: 12px 16px; text-align: left; font-weight: 600; color: var(--text); cursor: pointer;
}
.topbar-popover button + button { border-top: 1px solid var(--border); }
.topbar-popover button:active { background: #f1f5f9; }
.home-onboard .btn { min-height: 44px; }
.home-onboard.card { padding: 14px; margin-bottom: 0; }
.home-onboard h2 { font-size: 16px; margin-bottom: 4px; }
.home-onboard p { margin: 0 0 10px; font-size: 13px; }
.home-onboard .btn { min-height: 40px; }

.main.home-view {
  padding: 12px 14px calc(76px + var(--safe-bottom));
}
.home-stack .home-card {
  padding: 14px;
  margin-bottom: 10px;
}
.home-stack .home-card:last-child { margin-bottom: 0; }
.home-card h3 { margin: 0 0 10px; font-size: 15px; }
.home-card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin-bottom: 10px;
}
.home-card-head h3 { margin: 0; }
.home-foot { margin: 0; font-size: 13px; line-height: 1.4; }
.home-weekly-stats { margin-bottom: 8px; }
.home-weekly-stats .weekly-stat { padding: 8px 4px; }
.home-weekly-stats .weekly-stat strong { font-size: 20px; }
.home-weekly-stats .weekly-stat span { font-size: 11px; }
.home-list-row, .home-score-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.home-row-main { flex: 1; min-width: 0; }
.home-title {
  font-size: 15px; font-weight: 700; line-height: 1.3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.home-sub { font-size: 13px; margin-top: 3px; }
.home-aside {
  flex-shrink: 0; max-width: 38%; padding-left: 8px;
  font-size: 14px; font-weight: 600; color: var(--muted);
  text-align: right; line-height: 1.3;
}
.home-link {
  flex-shrink: 0; border: none; background: none;
  color: var(--primary); font-size: 13px; font-weight: 600;
  padding: 4px 0; cursor: pointer; white-space: nowrap;
}
.home-link:active { opacity: 0.7; }
.home-practice { width: auto !important; min-height: 38px !important; padding: 8px 14px !important; font-size: 14px !important; flex-shrink: 0; }
.home-tappable { cursor: pointer; }
.home-tappable:active { background: #fafbff; }
.home-hint { text-align: center; margin: 32px 8px; font-size: 14px; }
.back-btn {
  position: absolute; left: 16px; top: 50%; transform: translateY(-50%); z-index: 2;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text); cursor: pointer;
}
.back-btn.hidden { display: none !important; }

.main { flex: 1; padding: 16px; padding-bottom: calc(80px + var(--safe-bottom)); overflow-y: auto; -webkit-overflow-scrolling: touch; }
.main.dictation-mode { background: #0f172a; color: #f8fafc; padding-bottom: 16px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 640px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 8px 12px calc(8px + var(--safe-bottom));
}
.tabbar.hidden { display: none; }
.tab {
  border: none; background: transparent; padding: 12px 8px; border-radius: 12px;
  color: var(--muted); font-weight: 600;
}
.tab.active { background: #eef2ff; color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 12px 20px; border: none; border-radius: 14px;
  background: var(--primary); color: #fff; font-weight: 600; cursor: pointer; width: 100%;
}
.btn:active { transform: scale(0.98); }
.btn-secondary { background: #eef2ff; color: var(--primary); }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-icon { width: 40px; height: 40px; border: none; background: #f1f5f9; border-radius: 12px; font-size: 18px; }

.card {
  background: var(--surface); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); margin-bottom: 12px;
}
.card-highlight {
  background: linear-gradient(180deg, #eef2ff 0%, #fff 100%);
  border: 1px solid #c7d2fe;
}
.card h2, .card h3 { margin: 0 0 8px; font-size: 16px; }
.muted { color: var(--muted); font-size: 14px; }

.hero-btn { font-size: 18px; min-height: 56px; margin-top: 8px; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.check-label { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; cursor: pointer; margin-bottom: 0; }
.check-label input { width: 18px; height: 18px; flex-shrink: 0; }
.field input, .field textarea, .field select {
  width: 100%; padding: 12px 14px; border: 1px solid var(--border);
  border-radius: 12px; background: #fff;
}
.field textarea { min-height: 160px; resize: vertical; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.segmented button {
  min-height: 44px; border: 1px solid var(--border); background: #fff; border-radius: 12px;
}
.segmented button.active { border-color: var(--primary); background: #eef2ff; color: var(--primary); font-weight: 700; }

.tips {
  background: #fffbeb; border: 1px solid #fde68a; border-radius: 12px;
  padding: 12px 14px; margin-bottom: 14px; font-size: 14px; line-height: 1.6;
}
.tips strong { display: block; margin-bottom: 4px; color: #92400e; }

.list-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 0; border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item-main { flex: 1; min-width: 0; }
.list-item-title { font-weight: 700; margin-bottom: 4px; }
.list-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.list-actions .btn { width: auto; min-height: 40px; padding: 8px 12px; font-size: 14px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  background: #eef2ff; color: var(--primary); font-size: 12px; font-weight: 600;
}

.filter-bar { margin-bottom: 12px; }
.filter-pills {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.filter-pills::-webkit-scrollbar { display: none; }
.filter-pill {
  flex: 0 0 auto; border: 1px solid var(--border); background: #fff;
  border-radius: 999px; padding: 8px 14px; font-size: 14px; color: #64748b;
}
.filter-pill.active {
  border-color: var(--primary); background: #eef2ff; color: var(--primary); font-weight: 700;
}
.filter-manage {
  margin-top: 8px; width: auto; min-height: 36px; padding: 6px 12px; font-size: 14px;
}
.wordlist-toolbar-primary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}
.wordlist-toolbar-primary .btn { width: 100%; min-height: 44px; padding: 10px 12px; font-size: 14px; }
.wordlist-toolbar-fold {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.wordlist-toolbar-fold .btn { width: 100%; min-height: 36px; padding: 6px 10px; font-size: 13px; }

.cat-section {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  margin-bottom: 10px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}
.cat-section-head {
  display: flex;
  align-items: center;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}
.cat-section.collapsed .cat-section-head { border-bottom: none; }
.cat-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 8px 12px 14px;
  border: none;
  background: transparent;
  text-align: left;
  font-size: 15px;
  font-weight: 700;
  color: #334155;
  min-height: 48px;
}
.cat-chevron {
  width: 16px;
  flex-shrink: 0;
  color: #64748b;
  font-size: 11px;
}
.cat-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cat-count {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  padding: 2px 8px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 999px;
}
.cat-del-btn {
  flex-shrink: 0;
  width: 40px;
  min-width: 40px;
  height: 40px;
  margin-right: 6px;
  padding: 0;
  border: none;
  background: transparent;
  color: #94a3b8;
  font-size: 24px;
  line-height: 1;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.cat-del-btn:active { background: #fee2e2; color: var(--danger); }
.cat-section-body .list-item { padding-left: 14px; padding-right: 14px; }
.btn-danger-text { color: var(--danger) !important; }
.list-actions .btn-danger-text { min-height: 36px; padding: 6px 8px; font-size: 13px; }

.inline-field { display: flex; gap: 8px; align-items: center; }
.inline-field input { flex: 1; }
.inline-field .btn { width: auto; min-height: 44px; padding: 0 16px; flex-shrink: 0; }
.cat-del { width: auto; min-height: 40px; padding: 8px 12px; font-size: 14px; }

.badge-muted { background: #f1f5f9; color: #64748b; }

.weekly-card h3 { margin: 0 0 12px; font-size: 16px; }
.weekly-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.weekly-stat { text-align: center; padding: 10px 6px; background: #f8fafc; border-radius: 12px; }
.weekly-stat strong { display: block; font-size: 22px; color: var(--primary); line-height: 1.2; }
.weekly-stat span { font-size: 11px; color: var(--muted); }

.history-actions { margin-bottom: 12px; }
.history-actions .btn { width: auto; min-height: 40px; }

.score-detail-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.score-detail-head h3 { margin: 0; }
.score-toggle {
  flex-shrink: 0; width: 40px; height: 40px; border-radius: 10px; border: none;
  font-weight: 800; font-size: 18px; cursor: pointer;
}
.score-toggle.ok { background: #dcfce7; color: var(--success); }
.score-toggle.bad { background: #fee2e2; color: var(--danger); }

.mistake-list { display: flex; flex-direction: column; gap: 0; margin-bottom: 12px; }
.mistake-item { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
.mistake-item:last-child { border-bottom: none; }
.mistake-item input { width: 18px; height: 18px; margin-top: 4px; flex-shrink: 0; }
.mistake-main { flex: 1; min-width: 0; }
.mistake-word { font-weight: 700; margin-bottom: 4px; }
.mistake-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.mistake-actions .btn { width: auto; min-height: 36px; flex: 1; }

.section-title { margin: 0 0 4px; font-size: 16px; }

.trend-card { margin-bottom: 12px; }
.trend-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.trend-head h3 { margin: 0; font-size: 16px; }
.trend-stats { display: flex; gap: 12px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.trend-stats strong { color: var(--text); font-size: 14px; }
.trend-filters { margin-bottom: 8px; }
.trend-chart-wrap { width: 100%; margin: 0 -4px; }
.trend-chart { width: 100%; height: auto; display: block; }
.trend-grid { stroke: #f1f5f9; stroke-width: 1; }
.trend-axis { fill: #94a3b8; font-size: 10px; text-anchor: end; }
.trend-xlabel { fill: #94a3b8; font-size: 9px; }
.trend-line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.trend-area { fill: #eef2ff; }
.trend-dot { cursor: pointer; stroke: #fff; stroke-width: 2; }
.trend-foot { margin: 8px 0 0; font-size: 12px; text-align: center; }

.preview-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.preview-table th, .preview-table td { border-bottom: 1px solid var(--border); padding: 8px 4px; text-align: left; vertical-align: top; }
.preview-table input { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; }
.row-del { color: var(--danger); font-size: 20px; line-height: 1; padding: 4px 8px; }
.row-warn { background: #fff7ed; }

.dictation-screen { text-align: center; padding-top: 24px; }
.dictation-progress { font-size: 48px; font-weight: 800; margin: 24px 0 8px; }
.dictation-sub { color: #94a3b8; margin-bottom: 32px; }
.dictation-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dictation-controls .btn { background: #334155; }
.dictation-controls .btn-primary-lite { background: var(--primary); grid-column: span 2; }

.score-ring {
  width: 160px; height: 160px; border-radius: 50%; margin: 8px auto 16px;
  display: flex; align-items: center; justify-content: center; flex-direction: column;
  background: linear-gradient(135deg, #eef2ff, #fdf4ff); border: 6px solid #c7d2fe;
}
.score-ring .num { font-size: 42px; font-weight: 800; color: var(--primary); line-height: 1; }
.score-ring .label { font-size: 14px; color: var(--muted); }

.detail-row { display: flex; gap: 8px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.detail-row.ok .mark { color: var(--success); }
.detail-row.bad .mark { color: var(--danger); }
.mark { font-weight: 800; width: 20px; }

.photo-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.photo-grid img { width: 100%; border-radius: 12px; aspect-ratio: 3/4; object-fit: cover; background: #e2e8f0; }
.photo-deleted {
  aspect-ratio: 3/4; border-radius: 12px; background: #f1f5f9;
  display: flex; align-items: center; justify-content: center; color: var(--muted);
}

.ocr-banner, .wake-banner {
  padding: 10px 16px; font-size: 13px; text-align: center;
}
.ocr-banner { background: #fff7ed; color: #9a3412; }
.ocr-banner.warn { background: #fef3c7; }
.ocr-banner.danger { background: #fee2e2; color: #991b1b; }
.wake-banner { background: #1e293b; color: #f8fafc; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92); color: #fff; padding: 12px 18px; border-radius: 999px;
  font-size: 14px; z-index: 100; max-width: 90vw; text-align: center;
}
.loading {
  position: fixed; inset: 0; background: rgba(255,255,255,0.72); z-index: 99;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.spinner {
  width: 40px; height: 40px; border: 4px solid #e2e8f0; border-top-color: var(--primary);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

input[type="file"] { display: none; }
.file-btn { margin-bottom: 12px; }
.hidden-file { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; clip: rect(0, 0, 0, 0); }

.align-preview { font-size: 13px; }
.align-preview .pair { display: grid; grid-template-columns: 24px 1fr 1fr; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }

@media (min-width: 480px) {
  .dictation-controls { grid-template-columns: repeat(4, 1fr); }
  .dictation-controls .btn-primary-lite { grid-column: span 4; }
}
