/* ===== CSS Custom Properties ===== */
:root {
  --bg: #ffffff;
  --bg-secondary: #f5f5f5;
  --bg-card: #ffffff;
  --text: #1a1a1a;
  --text-secondary: #666666;
  --border: #e0e0e0;
  --primary: #e74c3c;
  --primary-hover: #c0392b;
  --accent: #3498db;
  --success: #27ae60;
  --danger: #e74c3c;
  --warning: #f39c12;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
  --radius: 12px;
  --nav-height: 64px;
  --header-height: 56px;
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #0f3460;
  --text: #e0e0e0;
  --text-secondary: #a0a0a0;
  --border: #2a2a4a;
  --primary: #e74c3c;
  --primary-hover: #ff6b6b;
  --accent: #64b5f6;
  --success: #66bb6a;
  --danger: #ef5350;
  --warning: #ffa726;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-top: var(--header-height);
  padding-bottom: var(--nav-height);
  transition: background 0.3s, color 0.3s;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Header ===== */
#app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
}

#header-title {
  font-size: 1.4rem;
  color: var(--primary);
}

#theme-toggle {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: background 0.2s;
}

#theme-toggle:hover {
  background: var(--border);
}

/* ===== Bottom Navigation ===== */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 16px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: color 0.2s;
  font-size: 0.75rem;
}

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

.nav-icon {
  font-size: 1.4rem;
}

.nav-label {
  font-size: 0.7rem;
  font-weight: 500;
}

/* ===== Views ===== */
.view {
  display: none;
  padding: 16px;
  max-width: 600px;
  margin: 0 auto;
}

.view.active {
  display: block;
}

.view h2 {
  margin-bottom: 16px;
  font-size: 1.3rem;
}

/* ===== Back Button ===== */
.back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px 0;
  margin-bottom: 12px;
}

/* ===== Category Grid ===== */
.category-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.category-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: var(--shadow);
  text-align: center;
}

.category-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

.category-card:active {
  transform: scale(0.97);
}

.category-emoji {
  font-size: 2rem;
  display: block;
  margin-bottom: 8px;
}

.category-name {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 4px;
}

.category-count {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.category-progress {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 8px;
}

.category-progress-fill {
  height: 100%;
  background: var(--success);
  border-radius: 2px;
  transition: width 0.3s;
}

/* ===== Subcategory items ===== */
.subcategory-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.subcategory-card:hover {
  transform: translateY(-2px);
}

.subcategory-card:active {
  transform: scale(0.97);
}

.sub-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.sub-action-btn {
  font-size: 0.7rem;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text);
  cursor: pointer;
}

.sub-action-btn:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* ===== Flashcard ===== */
#flashcard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

#flashcard-progress {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

#error-mode-label {
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--text-secondary);
}

.flashcard {
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 16px;
  text-align: center;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  position: relative;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: border-color 0.3s;
}

.flashcard.correct {
  border-color: var(--success);
}

.flashcard.wrong {
  border-color: var(--danger);
}

.card-front {
  font-size: 2.5rem;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 8px;
}

.card-hint {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.audio-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  font-size: 1.3rem;
  cursor: pointer;
  transition: background 0.2s;
}

.audio-btn:hover {
  background: var(--bg-secondary);
}

/* ===== Answer Section ===== */
#answer-form {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

#answer-input {
  flex: 1;
  padding: 12px 16px;
  font-size: 1.1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

#answer-input:focus {
  border-color: var(--accent);
}

#check-btn {
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

#check-btn:hover {
  background: var(--primary-hover);
}

/* ===== Answer Result ===== */
.answer-result {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 12px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.answer-result.correct {
  background: rgba(39, 174, 96, 0.15);
  color: var(--success);
  border: 1px solid var(--success);
}

.answer-result.wrong {
  background: rgba(231, 76, 60, 0.15);
  color: var(--danger);
  border: 1px solid var(--danger);
}

/* ===== Rating Buttons ===== */
.rating-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
}

.rate-btn {
  padding: 12px 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
  transition: background 0.2s, color 0.2s;
}

.rate-again:hover { background: var(--danger); color: #fff; }
.rate-hard:hover { background: var(--warning); color: #fff; }
.rate-good:hover { background: var(--success); color: #fff; }
.rate-easy:hover { background: var(--accent); color: #fff; }

/* ===== Session Summary ===== */
.session-summary {
  text-align: center;
  padding: 24px;
}

.session-summary h3 {
  font-size: 1.3rem;
  margin-bottom: 16px;
}

#summary-stats {
  margin-bottom: 24px;
  font-size: 1.1rem;
  line-height: 2;
}

.summary-easy {
  color: var(--accent);
  font-weight: 600;
}

.summary-good {
  color: var(--success);
  font-weight: 600;
}

.summary-hard {
  color: var(--warning);
  font-weight: 600;
}

.summary-again {
  color: var(--danger);
  font-weight: 600;
}

/* ===== List View ===== */
#list-search {
  width: 100%;
  padding: 10px 14px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  margin-bottom: 12px;
  outline: none;
}

#list-search:focus {
  border-color: var(--accent);
}

.list-table {
  width: 100%;
  border-collapse: collapse;
}

.list-table th,
.list-table td {
  padding: 10px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.list-table th {
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-transform: uppercase;
}

.list-table .jp-col {
  font-size: 1.2rem;
}

/* Mobile card list */
.list-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-card-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
}

.list-card-jp {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.list-card-romaji {
  font-size: 0.95rem;
  color: var(--accent);
}

.list-card-hint {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== Stats ===== */
.stats-section {
  margin-bottom: 24px;
}

.stats-section h3 {
  font-size: 1.1rem;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.stat-label {
  font-size: 0.9rem;
}

.stat-value {
  font-weight: 600;
  font-size: 0.9rem;
}

.stat-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 4px;
}

.stat-bar-fill {
  height: 100%;
  background: var(--success);
  border-radius: 3px;
  transition: width 0.3s;
}

.data-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ===== Buttons ===== */
.primary-btn {
  padding: 12px 24px;
  font-size: 1rem;
  font-weight: 600;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

.primary-btn:hover {
  background: var(--primary-hover);
}

.secondary-btn {
  padding: 10px 20px;
  font-size: 0.9rem;
  background: var(--bg-secondary);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

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

/* ===== Reveal Button ===== */
#reveal-btn {
  width: 100%;
  padding: 14px;
  font-size: 1.1rem;
  margin-bottom: 12px;
}

.reveal-answer {
  font-size: 2.2rem;
  font-weight: 700;
  text-align: center;
  margin: 8px 0;
}

.romaji-info {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* ===== Dashboard Header ===== */
.dashboard-header {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.dashboard-header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.streak-display {
  font-size: 1.1rem;
  font-weight: 700;
}

.streak-fire {
  font-size: 1.2rem;
}

.daily-goal-display {
  font-size: 0.9rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.goal-settings-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 2px 4px;
  border-radius: 4px;
  transition: background 0.2s;
}

.goal-settings-btn:hover {
  background: var(--border);
}

.daily-progress-bar {
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 4px;
}

.daily-progress-fill {
  height: 100%;
  background: var(--success);
  border-radius: 4px;
  transition: width 0.4s ease;
}

.daily-progress-fill.complete {
  background: var(--primary);
}

.daily-progress-text {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 4px;
  text-align: right;
}

/* ===== 7-Day Bar Chart ===== */
.week-chart {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.week-chart-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.week-chart-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
  width: 28px;
  text-align: right;
  flex-shrink: 0;
}

.week-chart-bar-container {
  flex: 1;
  display: flex;
  height: 20px;
  background: var(--bg-secondary);
  border-radius: 4px;
  overflow: hidden;
}

.week-chart-bar-easy {
  height: 100%;
  background: var(--accent);
  transition: width 0.4s ease;
}

.week-chart-bar-good {
  height: 100%;
  background: var(--success);
  transition: width 0.4s ease;
}

.week-chart-bar-hard {
  height: 100%;
  background: var(--warning);
  transition: width 0.4s ease;
}

.week-chart-bar-again {
  height: 100%;
  background: var(--danger);
  transition: width 0.4s ease;
}

.week-chart-count {
  font-size: 0.75rem;
  color: var(--text-secondary);
  width: 24px;
  flex-shrink: 0;
}

/* ===== Heatmap ===== */
.heatmap-container {
  overflow-x: auto;
}

.heatmap-months {
  display: flex;
  margin-left: 28px;
  margin-bottom: 4px;
}

.heatmap-month-label {
  font-size: 0.65rem;
  color: var(--text-secondary);
  flex: 1;
  text-align: left;
}

.heatmap-grid {
  display: grid;
  grid-template-rows: repeat(7, 1fr);
  grid-auto-flow: column;
  gap: 2px;
}

.heatmap-day-label {
  font-size: 0.6rem;
  color: var(--text-secondary);
  width: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 4px;
}

.heatmap-cell {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  background: var(--bg-secondary);
}

.heatmap-cell.level-0 { background: var(--bg-secondary); }
.heatmap-cell.level-1 { background: #9be9a8; }
.heatmap-cell.level-2 { background: #40c463; }
.heatmap-cell.level-3 { background: #30a14e; }
.heatmap-cell.level-4 { background: #216e39; }

[data-theme="dark"] .heatmap-cell.level-1 { background: #0e4429; }
[data-theme="dark"] .heatmap-cell.level-2 { background: #006d32; }
[data-theme="dark"] .heatmap-cell.level-3 { background: #26a641; }
[data-theme="dark"] .heatmap-cell.level-4 { background: #39d353; }

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: flex-end;
  margin-top: 8px;
  font-size: 0.65rem;
  color: var(--text-secondary);
}

.heatmap-legend .heatmap-cell {
  width: 10px;
  height: 10px;
}

.heatmap-separator {
  width: 4px;
  height: 12px;
  background: transparent;
}

.heatmap-separator-label {
  width: 4px !important;
  flex: 0 0 4px !important;
}

/* ===== Daily Challenge Card ===== */
.daily-challenge-card {
  background: var(--bg-card);
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
}

.daily-challenge-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

.daily-challenge-card:active {
  transform: scale(0.97);
}

.daily-challenge-emoji {
  font-size: 2rem;
  flex-shrink: 0;
}

.daily-challenge-text {
  display: flex;
  flex-direction: column;
}

.daily-challenge-title {
  font-weight: 700;
  font-size: 1rem;
}

.daily-challenge-sub {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== Mini Week Dots ===== */
.mini-week {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.mini-week-day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.mini-week-label {
  font-size: 0.6rem;
  color: var(--text-secondary);
}

.mini-week-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border);
}

.mini-week-dot.active {
  background: var(--success);
}

/* ===== Card Flip Animation ===== */
@keyframes card-flip {
  0% { transform: rotateY(0deg); }
  50% { transform: rotateY(90deg); }
  100% { transform: rotateY(0deg); }
}

.flashcard.flipping {
  animation: card-flip 0.4s ease-in-out;
}

/* ===== Stat Row Sub ===== */
.stat-row-sub {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 0 8px 0;
}

.stat-row-sub .stat-label,
.stat-row-sub .stat-value {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

/* ===== Confetti ===== */
#confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 10px;
  opacity: 0;
  animation: confetti-fall 3s ease-out forwards;
}

@keyframes confetti-fall {
  0% {
    opacity: 1;
    transform: translateY(0) rotate(0deg) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(100vh) rotate(720deg) scale(0.5);
  }
}

/* ===== Card Status Badges ===== */
.card-status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

.status-new {
  background: var(--border);
  color: var(--text-secondary);
}

.status-learning {
  background: rgba(243, 156, 18, 0.2);
  color: var(--warning);
}

.status-mastered {
  background: rgba(39, 174, 96, 0.2);
  color: var(--success);
}

/* ===== Global Search ===== */
#global-search {
  width: 100%;
  padding: 10px 14px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  margin-bottom: 12px;
  outline: none;
}

#global-search:focus {
  border-color: var(--accent);
}

.global-search-results {
  margin-bottom: 16px;
}

.global-search-group-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--primary);
  margin: 12px 0 6px 0;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.search-no-results {
  text-align: center;
  color: var(--text-secondary);
  padding: 24px;
  font-size: 0.95rem;
}

/* ===== Utility ===== */
.hidden {
  display: none !important;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .category-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .category-card {
    padding: 12px;
  }

  .category-emoji {
    font-size: 1.6rem;
  }

  .card-front {
    font-size: 2rem;
  }

  .rating-buttons {
    grid-template-columns: 1fr 1fr;
  }

  /* Use card layout on mobile list */
  .list-table {
    display: none;
  }
}

@media (min-width: 481px) {
  .list-cards {
    display: none;
  }
}

@media (min-width: 768px) {
  .category-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
