/* ── CSS Custom Properties ─────────────────────────────────────────────── */
:root {
  --color-bg:          #f5f5f7;
  --color-surface:     #ffffff;
  --color-surface-2:   #f0f0f5;
  --color-border:      #e0e0e8;
  --color-text:        #1a1a2e;
  --color-text-sub:    #5a5a72;
  --color-text-muted:  #9090a8;
  --color-primary:     #4f46e5;
  --color-primary-dk:  #3730a3;
  --color-good:        #16a34a;
  --color-medium:      #d97706;
  --color-bad:         #dc2626;
  --color-nav:         #ffffff;
  --color-nav-border:  #e8e8f0;
  --color-card:        #ffffff;
  --color-card-back:   #f0f0ff;
  --shadow-card:       0 4px 24px rgba(0,0,0,.10);
  --shadow-btn:        0 2px 8px rgba(79,70,229,.25);
  --radius-card:       18px;
  --radius-btn:        12px;
  --nav-height:        64px;
  --header-height:     56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg:          #0f0f1a;
    --color-surface:     #1a1a2e;
    --color-surface-2:   #22223a;
    --color-border:      #2e2e4a;
    --color-text:        #e8e8f8;
    --color-text-sub:    #a0a0c0;
    --color-text-muted:  #60607a;
    --color-primary:     #6366f1;
    --color-primary-dk:  #4f46e5;
    --color-good:        #22c55e;
    --color-medium:      #f59e0b;
    --color-bad:         #f87171;
    --color-nav:         #1a1a2e;
    --color-nav-border:  #2e2e4a;
    --color-card:        #1e1e38;
    --color-card-back:   #1a1a3a;
    --shadow-card:       0 4px 24px rgba(0,0,0,.40);
    --shadow-btn:        0 2px 8px rgba(99,102,241,.30);
  }
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP',
               'Yu Gothic', sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

/* ── Screen System ───────────────────────────────────────────────────── */
.screen {
  display: none;
  position: fixed;
  inset: 0 0 var(--nav-height) 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--color-bg);
}
.screen.active { display: block; }

/* Study screen: no scroll (card is centered in viewport) */
#screen-study {
  display: none;
  overflow: hidden;
}
#screen-study.active { display: flex; flex-direction: column; }

/* ── Headers ─────────────────────────────────────────────────────────── */
.screen-header {
  padding: 20px 20px 12px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}
.screen-header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -.5px;
}
.screen-header .subtitle {
  font-size: .85rem;
  color: var(--color-text-sub);
  margin-top: 2px;
}

/* ── Bottom Navigation ───────────────────────────────────────────────── */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--nav-height);
  display: flex;
  background: var(--color-nav);
  border-top: 1px solid var(--color-nav-border);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: .7rem;
  cursor: pointer;
  min-height: 44px;
  transition: color .2s;
}
.nav-btn.active { color: var(--color-primary); }
.nav-btn svg { display: block; }

/* ── Buttons ─────────────────────────────────────────────────────────── */
.btn-primary {
  display: block;
  width: 100%;
  min-height: 44px;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-btn);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-btn);
  transition: transform .15s, background .2s;
}
.btn-primary:active { transform: scale(.97); }
.btn-primary:disabled { background: var(--color-text-muted); box-shadow: none; cursor: not-allowed; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-dk); }

.btn-secondary {
  display: block;
  width: 100%;
  min-height: 44px;
  background: var(--color-surface-2);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-btn);
  font-size: .95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .2s;
}
.btn-secondary:hover { background: var(--color-border); }

.btn-danger {
  display: block;
  width: 100%;
  min-height: 44px;
  background: transparent;
  color: var(--color-bad);
  border: 1px solid var(--color-bad);
  border-radius: var(--radius-btn);
  font-size: .95rem;
  font-weight: 500;
  cursor: pointer;
  margin-top: 12px;
  transition: background .2s, color .2s;
}
.btn-danger:hover { background: var(--color-bad); color: #fff; }

/* ── Home Screen ─────────────────────────────────────────────────────── */
.home-main {
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.btn-start {
  padding: 20px;
  display: flex !important;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border-radius: 16px;
}
.btn-start-label { font-size: 1.2rem; font-weight: 700; }
.btn-start-sub   { font-size: .85rem; opacity: .85; font-weight: 400; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 14px;
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--color-primary); }
.stat-label { font-size: .7rem; color: var(--color-text-sub); text-align: center; }
.stat-total { font-size: .68rem; color: var(--color-text-muted); }

.phase-info {
  background: var(--color-surface-2);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: .85rem;
  color: var(--color-text-sub);
  text-align: center;
}

/* ── Study Screen ───────────────────────────────────────────────────── */
.study-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.btn-quit {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  color: var(--color-text-sub);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
}
.progress-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#cards-remaining {
  font-size: .8rem;
  color: var(--color-text-sub);
  font-weight: 500;
}
.progress-bar-track {
  height: 6px;
  background: var(--color-surface-2);
  border-radius: 99px;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 99px;
  transition: width .4s ease;
}

/* ── Flashcard ───────────────────────────────────────────────────────── */
.study-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 20px 120px;
  overflow: hidden;
}

.flashcard {
  width: 100%;
  max-width: 420px;
  aspect-ratio: 3 / 2;
  cursor: pointer;
  perspective: 1000px;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.4, 0, .2, 1);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
}
.flashcard.flipped .card-inner { transform: rotateY(180deg); }

.card-front, .card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius-card);
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  overflow: hidden;
}

.card-front {
  background: var(--color-card);
}
.card-back {
  background: var(--color-card-back);
  transform: rotateY(180deg);
  justify-content: flex-start;
  padding-top: 20px;
  gap: 6px;
}

.card-freq {
  font-size: .75rem;
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: 2px 8px;
  border-radius: 99px;
  align-self: flex-start;
}
.card-word {
  font-size: 2.4rem;
  font-weight: 700;
  letter-spacing: -.5px;
  text-align: center;
  color: var(--color-text);
  line-height: 1.1;
}
.card-hint {
  font-size: .75rem;
  color: var(--color-text-muted);
  margin-top: 4px;
}

.back-word {
  font-size: 1.4rem;
  font-weight: 700;
  align-self: flex-start;
  color: var(--color-primary);
}
.back-definition {
  font-size: .85rem;
  color: var(--color-text);
  line-height: 1.5;
  align-self: flex-start;
  border-left: 3px solid var(--color-primary);
  padding-left: 10px;
  margin-top: 4px;
}
.back-meaning {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
  align-self: flex-start;
  margin-top: 6px;
}
.back-example-block {
  margin-top: 6px;
  width: 100%;
  background: var(--color-surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.back-example {
  font-size: .82rem;
  color: var(--color-text);
  font-style: italic;
  line-height: 1.5;
}
.back-example-ja {
  font-size: .78rem;
  color: var(--color-text-sub);
  line-height: 1.5;
}

/* ── Answer Buttons ──────────────────────────────────────────────────── */
.answer-buttons {
  position: fixed;
  bottom: var(--nav-height);
  left: 0; right: 0;
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  z-index: 50;
}
.answer-buttons.hidden { display: none; }

.btn-answer {
  flex: 1;
  min-height: 50px;
  border: none;
  border-radius: 12px;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .15s, opacity .2s;
  color: #fff;
}
.btn-answer:active { transform: scale(.95); }
.btn-bad    { background: var(--color-bad); }
.btn-medium { background: var(--color-medium); }
.btn-good   { background: var(--color-good); }

/* ── Session Complete Overlay ────────────────────────────────────────── */
.session-complete {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 24px;
}
.session-complete.hidden { display: none; }

.complete-card {
  background: var(--color-surface);
  border-radius: 20px;
  padding: 28px 24px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 8px 40px rgba(0,0,0,.3);
}
.complete-icon { font-size: 2.5rem; text-align: center; }
.complete-card h2 { text-align: center; font-size: 1.3rem; }
.complete-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--color-surface-2);
  border-radius: 12px;
  padding: 14px;
}
.cs-row {
  display: flex;
  justify-content: space-between;
  font-size: .9rem;
  color: var(--color-text-sub);
}
.cs-row strong { color: var(--color-text); }

/* ── Progress Screen ─────────────────────────────────────────────────── */
.progress-main { padding: 20px; display: flex; flex-direction: column; gap: 20px; }

.progress-phase-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.progress-phase-card h2 { font-size: .95rem; font-weight: 600; }
.prog-bar-wrap { display: flex; flex-direction: column; gap: 6px; }
.prog-bar-track {
  height: 10px;
  background: var(--color-surface-2);
  border-radius: 99px;
  overflow: hidden;
}
.prog-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--color-primary), #818cf8);
  border-radius: 99px;
  transition: width .6s ease;
}
#prog-mastered-label { font-size: .8rem; color: var(--color-text-sub); }

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

.last7-section { display: flex; flex-direction: column; gap: 12px; }
.last7-section h3 { font-size: .9rem; color: var(--color-text-sub); }

.last7-bars {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  align-items: flex-end;
  height: 80px;
}
.day-bar-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  height: 100%;
  justify-content: flex-end;
}
.day-bar {
  width: 100%;
  min-height: 4px;
  background: var(--color-primary);
  border-radius: 4px 4px 0 0;
  opacity: .85;
  transition: height .4s ease;
}
.day-bar.empty { background: var(--color-border); opacity: 1; }
.day-label { font-size: .6rem; color: var(--color-text-muted); }

/* ── Settings Screen ─────────────────────────────────────────────────── */
.settings-main { padding: 20px; display: flex; flex-direction: column; gap: 8px; }

.settings-section {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.settings-section h2 { font-size: .9rem; color: var(--color-text-sub); font-weight: 600; }

.radio-group {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.radio-group label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: .85rem;
  color: var(--color-text);
}
.radio-group input[type="radio"] {
  accent-color: var(--color-primary);
  width: 18px; height: 18px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.toggle-label input[type="checkbox"] { display: none; }
.toggle-track {
  position: relative;
  width: 48px; height: 28px;
  background: var(--color-border);
  border-radius: 99px;
  flex-shrink: 0;
  transition: background .2s;
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 22px; height: 22px;
  background: #fff;
  border-radius: 50%;
  transition: transform .2s;
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
.toggle-label input:checked + .toggle-track { background: var(--color-primary); }
.toggle-label input:checked + .toggle-track::after { transform: translateX(20px); }
.toggle-text { font-size: .9rem; color: var(--color-text); }

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