:root {
  --bg: #f5f6fb;
  --surface: #ffffff;
  --text: #1f2333;
  --text-muted: #767b8f;
  --primary: #4f46e5;
  --primary-dark: #3f37c9;
  --success: #16a34a;
  --danger: #e11d48;
  --border: #e7e8f1;
  --shadow: 0 10px 30px rgba(31, 35, 51, 0.08);
  --shadow-sm: 0 4px 14px rgba(31, 35, 51, 0.06);
  --radius: 18px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.view {
  display: none;
  min-height: 100vh;
  flex-direction: column;
}
.view.active { display: flex; }

.hidden { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title {
  font-weight: 600;
  font-size: 16px;
}
.icon-btn {
  border: none;
  background: transparent;
  font-size: 20px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--bg); }
.icon-btn-spacer { width: 36px; }

/* ---------- Auth ---------- */
.auth-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  gap: 28px;
}
.brand { text-align: center; }
.brand-jp {
  font-size: 34px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: 2px;
}
.brand h1 {
  margin: 6px 0 2px;
  font-size: 22px;
}
.brand .sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 14px;
}

.card-panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  width: 100%;
  max-width: 360px;
}

.tabs {
  display: flex;
  background: var(--bg);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 18px;
}
.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 0;
  border-radius: 9px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text-muted);
  cursor: pointer;
}
.tab-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.auth-form { display: none; flex-direction: column; gap: 6px; }
.auth-form.active { display: flex; }
.auth-form label {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
}
.auth-form input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 15px;
  background: var(--bg);
}
.auth-form input:focus {
  outline: none;
  border-color: var(--primary);
}
.form-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin: 6px 0 0;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 13px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
  margin-top: 14px;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }

/* ---------- Home ---------- */
.home-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  gap: 30px;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.start-btn {
  width: 100%;
  max-width: 320px;
  padding: 22px;
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), #7c6ef2);
  border: none;
  border-radius: 20px;
  box-shadow: 0 14px 28px rgba(79, 70, 229, 0.28);
  cursor: pointer;
}
.start-btn:active { transform: scale(0.98); }

.home-grid {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.mini-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  cursor: pointer;
  text-align: left;
}
.mini-card-icon { font-size: 22px; }
.mini-card-title { font-weight: 700; font-size: 15px; }
.mini-card-desc { font-size: 12px; color: var(--text-muted); }

/* ---------- Mode select ---------- */
.mode-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 18px;
  padding: 24px;
}
.mode-btn {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 26px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.mode-btn:hover { border-color: var(--primary); }
.mode-flags { font-size: 26px; }
.mode-label { font-weight: 700; font-size: 16px; }

/* ---------- Cards ---------- */
.cards-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  padding: 24px 20px 34px;
}
.card-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 380px;
}

.flashcard {
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 60vh;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(.2,.9,.3,1);
  cursor: grab;
  touch-action: pan-y;
}
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard.dragging { transition: none; }

.flashcard-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 24px;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  border: 1px solid var(--border);
}
.flashcard-back { transform: rotateY(180deg); background: #f0eefd; }

.card-word {
  font-size: 30px;
  font-weight: 700;
  line-height: 1.35;
  word-break: break-word;
}

.card-hiragana {
  display: block;
  margin-top: 10px;
  font-size: 20px;
  font-weight: 500;
  color: var(--text-muted);
  word-break: break-word;
}
.card-hiragana:empty { display: none; }

.swipe-hint {
  margin-top: 18px;
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
}

.answer-buttons {
  display: flex;
  gap: 14px;
  width: 100%;
  max-width: 380px;
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.answer-buttons.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.answer-buttons .btn { flex: 1; padding: 16px; font-size: 15px; transition: box-shadow 0.15s ease, transform 0.15s ease; }
.answer-buttons .btn.selected {
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.18) inset, 0 6px 14px rgba(0, 0, 0, 0.18);
  transform: scale(0.97);
}

.deck-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-muted);
}

/* ---------- Word list ---------- */
.list-wrap {
  flex: 1;
  padding: 18px 20px 30px;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}
.search-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 15px;
  margin-bottom: 14px;
}
.search-input:focus { outline: none; border-color: var(--primary); }

.word-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.word-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
}
.word-row-text { display: flex; flex-direction: column; }
.word-row-jp { font-weight: 700; font-size: 15px; }
.word-row-tr { font-size: 13px; color: var(--text-muted); }
.word-badge {
  font-size: 13px;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 20px;
  white-space: nowrap;
}
.badge-none { background: #eef0f6; color: var(--text-muted); }
.badge-low { background: #fde2e6; color: var(--danger); }
.badge-mid { background: #fef3c7; color: #b45309; }
.badge-high { background: #dcfce7; color: var(--success); }

@media (min-width: 640px) {
  .card-panel, .card-stage, .list-wrap { margin: 0 auto; }
}
