:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8a8f);
  --surface: var(--tg-theme-secondary-bg-color, #f1f1f5);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --good: #2fb457;
  --bad: #e5484d;
  --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17181c);
    --text: var(--tg-theme-text-color, #f2f2f4);
    --hint: var(--tg-theme-hint-color, #8f9099);
    --surface: var(--tg-theme-secondary-bg-color, #24262c);
    --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

[hidden] { display: none !important; }

#app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
}

.view {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.page-title { margin: 4px 0 14px; font-size: 24px; }

.tiles {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.tile {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.tile:active { transform: scale(0.98); }
.tile-icon { flex: none; width: 40px; font-size: 28px; text-align: center; }
.tile-body { flex: 1; min-width: 0; }
.tile-title { font-size: 16px; font-weight: 700; }
.tile-sub { margin-top: 2px; font-size: 13px; color: var(--hint); }
.tile-tag { flex: none; padding: 2px 8px; border-radius: 8px; font-size: 11px; color: var(--hint); border: 1px solid var(--hint); }
.tile.primary { background: var(--accent); color: var(--accent-text); }
.tile.primary .tile-sub { color: var(--accent-text); opacity: 0.85; }

.home-error { padding: 24px 8px; text-align: center; color: var(--hint); line-height: 1.4; }

.topbar { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }

.back {
  flex: none;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.topbar-text { min-width: 0; }
.game-title { font-size: 17px; font-weight: 700; }
.game-hint { font-size: 12px; color: var(--hint); }

.search {
  width: 100%;
  margin-bottom: 8px;
  padding: 11px 14px;
  border: 0;
  border-radius: 12px;
  outline: none;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
}

.chips { display: flex; flex: none; gap: 6px; padding-bottom: 6px; overflow-x: auto; }

.chip {
  flex: none;
  padding: 7px 12px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--hint);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.chip.active { background: var(--accent); color: var(--accent-text); }
.list-meta { margin: 4px 0; font-size: 12px; color: var(--hint); }
.word-list { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.group-head {
  position: sticky;
  top: 0;
  padding: 10px 2px 4px;
  background: var(--bg);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--hint);
}

.word-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 4px;
  border: 0;
  border-bottom: 1px solid var(--surface);
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.word-row.off { opacity: 0.45; }
.w-text { flex: 1; min-width: 0; }
.w-es { font-weight: 600; overflow-wrap: anywhere; }
.w-ua { font-size: 13px; color: var(--hint); overflow-wrap: anywhere; }
.dots { display: flex; flex: none; align-items: center; gap: 4px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #b8b8c0; }
.dot.learning { background: #f0a030; }
.dot.archive { background: var(--good); }
.mark { color: var(--bad); font-weight: 800; }

.sheet { position: fixed; inset: 0; z-index: 10; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }

.sheet-panel {
  position: relative;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0;
  background: var(--bg);
}

.sheet-title { font-size: 20px; font-weight: 700; overflow-wrap: anywhere; }
.sheet-sub { margin-top: 2px; color: var(--hint); overflow-wrap: anywhere; }
.sheet-states { margin: 10px 0 14px; font-size: 13px; color: var(--hint); line-height: 1.5; }

.sheet-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 13px;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.sheet-btn.danger { color: var(--bad); }
.sheet-btn.plain { background: transparent; color: var(--hint); }
.sheet-btn.armed { background: var(--bad); color: #fff; }

header { flex: none; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border-radius: 12px;
}

.tab {
  flex: 1;
  border: 0;
  padding: 9px 4px;
  border-radius: 9px;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.tab.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.progress {
  height: 4px;
  margin-top: 12px;
  background: var(--surface);
  border-radius: 2px;
  overflow: hidden;
}

#progressBar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s ease;
}

.meta {
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  font-size: 12px;
  color: var(--hint);
}

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#deck {
  position: relative;
  width: 100%;
  height: min(100%, 420px);
  perspective: 1200px;
}

.card, .peek {
  position: absolute;
  inset: 0;
}

.peek {
  background: var(--surface);
  border-radius: 24px;
  transform: scale(0.94) translateY(14px);
  opacity: 0.6;
}

.card {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  will-change: transform;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.3, 0.8, 0.3, 1);
}

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

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--card-shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.face.back { transform: rotateY(180deg); }

.face .caption {
  position: absolute;
  top: 18px;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--hint);
}

.face .word {
  font-size: clamp(26px, 8vw, 40px);
  font-weight: 700;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.new-badge {
  position: absolute;
  top: 16px;
  right: 18px;
  padding: 2px 9px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.face .tap-hint {
  position: absolute;
  bottom: 18px;
  font-size: 13px;
  color: var(--hint);
}

.stamp {
  position: absolute;
  top: 22px;
  padding: 4px 12px;
  border: 3px solid;
  border-radius: 10px;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 0.05em;
  opacity: 0;
  pointer-events: none;
  z-index: 2;
}

.stamp.right { left: 20px; color: var(--good); border-color: var(--good); transform: rotate(-12deg); }
.stamp.wrong { right: 20px; color: var(--bad); border-color: var(--bad); transform: rotate(12deg); }

footer {
  flex: none;
  display: flex;
  gap: 12px;
  padding-top: 12px;
}

.action {
  flex: 1;
  border: 0;
  padding: 15px 8px;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
}

.action:active:not(:disabled) { transform: scale(0.97); }
.action:disabled { opacity: 0.3; cursor: default; }
.action.wrong { background: var(--bad); }
.action.right { background: var(--good); }

.message {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}

.message[hidden] { display: none; }

.message .big { font-size: 56px; margin-bottom: 8px; }
.message h2 { margin: 0 0 8px; font-size: 22px; }
.message p { margin: 0 0 16px; color: var(--hint); line-height: 1.4; }

.message .missed {
  max-height: 32vh;
  overflow-y: auto;
  width: 100%;
  margin: 0 0 16px;
  padding: 10px 14px;
  list-style: none;
  text-align: left;
  font-size: 14px;
  line-height: 1.5;
  background: var(--surface);
  border-radius: 12px;
}

.message button {
  border: 0;
  margin-top: 8px;
  padding: 13px 22px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.message button.secondary { background: var(--surface); color: var(--text); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  max-width: 90%;
  padding: 10px 16px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  font-size: 14px;
  text-align: center;
}


/* ---------- Словник ---------- */

.seg { display: flex; flex: none; gap: 4px; margin-bottom: 10px; padding: 4px; border-radius: 12px; background: var(--surface); }

.seg-btn {
  flex: 1;
  padding: 9px 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.seg-btn.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15); }

.topic-row {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.topic-row.all { background: var(--accent); color: var(--accent-text); }
.topic-name { font-size: 16px; font-weight: 700; }
.topic-sub { margin-top: 2px; font-size: 13px; color: var(--hint); }
.topic-row.all .topic-sub { color: var(--accent-text); opacity: 0.85; }
.topic-bar { height: 4px; margin-top: 8px; border-radius: 2px; background: rgba(127, 127, 127, 0.25); overflow: hidden; }
.topic-bar > div { height: 100%; background: var(--good); }
.source-note { margin: 6px 2px 0; font-size: 11px; color: var(--hint); }

/* ---------- Числа ---------- */

.num-body { flex: 1; display: flex; flex-direction: column; gap: 10px; min-height: 0; overflow-y: auto; }
.num-intro { margin: 0 2px 4px; font-size: 13px; line-height: 1.45; color: var(--hint); }
.num-tip { padding: 14px; border-radius: 14px; background: var(--surface); line-height: 1.5; font-size: 15px; }
.ex-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; padding: 4px 4px 8px; }
.ex-digits { font-weight: 700; text-align: right; color: var(--hint); }
.ex-words { overflow-wrap: anywhere; }

.q-progress { height: 4px; border-radius: 2px; background: var(--surface); overflow: hidden; flex: none; }
.q-progress > div { height: 100%; width: 0; background: var(--accent); transition: width 0.3s ease; }
.q-count { flex: none; font-size: 12px; color: var(--hint); }
.q-card { flex: none; padding: 22px 14px; border-radius: 20px; background: var(--surface); text-align: center; }
.q-label { font-size: 12px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--hint); }
.q-prompt { margin-top: 8px; font-size: clamp(26px, 8vw, 38px); font-weight: 700; line-height: 1.2; overflow-wrap: anywhere; }

.opts { display: grid; grid-template-columns: 1fr; gap: 8px; flex: none; }

.opt {
  padding: 14px 12px;
  border: 2px solid transparent;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  overflow-wrap: anywhere;
  cursor: pointer;
}

.opt:disabled { cursor: default; }
.opt.good { border-color: var(--good); background: rgba(47, 180, 87, 0.18); }
.opt.bad { border-color: var(--bad); background: rgba(229, 72, 77, 0.18); }

.type-input {
  width: 100%;
  padding: 14px;
  border: 2px solid var(--surface);
  border-radius: 14px;
  outline: none;
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  text-align: center;
}

.type-input:focus { border-color: var(--accent); }
.type-input.good { border-color: var(--good); }
.type-input.bad { border-color: var(--bad); }

.feedback { flex: none; padding: 12px 14px; border-radius: 12px; font-size: 15px; line-height: 1.45; }
.feedback.good { background: rgba(47, 180, 87, 0.18); }
.feedback.bad { background: rgba(229, 72, 77, 0.16); }
.feedback .note { display: block; margin-top: 2px; font-size: 13px; color: var(--hint); }

.btn {
  flex: none;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.btn.secondary { background: var(--surface); color: var(--text); }
.btn:disabled { opacity: 0.4; cursor: default; }
.result-big { flex: none; padding: 10px 0 0; text-align: center; font-size: 48px; }
.result-title { flex: none; margin: 0; text-align: center; font-size: 22px; }
.result-sub { flex: none; margin: 0; text-align: center; color: var(--hint); line-height: 1.4; }
.result-list { flex: none; max-height: 30vh; overflow-y: auto; margin: 0; padding: 10px 14px; list-style: none; border-radius: 12px; background: var(--surface); font-size: 14px; line-height: 1.6; }


/* ---------- Граматика ---------- */

.level-tag { display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 7px; background: var(--accent); color: var(--accent-text); font-size: 11px; font-weight: 700; vertical-align: middle; }
.g-title { margin: 0; font-size: 20px; }
.g-rules { margin: 0; padding: 12px 14px 12px 30px; border-radius: 14px; background: var(--surface); line-height: 1.5; font-size: 14px; }
.g-rules li { margin-bottom: 6px; }
.g-rules li:last-child { margin-bottom: 0; }
.g-ex { display: flex; flex-direction: column; gap: 8px; padding: 2px 2px 8px; }
.g-ex-row { padding-bottom: 8px; border-bottom: 1px solid var(--surface); }
.g-ex-es { font-weight: 600; overflow-wrap: anywhere; }
.g-ex-ua { font-size: 13px; color: var(--hint); overflow-wrap: anywhere; }
.seg-btn { font-size: 13px; }
