:root {
  --page-bg: #f1f1f1;
  --surface: #ffffff;
  --surface-2: #ececec;
  --text: #1c1c1e;
  --text-dim: #6e6e73;
  --border: #dcdcdc;
  --accent: #262626;
  --accent-2: #8a8a8e;
  --danger: #ef5a6f;
  --success: #2fbf83;
  --mastered: #c9971f;
  --shadow: 0 20px 45px -18px rgba(0, 0, 0, 0.18);
  --radius-lg: 28px;
  --radius-md: 16px;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page-bg: #1c1c1e;
    --surface: #2c2c2e;
    --surface-2: #3a3a3c;
    --text: #f2f2f7;
    --text-dim: #a0a0a5;
    --border: #48484a;
    --accent: #5a5a5e;
    --accent-2: #8a8a8e;
    --danger: #ff7185;
    --success: #43d896;
    --mastered: #e8b93f;
    --shadow: 0 20px 45px -18px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--page-bg);
}

.hidden { display: none !important; }

button {
  font-family: inherit;
  cursor: pointer;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--surface);
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 200;
  box-shadow: var(--shadow);
  max-width: 90vw;
  text-align: center;
  animation: toast-in 0.25s ease;
}
.toast.toast-error { background: var(--danger); color: #fff; }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, -10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.sync-indicator {
  position: fixed;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 0.74rem;
  z-index: 200;
  transition: opacity 0.2s ease;
}

/* ---------- Layout screens ---------- */
.screen {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 24px 18px 32px;
  display: flex;
  flex-direction: column;
}

.topbar { position: relative; text-align: center; color: var(--text); margin-bottom: 18px; }
.topbar h1 { margin: 0 0 4px; font-size: 1.6rem; }
.topbar-refresh { position: absolute; top: 0; right: 0; font-size: 1.15rem; }
.topbar-refresh.spinning { animation: refresh-spin 0.6s linear infinite; }
@keyframes refresh-spin { to { transform: rotate(360deg); } }
.app-title { font-family: "Manrope", -apple-system, sans-serif; font-weight: 600; }
.topbar .subtitle { margin: 0; opacity: 0.85; font-size: 0.92rem; }

/* ---------- Import card ---------- */
.import-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 22px;
}

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 26px 16px;
  text-align: center;
  color: var(--text-dim);
  transition: border-color 0.2s, background 0.2s;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.drag-over {
  border-color: var(--accent);
  background: var(--surface-2);
  outline: none;
}
.dropzone-icon { font-size: 2rem; margin-bottom: 8px; }
.dropzone strong { color: var(--text); }

.import-alt {
  display: flex;
  justify-content: center;
  gap: 18px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.88rem;
  text-decoration: underline;
  padding: 4px;
}

.paste-area { margin-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.paste-area textarea {
  width: 100%;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  resize: vertical;
  background: var(--surface-2);
  color: var(--text);
}

.format-help {
  margin-top: 14px;
  font-size: 0.82rem;
  color: var(--text-dim);
}
.format-help summary { cursor: pointer; color: var(--accent); }
.format-help pre {
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 0.76rem;
}
.format-help code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}

/* ---------- Deck list ---------- */
.decks-list { display: flex; flex-direction: column; gap: 10px; }
.empty-hint { color: var(--text-dim); text-align: center; font-size: 0.9rem; }

/* ---------- Browse / bulk edit ---------- */
.browse-list { display: flex; flex-direction: column; gap: 6px; padding-bottom: 24px; }
.browse-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border-radius: 12px;
  padding: 8px 10px;
  box-shadow: var(--shadow);
}
.browse-row-grid {
  display: grid;
  gap: 8px;
  align-items: center;
}
.browse-row-head {
  background: transparent;
  box-shadow: none;
  padding: 0 10px;
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.browse-row-stats {
  font-size: 0.72rem;
  color: var(--text-dim);
  padding: 0 2px;
}
.browse-row input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.92rem;
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
  min-width: 0;
}
.browse-row select.gaengig-select {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  font-size: 0.86rem;
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
  min-width: 0;
  text-align: center;
}
.browse-row-delete {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 1rem;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
}
.browse-row-delete:hover { background: var(--surface-2); color: var(--danger); }
.browse-row-mastered { border-left: 3px solid var(--mastered); padding-left: 7px; }
.browse-add-row {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.9rem;
  font-weight: 600;
  margin-top: 4px;
}
.browse-add-row:hover { color: var(--text); border-color: var(--text-dim); }
.browse-empty-hint { color: var(--text-dim); text-align: center; font-size: 0.88rem; padding: 16px 0; }

.deck-item {
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  gap: 10px;
}
.deck-item-info { min-width: 0; flex: 1; }
.deck-item-name { font-weight: 600; overflow-wrap: break-word; word-break: break-word; }
.deck-item-meta { color: var(--text-dim); font-size: 0.82rem; margin-top: 2px; }
.deck-item-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 999px;
  padding: 11px 20px;
  font-size: 0.92rem;
  font-weight: 600;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-text { color: var(--danger); }
.btn-sm { padding: 8px 14px; font-size: 0.84rem; }
.btn-full { width: 100%; }

.settings-section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 4px; margin-bottom: 14px; }
.settings-section .field-label { display: block; font-weight: 600; font-size: 0.88rem; color: var(--text); margin-bottom: 8px; }
.settings-hint { font-size: 0.78rem; color: var(--text-dim); margin: 8px 0 0; }

.gaengig-filter-row { display: flex; flex-wrap: wrap; gap: 6px; }
.gaengig-second-label { display: block; margin-top: 12px; }
.gfilter-btn, .gfilter-max-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.8rem;
}
.gfilter-btn.active, .gfilter-max-btn.active { background: var(--mastered); color: #fff; border-color: var(--mastered); font-weight: 700; }

.gaengig-distribution {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 10px;
}
.gaengig-distribution span.gd-stars { color: var(--mastered); letter-spacing: 1px; margin-right: 6px; }

.icon-btn {
  background: var(--surface-2);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  flex-shrink: 0;
}
.icon-btn:hover { filter: brightness(0.96); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 40, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 150;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 100%;
  max-width: 420px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal h2 { margin-top: 0; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 0.88rem; color: var(--text-dim); }
.field span { font-weight: 600; color: var(--text); }
.field input[type="text"], .field select {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  font-size: 0.95rem;
  background: var(--surface-2);
  color: var(--text);
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }

.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.import-summary { font-size: 0.85rem; color: var(--text-dim); background: var(--surface-2); padding: 8px 12px; border-radius: 10px; }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.modal-actions-split { justify-content: space-between; }
.modal-actions-right { display: flex; gap: 10px; }

.voice-pickers { display: flex; flex-direction: column; gap: 12px; margin: 8px 0 4px; }
.voice-picker-row { border-top: 1px solid var(--border); padding-top: 10px; }
.voice-picker-row .row-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; font-size: 0.88rem; font-weight: 600; }
.voice-picker-row select { width: 100%; }
.voice-picker-row .voice-warn { font-size: 0.78rem; color: var(--text-dim); }
.test-voice-btn {
  border: none;
  background: var(--surface-2);
  border-radius: 50%;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
}

/* ---------- Study screen ---------- */
.study-header {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text);
  margin-bottom: 14px;
}
.deck-title-wrap { flex: 1; min-width: 0; }
.deck-title { font-weight: 700; font-size: 1.02rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 6px; }
.progress-track { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--accent-2); transition: width 0.3s ease; border-radius: 999px; }

.direction-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.direction-bar select {
  flex: 1;
  min-width: 0;
  border: none;
  border-radius: 999px;
  padding: 9px 12px;
  font-size: 0.86rem;
  font-weight: 600;
  background: var(--surface);
  color: var(--text);
}
.direction-bar .icon-btn { background: var(--surface); }

.stats-row {
  display: flex;
  justify-content: center;
  gap: 16px;
  color: var(--text-dim);
  font-size: 0.86rem;
  margin-bottom: 14px;
}
.stat { display: flex; align-items: center; gap: 4px; }

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

.flash-card {
  position: absolute;
  width: 100%;
  max-width: 380px;
  height: 320px;
  border-radius: var(--radius-lg);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.32s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.32s ease;
}
.flash-card.dragging { transition: none; }
.flash-card.no-anim { transition: none !important; }
.flash-card.fly-right { transform: translate(160%, -8%) rotate(22deg); opacity: 0; }
.flash-card.fly-left { transform: translate(-160%, -8%) rotate(-22deg); opacity: 0; }
.flash-card.fly-up { transform: translate(0%, -170%) scale(0.92); opacity: 0; }

.card-behind {
  transform: scale(0.94) translateY(10px);
  opacity: 0.55;
  z-index: 1;
  pointer-events: none;
}
.card-behind .card-face {
  width: 100%; height: 100%;
  border-radius: var(--radius-lg);
  background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
}
.card-behind .card-text { font-size: 1.3rem; font-weight: 600; color: var(--text-dim); padding: 0 20px; text-align: center; }

#card-current { z-index: 2; cursor: grab; }
#card-current:active { cursor: grabbing; }

.flash-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0.2, 0.2, 1);
}
.flash-card-inner.is-flipped { transform: rotateY(180deg); }
.flash-card-inner.no-anim { transition: none !important; }

.flash-card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 28px 22px;
  text-align: center;
  gap: 14px;
  overflow-y: auto;
  touch-action: pan-y;
}
.flash-card-face.face-overflowing { justify-content: flex-start; }
.flash-card-back { transform: rotateY(180deg); background: var(--surface-2); }

.card-text { font-size: 1.7rem; font-weight: 700; line-height: 1.25; word-break: break-word; white-space: pre-line; }
.card-note { font-size: 0.9rem; color: var(--text-dim); font-style: italic; }

.edit-card-btn {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 4;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--surface-2);
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.75;
}
.edit-card-btn:hover { opacity: 1; }

.swipe-overlay {
  position: absolute;
  top: 24px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 3px solid currentColor;
  font-weight: 800;
  font-size: 1.1rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0;
  pointer-events: none;
  z-index: 3;
}
.swipe-overlay-know { right: 18px; color: var(--success); transform: rotate(8deg); }
.swipe-overlay-learning { left: 18px; color: var(--danger); transform: rotate(-8deg); }
.swipe-overlay-mastered { left: 50%; color: var(--mastered); transform: translateX(-50%); }

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-bottom: 12px;
}
.round-btn {
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  font-size: 1.3rem;
}
.round-btn-ghost { width: 46px; height: 46px; background: var(--surface); color: var(--text); }
.round-btn-accent { width: 62px; height: 62px; background: var(--accent); color: #fff; font-size: 1.6rem; }
.round-btn:active { transform: scale(0.94); }
.round-btn:disabled { opacity: 0.5; cursor: default; }

.autoplay-speed {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-dim);
  font-size: 0.82rem;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.speed-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.78rem;
}
.speed-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 700; }

.complete-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 40, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 120;
}
.complete-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 30px 26px;
  text-align: center;
  max-width: 360px;
  box-shadow: var(--shadow);
}
.complete-emoji { font-size: 3rem; margin-bottom: 6px; }
.complete-card h2 { margin: 0 0 8px; }
.complete-card p { color: var(--text-dim); margin: 0 0 18px; }
.complete-card .modal-actions { justify-content: center; }

@media (max-width: 380px) {
  .card-text { font-size: 1.4rem; }
  .round-btn-danger, .round-btn-success { width: 52px; height: 52px; }
}
