:root {
  --navy: #1e2761;
  --navy-light: #2c3684;
  --ice: #cadcfc;
  --yellow: #ffd43b;
  --blue: #306998;
  --green: #22c55e;
  --red: #ef4444;
  --bg: #f4f6fb;
  --card-bg: #ffffff;
  --text: #1b1b2a;
  --muted: #6b7280;
  --radius: 20px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

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

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(16px + var(--safe-bottom));
}

.topbar {
  padding: calc(14px + var(--safe-top)) 20px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
}

.back-btn {
  background: none;
  border: none;
  color: var(--navy);
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  border-radius: 8px;
}
.back-btn.hidden { display: none; }
.hidden { display: none !important; }
.back-btn:active { background: var(--ice); }

.topbar h1 {
  font-size: 20px;
  margin: 0;
  color: var(--navy);
  letter-spacing: 0.2px;
}

.tabs {
  display: flex;
  background: #e7ebf5;
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab-btn.active {
  background: var(--navy);
  color: #fff;
}

.view { padding: 0 20px; flex: 1; display: flex; flex-direction: column; }
.view.hidden { display: none; }

/* Landing page */
.landing-subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 4px 0 16px;
}

.deck-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.deck-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card-bg);
  border: 1px solid #e7ebf5;
  border-radius: 14px;
  padding: 16px 18px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: box-shadow 0.15s, border-color 0.15s;
}

.deck-card:hover {
  box-shadow: 0 4px 14px rgba(30,39,97,0.1);
  border-color: #bbc;
}

.deck-card-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.deck-card-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.deck-card-desc {
  font-size: 0.82rem;
  color: var(--muted);
}

.deck-card-badge {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--navy);
  background: var(--ice);
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: 12px;
}

.progress-row {
  display: flex;
  justify-content: space-between;
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 4px;
  font-weight: 600;
}

.progress-bar {
  height: 6px;
  background: #e2e7f3;
  border-radius: 999px;
  margin-top: 6px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--blue), var(--yellow));
  width: 0%;
  transition: width 0.35s ease;
}

.card-stage {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46vh;
  margin-top: 18px;
}

.card-wrap {
  position: relative;
  width: 100%;
  max-width: 380px;
  aspect-ratio: 3 / 4;
  touch-action: pan-y;
}

.swipe-hint {
  position: absolute;
  top: 16px;
  z-index: 5;
  padding: 6px 12px;
  border-radius: 8px;
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 1px;
  opacity: 0;
  transform: scale(0.9);
  transition: opacity 0.08s linear;
  pointer-events: none;
  border: 3px solid;
}

.hint-right {
  right: 16px;
  color: var(--green);
  border-color: var(--green);
  transform: rotate(8deg);
}

.hint-left {
  left: 16px;
  color: var(--red);
  border-color: var(--red);
  transform: rotate(-8deg);
}

.flashcard {
  position: relative;
  width: 100%;
  height: 100%;
  cursor: pointer;
  perspective: 1400px;
  border-radius: var(--radius);
  outline: none;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.flashcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.5s cubic-bezier(0.4, 0.2, 0.2, 1);
  transform-style: preserve-3d;
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(30, 39, 97, 0.18), 0 2px 6px rgba(30,39,97,0.08);
}

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

.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: var(--radius);
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 14px;
}

.face-front {
  background: var(--card-bg);
  border: 1px solid #e7ebf5;
}

.face-back {
  background: var(--navy);
  color: #fff;
  transform: rotateY(180deg);
}

.face-tag {
  position: absolute;
  top: 16px;
  left: 18px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 2px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--ice);
  color: var(--navy);
}

.face-back .face-tag {
  background: var(--yellow);
  color: var(--navy);
}

.face-text {
  font-size: 18px;
  line-height: 1.45;
  font-weight: 600;
  white-space: pre-line;
  margin: 0;
  max-height: 80%;
  overflow-y: auto;
}

.face-back .face-text {
  font-weight: 500;
  font-size: 16.5px;
}

/* inline code — both faces */
.face-text code {
  font-family: "SFMono-Regular", Menlo, Consolas, "Courier New", monospace;
  background: rgba(0, 0, 0, 0.07);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.88em;
}
.face-back .face-text code {
  background: rgba(255, 255, 255, 0.18);
}

/* face with any code block — switch to left-aligned layout */
.face.has-code {
  align-items: flex-start;
  text-align: left;
  justify-content: flex-start;
  padding-top: 44px;
}
.face.has-code .face-text {
  width: 100%;
}

/* code block */
.face-text pre {
  margin: 6px 0;
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  white-space: pre;
  width: 100%;
  box-sizing: border-box;
  line-height: 1.55;
  font-family: "SFMono-Regular", Menlo, Consolas, "Courier New", monospace;
  font-size: 13px;
}
.face-front .face-text pre {
  background: rgba(0, 0, 0, 0.05);
}
.face-back .face-text pre {
  background: rgba(0, 0, 0, 0.28);
}
.face .face-text pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

.tap-hint {
  position: absolute;
  bottom: 14px;
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.face-back .tap-hint {
  color: var(--ice);
}

.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 22px;
  margin-top: 22px;
}

.btn {
  border: none;
  cursor: pointer;
  font-family: inherit;
}

.btn.round {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: #fff;
  color: var(--red);
  box-shadow: 0 4px 14px rgba(30,39,97,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn.round.secondary {
  width: 46px;
  height: 46px;
  color: var(--navy);
  background: var(--ice);
  box-shadow: none;
}

.btn.round.success {
  color: var(--green);
}

.btn.round:active { transform: scale(0.92); }

.controls-caption {
  text-align: center;
  font-size: 11.5px;
  color: var(--muted);
  margin: 12px 0 0;
}

.btn.text-btn {
  background: none;
  color: var(--blue);
  font-size: 13.5px;
  font-weight: 700;
  margin: 18px auto 6px;
  padding: 8px;
}

.btn.primary {
  background: var(--navy);
  color: #fff;
  font-weight: 700;
  padding: 12px 22px;
  border-radius: 12px;
  font-size: 14px;
  margin-top: 14px;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.empty-state.hidden { display: none; }
.empty-emoji { font-size: 44px; margin: 0 0 4px; }

.learned-header { margin-top: 6px; }
.learned-header h2 { margin: 6px 0 2px; color: var(--navy); font-size: 20px; }
.muted { color: var(--muted); font-size: 13px; margin: 0 0 14px; }

.learned-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.learned-item {
  background: #fff;
  border: 1px solid #e7ebf5;
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.learned-item .li-text {
  flex: 1;
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.35;
}

.learned-item .li-btn {
  flex-shrink: 0;
  background: var(--ice);
  color: var(--navy);
  border: none;
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--navy);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 50;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (min-width: 500px) {
  .app { padding-top: 8px; }
}
