* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
/* Tillåt markering/redigering i inmatningsfält */
input, textarea {
  -webkit-user-select: text;
  user-select: text;
}

:root {
  --bg: #0d1426;
  --surface: #16203a;
  --surface-2: #1f2c4d;
  --text: #f2f4f8;
  --muted: #9aa3b2;
  --accent: #5b8cff;
  --good: #5bbf72;
  --fail: #e05a4f;
  --easy: #f4c542;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  /* min-height (inte height) så body alltid fyller skärmen på iOS standalone-PWA –
     height:100dvh kan där resolvas till mindre än skärmen och lämna tomrum nedtill */
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

#status-banner {
  position: fixed;
  top: env(safe-area-inset-top);
  left: 0; right: 0;
  text-align: center;
  font-size: 0.8rem;
  padding: 6px;
  background: var(--surface-2);
  color: var(--muted);
  z-index: 50;
}

/* ---- Skärmar ---- */
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 16px 10px;
}
.app-header h1 { font-size: 1.3rem; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 9px; }
.app-logo { width: 30px; height: 30px; border-radius: 8px; flex: none; }

.icon-btn {
  background: var(--surface);
  color: var(--text);
  border: none;
  width: 38px; height: 38px;
  border-radius: 10px;
  font-size: 1.1rem;
  cursor: pointer;
  flex: none;
}
.icon-btn:active { background: var(--surface-2); }

.back-btn {
  background: var(--surface);
  color: var(--text);
  border: none;
  width: 38px; height: 38px;
  border-radius: 10px;
  font-size: 1.2rem;
  cursor: pointer;
}

/* ---- Listor ---- */
/* flex:1 + min-height:0 → listan scrollar internt i den nu fixhöjda (100dvh) skärmen
   istället för att hela sidan växer/scrollar */
.list { padding: 8px 16px 24px; overflow-y: auto; flex: 1; min-height: 0; }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s ease;
  touch-action: manipulation;
}
.row:active { background: var(--surface-2); }
.row.dragging {
  transition: none;
  position: relative;
  z-index: 5;
  background: var(--surface-2);
  box-shadow: 0 10px 28px rgba(0,0,0,0.55);
  opacity: 0.97;
}
.row-title { font-size: 1.05rem; font-weight: 600; }
.row-meta { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.due-tag {
  background: var(--easy);
  color: #0d1426;
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 0.72rem;
  font-weight: 600;
  margin-right: 8px;
}
#due-btn { background: var(--easy); color: #0d1426; }
.empty { color: var(--muted); padding: 24px 16px; text-align: center; }
.version-tag { margin-top: auto; text-align: center; color: var(--muted); font-size: 0.7rem; opacity: 0.55; padding: 10px 0 16px; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 16px 12px;
  padding: 10px 14px;
  background: var(--surface);
  border-radius: 12px;
}
.settings-row label { color: var(--muted); font-size: 0.9rem; }
.settings-row select {
  background: var(--surface-2);
  color: var(--text);
  border: none;
  border-radius: 9px;
  padding: 8px 12px;
  font-size: 0.9rem;
}

.primary-btn {
  margin: 4px 16px 12px;
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn.alt { background: var(--surface-2); color: var(--text); }
.primary-btn.green { background: var(--good); }

.editor-actions { display: flex; gap: 8px; margin: 4px 16px 12px; }
.editor-actions .primary-btn { margin: 0; flex: 1; font-size: 0.92rem; padding: 13px 8px; }

/* segmented control + uppslagsrad i översättningsmodalen */
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
}
.seg button.seg-on { background: var(--accent); color: #fff; }
.t-row { display: flex; gap: 8px; }
.t-row input { flex: 1; }
.t-lookup { flex: none; width: 54px; padding: 0; font-size: 1.2rem; }

/* ---- Träning ---- */
#train-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 6px;
}
#dir-select {
  flex: 1;
  background: var(--surface);
  color: var(--text);
  border: none;
  border-radius: 10px;
  padding: 10px;
  font-size: 0.9rem;
}
#progress-pill {
  background: var(--surface);
  color: var(--muted);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.card-area {
  flex: 1;
  position: relative; /* containing block för kortet + gör att det målas ovanför knapparna (static) */
  margin-bottom: 16px;
  min-height: 0;
  /* ingen overflow:hidden – kortet ska få glida ut nedanför (hopplöst) ovanpå knapparna.
     Kortet är absolut inpassat (inset) så inget överflöd sker i normalläge ändå. */
}
/* Kortet positioneras absolut och fyller card-area minus insets. Absoluta mått
   resolvas mot card-areas (definite) layout-höjd → kringgår WebKit-buggen där
   aspect-ratio+width åsidosätter max-height och får kortet att klippas. */
.card-stack { position: absolute; inset: 12px 22px; margin: auto; max-width: 372px; }

/* Kort i högen bakom det aktiva – antal styrs av klass på .card-stack */
.card-stack::before, .card-stack::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 20px;
  box-shadow: 0 8px 22px rgba(0,0,0,0.35);
  display: none;
}
.card-stack::before { background: #131c33; transform: rotate(-2.5deg) translateY(4px); z-index: 0; }
.card-stack::after  { background: #1c2a4d; transform: rotate(2deg) translateY(2px); z-index: 1; }
/* 2+ kort kvar under: visa båda. 1 kvar: visa bara det närmaste. Sista: inga. */
.card-stack.stack-2::before, .card-stack.stack-2::after { display: block; }
.card-stack.stack-1::after { display: block; }

#speak-btn {
  position: absolute;
  top: 12px; right: 12px;
  z-index: 10;
  width: 46px; height: 46px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.35);
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  animation: speak-fade 0.2s ease-out;
}
#speak-btn:active { background: var(--accent); }

#edit-card-btn {
  position: absolute;
  top: 12px; left: 12px;
  z-index: 10;
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.35);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
#edit-card-btn:active { background: var(--accent); }
#speak-btn.auto { background: var(--accent); color: #fff; box-shadow: 0 0 0 3px rgba(91,140,255,0.45); }
@keyframes speak-fade { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }

.card {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  perspective: 1200px;
  cursor: grab;
  touch-action: none;
  will-change: transform;
}
.card.snapping { transition: transform 0.25s ease; }

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.45s;
  transform-style: preserve-3d;
}
.card.flipped .card-inner { transform: rotateY(180deg); }

.card-front, .card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 28px;
  border-radius: 20px;
  background: var(--surface);
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  font-size: 1.7rem;
  font-weight: 600;
  line-height: 1.35;
}
.card-back {
  flex-direction: column;
  gap: 18px;
  transform: rotateY(180deg);
  background: var(--surface-2);
  color: var(--text);
}
/* Minnesregel – liten dämpad text under svaret. 💡 prefix endast om regel finns. */
.card-hint {
  font-size: 0.95rem;
  font-weight: 400;
  line-height: 1.4;
  color: var(--muted);
  max-width: 90%;
}
.card-hint:not(:empty)::before { content: "💡 "; }
.word-hint-flag { font-size: 0.8rem; opacity: 0.7; }

/* fly-out / emerge animationer */
.card.fly-right { transition: transform 0.22s ease-in; transform: translateX(140%) rotate(18deg); }
.card.fly-left  { transition: transform 0.22s ease-in; transform: translateX(-140%) rotate(-18deg); }
.card.fly-up    { transition: transform 0.22s ease-in; transform: translateY(-140%) scale(0.9); }
.card.fly-down  { transition: transform 0.22s ease-in; transform: translateY(140%) rotate(-6deg); }
.card.emerge    { animation: emerge 0.26s ease-out; }
@keyframes emerge {
  from { opacity: 0; transform: scale(0.92) translateY(12px); }
  to   { opacity: 1; transform: none; }
}

/* fly-in (shake-to-undo): kortet glider tillbaka in från samma håll det lämnade.
   0,2s delay + långsam (0,7s) inglidning så man hinner se det efter skaket.
   fill:both håller kortet utanför skärmen under delayen (ingen hoppighet). */
.card.in-right { animation: in-right 0.7s ease-out 0.7s both; }
.card.in-left  { animation: in-left 0.7s ease-out 0.7s both; }
.card.in-up    { animation: in-up 0.7s ease-out 0.7s both; }
.card.in-down  { animation: in-down 0.7s ease-out 0.7s both; }
@keyframes in-right { from { transform: translateX(140%) rotate(18deg); }  to { transform: none; } }
@keyframes in-left  { from { transform: translateX(-140%) rotate(-18deg); } to { transform: none; } }
@keyframes in-up    { from { transform: translateY(-140%) scale(0.9); }     to { transform: none; } }
@keyframes in-down  { from { transform: translateY(140%) rotate(-6deg); }   to { transform: none; } }

#swipe-feedback {
  position: absolute;       /* centreras i card-area = över kortet (ej viewporten) */
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 6rem;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  z-index: 40;
}
#swipe-feedback.show { animation: pop 0.5s ease-out; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  30% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}
/* Tydligare, längre overlay för ångra-pilen */
/* ↩️ poppar upp direkt, ligger kvar genom hela pausen (0,7s) och tonar bort
   medan kortet glider in. Total 1,4s = 0,7s paus + 0,7s inglidning. */
#swipe-feedback.undo-show { animation: undo-pop 1.4s ease-out; }
@keyframes undo-pop {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.4) rotate(-35deg); }
  12%  { opacity: 1; transform: translate(-50%, -50%) scale(1.35) rotate(0deg); }
  50%  { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.0); }
}

.train-footer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 14px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 0.8rem;
  opacity: 0.4;
}
.train-footer .hint-row { display: flex; justify-content: space-around; }
.train-footer .hint-down { justify-content: center; }

/* Toggle: automatisk uppläsning */
.autospeak-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  color: var(--text);
  font-size: 0.92rem;
}
.switch { position: relative; display: inline-block; width: 46px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--surface-2); border-radius: 28px; transition: 0.2s; }
.switch .slider::before {
  content: ""; position: absolute; height: 22px; width: 22px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider { background: var(--good); }
.switch input:checked + .slider::before { transform: translateX(18px); }

/* ---- Handsfree ---- */
.hf-btn {
  margin: 2px 16px 10px;
  padding: 11px 13px;
  border: none;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  width: calc(100% - 32px);
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.hf-status {
  font-size: 0.8rem;
  font-weight: 400;
  color: rgba(255,255,255,0.6);
}
.hf-btn.active {
  background: var(--accent);
  color: #fff;
  animation: hf-pulse 1.8s ease-in-out infinite;
}
@keyframes hf-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(91,140,255,0.5); }
  50%       { box-shadow: 0 0 0 10px rgba(91,140,255,0); }
}

/* ---- Klar-skärm ---- */
#congrats-screen {
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 14px;
  padding: 24px;
  position: relative;
  z-index: 300; /* ovanför konfettin (z-index 200) så knapparna alltid är klickbara */
}
.congrats-emoji { font-size: 4rem; }
#congrats-screen h2 { font-size: 1.6rem; }
#congrats-sub { color: var(--muted); }
#congrats-screen .primary-btn { margin-top: 12px; min-width: 160px; }
.congrats-emoji { animation: congrats-pop 0.5s ease-out; }
@keyframes congrats-pop {
  0% { transform: scale(0.3); opacity: 0; }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); opacity: 1; }
}

/* ---- Konfetti ---- */
.confetti-root {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 200;
}
.confetti-root i {
  position: absolute;
  top: -24px;
  border-radius: 2px;
  animation-name: confetti-fall;
  animation-timing-function: ease-in;
  animation-fill-mode: forwards;
}
@keyframes confetti-fall {
  0%   { opacity: 1; transform: translateY(-24px) translateX(0) rotate(0deg); }
  100% { opacity: 1; transform: translateY(105vh) translateX(var(--drift)) rotate(var(--spin)); }
}

.editor-toolbar { display: flex; align-items: center; gap: 8px; margin: 0 16px 10px; }
.search-wrap { position: relative; flex: 1; min-width: 0; }
.editor-search {
  width: 100%;
  box-sizing: border-box;
  background: var(--surface-2);
  border: 1px solid #2c3c63;
  border-radius: 10px;
  color: var(--text);
  padding: 11px 36px 11px 14px;
  font-size: 0.95rem;
  font-family: inherit;
}
.editor-search::placeholder { color: var(--muted); }
.search-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  padding: 6px 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.search-clear:active { color: var(--text); }

/* ---- Editor: ordrader ---- */
.word-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
}
.word-texts { flex: 1; min-width: 0; cursor: pointer; }
.word-front { font-weight: 600; }
.word-back { color: var(--muted); font-size: 0.9rem; margin-top: 2px; }
.word-del {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  flex: none;
  padding: 6px;
}
.word-del:active { color: var(--fail); }

/* edit-knapp på lektions-/ämnesrader */
.row-edit {
  background: var(--surface-2);
  border: none;
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 9px;
  font-size: 0.95rem;
  cursor: pointer;
  flex: none;
}
.row-edit:active { background: var(--accent); }

/* ---- Modal ---- */
#modal-root {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: calc(env(safe-area-inset-top) + 8px);
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
}
.modal {
  position: relative;
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border-radius: 0 0 20px 20px;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 85dvh;
  overflow-y: auto;
  animation: slideup 0.22s ease-out;
}
@media (min-width: 520px) {
  #modal-root { align-items: center; }
  .modal { border-radius: 20px; }
}
@keyframes slideup { from { transform: translateY(-30px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal h3 { font-size: 1.1rem; }
.modal label { font-size: 0.82rem; color: var(--muted); margin-bottom: -6px; }
.modal input, .modal textarea {
  background: var(--surface-2);
  border: 1px solid #2c3c63;
  border-radius: 10px;
  color: var(--text);
  padding: 12px;
  font-size: 1rem;
  font-family: inherit;
  width: 100%;
}
.modal textarea { min-height: 140px; resize: vertical; line-height: 1.4; }
.modal-hint { font-size: 0.8rem; color: var(--muted); }
.modal-actions { display: flex; gap: 10px; margin-top: 4px; }
.modal-actions button {
  flex: 1;
  padding: 13px;
  border: none;
  border-radius: 11px;
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--fail); color: #fff; }
.sheet-btn {
  width: 100%;
  text-align: left;
  background: var(--surface-2);
  color: var(--text);
  border: none;
  border-radius: 11px;
  padding: 15px;
  font-size: 1rem;
  cursor: pointer;
}
.sheet-btn.danger { color: var(--fail); }
.full-btn {
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: 11px;
  font-size: 0.98rem;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
.modal input[type="file"] { color: var(--muted); font-size: 0.85rem; }
.full-btn.danger { background: transparent; color: var(--fail); }
