:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-jp: "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 9999px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
  --transition: 0.2s ease;
  --sidebar-width: 240px;
  --topbar-height: 56px;
  --bottom-nav-height: 64px;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f8fafc;
  --bg-elevated: #ffffff;
  --bg-muted: #f1f5f9;
  --text: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-muted: #eef2ff;
  --success: #16a34a;
  --success-muted: #dcfce7;
  --error: #dc2626;
  --error-muted: #fee2e2;
  --warning: #d97706;
  --card-bg: #ffffff;
  --input-bg: #ffffff;
  --overlay: rgba(15, 23, 42, 0.4);
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f172a;
  --bg-elevated: #1e293b;
  --bg-muted: #334155;
  --text: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --primary: #818cf8;
  --primary-hover: #a5b4fc;
  --primary-muted: #312e81;
  --success: #4ade80;
  --success-muted: #14532d;
  --error: #f87171;
  --error-muted: #7f1d1d;
  --warning: #fbbf24;
  --card-bg: #1e293b;
  --input-bg: #0f172a;
  --overlay: rgba(0, 0, 0, 0.6);
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  overflow-x: hidden;
}

.app {
  display: flex;
  min-height: 100dvh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  transform: translateX(-100%);
  transition: transform var(--transition);
}

.sidebar.open { transform: translateX(0); }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.brand-icon {
  width: 36px;
  height: 36px;
  background: var(--primary);
  color: white;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-jp);
  font-weight: 700;
  font-size: 18px;
}

.brand-text { font-weight: 700; font-size: 1.125rem; }

.sidebar-nav { padding: 12px; flex: 1; overflow-y: auto; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9375rem;
  transition: background var(--transition), color var(--transition);
  margin-bottom: 2px;
}

.nav-link:hover { background: var(--bg-muted); color: var(--text); }
.nav-link.active { background: var(--primary-muted); color: var(--primary); font-weight: 600; }
.nav-link svg { flex-shrink: 0; }

/* Main */
.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  margin-left: 0;
}

.topbar {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.page-title {
  font-size: 1.125rem;
  font-weight: 600;
  flex: 1;
}

.topbar-actions { display: flex; gap: 4px; }

.content {
  flex: 1;
  padding: 16px;
  padding-bottom: calc(var(--bottom-nav-height) + 16px);
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

/* Bottom nav */
.bottom-nav {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottom-nav-height);
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  z-index: 90;
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom-nav .nav-link {
  flex: 1;
  flex-direction: column;
  gap: 4px;
  padding: 8px 4px;
  font-size: 0.6875rem;
  text-align: center;
  border-radius: 0;
  margin: 0;
}

.bottom-nav .nav-link svg { width: 22px; height: 22px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), transform 0.1s;
  font-family: inherit;
}

.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-muted); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg-muted); color: var(--text); }
.btn-success { background: var(--success); color: white; }
.btn-error { background: var(--error); color: white; }
.btn-sm { padding: 6px 12px; font-size: 0.8125rem; }
.btn-lg { padding: 14px 24px; font-size: 1rem; }
.btn-block { width: 100%; }

.btn-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
}

.btn-icon:hover { background: var(--bg-muted); color: var(--text); }

[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }

.menu-toggle { display: flex; }

/* Cards */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.card-title {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 12px;
}

/* Grid */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }

/* Stats */
.stat-value { font-size: 1.75rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 0.8125rem; color: var(--text-secondary); }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input, .form-select {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.9375rem;
  font-family: inherit;
  transition: border-color var(--transition);
}

.form-input:focus, .form-select:focus {
  outline: none;
  border-color: var(--primary);
}

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 0.8125rem;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}

.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.active { background: var(--primary); border-color: var(--primary); color: white; }

/* Japanese text */
.jp-text {
  font-family: var(--font-jp);
  font-size: 2rem;
  font-weight: 500;
  line-height: 1.4;
}

.jp-reading { font-family: var(--font-jp); color: var(--text-secondary); }

/* Flashcard */
.flashcard-container {
  perspective: 1000px;
  max-width: 480px;
  margin: 0 auto;
}

.flashcard {
  position: relative;
  width: 100%;
  min-height: 260px;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.flashcard.flipped { transform: rotateY(180deg); }

.flashcard-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--card-bg);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  box-shadow: var(--shadow-sm);
}

.flashcard-back { transform: rotateY(180deg); }

.card-back-content {
  width: 100%;
  max-width: 360px;
}

.card-back-reading {
  font-size: 1.125rem;
  margin-bottom: 6px;
}

.card-back-romaji {
  color: var(--text-secondary);
  font-size: 0.875rem;
  margin-bottom: 10px;
}

.card-back-meaning {
  font-size: 1.0625rem;
  line-height: 1.5;
  margin-bottom: 4px;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-top: 14px;
}

.card-meta span {
  font-size: 0.6875rem;
  letter-spacing: 0.03em;
  text-transform: lowercase;
  color: var(--text-muted);
  padding: 1px 0;
  border-bottom: 1px solid var(--border);
}

.text-muted { color: var(--text-muted); }

.typing-kanji-hint {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

/* Typing */
.typing-display {
  text-align: center;
  padding: 32px 16px;
  min-height: 160px;
  position: relative;
}

.typing-word {
  font-family: var(--font-jp);
  font-size: 2.5rem;
  letter-spacing: 0.1em;
  margin-bottom: 8px;
}

.typing-char {
  display: inline-block;
  transition: color 0.1s;
}

.typing-char.done { color: var(--success); }
.typing-char.current {
  color: var(--primary);
  border-bottom: 3px solid var(--primary);
  animation: blink 1s infinite;
}
.typing-char.pending { color: var(--text-muted); }

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.typing-char.wrong {
  color: var(--error);
  animation: shake 0.3s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.typing-input-hidden {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: none;
  background: transparent;
  caret-color: transparent;
  z-index: 2;
  cursor: text;
}

.typing-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-top: 16px;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.typing-hint { color: var(--text-secondary); font-size: 0.9375rem; margin-top: 8px; }

/* Conjugation table */
.conj-table { width: 100%; border-collapse: collapse; }
.conj-table th, .conj-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.9375rem;
}
.conj-table th { color: var(--text-secondary); font-weight: 500; font-size: 0.8125rem; }
.conj-table td.jp { font-family: var(--font-jp); }

/* Grammar list */
.grammar-item {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.grammar-item:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.grammar-pattern { font-family: var(--font-jp); font-size: 1.25rem; font-weight: 600; }
.grammar-meaning { color: var(--text-secondary); font-size: 0.875rem; margin-top: 4px; }

.example-line {
  padding: 12px;
  background: var(--bg-muted);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
}

.example-jp { font-family: var(--font-jp); font-size: 1.0625rem; }
.example-en { color: var(--text-secondary); font-size: 0.875rem; margin-top: 4px; }

/* Counter */
.counter-result {
  text-align: center;
  padding: 40px 20px;
}

.counter-reading {
  font-family: var(--font-jp);
  font-size: 3rem;
  font-weight: 600;
  color: var(--primary);
}

/* AI tip */
.ai-tip {
  padding: 16px;
  background: var(--primary-muted);
  border-radius: var(--radius-md);
  font-size: 0.9375rem;
  line-height: 1.6;
  white-space: pre-wrap;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-secondary);
}

/* Overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 99;
}

.sidebar-overlay.visible { display: block; }

/* Tabs */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 0;
  background: var(--bg-muted);
  padding: 4px;
  border-radius: var(--radius-sm);
}

.tabs-inline {
  width: auto;
  flex: 0 0 auto;
}

.tabs-inline .tab {
  flex: 0 0 auto;
  min-width: 88px;
}

.tabs-icon .tab {
  min-width: 36px;
  padding: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tab-icon svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.tab {
  flex: 1;
  padding: 8px 14px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8125rem;
  font-family: inherit;
  transition: all var(--transition);
  white-space: nowrap;
}

.tab.active {
  background: var(--bg-elevated);
  color: var(--text);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.tab:hover:not(.active) {
  color: var(--text);
}

/* Desktop */
@media (min-width: 768px) {
  .sidebar { transform: translateX(0); }
  .main-wrapper { margin-left: var(--sidebar-width); }
  .menu-toggle { display: none; }
  .bottom-nav { display: none; }
  .content { padding-bottom: 24px; }
  .sidebar-overlay { display: none !important; }
}

@media (max-width: 480px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .jp-text { font-size: 1.5rem; }
  .typing-word { font-size: 1.75rem; }
  .conj-grid { grid-template-columns: 1fr; }
  .source-grid { grid-template-columns: repeat(2, 1fr); }
  .study-mode-row {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .tabs-inline .tab {
    min-width: 76px;
    padding: 8px 12px;
  }
  .tabs-icon .tab {
    min-width: 36px;
    padding: 8px;
  }
}

/* Study pages (Words, Conj) */
.study-header {
  margin-bottom: 16px;
}

.study-source-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.source-select {
  appearance: none;
  -webkit-appearance: none;
  field-sizing: content;
  width: auto;
  max-width: calc(100% - 148px);
  flex: 0 1 auto;
  min-width: 0;
  margin-left: -6px;
  padding: 8px 1.125rem 8px 8px;
  font-size: 0.9375rem;
  font-weight: 600;
  font-family: inherit;
  color: var(--text);
  background-color: var(--bg);
  background-image: var(--select-chevron);
  background-repeat: no-repeat;
  background-position: right 0 center;
  background-size: 12px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.source-select option {
  background: var(--bg-elevated);
  color: var(--text);
  font-weight: 400;
}

.source-select:hover {
  color: var(--primary);
}

.source-select:focus {
  outline: none;
}

.source-select:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent);
  outline-offset: 2px;
}

.study-source-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: auto;
}

.study-mode-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: nowrap;
}

.study-mode-row-end {
  justify-content: flex-end;
}

.study-mode-row .tabs-inline {
  flex: 0 1 auto;
  min-width: 0;
}

.study-mode-row .study-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  margin-left: auto;
}

.study-toolbar,
.words-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.study-toolbar-right,
.words-toolbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-left: auto;
}

.study-toolbar-end {
  justify-content: flex-end;
}

.study-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.icon-toggle {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color var(--transition), background var(--transition);
  padding: 0;
}

.icon-toggle:hover {
  background: var(--bg-muted);
  color: var(--text-secondary);
}

.icon-toggle.active {
  color: var(--primary);
}

.icon-toggle svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.study-counter,
.words-counter {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  margin-bottom: 12px;
}

.study-nav,
.words-nav-hint {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: 20px 0 12px;
  flex-wrap: wrap;
}

.study-nav .btn,
.words-nav-hint .btn {
  min-width: 68px;
  padding: 8px 14px;
  font-size: 0.8125rem;
}

.study-nav .btn-icon-nav {
  min-width: 40px;
  width: 40px;
  height: 40px;
  padding: 0;
}

.study-nav .btn-icon-nav svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.study-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: calc(var(--topbar-height) + 16px) 16px 16px;
}

.study-modal[hidden] {
  display: none;
}

.study-modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay);
}

.study-modal-panel {
  position: relative;
  width: min(100%, 480px);
  max-height: calc(100dvh - var(--topbar-height) - 32px);
  overflow-y: auto;
  z-index: 1;
}

.study-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.study-modal-title {
  font-size: 1rem;
  font-weight: 600;
}

.range-select {
  appearance: none;
  -webkit-appearance: none;
  padding: 6px 28px 6px 10px;
  font-size: 0.8125rem;
  font-family: inherit;
  color: var(--text);
  background-color: var(--bg-elevated);
  background-image: var(--select-chevron);
  background-repeat: no-repeat;
  background-position: right 8px center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-width: 100px;
  max-width: 140px;
}

.range-select option {
  background: var(--bg-elevated);
  color: var(--text);
}

.range-select:hover {
  border-color: var(--primary);
}

.range-select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 20%, transparent);
}

.settings-active-source {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.flashcard-hint {
  color: var(--text-muted);
  font-size: 0.8125rem;
  margin-top: 16px;
}

.conj-container .conj-flashcard {
  max-height: min(70vh, 520px);
}

.conj-back {
  align-items: stretch;
  justify-content: flex-start;
  overflow: hidden;
  padding: 16px;
}

.conj-grid {
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 12px;
  text-align: left;
  -webkit-overflow-scrolling: touch;
}

.conj-grid-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
}

.conj-grid-label {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.conj-meaning {
  margin-top: 12px;
  font-size: 0.9375rem;
  color: var(--text-secondary);
  line-height: 1.5;
}

.conj-grid-value {
  font-size: 0.9375rem;
  line-height: 1.35;
}

.settings-hint {
  color: var(--text-secondary);
  font-size: 0.8125rem;
  margin-bottom: 14px;
  line-height: 1.5;
}

.source-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}

.source-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  cursor: pointer;
  text-align: left;
  transition: border-color var(--transition), background var(--transition);
  font-family: inherit;
  color: var(--text);
}

.source-chip:hover:not(.unavailable) {
  border-color: var(--primary);
}

.source-chip.active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 8%, var(--bg-elevated));
}

.source-chip.unavailable {
  opacity: 0.45;
  cursor: not-allowed;
}

.source-chip-label {
  font-size: 0.875rem;
  font-weight: 600;
}

.source-chip-sub {
  font-size: 0.6875rem;
  color: var(--text-muted);
  line-height: 1.3;
}
