:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --surface-soft: #f7f7f5;
  --surface-muted: #f1f2f4;
  --text: #111827;
  --text-soft: #374151;
  --muted: #6b7280;
  --muted-2: #9ca3af;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #ecfdf3;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --black: #111827;
  --radius-sm: 10px;
  --radius-md: 14px;
  --shadow-soft: 0 16px 48px rgba(17, 24, 39, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  padding: 24px;
  background:
    radial-gradient(circle at 12% 4%, rgba(37, 99, 235, 0.08), transparent 26%),
    linear-gradient(180deg, #ffffff 0%, var(--bg) 58%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

body.modal-open {
  overflow: hidden;
}

body.embed-mode {
  min-height: auto;
  padding: 0;
  background: transparent;
}

body.embed-mode .app-header {
  display: none;
}

body.embed-mode .app-shell {
  width: 100%;
  margin: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

button, input, select, textarea {
  font: inherit;
}

.app-shell {
  width: min(100%, 760px);
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: #fff;
  padding: 22px;
  box-shadow: var(--shadow-soft);
}

.app-header {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: 0 22px;
}

.app-settings {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(17, 24, 39, 0.28);
  overflow: auto;
  padding: 24px 16px;
}

.app-settings.is-open {
  display: block;
}

.app-settings-panel {
  width: min(100%, 760px);
  margin: 0 auto;
}

.app-settings-panel .block {
  position: relative;
}

.settings-head {
  padding-right: 48px;
  margin-bottom: 2px;
}

.settings-head .block-head {
  width: 100%;
}

.settings-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 34px;
  min-height: 34px;
  padding: 0;
}

.advanced-content > .block-help:first-child {
  margin: 0 0 12px;
  padding-right: 48px;
}

.app-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  padding-bottom: 14px;
  margin-bottom: 12px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.brand-icon {
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  flex: 0 0 36px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  color: var(--accent);
  font-weight: 800;
}

.brand h1 {
  margin: 0 0 4px;
  font-size: 23px;
  letter-spacing: -0.04em;
  line-height: 1.1;
}

.brand p {
  margin: 0;
  color: var(--muted);
}

.block {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #fff;
  padding: 14px;
  margin-top: 12px;
}

.block-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-weight: 700;
  margin-bottom: 12px;
}

.block-head.no-margin { margin-bottom: 0; }

.block-head.is-collapsible {
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  text-align: left;
  cursor: pointer;
}

.block-head.is-collapsible * {
  color: inherit;
}

.block-head-meta {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  margin-left: auto;
}

.block-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: inherit;
  font: inherit;
  font-weight: 700;
  width: 20px;
  line-height: 1;
  pointer-events: none;
}

.block-help {
  color: var(--muted);
  font-size: 12px;
  margin: 8px 0 0;
}

.block-help.is-error { color: var(--danger); }
.block-help.is-success { color: var(--success); }
.block-help.is-info { color: var(--muted); }

.block-head .block-help {
  margin: 0;
  font-weight: 400;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: right;
}

.caption {
  color: var(--muted);
  font-size: 12px;
}

.field-row {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
}

.utm-settings-row {
  display: flex;
  justify-content: flex-end;
  margin-top: 14px;
}

.btn.settings-link {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  min-height: 24px;
  min-width: max-content;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: rgb(107, 114, 128);
  opacity: 1;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  line-height: 1.2;
  margin-left: 4px;
}

.btn.settings-link:hover {
  transform: none;
  color: var(--text);
  text-decoration: underline;
}

.btn.settings-link:focus-visible {
  outline: 2px solid rgba(37, 99, 235, 0.45);
  outline-offset: 2px;
  border-radius: 6px;
}

.field-row label { margin: 0; }

label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-soft);
  font-weight: 600;
  font-size: 12px;
}

.input, .select, .textarea {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
  outline: none;
  transition: border-color .14s ease, box-shadow .14s ease, background .14s ease;
}

.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.input.is-error {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.08);
}

.textarea {
  min-height: 76px;
  resize: vertical;
  background: var(--surface-soft);
}

.input-wrap {
  position: relative;
}

.input-wrap .input {
  padding-right: 44px;
}

.input-clear {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  width: 26px;
  height: 26px;
  min-height: 26px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.input-clear:hover {
  color: var(--text);
  background: var(--surface-soft);
  transform: translateY(-50%);
}

.input-clear[hidden] {
  display: none;
}

.result-row, .short-row {
  display: grid;
  gap: 10px;
  align-items: center;
}

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

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-top: 12px;
}

.result-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.result-controls .caption {
  font-size: 12px;
  white-space: nowrap;
}

.result-controls .select {
  width: 112px;
  min-height: 40px;
  font-size: 14px;
}

.result-controls .btn {
  min-height: 38px;
  min-width: auto;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 650;
  padding: 9px 14px;
}

.result-controls .btn.secondary {
  width: 40px;
  min-width: 40px;
  padding: 0;
}

.result-controls .btn-text {
  display: none;
}

.result-controls .btn.primary {
  background: var(--black);
  color: #fff;
}

.result-controls + .success-box,
.result-controls + .error-box,
.result-controls + .info-box {
  margin-top: 10px;
  width: 100%;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  padding: 9px 14px;
  cursor: pointer;
  text-decoration: none;
  font-weight: 650;
  white-space: nowrap;
  transition: transform .12s ease, background .12s ease, border-color .12s ease;
  background: transparent;
}

.btn:hover { transform: translateY(-1px); }

.btn.primary { background: var(--black); color: #fff; }
.btn.secondary { background: #fff; color: var(--text); border-color: var(--border-strong); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.icon { width: 38px; padding: 0; }
.btn.disabled,
.btn:disabled {
  background: var(--surface-muted);
  color: var(--muted-2);
  border-color: transparent;
  cursor: not-allowed;
}

.btn.disabled:hover,
.btn:disabled:hover {
  transform: none;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 24px;
  padding: 3px 9px;
  border-radius: 99px;
  font-weight: 700;
  font-size: 12px;
  background: var(--surface-muted);
  color: var(--muted);
}

.pill.gray { background: var(--surface-muted); color: var(--muted); }

.success-box, .error-box, .info-box {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-weight: 600;
}

.success-box {
  background: var(--success-soft);
  color: var(--success);
  border-color: rgba(22, 163, 74, 0.14);
}

.error-box {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: rgba(220, 38, 38, 0.14);
}

.info-box {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: rgba(37, 99, 235, 0.16);
}

.advanced-content[hidden] {
  display: none;
}

.advanced-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.advanced-content textarea {
  min-height: 140px;
  background: #fff;
}

.advanced-divider {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 14px 0;
}

.json-row {
  display: grid;
  grid-template-columns: 1.4fr auto auto auto;
  gap: 10px;
  align-items: center;
  margin-top: 8px;
}

.json-row .btn {
  min-height: 40px;
}

.advanced-content .btn {
  min-height: 40px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 650;
}

.hidden-input {
  display: none;
}

@media (min-width: 431px) and (max-width: 600px) {
  .result-controls {
    gap: 8px;
  }

  .result-controls .caption {
    font-size: 11px;
  }

  .result-controls .select {
    width: 96px;
  }

  .result-controls .btn.primary {
    padding: 9px 12px;
  }

  .result-controls .btn.secondary {
    flex: 1 1 calc(50% - 4px);
    width: auto;
    min-width: 0;
    padding: 9px 12px;
  }

  .result-controls .btn-text {
    display: inline;
  }
}

@media (max-width: 430px) {
  body { padding: 0; }

  .app-shell {
    width: 100%;
    border-radius: 16px;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    padding: 16px 12px;
    margin-top: 0;
  }

  .app-header {
    width: 100%;
    padding: 0 12px;
    padding-top: 10px;
    padding-bottom: 4px;
  }

  .app-settings {
    padding: 0;
  }

  .app-settings-panel {
    width: 100%;
  }

  .app-settings-panel .block {
    margin: 0;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    min-height: 100vh;
  }

  .app-top,
  .result-row,
  .advanced-grid {
    grid-template-columns: 1fr;
  }

  .app-top {
    display: grid;
    margin-bottom: 0;
    padding-bottom: 6px;
    gap: 10px;
  }

  .brand {
    align-items: center;
    gap: 10px;
  }

  .brand h1 {
    margin: 0;
    line-height: 1.05;
  }

  .brand p {
    display: none;
  }

  .btn.settings-link {
    font-size: 14px;
    min-height: 24px;
  }

  .field-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .advanced-content .actions-row {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .json-row {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .json-row > :nth-child(1),
  .json-row > :nth-child(2) {
    grid-column: 1 / -1;
  }

  .advanced-content .actions-row > *,
  .advanced-content .btn.secondary,
  .advanced-content .btn.primary {
    width: 100%;
  }

  .advanced-content .btn {
    min-height: 48px;
    border-radius: 14px;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.2;
  }

  .json-row .btn {
    width: 100%;
  }

  .json-row > :nth-child(3),
  .json-row > :nth-child(4) {
    grid-column: auto;
  }

  .result-row .btn.secondary {
    color: var(--accent);
    font-size: 16px;
    font-weight: 700;
    min-height: 52px;
    border-radius: 14px;
    margin-top: 2px;
  }

  .result-controls .caption,
  .result-controls .select,
  .result-controls .btn.primary {
    font-size: inherit;
    width: 100%;
    min-width: 0;
  }

  .result-controls .btn.secondary {
    flex: 1 1 calc(50% - 5px);
    width: auto;
    min-width: 0;
    font-size: inherit;
    padding: 9px 12px;
  }

  .result-controls .btn-text {
    display: inline;
  }
}
