:root {
  color-scheme: light;
  --tg-bg: #f7f8f3;
  --tg-card: #ffffff;
  --tg-text: #17211d;
  --tg-muted: #6b746e;
  --tg-link: #2467a8;
  --tg-button: #126c57;
  --tg-button-text: #ffffff;

  --bg: var(--tg-bg);
  --panel: var(--tg-card);
  --panel-soft: #eef4f0;
  --ink: var(--tg-text);
  --muted: var(--tg-muted);
  --line: rgba(28, 42, 36, 0.12);
  --accent: var(--tg-button);
  --accent-strong: #0b5142;
  --blue: #315f9f;
  --gold: #a86d12;
  --red: #b33f4a;
  --rose: #95506d;
  --violet: #6556a8;
  --success: #1d7d4f;
  --shadow: 0 16px 38px rgba(18, 28, 24, 0.11);
  --soft-shadow: 0 8px 22px rgba(18, 28, 24, 0.08);
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 20% -10%, rgba(18, 108, 87, 0.13), transparent 260px),
    linear-gradient(180deg, rgba(49, 95, 159, 0.06), transparent 310px),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
}

.screen {
  min-height: 100vh;
  animation: screen-in 220ms ease both;
}

.screen-centered {
  display: grid;
  place-items: center;
}

.shell {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: calc(14px + var(--safe-top)) 14px calc(28px + var(--safe-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 58px;
  padding: 6px 0 12px;
  background: linear-gradient(180deg, var(--bg) 70%, rgba(247, 248, 243, 0));
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--ink);
  background: transparent;
  text-align: left;
  padding: 0;
}

.brand strong {
  display: block;
  font-size: 18px;
  line-height: 1.1;
  color: var(--accent-strong);
}

.brand small {
  display: block;
  max-width: 58vw;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-mark {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--accent), var(--blue));
  color: #fff;
  font-weight: 800;
  box-shadow: var(--soft-shadow);
}

.app-mark.small {
  width: 38px;
  height: 38px;
  font-size: 13px;
}

.language-field {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 11px;
}

.language-select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  padding: 0 10px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset;
}

.language-screen,
.onboarding-screen {
  width: min(100%, 460px);
  padding: 28px 18px;
}

.language-panel,
.onboarding-panel {
  background: color-mix(in srgb, var(--panel) 96%, var(--accent) 4%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
}

.language-panel h1,
.onboarding-panel h1 {
  margin: 16px 0 6px;
  font-size: 27px;
  line-height: 1.1;
  letter-spacing: 0;
}

.language-panel p,
.onboarding-subtitle {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.45;
}

.language-grid {
  display: grid;
  gap: 10px;
}

.language-button,
.primary-button,
.secondary-button {
  min-height: 44px;
  border-radius: var(--radius);
  transition:
    transform 160ms ease,
    background 160ms ease,
    border-color 160ms ease,
    box-shadow 160ms ease,
    opacity 160ms ease;
}

.language-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 12px 14px;
  text-align: left;
  box-shadow: var(--soft-shadow);
}

.language-button span {
  font-weight: 800;
}

.language-button small {
  color: var(--muted);
}

.onboarding-grid {
  display: grid;
  gap: 9px;
  margin-top: 18px;
}

.onboarding-item {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 12px;
}

.onboarding-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 5px;
}

.onboarding-item strong {
  display: block;
  color: var(--ink);
  line-height: 1.35;
}

.onboarding-actions {
  display: grid;
  gap: 9px;
  margin-top: 16px;
}

.primary-button {
  background: var(--accent);
  color: var(--tg-button-text);
  padding: 0 16px;
  box-shadow: 0 10px 22px rgba(18, 108, 87, 0.2);
}

.primary-button:disabled {
  background: #cbd8d2;
  color: rgba(23, 33, 29, 0.55);
  opacity: 1;
  box-shadow: none;
}

.secondary-button {
  border: 1px solid rgba(18, 108, 87, 0.14);
  background: var(--panel-soft);
  color: var(--accent-strong);
  padding: 0 14px;
}

.danger-button {
  background: var(--red);
  box-shadow: 0 10px 22px rgba(179, 63, 74, 0.2);
}

.tap-target:active {
  transform: scale(0.985);
}

.elevated {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.home-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.16fr) minmax(270px, 0.84fr);
  gap: 12px;
  margin-bottom: 12px;
}

.stats-panel,
.exam-panel,
.question-card,
.empty-state {
  padding: 16px;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.panel-header h2,
.exam-panel h2,
.toolbar h2 {
  margin: 0;
  color: var(--ink);
  font-size: 19px;
  line-height: 1.15;
  letter-spacing: 0;
}

.panel-header p:not(.eyebrow),
.stats-note {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.score-ring {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: conic-gradient(var(--success) var(--score), #e6ece7 0);
  transition: background 360ms ease;
}

.score-ring span {
  display: grid;
  place-items: center;
  width: 59px;
  height: 59px;
  border-radius: 50%;
  background: var(--panel);
  color: var(--accent-strong);
  font-weight: 800;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 9px;
}

.stat-cell {
  min-width: 0;
  border-radius: var(--radius);
  background: var(--panel-soft);
  padding: 12px;
}

.stat-cell span {
  display: block;
  color: var(--accent-strong);
  font-size: 23px;
  line-height: 1;
  font-weight: 850;
}

.stat-cell small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
}

.exam-panel {
  background: color-mix(in srgb, var(--panel) 92%, #f4dfb1 8%);
}

.exam-rule {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
}

.exam-rule:last-child {
  border-bottom: 0;
}

.exam-rule strong {
  color: var(--ink);
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.smart-panel {
  padding: 16px;
  margin-bottom: 12px;
  background: color-mix(in srgb, var(--panel) 94%, var(--accent) 6%);
}

.smart-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.smart-header h2 {
  margin: 0;
  color: var(--ink);
  font-size: 18px;
  line-height: 1.25;
}

.smart-badge {
  flex: 0 0 auto;
  border-radius: 999px;
  background: rgba(18, 108, 87, 0.1);
  color: var(--accent-strong);
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 800;
}

.smart-badge.danger {
  background: rgba(179, 63, 74, 0.12);
  color: var(--red);
}

.smart-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.smart-card {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 13px;
  box-shadow: var(--soft-shadow);
}

.smart-card span,
.smart-card small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.smart-card strong {
  display: block;
  margin-top: 5px;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.25;
}

.smart-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.smart-actions .primary-button,
.smart-actions .secondary-button {
  min-height: 38px;
}

.muted-card {
  background: color-mix(in srgb, var(--panel) 88%, var(--panel-soft) 12%);
}

.analytics-panel {
  padding: 16px;
  margin-bottom: 12px;
}

.analytics-header {
  margin-bottom: 12px;
}

.analytics-header h2 {
  margin: 0;
  color: var(--ink);
  font-size: 19px;
  line-height: 1.2;
}

.analytics-header p:not(.eyebrow) {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.analytics-list {
  display: grid;
  gap: 8px;
}

.analytics-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(118px, 0.34fr);
  align-items: center;
  gap: 12px;
  width: 100%;
  border: 1px solid var(--line);
  border-left-width: 4px;
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  padding: 11px 12px;
  text-align: left;
  box-shadow: var(--soft-shadow);
}

.analytics-row.green { border-left-color: var(--success); }
.analytics-row.yellow { border-left-color: var(--gold); }
.analytics-row.red { border-left-color: var(--red); }
.analytics-row.neutral { border-left-color: #aab4ae; }

.analytics-main,
.analytics-score {
  min-width: 0;
}

.analytics-main strong,
.analytics-main small,
.analytics-score span {
  display: block;
}

.analytics-main strong {
  font-size: 15px;
  line-height: 1.25;
}

.analytics-main small,
.analytics-score span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.analytics-score span {
  text-align: right;
  font-weight: 800;
}

.analytics-track {
  display: block;
  height: 7px;
  margin-top: 7px;
  border-radius: 999px;
  background: #e5ebe5;
  overflow: hidden;
}

.analytics-track i {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--red), var(--gold), var(--success));
  transition: width 320ms ease;
}

.mode-button,
.list-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--soft-shadow);
  text-align: left;
}

.mode-button {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 10px;
  min-height: 84px;
  padding: 13px;
}

.mode-icon {
  grid-row: 1 / span 2;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius);
  color: #fff;
  font-weight: 800;
}

.mode-button.green .mode-icon { background: var(--accent); }
.mode-button.blue .mode-icon { background: var(--blue); }
.mode-button.gold .mode-icon { background: var(--gold); }
.mode-button.violet .mode-icon { background: var(--violet); }
.mode-button.red .mode-icon { background: var(--red); }
.mode-button.rose .mode-icon { background: var(--rose); }

.mode-button strong,
.list-title {
  display: block;
  min-width: 0;
  font-weight: 800;
}

.mode-button small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 2px 0 12px;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.fallback-back {
  min-width: 84px;
}

.list-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.list-card {
  display: grid;
  gap: 7px;
  min-height: 78px;
  padding: 14px;
}

.progress-track,
.question-progress {
  display: block;
  width: 100%;
  height: 7px;
  border-radius: 999px;
  background: #e5ebe5;
  overflow: hidden;
}

.progress-bar,
.question-progress span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), var(--blue));
  transition: width 300ms ease;
}

.question-stage {
  transform-origin: 50% 12%;
}

.question-card {
  overflow: hidden;
  will-change: transform, opacity;
}

.question-card.motion-initial {
  animation: question-soft-in 240ms ease both;
}

.question-stage.is-entering .question-card {
  animation: question-soft-in 240ms ease both;
}

.question-stage.motion-answer .question-card,
.question-stage.motion-favorite .question-card {
  animation-name: question-answer-in;
}

.question-stage.motion-next .question-card {
  animation-name: question-next-in;
}

.question-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 10px;
}

.question-progress {
  margin-bottom: 14px;
}

.question-text {
  margin: 0 0 14px;
  color: var(--ink);
  font-size: 20px;
  line-height: 1.36;
  letter-spacing: 0;
}

.question-image {
  display: block;
  width: 100%;
  max-height: 330px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  margin: 0 0 14px;
}

.answer-status {
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.option-grid {
  display: grid;
  gap: 9px;
}

.option-button {
  width: 100%;
  min-height: 50px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  text-align: left;
  padding: 12px;
  line-height: 1.35;
  transition:
    transform 140ms ease,
    border-color 180ms ease,
    background 180ms ease,
    box-shadow 180ms ease;
}

.option-button:not(:disabled):hover,
.mode-button:hover,
.list-card:hover,
.language-button:hover {
  box-shadow: var(--shadow);
}

.option-button.correct {
  border-color: rgba(29, 125, 79, 0.5);
  background: rgba(29, 125, 79, 0.12);
  animation: option-result 240ms ease both;
}

.option-button.wrong {
  border-color: rgba(179, 63, 74, 0.52);
  background: rgba(179, 63, 74, 0.13);
  animation: option-result 240ms ease both;
}

.explanation {
  margin-top: 14px;
  border-left: 4px solid var(--accent);
  background: var(--panel-soft);
  padding: 12px;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--ink);
  line-height: 1.45;
}

.reveal {
  animation: reveal 220ms ease both;
}

.question-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 14px;
}

.favorite-button.active {
  border-color: rgba(149, 80, 109, 0.35);
  background: rgba(149, 80, 109, 0.12);
  color: var(--rose);
  animation: favorite-pop 220ms ease both;
}

.fallback-next {
  min-width: 118px;
}

.telegram-embedded .fallback-back,
.telegram-embedded .fallback-next {
  display: none;
}

.empty-state {
  color: var(--muted);
  text-align: center;
}

.confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: end center;
  padding: 16px 14px calc(16px + var(--safe-bottom));
  background: rgba(16, 24, 21, 0.28);
  backdrop-filter: blur(6px);
  animation: overlay-in 160ms ease both;
}

.confirm-overlay.is-closing {
  animation: overlay-out 160ms ease both;
}

.confirm-dialog {
  width: min(100%, 420px);
  padding: 16px;
  animation: dialog-in 180ms ease both;
}

.confirm-dialog h2 {
  margin: 0;
  color: var(--ink);
  font-size: 20px;
  line-height: 1.2;
}

.confirm-dialog p {
  margin: 8px 0 14px;
  color: var(--muted);
  line-height: 1.45;
}

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

@keyframes screen-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes question-soft-in {
  from {
    opacity: 0.72;
    transform: translateY(6px) scale(0.995);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes question-answer-in {
  from {
    opacity: 0.86;
    transform: translateY(3px) scale(0.997);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes question-next-in {
  from {
    opacity: 0;
    transform: translateX(14px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes option-result {
  from {
    transform: scale(0.992);
  }
  to {
    transform: scale(1);
  }
}

@keyframes favorite-pop {
  0% {
    transform: scale(0.985);
  }
  70% {
    transform: scale(1.018);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes overlay-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes overlay-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 760px) {
  .shell {
    padding-inline: 12px;
  }

  .home-grid,
  .smart-grid,
  .menu-grid,
  .list-grid {
    grid-template-columns: 1fr;
  }

  .analytics-row {
    grid-template-columns: 1fr;
  }

  .analytics-score span {
    text-align: left;
  }

  .stats-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .topbar {
    align-items: flex-start;
  }

  .brand small {
    max-width: 48vw;
  }

  .mode-button {
    min-height: 72px;
  }

  .question-text {
    font-size: 18px;
  }
}

@media (max-width: 430px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .panel-header,
  .toolbar,
  .smart-header,
  .question-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar-actions,
  .smart-actions {
    justify-content: stretch;
  }

  .toolbar-actions button,
  .smart-actions button,
  .confirm-actions button {
    flex: 1 1 auto;
  }

  .confirm-actions {
    grid-template-columns: 1fr;
  }

  .score-ring {
    width: 68px;
    height: 68px;
  }

  .score-ring span {
    width: 53px;
    height: 53px;
  }

  .language-field {
    min-width: 108px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 1ms !important;
  }
}
