:root {
  --tg-bg: var(--tg-theme-bg-color, #ffffff);
  --tg-text: var(--tg-theme-text-color, #222222);
  --tg-hint: var(--tg-theme-hint-color, #8a8f98);
  --tg-link: var(--tg-theme-link-color, #2481cc);
  --tg-button: var(--tg-theme-button-color, #7e57c2);
  --tg-button-text: var(--tg-theme-button-text-color, #ffffff);
  --tg-secondary-bg: var(--tg-theme-secondary-bg-color, #f2f3f7);
  --tg-section-bg: var(--tg-theme-section-bg-color, var(--tg-secondary-bg));
  --tg-header-bg: var(--tg-theme-header-bg-color, var(--tg-bg));
  --tg-accent: var(--tg-theme-accent-text-color, var(--tg-link));
  --tg-destructive: var(--tg-theme-destructive-text-color, #e53935);
  --tg-separator: var(--tg-theme-section-separator-color, rgba(128, 128, 128, 0.15));

  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 20px;
  --gap: 14px;
  --gap-sm: 8px;
  --gap-lg: 20px;
  --shadow-card: 0 6px 24px rgba(0, 0, 0, 0.08);
  --max-width: 720px;
}

* {
  box-sizing: border-box;
}

/* Ensure [hidden] always wins over `display: flex/grid` on .loader, .view,
 * etc. Class selectors would otherwise beat the UA `[hidden] { display:
 * none }` rule. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100%;
}

body {
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.app {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 18px 16px 32px;
}

.view {
  animation: fadeIn 0.2s ease;
}

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

/* ---------- Hero ---------- */
.hero {
  text-align: center;
  padding: 24px 8px 18px;
  background: linear-gradient(
    135deg,
    rgba(126, 87, 194, 0.12),
    rgba(255, 152, 0, 0.08)
  );
  border-radius: var(--radius-lg);
  margin-bottom: 18px;
}
.hero-emoji {
  font-size: 48px;
  margin-bottom: 4px;
  filter: drop-shadow(0 3px 8px rgba(126, 87, 194, 0.25));
}
.hero-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.hero-subtitle {
  margin: 4px 0 0;
  color: var(--tg-hint);
  font-size: 14px;
}

/* ---------- Section headers ---------- */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 2px 12px;
}
.section-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--tg-hint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---------- List filter tabs ---------- */
.list-tabs {
  display: inline-flex;
  align-items: center;
  gap: 0;
  padding: 3px;
  background: var(--tg-secondary-bg);
  border: 1px solid var(--tg-separator);
  border-radius: 999px;
  margin: 0 0 12px;
  flex-wrap: wrap;
}
.list-tab {
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  border: none;
  color: var(--tg-hint);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  padding: 6px 14px;
  min-height: 28px;
  border-radius: 999px;
  cursor: pointer;
  transition:
    background 0.18s ease,
    color 0.18s ease,
    box-shadow 0.18s ease;
}
.list-tab:hover:not(.active):not([aria-selected="true"]) {
  color: var(--tg-text);
}
.list-tab.active,
.list-tab[aria-selected="true"] {
  background: var(--tg-button);
  color: var(--tg-button-text);
  box-shadow: 0 1px 3px rgba(126, 87, 194, 0.35);
}
.list-tab:focus-visible {
  outline: 2px solid var(--tg-button);
  outline-offset: 2px;
}

/* ---------- Lottery list card ---------- */
.list {
  display: grid;
  gap: 12px;
}
.card {
  background: var(--tg-section-bg);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-card);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  cursor: pointer;
  border: 1px solid var(--tg-separator);
}
.card:active {
  transform: scale(0.98);
}
.card-prize {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 6px;
  word-break: break-word;
}
.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  color: var(--tg-hint);
  font-size: 13px;
}
.card-meta span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.card-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: auto;
}
.badge-active {
  background: rgba(76, 175, 80, 0.15);
  color: #2e7d32;
}
.badge-drawn {
  background: rgba(126, 87, 194, 0.15);
  color: var(--tg-button);
}
.badge-cancelled {
  background: rgba(244, 67, 54, 0.12);
  color: var(--tg-destructive);
}

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-weight: 600;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.btn:active {
  transform: scale(0.98);
}
.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.btn-primary {
  background: var(--tg-button);
  color: var(--tg-button-text);
  box-shadow: 0 4px 14px rgba(126, 87, 194, 0.25);
}
.btn-secondary {
  background: var(--tg-secondary-bg);
  color: var(--tg-text);
  border: 1px solid var(--tg-separator);
}
.btn-danger {
  background: rgba(244, 67, 54, 0.1);
  color: var(--tg-destructive);
  border: 1px solid rgba(244, 67, 54, 0.25);
}
.btn-block {
  display: block;
  width: 100%;
}
.btn-ghost {
  background: transparent;
  border: none;
  color: var(--tg-link);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
}
.btn-create {
  margin-top: 4px;
}

.back-btn {
  background: transparent;
  border: none;
  color: var(--tg-link);
  font-size: 15px;
  padding: 6px 0;
  margin-bottom: 6px;
  cursor: pointer;
  font-weight: 500;
}

.view-title {
  margin: 4px 0 16px;
  font-size: 20px;
  font-weight: 700;
}

.edit-immutable {
  margin: -6px 0 14px;
  padding: 10px 14px;
  background: var(--tg-section-bg);
  border: 1px solid var(--tg-separator);
  border-radius: var(--radius-sm);
  color: var(--tg-hint);
  font-size: 13px;
  line-height: 1.5;
}
.edit-immutable strong {
  color: var(--tg-text);
  font-weight: 600;
}

/* ---------- Empty state ---------- */
.empty {
  text-align: center;
  padding: 32px 12px;
  color: var(--tg-hint);
}
.empty.small {
  padding: 14px;
}
.empty-emoji {
  font-size: 40px;
  margin-bottom: 6px;
}

/* ---------- Form ---------- */
.form {
  display: grid;
  gap: 16px;
}
.field {
  display: grid;
  gap: 6px;
}
.field-fieldset {
  border: none;
  padding: 0;
  margin: 0;
}
.label {
  font-size: 13px;
  font-weight: 600;
  color: var(--tg-hint);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.field input[type="text"],
.field input[type="number"],
.field input[type="datetime-local"],
.field textarea {
  background: var(--tg-section-bg);
  color: var(--tg-text);
  border: 1px solid var(--tg-separator);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font: inherit;
  width: 100%;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field textarea {
  resize: vertical;
  min-height: 84px;
  line-height: 1.5;
}
.field input:focus,
.field textarea:focus {
  border-color: var(--tg-button);
  box-shadow: 0 0 0 3px rgba(126, 87, 194, 0.15);
}
.field input[aria-invalid="true"],
.field textarea[aria-invalid="true"] {
  border-color: var(--tg-destructive);
  box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.15);
}
.hint {
  font-size: 12px;
  color: var(--tg-hint);
}
.hint.error {
  color: var(--tg-destructive);
  font-weight: 500;
}

.radio-group {
  display: grid;
  gap: 10px;
}
.radio {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--tg-section-bg);
  border: 1px solid var(--tg-separator);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.radio input[type="radio"] {
  accent-color: var(--tg-button);
  margin: 0;
}
.radio-emoji {
  font-size: 18px;
}
.radio-title {
  flex: 1;
  font-weight: 500;
}
.radio:has(input:checked) {
  border-color: var(--tg-button);
  background: rgba(126, 87, 194, 0.08);
}

/* ---------- Detail ---------- */
.detail-card {
  background: linear-gradient(
    135deg,
    rgba(126, 87, 194, 0.14),
    rgba(255, 152, 0, 0.08)
  );
  border-radius: var(--radius-lg);
  padding: 22px 20px;
  margin-bottom: 18px;
  border: 1px solid var(--tg-separator);
}
.detail-prize {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 10px;
  word-break: break-word;
}
.detail-description {
  margin: 0 0 12px;
  font-size: 14px;
  line-height: 1.55;
  color: var(--tg-text);
  opacity: 0.88;
  white-space: pre-wrap;
  word-break: break-word;
}
.detail-meta {
  display: grid;
  gap: 6px;
  color: var(--tg-text);
  font-size: 14px;
}
.detail-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.detail-meta-row .icon {
  font-size: 16px;
}
.detail-meta-row .value {
  font-weight: 500;
}

.detail-actions {
  display: grid;
  gap: 10px;
  margin-bottom: 22px;
}
@media (min-width: 560px) {
  .detail-actions {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }
}

.participants-section h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--tg-hint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 10px;
}
.participants {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  background: var(--tg-section-bg);
  border: 1px solid var(--tg-separator);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 13px;
  color: var(--tg-text);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
a.chip {
  /* Visually distinct from plain `.chip` spans so users can see at a
     glance that the nickname is tappable. We pick up the Telegram
     theme's link color, slightly brighten the border, and append a
     chevron via ::after so the affordance also reads on devices where
     no hover state ever fires (mobile Telegram clients). */
  color: var(--tg-link, var(--tg-button, inherit));
  text-decoration: none;
  cursor: pointer;
  border-color: var(--tg-link, var(--tg-button));
  transition: background-color 0.15s ease, border-color 0.15s ease,
    transform 0.05s ease;
  -webkit-tap-highlight-color: rgba(0, 122, 255, 0.18);
}
a.chip::after {
  content: " ›";
  font-weight: 700;
  opacity: 0.6;
}
a.chip:hover,
a.chip:focus-visible {
  background: var(--tg-secondary-bg, rgba(0, 122, 255, 0.08));
  border-color: var(--tg-link, var(--tg-button));
  outline: none;
}
a.chip:hover::after,
a.chip:focus-visible::after {
  opacity: 1;
}
a.chip:active {
  transform: scale(0.97);
}
.chip.winner {
  background: rgba(255, 193, 7, 0.18);
  border-color: rgba(255, 193, 7, 0.45);
  color: #b36b00;
  font-weight: 600;
}
a.chip.winner {
  /* Winners keep their gold palette, but we still need to override the
     link-color default so the trophy-yellow style wins. */
  color: #b36b00;
  border-color: rgba(255, 193, 7, 0.7);
}
a.chip.winner:hover,
a.chip.winner:focus-visible {
  background: rgba(255, 193, 7, 0.3);
  border-color: rgba(255, 193, 7, 0.9);
}

/* ---------- Countdown ---------- */
.countdown {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--tg-button);
}

/* ---------- Winners banner ---------- */
.winners-banner {
  background: linear-gradient(135deg, #ffd54f, #ff8a65);
  color: #3a2b00;
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 0 0 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}
.winners-banner .emoji {
  font-size: 22px;
}

/* ---------- Top header + language toggle ---------- */
.app-header {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  min-height: 32px;
  margin-bottom: 8px;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  background: var(--tg-secondary-bg);
  border: 1px solid var(--tg-separator);
  border-radius: 999px;
  padding: 3px;
  gap: 0;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.lang-switch-opt {
  -webkit-appearance: none;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--tg-hint);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  min-width: 36px;
  height: 26px;
  padding: 0 12px;
  line-height: 1;
  border-radius: 999px;
  cursor: pointer;
  transition:
    background 0.2s ease,
    color 0.2s ease,
    box-shadow 0.2s ease;
}

.lang-switch-opt:hover:not([aria-pressed="true"]) {
  color: var(--tg-text);
}

.lang-switch-opt[aria-pressed="true"] {
  background: var(--tg-button);
  color: var(--tg-button-text);
  box-shadow: 0 1px 3px rgba(126, 87, 194, 0.35);
}

.lang-switch-opt:focus-visible {
  outline: 2px solid var(--tg-button);
  outline-offset: 2px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, -140%);
  background: rgba(0, 0, 0, 0.88);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 10;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
  opacity: 0;
  max-width: 88vw;
  text-align: center;
}
.toast.show {
  transform: translate(-50%, 0);
  opacity: 1;
}
.toast.success {
  background: #2e7d32;
}
.toast.error {
  background: #c62828;
}

/* ---------- Loader ---------- */
.loader {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  backdrop-filter: blur(3px);
}
.spinner {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  animation: spin 0.75s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Desktop tweaks ---------- */
@media (min-width: 720px) {
  .app {
    padding: 28px 24px 40px;
  }
  .hero {
    padding: 34px 12px 26px;
  }
  .hero-emoji {
    font-size: 64px;
  }
  .hero-title {
    font-size: 28px;
  }
  .hero-subtitle {
    font-size: 15px;
  }
  .list {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
  .detail-prize {
    font-size: 26px;
  }
}

/* ---------- Dark theme tweaks ---------- */
@media (prefers-color-scheme: dark) {
  .hero {
    background: linear-gradient(
      135deg,
      rgba(126, 87, 194, 0.2),
      rgba(255, 152, 0, 0.12)
    );
  }
  .detail-card {
    background: linear-gradient(
      135deg,
      rgba(126, 87, 194, 0.22),
      rgba(255, 152, 0, 0.14)
    );
  }
  .badge-active {
    background: rgba(76, 175, 80, 0.22);
    color: #a5d6a7;
  }
  .badge-drawn {
    background: rgba(126, 87, 194, 0.28);
    color: #d1c4e9;
  }
  .chip.winner {
    color: #ffe082;
  }
  a.chip.winner {
    color: #ffe082;
  }
}
