:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-soft: #eef2f7;
  --text: #111827;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-soft: #eff6ff;
  --primary-dark: #1d4ed8;
  --success: #059669;
  --success-soft: #ecfdf5;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --danger: #dc2626;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 2px 10px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 8px 28px rgba(15, 23, 42, 0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Noto Sans JP", sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

.hidden {
  display: none !important;
}

.app-shell {
  max-width: 480px;
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  position: relative;
}

/* Top Bar */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(246, 247, 249, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: calc(10px + var(--safe-top)) 16px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid rgba(229, 231, 235, 0.7);
}

.lang-switch {
  display: inline-flex;
  background: var(--surface-soft);
  padding: 3px;
  border-radius: 999px;
  gap: 2px;
}

.lang-pill {
  padding: 6px 13px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 999px;
  color: var(--text-muted);
  transition: all 0.16s ease;
}

.lang-pill.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface);
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.sync-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #9ca3af;
}

.sync-pill.synced .sync-dot {
  background: var(--success);
}

.sync-pill.syncing .sync-dot {
  background: var(--primary);
}

.sync-pill.error .sync-dot {
  background: var(--warn);
}

/* Main Content */
.main-content {
  flex: 1;
  padding: 16px 16px calc(92px + var(--safe-bottom));
}

.tab-view {
  display: none;
}

.tab-view.active {
  display: block;
}

/* Cards & Buttons */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(229, 231, 235, 0.65);
  margin-bottom: 14px;
}

.hero-card {
  padding: 22px 20px;
}

.stage-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}

.hero-title {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.hero-sub {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text-muted);
}

.progress-track {
  height: 8px;
  background: var(--surface-soft);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #0d9488);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.stats-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 18px;
}

.btn-primary {
  width: 100%;
  min-height: 52px;
  border-radius: var(--radius-md);
  background: var(--primary);
  color: #ffffff;
  font-size: 16px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.22);
  transition: transform 0.12s ease, background 0.12s ease;
}

.btn-primary:active {
  transform: scale(0.985);
  background: var(--primary-dark);
}

.btn-primary:disabled {
  opacity: 0.45;
  box-shadow: none;
}

.btn-secondary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

.btn-soft {
  min-height: 46px;
  border-radius: var(--radius-md);
  background: var(--surface-soft);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn-soft:active {
  background: #e2e8f0;
}

.btn-outline {
  min-height: 44px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 14px;
  font-weight: 600;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.mini-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.stat-box {
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: 12px 10px;
  text-align: center;
  border: 1px solid rgba(229, 231, 235, 0.65);
}

.stat-num {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  z-index: 25;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 6px 8px calc(6px + var(--safe-bottom));
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 5px 0;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
}

.nav-item.active {
  color: var(--primary);
}

.nav-icon {
  font-size: 19px;
  line-height: 1.2;
}

/* Path Roadmap */
.stage-block {
  margin-bottom: 18px;
}

.stage-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 6px 4px 8px;
}

.stage-header h3 {
  margin: 0;
  font-size: 16px;
}

.stage-header span {
  font-size: 12px;
  color: var(--text-muted);
}

.lesson-item {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.lesson-item.current {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.lesson-item.locked {
  opacity: 0.6;
}

.lesson-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 2px;
}

.lesson-goal {
  font-size: 12px;
  color: var(--text-muted);
}

.lesson-status {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

/* Library & Ear-training Player */
.ear-player {
  background: linear-gradient(135deg, #1e293b, #0f172a);
  color: #ffffff;
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  margin-bottom: 14px;
}

.ear-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.ear-btn {
  background: #ffffff;
  color: #0f172a;
  font-size: 13px;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 999px;
  white-space: nowrap;
}

.ear-now {
  margin-top: 10px;
  font-size: 13px;
  color: #cbd5e1;
}

.filter-pills {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 10px;
}

.filter-pill {
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.filter-pill.active {
  background: var(--text);
  color: #ffffff;
  border-color: var(--text);
}

.lib-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.lib-target {
  font-size: 16px;
  font-weight: 600;
}

.lib-phonetic {
  font-size: 12px;
  color: var(--primary);
  margin-top: 2px;
}

.lib-meaning {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--surface-soft);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

/* Fullscreen Study Overlay */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: calc(10px + var(--safe-top)) 16px calc(18px + var(--safe-bottom));
  overflow-y: auto;
}

.study-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.study-progress-wrap {
  flex: 1;
  height: 8px;
  background: var(--surface-soft);
  border-radius: 999px;
  overflow: hidden;
}

.study-progress-bar {
  height: 100%;
  background: var(--primary);
  transition: width 0.2s ease;
}

.ruby-toggle {
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.study-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 22px 18px;
  box-shadow: var(--shadow-md);
  flex: 1;
  display: flex;
  flex-direction: column;
}

.sentence-box {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.95;
  margin: 10px 0 12px;
  word-break: break-word;
}

.ruby-word {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  margin: 0 3px;
  padding: 2px 4px;
  border-radius: 8px;
  cursor: pointer;
  vertical-align: bottom;
}

.ruby-word.selected {
  background: var(--primary-soft);
}

.ruby-top {
  font-size: 11px;
  font-weight: 500;
  color: var(--primary);
  line-height: 1.2;
  min-height: 14px;
}

.ruby-word.hide-ruby .ruby-top {
  opacity: 0;
}

.ruby-word.hide-ruby.revealed .ruby-top {
  opacity: 1;
}

.study-meaning {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 14px;
}

.audio-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.breakdown-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.breakdown-item {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
}

.breakdown-head {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  margin-bottom: 2px;
}

.role-tag {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 1px 6px;
  border-radius: 6px;
}

.tip-box {
  background: var(--warn-soft);
  color: #92400e;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  margin-top: auto;
  margin-bottom: 14px;
}

/* Chunk Builder */
.answer-zone {
  min-height: 76px;
  border: 2px dashed #cbd5e1;
  border-radius: var(--radius-md);
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 12px 0 16px;
  background: #fafafa;
}

.answer-zone.correct {
  border-color: var(--success);
  background: var(--success-soft);
}

.answer-zone.wrong {
  border-color: var(--danger);
  background: #fef2f2;
}

.chunk-pool {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
}

.chunk-btn {
  background: var(--surface);
  border: 1.5px solid #cbd5e1;
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 15px;
  font-weight: 600;
  box-shadow: 0 2px 0 #cbd5e1;
}

.chunk-btn:active {
  transform: translateY(1px);
  box-shadow: none;
}

.chunk-btn.used {
  opacity: 0.25;
  pointer-events: none;
}

.chunk-btn.placed {
  background: var(--primary-soft);
  border-color: var(--primary);
  box-shadow: none;
}

.rate-row {
  display: grid;
  grid-template-columns: 1fr 1.3fr;
  gap: 10px;
  margin-top: auto;
}

.btn-again {
  min-height: 50px;
  border-radius: var(--radius-md);
  background: var(--warn-soft);
  color: #b45309;
  font-size: 15px;
  font-weight: 700;
}

.btn-good {
  min-height: 50px;
  border-radius: var(--radius-md);
  background: var(--success);
  color: #ffffff;
  font-size: 15px;
  font-weight: 700;
}

/* Modal Sheet */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet-panel {
  width: 100%;
  max-width: 480px;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 18px 16px calc(24px + var(--safe-bottom));
}

.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.sheet-head h3 {
  margin: 0;
  font-size: 18px;
}

.phonetic-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.phonetic-tile {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 6px;
  text-align: center;
}

.phonetic-tile:active {
  background: var(--primary-soft);
  border-color: var(--primary);
}

.phonetic-sym {
  font-size: 18px;
  font-weight: 700;
}

.phonetic-ex {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 10px 0 4px;
}

.form-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 14px;
  background: var(--bg);
}

.quiz-option {
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
}

.quiz-option.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.toast {
  position: fixed;
  bottom: calc(84px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  background: rgba(17, 24, 39, 0.92);
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
  padding: 9px 16px;
  border-radius: 999px;
  max-width: 90%;
  text-align: center;
}
