:root {
  color-scheme: var(--tg-color-scheme, light);
  --bg: var(--tg-theme-bg-color, #f3f5f7);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #17212b);
  --muted: var(--tg-theme-hint-color, #707b86);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --destructive: var(--tg-theme-destructive-text-color, #d14b4b);
  --border: color-mix(in srgb, var(--muted) 24%, transparent);
  --shadow: 0 10px 30px color-mix(in srgb, #000 8%, transparent);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

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

body {
  min-width: 280px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

[hidden] {
  display: none !important;
}

.shell {
  width: min(100%, 760px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding:
    max(20px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(28px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.app-header,
.section-heading,
.editor-heading,
.filter-card-header,
.filter-actions,
.form-actions {
  display: flex;
  align-items: center;
}

.app-header {
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 28px;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 5px;
  font-size: clamp(1.65rem, 7vw, 2.2rem);
  line-height: 1.08;
  letter-spacing: -0.03em;
}

h2 {
  margin-bottom: 4px;
  font-size: 1.12rem;
}

h3 {
  margin-bottom: 4px;
  font-size: 1rem;
}

.eyebrow {
  margin-bottom: 4px;
  color: var(--link);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.14em;
}

.subtitle,
.muted,
.field-hint {
  color: var(--muted);
}

.subtitle,
.muted {
  margin-bottom: 0;
  font-size: 0.85rem;
  line-height: 1.4;
}

.language-control select {
  min-height: 42px;
  padding: 0 30px 0 12px;
  color: var(--text);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.section-heading {
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.button {
  min-height: 46px;
  padding: 0 18px;
  border: 0;
  border-radius: 13px;
  font-weight: 700;
  transition: transform 120ms ease, filter 120ms ease;
}

.button:active:not(:disabled),
.icon-button:active:not(:disabled),
.preset-option:has(input:checked) {
  transform: scale(0.98);
}

.button-primary {
  color: var(--button-text);
  background: var(--button);
}

.button-primary:hover:not(:disabled) {
  filter: brightness(1.04);
}

.button-secondary {
  color: var(--text);
  background: color-mix(in srgb, var(--muted) 12%, transparent);
}

.button-compact {
  min-height: 42px;
  padding: 0 14px;
  white-space: nowrap;
}

.empty-state,
.fatal-state,
.loading-state {
  text-align: center;
}

.empty-state {
  padding: 38px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
}

.empty-state p,
.fatal-state p {
  max-width: 390px;
  margin: 0 auto;
  color: var(--muted);
  line-height: 1.5;
}

.state-icon {
  display: grid;
  width: 46px;
  height: 46px;
  margin: 0 auto 14px;
  place-items: center;
  color: var(--link);
  background: color-mix(in srgb, var(--link) 12%, transparent);
  border-radius: 50%;
  font-size: 1.45rem;
  font-weight: 700;
}

.fatal-state {
  padding: 18vh 20px 20px;
}

.fatal-state .button {
  margin-top: 22px;
}

.loading-state {
  display: flex;
  min-height: 70vh;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-duration: 1400ms;
  }

  .button,
  .icon-button {
    transition: none;
  }
}

.filters-list {
  display: grid;
  gap: 12px;
}

.filter-card,
.editor-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.filter-card {
  padding: 16px;
}

.filter-card-header {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.filter-name {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 1.05rem;
}

.filter-details {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 0 0 15px;
}

.filter-detail {
  padding: 10px;
  background: color-mix(in srgb, var(--muted) 8%, transparent);
  border-radius: 11px;
}

.filter-detail dt {
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 0.72rem;
}

.filter-detail dd {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 650;
}

.filter-actions {
  justify-content: flex-end;
  gap: 8px;
}

.text-button {
  min-height: 40px;
  padding: 0 12px;
  color: var(--link);
  background: transparent;
  border: 0;
  border-radius: 10px;
  font-weight: 700;
}

.text-button:hover:not(:disabled) {
  background: color-mix(in srgb, var(--link) 8%, transparent);
}

.text-button-danger {
  color: var(--destructive);
}

.editor-card {
  margin-top: 20px;
  padding: 18px;
  scroll-margin-top: 14px;
}

.editor-heading {
  justify-content: space-between;
  margin-bottom: 20px;
}

.editor-heading h2 {
  margin: 0;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  padding: 0;
  place-items: center;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 50%;
  font-size: 1.7rem;
  line-height: 1;
}

.icon-button:hover {
  background: color-mix(in srgb, var(--muted) 10%, transparent);
}

.field {
  min-width: 0;
  margin: 0 0 20px;
  padding: 0;
  border: 0;
}

.field > label:first-child,
.field > legend {
  display: block;
  margin-bottom: 8px;
  padding: 0;
  font-size: 0.88rem;
  font-weight: 750;
}

input[type="search"],
input[type="number"] {
  width: 100%;
  min-height: 48px;
  padding: 0 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  outline: none;
}

input:focus,
select:focus,
button:focus-visible {
  border-color: var(--link);
  outline: 3px solid color-mix(in srgb, var(--link) 22%, transparent);
  outline-offset: 1px;
}

input[aria-invalid="true"] {
  border-color: var(--destructive);
}

.field-hint {
  min-height: 1.1em;
  margin: 6px 2px 0;
  font-size: 0.76rem;
  line-height: 1.4;
}

.check-row {
  display: flex !important;
  min-height: 42px;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px !important;
  font-size: 0.9rem !important;
  font-weight: 600 !important;
}

.check-row input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--button);
}

.preset-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.preset-option {
  position: relative;
  display: inline-flex;
}

.preset-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.preset-option span {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  padding: 0 13px;
  color: var(--text);
  background: color-mix(in srgb, var(--muted) 9%, transparent);
  border: 1px solid transparent;
  border-radius: 12px;
  font-size: 0.84rem;
  font-weight: 650;
}

.preset-option input:focus-visible + span {
  outline: 3px solid color-mix(in srgb, var(--link) 22%, transparent);
  outline-offset: 1px;
}

.preset-option input:checked + span {
  color: var(--button-text);
  background: var(--button);
}

.range-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: end;
  gap: 8px;
  margin-top: 10px;
}

.range-fields label {
  min-width: 0;
  color: var(--muted);
  font-size: 0.74rem;
}

.range-fields label span {
  display: block;
  min-height: 1.15em;
  margin: 0 0 5px 2px;
}

.range-separator {
  padding-bottom: 14px;
  color: var(--muted);
}

.form-error {
  margin: -4px 0 16px;
  padding: 11px 12px;
  color: var(--destructive);
  background: color-mix(in srgb, var(--destructive) 10%, transparent);
  border-radius: 10px;
  font-size: 0.82rem;
  line-height: 1.4;
}

.form-actions {
  justify-content: flex-end;
  gap: 10px;
}

.form-actions .button {
  flex: 1;
}

.toast {
  position: fixed;
  z-index: 10;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  left: max(16px, env(safe-area-inset-left));
  width: min(calc(100% - 32px), 520px);
  margin: 0 auto;
  padding: 13px 16px;
  color: var(--button-text);
  background: color-mix(in srgb, var(--text) 92%, transparent);
  border-radius: 13px;
  box-shadow: 0 12px 32px color-mix(in srgb, #000 22%, transparent);
  font-size: 0.86rem;
  font-weight: 650;
  text-align: center;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@media (min-width: 560px) {
  .shell {
    padding-top: 34px;
  }

  .filters-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .editor-card {
    padding: 22px;
  }
}
