:root {
  --bg: #FFF9F2;
  --bg-elevated: #FFFFFF;
  --bg-sidebar: #FFF0E6;
  --text: #4A3B32;
  --text-muted: #8C7B6E;
  --accent: #FFB7C5;
  --accent-dark: #FF8FAB;
  --accent-light: #FFE4E9;
  --border: #E8D5C4;
  --shadow: rgba(74, 59, 50, 0.08);
  --shadow-hover: rgba(74, 59, 50, 0.12);
  --r: 16px;
  --r-sm: 12px;
  --progress: #FFB7C5;
  --btn-primary-bg: #FFB7C5;
  --btn-primary-text: #4A3B32;
  --btn-secondary-bg: #FFF0E6;
  --btn-secondary-text: #4A3B32;
  --kanji-color: #8C7B6E;
  --english-color: #A09080;
}

.dark {
  --bg: #2D2420;
  --bg-elevated: #3A2E28;
  --bg-sidebar: #362A24;
  --text: #F0E6DC;
  --text-muted: #B0A090;
  --accent: #FFB7C5;
  --accent-dark: #FF8FAB;
  --accent-light: #4A3A3E;
  --border: #5A4A40;
  --shadow: rgba(0, 0, 0, 0.25);
  --shadow-hover: rgba(0, 0, 0, 0.35);
  --progress: #FFB7C5;
  --btn-primary-bg: #FFB7C5;
  --btn-primary-text: #2D2420;
  --btn-secondary-bg: #4A3A34;
  --btn-secondary-text: #F0E6DC;
  --kanji-color: #B0A090;
  --english-color: #908070;
}

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

html, body {
  height: 100%;
}

body {
  font-family: 'Sora', 'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.3s ease, color 0.3s ease;
  overflow: hidden;
}

/* ── Sakura petals ── */
.petals-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.petal {
  position: absolute;
  width: 16px;
  height: 16px;
  background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
  border-radius: 100% 0 100% 0;
  opacity: 0.4;
  animation: fall linear infinite;
}

@keyframes fall {
  0% {
    transform: translateY(-20px) rotate(0deg) translateX(0);
    opacity: 0;
  }
  10% { opacity: 0.4; }
  90% { opacity: 0.3; }
  100% {
    transform: translateY(100vh) rotate(720deg) translateX(40px);
    opacity: 0;
  }
}

/* ── Layout ── */
.app {
  display: flex;
  height: 100vh;
  position: relative;
  z-index: 1;
}

/* ── Sidebar ── */
.sidebar {
  width: 260px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 24px;
  gap: 20px;
  transition: background 0.3s ease;
}

.sidebar-mobile {
  display: none;
}

.sidebar-desktop {
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
}

.sidebar-header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.5px;
}

.sidebar-header .tagline {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 400;
  margin-top: 2px;
}

.level-nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  overflow-y: auto;
}

.level-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--r-sm);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
  text-align: left;
  color: var(--text);
}

.level-btn:hover {
  background: var(--accent-light);
}

.level-btn.active {
  background: var(--accent);
  border-color: var(--accent-dark);
  box-shadow: 0 2px 8px var(--shadow-hover);
}

.level-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  flex-shrink: 0;
}

.level-btn.active .level-num {
  background: var(--bg);
}

.level-label {
  font-size: 0.88rem;
  font-weight: 500;
  flex: 1;
}

.level-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg-elevated);
  padding: 2px 8px;
  border-radius: 10px;
}

.btn-settings {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-settings:hover {
  background: var(--accent-light);
}

.btn-fullscreen {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-fullscreen:hover {
  background: var(--accent-light);
}

.material-symbols-outlined {
  font-variation-settings:
    'FILL' 0,
    'wght' 400,
    'GRAD' 0,
    'opsz' 24;
  color: var(--text);
  font-size: 1.1rem;
}

.header-icon {
  font-size: 1.2em;
  vertical-align: -0.15em;
}

/* ── Study area ── */
.study-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  gap: 40px;
  position: relative;
}

.flashcard-container {
  width: 100%;
  max-width: 700px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.flashcard {
  width: 100%;
  aspect-ratio: 3 / 2;
  max-height: 55vh;
  background: var(--bg-elevated);
  border-radius: var(--r);
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.flashcard-hiragana {
  font-family: 'Noto Sans JP', sans-serif;
  font-size: clamp(3.5rem, 12vw, 8rem);
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
  text-align: center;
}

.flashcard-kanji {
  font-family: 'Noto Sans JP', sans-serif;
  font-size: clamp(1.4rem, 3.5vw, 2.4rem);
  font-weight: 500;
  color: var(--kanji-color);
  line-height: 1.3;
  text-align: center;
  min-height: 1.5em;
}

.flashcard-english {
  font-family: 'Sora', sans-serif;
  font-size: clamp(1rem, 2.5vw, 1.5rem);
  font-weight: 400;
  color: var(--english-color);
  line-height: 1.4;
  text-align: center;
  text-transform: capitalize;
}

/* Progress bar */
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-sidebar);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-dark));
  border-radius: 3px;
  transition: width 0.4s ease;
}

.status-text {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  min-height: 1.2em;
}

/* ── Controls ── */
.controls {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.control-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 24px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 72px;
}

.control-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px var(--shadow-hover);
}

.control-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.control-btn.primary {
  background: var(--btn-primary-bg);
  border-color: var(--accent-dark);
  color: var(--btn-primary-text);
}

.control-btn.primary:hover:not(:disabled) {
  background: var(--accent-dark);
}

.control-icon {
  font-size: 1.4rem;
  line-height: 1;
}

.control-label {
  font-size: 0.75rem;
  font-weight: 500;
}

/* ── Settings overlay ── */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.settings-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.settings-panel {
  background: var(--bg-elevated);
  border-radius: var(--r);
  box-shadow: 0 16px 48px rgba(0,0,0,0.2);
  width: 90%;
  max-width: 420px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  transform: translateY(20px);
  transition: transform 0.25s ease;
}

.settings-overlay.open .settings-panel {
  transform: translateY(0);
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

.settings-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
}

.close-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--bg-sidebar);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.close-btn:hover {
  background: var(--accent-light);
}

.settings-body {
  padding: 20px 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-row.toggle-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.setting-row label {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
}

.setting-control {
  display: flex;
  align-items: center;
  gap: 12px;
}

.setting-control input[type="range"] {
  flex: 1;
  accent-color: var(--accent-dark);
}

.value {
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 40px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

select {
  padding: 8px 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

/* Toggle switch */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border);
  border-radius: 24px;
  transition: background 0.2s ease;
}

.slider::before {
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.switch input:checked + .slider {
  background: var(--accent-dark);
}

.switch input:checked + .slider::before {
  transform: translateX(20px);
}

.btn-secondary {
  padding: 10px 16px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--btn-secondary-bg);
  color: var(--btn-secondary-text);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
  align-self: flex-start;
}

.btn-secondary:hover {
  background: var(--accent-light);
}

/* ── Fullscreen mode ── */
.app.fullscreen .sidebar {
  display: none;
}

.app.fullscreen .study-area {
  padding: 60px;
}

/* ── Pitch accent ── */
.pitch-accent {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 4px;
  height: 28px;
  margin-bottom: 4px;
  min-height: 28px;
}

.pitch-mora {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 20px;
}

.pitch-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--accent-dark);
  transition: transform 0.3s ease, background 0.3s ease;
}

.pitch-dot.high {
  background: var(--accent-dark);
  transform: translateY(-4px);
}

.pitch-dot.low {
  background: transparent;
  transform: translateY(6px);
}

.pitch-line {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--accent-dark);
  opacity: 0.4;
  z-index: -1;
}

.pitch-accent {
  position: relative;
}

/* ── Hide / Reveal ── */
.flashcard-kanji,
.flashcard-english {
  transition: opacity 0.5s ease, filter 0.5s ease;
}

.flashcard-kanji.hidden-field,
.flashcard-english.hidden-field {
  opacity: 0;
  filter: blur(6px);
  pointer-events: none;
  user-select: none;
}

/* ── Animations ── */
.flashcard.slide-in {
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ── Touch improvements ── */
.control-btn,
.level-btn,
.btn-settings,
.btn-fullscreen,
.close-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ── Mobile sidebar ── */
.mobile-top-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.mobile-title {
  font-size: 1.2rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.mobile-title .header-icon {
  font-size: 1.4rem;
}

.mobile-level-select {
  flex: 1;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  min-width: 0;
}

.mobile-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.mobile-round-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 0;
  flex-shrink: 0;
}

.mobile-round-btn:hover {
  background: var(--accent-light);
}

.mobile-round-btn .material-symbols-outlined {
  font-size: 1.2rem;
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .app {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    height: auto;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 12px;
    flex-shrink: 0;
  }

  .sidebar-desktop {
    display: none;
  }

  .sidebar-mobile {
    display: block;
    width: 100%;
  }

  .study-area {
    padding: 16px;
    gap: 16px;
    justify-content: flex-start;
    padding-top: 20px;
  }

  .flashcard {
    aspect-ratio: auto;
    max-height: none;
    min-height: 260px;
    padding: 24px 16px;
    gap: 12px;
  }

  .controls {
    gap: 10px;
    padding-bottom: env(safe-area-inset-bottom, 12px);
  }

  .control-btn {
    padding: 12px 16px;
    min-width: 68px;
    min-height: 56px;
    gap: 6px;
  }

  .control-icon {
    font-size: 1.6rem;
  }

  .control-label {
    font-size: 0.8rem;
  }
}

@media (max-width: 480px) {
  .settings-panel {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }

  .settings-overlay.open .settings-panel {
    transform: translateY(0);
  }

  .flashcard {
    min-height: 220px;
    padding: 20px 14px;
  }

  .flashcard-hiragana {
    font-size: clamp(2.8rem, 14vw, 4.5rem);
  }

  .flashcard-kanji {
    font-size: clamp(1.1rem, 4vw, 1.6rem);
  }

  .flashcard-english {
    font-size: clamp(0.85rem, 3vw, 1.1rem);
  }

  .pitch-accent {
    height: 22px;
    min-height: 22px;
    gap: 3px;
  }

  .pitch-dot {
    width: 8px;
    height: 8px;
    border-width: 1.5px;
  }

  .controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg);
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    gap: 8px;
    z-index: 10;
  }

  .study-area {
    padding-bottom: 100px;
  }

  .status-text {
    margin-bottom: 8px;
  }
}

@media (max-width: 360px) {
  .control-btn {
    min-width: 58px;
    padding: 10px 12px;
  }

  .flashcard-hiragana {
    font-size: 2.4rem;
  }
}

/* Disable blur on mobile for performance */
@media (max-width: 480px) {
  .flashcard-kanji.hidden-field,
  .flashcard-english.hidden-field {
    filter: none;
    opacity: 0.05;
  }
}

/* Prevent pull-to-refresh on mobile */
body {
  overscroll-behavior-y: none;
}
