:root {
  color-scheme: light;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--background);
  font-synthesis: none;
  --text: #1c1c34;
  --background: #f6f7fb;
  --panel: #fff;
  --muted: #63647d;
  --field: #1c1c31;
  --field-text: #e9e6ff;
  --field-hint: #ada6cb;
  --field-border: #27283f;
  --field-caret: #ccbfff;
  --accent: #5141ce;
  --button-text: #fff;
  --action-text: #4936b6;
  --action-surface: #f4f1ff;
  --action-line: #e1dcfa;
  --line: #e4e5ef;
  --danger-text: #802742;
  --mark: linear-gradient(145deg, #6957f1, #4c38c1);
  --safe-top: max(env(safe-area-inset-top, 0px),
    calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px)));
  --safe-right: max(env(safe-area-inset-right, 0px),
    calc(var(--tg-safe-area-inset-right, 0px) + var(--tg-content-safe-area-inset-right, 0px)));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px),
    calc(var(--tg-safe-area-inset-bottom, 0px) + var(--tg-content-safe-area-inset-bottom, 0px)));
  --safe-left: max(env(safe-area-inset-left, 0px),
    calc(var(--tg-safe-area-inset-left, 0px) + var(--tg-content-safe-area-inset-left, 0px)));
}

:root[data-telegram-theme] {
  --text: var(--tg-theme-text-color, #1c1c34);
  --background: var(--tg-theme-secondary-bg-color, #f6f7fb);
  --panel: var(--tg-theme-bg-color, #fff);
  --muted: var(--tg-theme-hint-color, #63647d);
  --field: var(--tg-theme-secondary-bg-color, #f6f7fb);
  --field-text: var(--text);
  --field-hint: var(--muted);
  --field-border: var(--line);
  --field-caret: var(--text);
  --accent: var(--tg-theme-button-color, #5141ce);
  --button-text: var(--tg-theme-button-text-color, #fff);
  --action-text: var(--tg-theme-link-color, #4936b6);
  --action-surface: var(--field);
  --action-line: var(--line);
  --line: var(--tg-theme-section-separator-color, #e4e5ef);
  --danger-text: var(--tg-theme-destructive-text-color, #802742);
  --mark: var(--accent);
}

:root[data-telegram-theme="dark"] {
  color-scheme: dark;
  --text: var(--tg-theme-text-color, #f1f0fa);
  --background: var(--tg-theme-secondary-bg-color, #15161d);
  --panel: var(--tg-theme-bg-color, #21222c);
  --muted: var(--tg-theme-hint-color, #a6a7b6);
  --field: var(--background);
  --action-text: var(--tg-theme-link-color, #b1a8ff);
  --line: var(--tg-theme-section-separator-color, #393a48);
  --danger-text: var(--tg-theme-destructive-text-color, #ffabbd);
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  min-height: var(--tg-viewport-stable-height, 100dvh);
  margin: 0;
}

[hidden] { display: none !important; }

.workspace {
  width: min(100%, 1248px);
  margin: 0 auto;
  --top-spacing: clamp(32px, 6vw, 82px);
  --bottom-spacing: 40px;
  --side-spacing: clamp(16px, 4vw, 48px);
  padding: calc(var(--top-spacing) + var(--safe-top))
    calc(var(--side-spacing) + var(--safe-right))
    calc(var(--bottom-spacing) + var(--safe-bottom))
    calc(var(--side-spacing) + var(--safe-left));
}

.masthead {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: clamp(26px, 4vw, 40px);
}

.brand-mark {
  width: 56px;
  height: 56px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid var(--accent);
  border-radius: 17px;
  background: var(--mark);
  color: var(--button-text);
  box-shadow: 0 5px 12px #6654e32b;
}

.brand-mark svg { width: 29px; height: 29px; }

h1, h2, p { margin: 0; }

h1 {
  font-size: clamp(1.65rem, 3.4vw, 2.5rem);
  line-height: 1.12;
  font-weight: 750;
  letter-spacing: -.045em;
}

.masthead p {
  margin-top: 7px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.4;
}

.panels {
  display: grid;
  grid-template-columns: minmax(0, .92fr) minmax(0, 1.08fr);
  gap: 22px;
  align-items: stretch;
}

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 470px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--panel);
  box-shadow: 0 4px 20px #29294a08;
}

.panel-header {
  min-height: 61px;
  padding: 17px 22px;
  display: flex;
  align-items: center;
}

.panel-header label,
.panel-header h2 {
  display: flex;
  align-items: center;
  gap: 11px;
  font-size: 1rem;
  font-weight: 650;
  line-height: 1.35;
}

.panel-header svg,
.code-icon {
  width: 21px;
  height: 21px;
  flex: none;
  color: var(--action-text);
}

.code-icon {
  display: grid;
  place-items: center;
  font: 700 .81rem/1 ui-monospace, monospace;
}

textarea {
  display: block;
  flex: 1;
  width: calc(100% - 28px);
  min-height: 246px;
  margin: 0 14px;
  padding: 19px;
  border: 1px solid var(--field-border);
  border-radius: 12px;
  resize: vertical;
  background: var(--field);
  color: var(--field-text);
  caret-color: var(--field-caret);
  color-scheme: dark;
  font: 400 1rem/1.65 ui-monospace, "Cascadia Code", Consolas, monospace;
  tab-size: 4;
}

textarea::placeholder { color: var(--field-hint); }
[data-telegram-theme="light"] textarea { color-scheme: light; }

.editor-actions {
  display: flex;
  padding: 14px;
}

button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 9px;
  min-height: 46px;
  padding: 0 20px;
  border: 0;
  border-radius: 10px;
  color: var(--button-text);
  background: var(--accent);
  font-family: inherit;
  font-weight: 650;
  font-size: 1rem;
  cursor: pointer;
}

button svg { width: 21px; height: 21px; }
button:not(:disabled):hover { filter: brightness(.95); }
button:not(:disabled):active { filter: brightness(.88); }
button:disabled { opacity: .65; cursor: wait; }

button:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.preview .panel-header {
  justify-content: space-between;
  gap: 8px;
  padding-block: 9px;
}

.preview-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 6px;
}

.share-button {
  width: auto;
  min-height: 44px;
  padding-inline: 11px;
  border: 1px solid var(--action-line);
  background: var(--action-surface);
  color: var(--action-text);
  font-size: .875rem;
  white-space: nowrap;
}

.share-button:disabled { cursor: default; }

.share-notice {
  margin: 0 15px 12px;
  color: var(--danger-text);
  font-size: .875rem;
}

.canvas {
  position: relative;
  display: grid;
  place-items: center;
  flex: 1;
  min-width: 0;
  min-height: 295px;
  margin: 0 14px 14px;
  padding: clamp(14px, 3vw, 34px);
  overflow: auto;
  border: 1px solid #ededf7;
  border-radius: 12px;
  background: radial-gradient(circle, #dcdcf1 1px, transparent 1px) 0 0 / 16px 16px #f8f8fd;
}

.diagram { width: 100%; text-align: center; }
.diagram svg { display: block; height: auto; margin: auto; }

.notice {
  position: absolute;
  inset: 50% 14px auto;
  transform: translateY(-50%);
  width: fit-content;
  max-width: calc(100% - 28px);
  max-height: calc(100% - 28px);
  margin: 0 auto;
  padding: 14px 17px;
  overflow: auto;
  border: 1px solid #edc9d3;
  border-radius: 10px;
  background: #fff5f7;
  color: #802742;
  font-size: .9375rem;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

@media (max-width: 740px) {
  .workspace { --top-spacing: 24px; --bottom-spacing: 24px; }

  .masthead { gap: 12px; }
  .brand-mark { width: 46px; height: 46px; border-radius: 14px; }
  .brand-mark svg { width: 25px; height: 25px; }
  .panels { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .panel { min-height: 0; border-radius: 16px; scroll-margin-top: calc(12px + var(--safe-top)); }
  .panel-header { min-height: 53px; padding: 14px 17px; }

  textarea {
    width: calc(100% - 20px);
    min-height: 222px;
    margin-inline: 10px;
    padding: 13px;
  }

  .editor-actions { padding: 10px; }
  .editor-actions button { width: 100%; }
  .canvas { margin: 0 10px 10px; }
}

[data-telegram-main-button] .editor-actions { display: none; }
[data-telegram-main-button] textarea { margin-bottom: 14px; }

[data-telegram-theme] .brand-mark,
[data-telegram-theme] .panel {
  box-shadow: none;
}

.type-picker {
  display: grid;
  gap: 6px;
  margin: 0 14px 12px;
}

.type-picker > label {
  color: var(--muted);
  font-size: .875rem;
  font-weight: 650;
}

.type-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.type-select {
  flex: 1;
  min-width: 0;
  position: relative;
}

.type-select::after {
  position: absolute;
  right: 16px;
  top: calc(50% - 5px);
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--action-text);
  border-bottom: 2px solid var(--action-text);
  transform: rotate(45deg);
  pointer-events: none;
  content: "";
}

.type-select select {
  appearance: none;
  width: 100%;
  min-height: 44px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font: 600 1rem system-ui, sans-serif;
  text-overflow: ellipsis;
}

.type-select select:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.info-button {
  flex: none;
  width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid var(--action-line);
  background: var(--action-surface);
  color: var(--action-text);
  font-size: 1.25rem;
}

#diagram-info span {
  display: grid;
  place-items: center;
  width: 21px;
  height: 21px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  font: 700 .87rem/1 system-ui, sans-serif;
}

.diagram-help {
  width: min(530px, calc(100% - 32px - var(--safe-right) - var(--safe-left)));
  max-height: calc(100vh - 32px);
  max-height: calc(var(--tg-viewport-stable-height, 100dvh) - 32px - var(--safe-top) - var(--safe-bottom));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  line-height: 1.5;
  overflow: auto;
}

.diagram-help::backdrop { background: rgb(21 20 41 / 52%); }

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

.help-header h2 { font-size: 1.3rem; line-height: 1.25; }
.diagram-help h3 { font-size: 1rem; margin: 18px 0 10px; }

.help-declaration {
  margin: 14px 0 0;
  padding: 9px 12px;
  border-radius: 8px;
  background: var(--action-surface);
  color: var(--action-text);
  overflow: auto;
}

.help-syntax { margin: 0 0 16px; }
.help-syntax dt { font-size: .9rem; font-weight: 650; overflow-wrap: anywhere; }
.help-syntax dd { margin: 2px 0 10px; color: var(--muted); }
.diagram-help a { color: var(--action-text); font-weight: 650; }

@media (max-width: 740px) {
  .type-picker { margin-inline: 10px; }
}
