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

:root {
  --primary: #6C63FF;
  --primary2: #9B59B6;
  --bg: #F5F5FF;
  --card: #fff;
  --text: #1a1a2e;
  --sub: #666;
  --radius: 20px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: linear-gradient(140deg, #6C63FF 0%, #9B59B6 100%);
  min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}

.app {
  background: var(--bg);
  border-radius: 28px;
  width: 100%; max-width: 420px;
  min-height: min(700px, calc(100dvh - 32px));
  box-shadow: 0 30px 80px rgba(0,0,0,.35);
  overflow: hidden;
  display: flex; flex-direction: column;
}

@media (max-width: 440px) {
  body { padding: 0; align-items: stretch; }
  .app { border-radius: 0; max-width: 100%; min-height: 100dvh; box-shadow: none; }
}

/* ── Header ── */
header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff; padding: 13px 18px;
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
.logo { font-size: 1.25rem; font-weight: 800; letter-spacing: -.5px; }
.header-right { display: flex; align-items: center; gap: 8px; }
.user-info { display: flex; align-items: center; gap: 7px; }
.user-avatar { width: 30px; height: 30px; border-radius: 50%; border: 2px solid rgba(255,255,255,.5); object-fit: cover; }
.user-name { font-size: .78rem; opacity: .9; max-width: 80px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-logout { font-size: .72rem; padding: 4px 9px; background: rgba(255,255,255,.2); border: none; color: #fff; border-radius: 8px; cursor: pointer; }
.btn-login  { font-size: .82rem; padding: 6px 13px; background: #fff; color: var(--primary); border: none; border-radius: 10px; font-weight: 700; cursor: pointer; }

/* ── Sections ── */
.section { display: none; flex-direction: column; flex: 1; min-height: 0; }
.section.active { display: flex; }

/* ── Home ── */
#homeSection { align-items: center; justify-content: center; gap: 24px; padding: 28px 24px; }
.home-hero { text-align: center; }
.camera-icon-wrap {
  width: 86px; height: 86px;
  background: linear-gradient(135deg, var(--primary), var(--primary2));
  border-radius: 22px; display: flex; align-items: center; justify-content: center;
  font-size: 2.2rem; margin: 0 auto 14px;
  box-shadow: 0 10px 28px rgba(108,99,255,.4);
}
.home-hero h2 { font-size: 1.1rem; color: var(--text); line-height: 1.7; font-weight: 700; }
.home-btns { display: flex; flex-direction: column; gap: 11px; width: 100%; }
.home-sub-btns { display: flex; gap: 10px; width: 100%; }
.btn-review {
  flex: 1; padding: 12px 8px;
  background: linear-gradient(135deg, #FF6B6B, #FF8E53);
  color: #fff; border: none; border-radius: 14px;
  font-size: .9rem; font-weight: 700; cursor: pointer;
  position: relative;
  box-shadow: 0 4px 16px rgba(255,107,107,.35);
}
.btn-vocab {
  flex: 1; padding: 12px 8px;
  background: transparent; color: var(--primary);
  border: 2px solid var(--primary); border-radius: 14px;
  font-size: .9rem; font-weight: 700; cursor: pointer;
}
.review-badge {
  position: absolute; top: -6px; right: -6px;
  background: #FF3B30; color: #fff;
  font-size: .68rem; font-weight: 800;
  min-width: 20px; height: 20px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 5px;
}

/* ── Preview ── */
#previewSection { padding: 20px; gap: 14px; }
.preview-img-wrap { flex: 1; border-radius: var(--radius); overflow: hidden; background: #ddd; max-height: 340px; }
#previewImg { width: 100%; height: 100%; object-fit: cover; display: block; }
.preview-controls { display: flex; flex-direction: column; gap: 10px; }

/* ── Loading ── */
.loading-section { align-items: center; justify-content: center; gap: 20px; }
.spinner {
  width: 52px; height: 52px;
  border: 4px solid #e0e0ff; border-top-color: var(--primary);
  border-radius: 50%; animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--sub); font-size: .95rem; }

/* ── hidden file inputs ── */
.hidden-input {
  position: absolute; left: -9999px;
  width: 0; height: 0; opacity: 0; pointer-events: none;
}

/* ── Result Cards（フリップ） ── */
#resultSection { padding: 14px 0 18px; gap: 10px; }
.cards-wrap {
  display: flex; overflow-x: scroll; scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
  gap: 12px; padding: 0 20px;
}
.cards-wrap::-webkit-scrollbar { display: none; }

.word-card {
  scroll-snap-align: center;
  flex: 0 0 calc(100% - 40px);
  height: 430px;
  perspective: 1100px;
  cursor: pointer;
}
.word-card-inner {
  width: 100%; height: 100%; position: relative;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.4,0,.2,1);
}
.word-card.flipped .word-card-inner { transform: rotateY(180deg); }

.word-card-front,
.word-card-back {
  position: absolute; width: 100%; height: 100%;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: var(--radius); overflow: hidden;
  display: flex; flex-direction: column;
  border: 1px solid #e8e8ff;
  box-shadow: 0 4px 20px rgba(108,99,255,.1);
}
.word-card-back { transform: rotateY(180deg); }

/* 写真帯 */
.card-photo-strip {
  height: 160px; background: #f0f0f0; flex-shrink: 0;
  position: relative; overflow: hidden;
}
.card-photo-strip img {
  width: 100%; height: 100%; object-fit: contain; display: block;
}
.card-num-badge {
  position: absolute; top: 6px; left: 8px;
  background: rgba(0,0,0,.45); color: #fff;
  font-size: .62rem; font-weight: 800; padding: 2px 7px; border-radius: 20px;
}
.card-flip-hint {
  position: absolute; top: 6px; right: 8px;
  background: rgba(0,0,0,.35); color: rgba(255,255,255,.9);
  font-size: .58rem; padding: 2px 7px; border-radius: 20px;
}

/* 表：英語ボディ */
.card-front-body {
  flex: 1; min-height: 0;
  background: linear-gradient(155deg, var(--primary), var(--primary2));
  padding: 12px 16px; color: #fff;
  display: flex; flex-direction: column; gap: 5px;
}
.en-word { font-size: 1.9rem; font-weight: 800; text-transform: lowercase; line-height: 1.1; }
.card-ph-row { display: flex; align-items: center; justify-content: space-between; }
.phonetic { font-size: .85rem; opacity: .85; }
.audio-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  background: rgba(255,255,255,.2); border: none; border-radius: 10px;
  font-size: 1.1rem; cursor: pointer;
}
.audio-btn:active { background: rgba(255,255,255,.4); }
.card-divider { height: 1px; background: rgba(255,255,255,.25); margin: 3px 0; flex-shrink: 0; }
.card-example-row {
  display: flex; align-items: flex-start; gap: 6px; flex: 1; min-height: 0;
}
.card-example-text {
  font-size: .82rem; opacity: .9; font-style: italic; line-height: 1.5; flex: 1;
  overflow: hidden; display: -webkit-box;
  -webkit-line-clamp: 3; -webkit-box-orient: vertical;
}
.audio-btn-ex {
  width: 32px; height: 32px; flex-shrink: 0;
  background: rgba(255,255,255,.2); border: none; border-radius: 9px;
  font-size: 1rem; cursor: pointer; margin-top: 1px;
}
.audio-btn-ex:active { background: rgba(255,255,255,.4); }

/* 裏：日本語ボディ */
.card-back-body {
  flex: 1; min-height: 0; background: #fff;
  padding: 14px 16px 10px; display: flex; flex-direction: column;
  justify-content: flex-start; gap: 10px; overflow: hidden;
}
.back-ja-word { font-size: 2rem; font-weight: 900; color: var(--primary); line-height: 1.1; }
.back-example-ja {
  font-size: .86rem; color: var(--sub); line-height: 1.65;
  overflow: hidden; display: -webkit-box;
  -webkit-line-clamp: 3; -webkit-box-orient: vertical;
}

/* カード内保存ボタン */
.card-save-btn {
  width: 100%; padding: 8px;
  background: rgba(255,255,255,.2);
  border: 1.5px solid rgba(255,255,255,.5);
  border-radius: 10px; color: #fff;
  font-size: .82rem; font-weight: 700; cursor: pointer;
  margin-top: auto; flex-shrink: 0;
  transition: background .2s;
}
.card-save-btn:active { background: rgba(255,255,255,.35); }
.card-save-btn--back {
  background: #f0f0ff;
  border-color: var(--primary); color: var(--primary);
}
.card-save-btn--back:active { background: #e0e0ff; }
.card-save-btn:disabled { opacity: .55; cursor: default; }

.related-words {
  display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px;
}
.related-chip {
  display: inline-flex; align-items: center; gap: 3px;
  background: #f0f0ff; border-radius: 20px;
  padding: 3px 10px; font-size: .78rem;
}
.related-en { font-weight: 700; color: var(--primary); }
.related-sep { color: #aaa; font-size: .75rem; }
.related-ja { color: var(--text); }

.card-dots { display: flex; justify-content: center; gap: 7px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: #ccc; transition: background .2s, transform .2s; }
.dot.active { background: var(--primary); transform: scale(1.3); }
.result-nav {
  display: flex; gap: 10px; padding: 0 20px;
}
.btn-result-nav {
  flex: 1; padding: 12px 8px;
  background: #fff; color: var(--primary);
  border: 2px solid var(--primary); border-radius: 14px;
  font-size: .88rem; font-weight: 700; cursor: pointer;
  transition: background .2s;
}
.btn-result-nav:hover { background: #f0f0ff; }
.btn-result-nav:active { background: #e0e0ff; }

.result-actions { display: flex; flex-direction: column; gap: 10px; padding: 0 20px; margin-top: 4px; }

/* ── Review / Flashcard ── */
#reviewSection { flex-direction: column; }
.review-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; flex-shrink: 0;
  border-bottom: 1px solid #eee;
}
.review-progress { font-size: .88rem; font-weight: 700; color: var(--sub); }

.flashcard-area {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 16px 20px; min-height: 0;
}

.flashcard {
  width: 100%; max-width: 340px;
  height: 380px;
  perspective: 1200px; cursor: pointer;
}

.flashcard-inner {
  width: 100%; height: 100%; position: relative;
  transform-style: preserve-3d;
  transition: transform .55s cubic-bezier(.4,0,.2,1);
}

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

.flashcard-front,
.flashcard-back {
  position: absolute; width: 100%; height: 100%;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: 24px; overflow: hidden;
}

.flashcard-front {
  background: #1a1a2e;
  display: flex; flex-direction: column;
}
.flashcard-front img {
  width: 100%; flex: 1; object-fit: cover; display: block;
}
.flip-hint {
  text-align: center; padding: 14px;
  color: rgba(255,255,255,.7); font-size: .85rem; font-weight: 600;
  background: rgba(0,0,0,.3);
}
.rc-badge {
  position: absolute; top: 10px; right: 10px;
  background: rgba(0,0,0,.55); color: #fff;
  font-size: .7rem; font-weight: 800;
  padding: 3px 9px; border-radius: 20px;
}

.flashcard-back {
  transform: rotateY(180deg);
  background: var(--card);
  border: 1px solid #e8e8ff;
  box-shadow: 0 4px 24px rgba(108,99,255,.1);
  overflow-y: auto;
  display: flex; flex-direction: column;
}
.back-words { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.back-word-item { border-bottom: 1px solid #f0f0ff; padding-bottom: 12px; }
.back-word-item:last-child { border-bottom: none; padding-bottom: 0; }
.back-en { font-size: 1.5rem; font-weight: 800; color: var(--primary); }
.back-phonetic { font-size: .8rem; color: var(--sub); margin-top: 1px; }
.back-ja { font-size: 1rem; font-weight: 700; color: var(--text); margin-top: 4px; }
.back-def { font-size: .78rem; color: var(--sub); line-height: 1.5; margin-top: 4px; }
.back-ex { font-size: .75rem; color: #999; font-style: italic; margin-top: 3px; }

.review-actions {
  display: flex; gap: 12px; padding: 14px 20px;
  flex-shrink: 0;
}
.btn-again {
  flex: 1; padding: 15px;
  background: #fff; color: #FF6B6B;
  border: 2px solid #FF6B6B; border-radius: 14px;
  font-size: .95rem; font-weight: 700; cursor: pointer;
}
.btn-knew {
  flex: 1; padding: 15px;
  background: linear-gradient(135deg, #4CAF50, #45B75A);
  color: #fff; border: none; border-radius: 14px;
  font-size: .95rem; font-weight: 700; cursor: pointer;
  box-shadow: 0 4px 14px rgba(76,175,80,.35);
}

.review-done {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; padding: 24px;
}
.done-emoji { font-size: 4rem; }
.done-msg { font-size: 1.1rem; font-weight: 700; color: var(--text); text-align: center; }

/* ── Vocabulary ── */
#vocabSection { flex-direction: column; }
.vocab-header {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 18px; border-bottom: 1px solid #eee; flex-shrink: 0;
}
.vocab-header h2 { font-size: 1.05rem; font-weight: 800; color: var(--text); }
.btn-back-dark { background: transparent; border: none; color: var(--primary); font-size: .9rem; font-weight: 700; cursor: pointer; padding: 4px; }
.vocab-grid {
  display: flex; flex-direction: column;
  gap: 10px; padding: 14px; overflow-y: auto; flex: 1;
}
.vocab-entry {
  background: var(--card); border-radius: 14px; overflow: hidden;
  box-shadow: 0 2px 10px rgba(108,99,255,.09); border: 1px solid #eee;
  display: flex; flex-direction: row; align-items: stretch;
}
.vocab-thumb {
  width: 80px; height: 80px; flex-shrink: 0;
  object-fit: cover; display: block;
}
.vocab-info {
  flex: 1; padding: 10px 12px; min-width: 0;
  display: flex; flex-direction: column; justify-content: center; gap: 3px;
}
.vocab-words { font-size: .88rem; font-weight: 700; color: var(--text); line-height: 1.4; }
.vocab-ja { font-size: .78rem; color: var(--sub); }
.vocab-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.vocab-date { font-size: .68rem; color: #aaa; }
.vocab-streak {
  font-size: .68rem; font-weight: 800; padding: 2px 8px; border-radius: 10px;
  background: #e8e8ff; color: var(--primary);
}
.vocab-streak.mastered { background: #e8f5e9; color: #4CAF50; }
.vocab-empty { text-align: center; padding: 40px 0; color: var(--sub); }

/* ── Buttons ── */
.btn-primary {
  display: block; width: 100%; padding: 15px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff; border: none; border-radius: 14px;
  font-size: 1rem; font-weight: 700; cursor: pointer;
  box-shadow: 0 6px 20px rgba(108,99,255,.35);
  text-align: center; text-decoration: none;
  transition: opacity .2s, transform .1s;
}
.btn-primary:disabled { opacity: .6; cursor: default; }
.btn-primary:active { transform: scale(.97); }

.btn-secondary {
  display: block; width: 100%; padding: 15px;
  background: #fff; color: var(--primary);
  border: 2px solid var(--primary); border-radius: 14px;
  font-size: 1rem; font-weight: 700; cursor: pointer; text-align: center;
}

.btn-ghost {
  width: 100%; padding: 13px;
  background: transparent; color: var(--sub);
  border: 2px solid #ddd; border-radius: 14px;
  font-size: .95rem; cursor: pointer;
}
