:root {
  color-scheme: dark; --page: #171a30; --panel: #22253e; --canvas: #101326;
  --text: #f3f2fc; --muted: #b3b5cc; --line: #3b3e59; --accent: #cec2ff;
  --pink: #ff75b5; --drawer: 360px; --gutter: clamp(12px, 2vw, 24px);
  --edge-left: max(var(--gutter), env(safe-area-inset-left)); --edge-right: max(var(--gutter), env(safe-area-inset-right));
  font: 16px "Trebuchet MS", "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
body { margin: 0; height: 100dvh; display: grid; grid-template-columns: minmax(0,1fr); grid-template-rows: auto minmax(0,1fr); background: var(--page); color: var(--text); }
/* Статические подписи не выделяются долгим касанием. В полях сохраняем
   выделение и стандартное редактирование, включая меню вставки. */
body { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
input[type="number"], input[type="text"], textarea, [contenteditable="true"] { user-select: text; -webkit-user-select: text; -webkit-touch-callout: default; }
button, input, select, summary { font: inherit; -webkit-tap-highlight-color: transparent; }
button, select, summary, input[type="checkbox"], input[type="radio"] { cursor: pointer; }
button, summary { touch-action: manipulation; }
button:disabled { opacity: .38; cursor: default; }
button:not(:disabled):hover, summary:hover { filter: brightness(1.12); }
:focus-visible { outline: 2px solid var(--pink); outline-offset: 3px; }
.topbar { min-width: 0; min-height: 74px; padding: max(12px, env(safe-area-inset-top)) var(--edge-right) 12px var(--edge-left); display: flex; gap: 22px; align-items: center; border-bottom: 1px solid var(--line); }
.brand { flex-shrink: 0; display: flex; align-items: center; gap: 10px; font-size: 31px; font-weight: 700; letter-spacing: -1.5px; }
.brand-dot { color: var(--pink); }
.header-actions { margin-left: auto; min-width: 0; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; align-items: center; }
.header-actions > *, .history-actions > * { flex-shrink: 0; }
.button { display: inline-flex; justify-content: center; align-items: center; gap: 8px; min-height: 42px; border: 0; border-radius: 9px; padding: 10px 14px; font-size: 14px; font-weight: 700; }
.button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.primary { background: var(--accent); color: #241b42; }
.secondary { background: #36394f; color: var(--text); }
.quiet { background: transparent; color: var(--muted); font-weight: 400; }
.studio { min-width: 0; min-height: 0; display: flex; padding: 0 0 env(safe-area-inset-bottom); }
body[data-settings-open="true"] .studio { margin-right: var(--drawer); }
.workspace { width: 100%; min-width: 0; min-height: 0; display: grid; grid-template-rows: minmax(0,1fr); }
.history-actions { display: flex; align-items: center; gap: 0; }
.canvas-stage { min-height: 0; min-width: 0; display: grid; place-items: center; overflow: hidden; }
.canvas-stack { position: relative; width: 1px; height: 1px; max-width: 100%; max-height: 100%; overflow: hidden; isolation: isolate; background: var(--page); }
.canvas-stack canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#guides { touch-action: none; cursor: url("./brush-cursor.svg") 12 12, default; }
#guides[data-pan="ready"] { cursor: grab; }
#guides[data-pan="dragging"] { cursor: grabbing; }
.canvas-stack:has(#guides:focus-visible) { outline: 2px solid var(--pink); outline-offset: -2px; }
.empty-hint { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; pointer-events: none; padding: 22px; color: #d2d2e8; background: radial-gradient(circle, var(--canvas), transparent 64%); }
.hint-star { width: 60px; height: 60px; fill: none; stroke: var(--accent); stroke-width: 1; margin-bottom: 18px; }
.empty-hint h1 { margin: 0 0 10px; font-size: clamp(18px, 2.2vw, 28px); font-weight: 400; letter-spacing: -.5px; }
.empty-hint p { margin: 0; font-size: 14px; line-height: 1.65; color: #a8abc3; }
.inline-check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; line-height: 1.4; }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.export-menu { position: relative; }
.export-menu summary { list-style: none; }
.export-menu summary::-webkit-details-marker { display: none; }
.export-popover { position: absolute; right: 0; top: calc(100% + 12px); z-index: 20; width: min(290px, calc(100vw - var(--edge-left) - var(--edge-right))); max-height: min(70dvh, calc(100dvh - 100px)); overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 15px; padding: 22px; background: var(--panel); border: 1px solid #575a76; border-radius: 14px; box-shadow: 0 18px 45px #0006; }
.export-popover .field-help { margin: 0; }
.settings-panel { position: fixed; inset: 0 0 0 auto; width: min(var(--drawer), 100vw); max-width: none; height: 100dvh; max-height: none; margin: 0; padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); border: 0; border-left: 1px solid var(--line); background: var(--panel); color: var(--text); z-index: 25; box-shadow: -14px 0 45px #0002; }
.settings-panel[open] { display: flex; flex-direction: column; animation: drawer-in .2s ease-out; }
.settings-panel::backdrop { background: #07091788; }
.drawer-header { min-height: 74px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 20px 12px 25px; border-bottom: 1px solid var(--line); }
.drawer-header h2 { margin: 0; font-size: 20px; }
.close-button { font-size: 28px; line-height: 1; min-width: 42px; padding: 5px; }
.settings-tabs { flex-shrink: 0; display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 4px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.settings-tabs button { min-width: 0; overflow-wrap: normal; padding: 9px 3px; min-height: 44px; border: 0; background: transparent; color: var(--muted); border-radius: 8px; font-size: 13px; line-height: 1.3; }
.settings-tabs button { display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 5px; }
.settings-tabs svg { width: 20px; height: 20px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.settings-tabs [aria-selected="true"] { background: #49415f; color: #fff; }
.drawer-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 24px max(28px, env(safe-area-inset-bottom)); scrollbar-width: thin; scrollbar-color: #686884 transparent; }
.drawer-header { flex-shrink: 0; }
.basic-settings { padding: 4px 0 20px; }
.shared-controls { padding-top: 4px; }
.shared-controls .basic-spin { border-top: 0; padding-top: 14px; margin-top: 0; }
/* Нативный checkbox сохраняет клавиатуру/доступность switch; fieldset
   блокирует выбор и удаление файла целиком без отдельных обработчиков. */
.image-toggle { display: flex; align-items: center; justify-content: space-between; gap: 16px; font-size: 16px; font-weight: 700; cursor: pointer; }
.image-toggle input { appearance: none; flex: 0 0 44px; width: 44px; height: 26px; border: 0; border-radius: 20px; background: var(--track); padding: 4px; transition: background .15s; }
.image-toggle input::before { content: ""; display: block; width: 18px; height: 18px; border-radius: 50%; background: var(--panel); transition: transform .15s; }
.image-toggle input:checked { background: var(--accent); }
.image-toggle input:checked::before { transform: translateX(18px); }
.image-controls { min-width: 0; margin: 0; padding: 22px 0; border: 0; border-bottom: 1px solid var(--line); }
.image-controls:disabled { opacity: .45; }
.image-controls:disabled .upload-control { cursor: default; }
@media (prefers-reduced-motion: reduce) { .image-toggle input, .image-toggle input::before { transition: none; } }
.basic-spin { padding-top: 18px; margin-top: 14px; border-top: 1px solid var(--line); }
.basic-spin h3 { margin-bottom: 12px; }
.basic-spin > label { font-size: 14px; }
.solid-color-field { min-height: 44px; }
.spin-control { display: grid; grid-template-columns: 42px minmax(0,1fr) 42px; gap: 10px; margin: 12px 0 8px; }
#spin-speed { width: 100%; border: 1px solid var(--line); border-radius: 8px; background: #191c32; color: var(--text); text-align: center; font: inherit; }
#spin-rpm { color: var(--accent); font-size: 14px; }
#pan-lock[aria-pressed="true"] { color: var(--accent); outline: 1px solid currentColor; outline-offset: -2px; }
#pan-lock[aria-pressed="false"] .lock-closed, #pan-lock[aria-pressed="true"] .lock-open { display: none; }
#spin-toggle:not([data-running="true"]) .spin-pause, #spin-toggle[data-running="true"] .spin-play { display: none; }
#spin-toggle[data-running="true"] { color: var(--accent); }
#view-reset { min-width: 64px; font-variant-numeric: tabular-nums; }
#solid-color { width: 54px; height: 38px; padding: 3px; border: 1px solid #72718e; border-radius: 8px; background: transparent; cursor: pointer; }
.drawer-actions { flex-shrink: 0; display: grid; grid-template-columns: 1fr; align-items: stretch; gap: 6px; padding: 12px 16px max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.drawer-actions #history-actions { grid-column: 1 / -1; justify-content: center; }
.drawer-actions .button { width: 100%; padding: 8px; }
.drawer-actions #screen-toggle { font-size: 13px; }
.drawer-actions .export-popover { top: auto; bottom: calc(100% + 12px); max-height: 65dvh; overflow-y: auto; }
.drawer-actions .history-actions .button span, .drawer-actions .export-menu summary > span { display: inline; }
.control-section { padding: 23px 0; border-bottom: 1px solid var(--line); }
.control-section:last-child { border: 0; padding-bottom: 0; }
h3 { margin: 0 0 19px; font-size: 17px; font-weight: 700; }
.field-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 18px 0 7px; font-size: 14px; }
.field-heading output { color: var(--accent); font-variant-numeric: tabular-nums; }
.field-help { font-size: 13px; line-height: 1.6; color: var(--muted); margin: 12px 0 0; }
input[type="range"] { width: 100%; min-width: 0; height: 26px; margin: 0; appearance: none; background: transparent; accent-color: var(--accent); cursor: pointer; }
input[type="range"]::-webkit-slider-runnable-track { height: 4px; background: #545775; border-radius: 3px; }
input[type="range"]::-webkit-slider-thumb { appearance: none; width: 15px; height: 15px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); margin-top: -5.5px; box-shadow: 0 0 0 1px var(--accent); }
input[type="range"]::-moz-range-track { height: 4px; background: #545775; border-radius: 3px; }
input[type="range"]::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); }
.segmented { display: flex; min-width: 0; padding: 4px; margin: 15px 0 0; border: 1px solid #444660; border-radius: 9px; background: #191c32; }
.segmented label { flex: 1; position: relative; min-width: 0; }
.segmented input, .background-options input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented span { display: flex; min-height: 36px; align-items: center; justify-content: center; text-align: center; padding: 7px 12px; font-size: 14px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.segmented input:checked + span { background: #46405f; color: #fff; }
.segmented input:focus-visible + span, .background-options input:focus-visible + span { outline: 2px solid var(--pink); outline-offset: 2px; }
.mode-switch { margin: 0; min-width: 214px; }
select { width: 100%; border: 1px solid #515470; border-radius: 8px; background: #292d49; color: var(--text); padding: 11px 9px; font-size: 14px; }
.language-select { width: auto; min-height: 42px; flex-shrink: 0; }
.palette-preview { display: flex; align-items: center; gap: 20px; margin: 22px 0; }
.palette-preview .field-help { margin: 0; }
.color-orbit { width: 78px; height: 78px; flex-shrink: 0; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(from 90deg,#ff75b5,#ffce78,#8f9cff,#ff75b5); }
.orbit-hole { width: 50px; height: 50px; border-radius: 50%; background: var(--panel); }
.color-stop { display: grid; grid-template-columns: 23px 32px minmax(30px,1fr) 42px 30px; gap: 10px; align-items: center; min-height: 46px; font-size: 14px; color: var(--muted); }
.color-stop input[type="color"] { border: 0; background: none; padding: 0; width: 32px; height: 32px; cursor: pointer; }
.color-stop input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
.color-stop input[type="color"]::-webkit-color-swatch { border: 1px solid #ffffff55; border-radius: 50%; }
.color-stop input[type="color"]::-moz-color-swatch { border: 1px solid #ffffff55; border-radius: 50%; }
.color-stop output { text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; }
.remove-color { border: 0; background: transparent; color: var(--muted); min-width: 30px; height: 38px; font-size: 21px; border-radius: 6px; }
.add-color { margin-top: 13px; width: 100%; justify-content: space-between; font-weight: 400; }
#color-count { font-size: 13px; color: var(--muted); }
.background-options { display: flex; gap: 12px; min-width: 0; padding: 0; border: 0; margin: 0; }
.background-options label { position: relative; flex: 1; }
.background-swatch { display: block; padding: 13px; text-align: center; border: 1px solid #555977; border-radius: 8px; font-size: 14px; cursor: pointer; }
.background-swatch.dark { background: #101326; color: #fff; }
.background-swatch.light { background: #fff; color: #252942; }
.background-options input:checked + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.upload-control { position: relative; display: grid; place-items: center; border: 1px dashed #9790b3; border-radius: 9px; padding: 15px; margin-top: 15px; font-size: 14px; color: #eee8ff; cursor: pointer; }
.upload-control input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.upload-control:focus-within { outline: 2px solid var(--pink); outline-offset: 2px; }
.image-error-details summary { cursor: pointer; }
.image-error-details code { display: block; margin-top: 8px; overflow-wrap: anywhere; user-select: text; }
#image-status:empty { display: none; }
#image-status { color: var(--text); }
#image-status[data-error="true"] { color: var(--pink); }
.image-info { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 12px; line-height: 1.5; color: var(--muted); }
#image-name { overflow-wrap: anywhere; min-width: 0; }
.image-info button { margin-left: auto; padding: 8px; flex-shrink: 0; }
.notice { position: fixed; bottom: max(20px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); background: #e7dfff; color: #271d41; border-radius: 12px; padding: 13px 22px; font-size: 14px; max-width: calc(100% - var(--edge-left) - var(--edge-right)); box-shadow: 0 8px 35px #0003; z-index: 50; pointer-events: none; }
.notice:empty, [hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
body[data-background="light"] .empty-hint { color: #242a44; }
body[data-background="light"] .empty-hint p { color: #535a75; }
body[data-background="light"] .hint-star { stroke: #7755a7; }
@keyframes drawer-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
@media (max-width: 900px) {
  :root { --drawer: 340px; }
  .topbar { padding-top: max(10px, env(safe-area-inset-top)); padding-bottom: 10px; gap: 13px; }
  .button { padding: 10px 12px; }
}
@media (max-width: 1100px) {
  .header-actions #settings-open span, .header-actions #screen-toggle span,
  .header-actions #clear span, .header-actions .history-actions .button span, .view-reset-label { display: none; }
  .header-actions .history-actions .button { padding: 8px 10px; }
}
@media (max-width: 760px) {
  body[data-settings-open="true"] .studio { margin-right: 0; }
  .header-actions { gap: 7px; }
  #settings-open span { display: none; }
  .header-actions #screen-toggle span { display: none; }
  .header-actions #clear span { display: none; }
  .history-actions .button span { display: none; }
  .history-actions .button { padding: 8px 10px; }
  .drawer-header { min-height: 66px; }
}
@media (max-width: 560px) {
  .topbar { gap: 8px; padding: max(9px, env(safe-area-inset-top)) var(--edge-right) 9px var(--edge-left); flex-wrap: wrap; min-height: 62px; }
  .brand { display: none; }
  .brand img { width: 29px; height: 29px; }
  .mode-switch { order: 3; width: 100%; min-width: 0; }
  .mode-switch span { min-height: 28px; padding: 4px 9px; }
  .header-actions { margin-left: auto; }
  /* Девять равных ячеек исключают переполнение даже при масштабе 1600%.
     Группа истории сохраняет DOM и обработчики, но отдаёт сетке свои кнопки. */
  .header-actions { width: 100%; display: grid; grid-template-columns: repeat(9,minmax(0,1fr)); gap: 2px; }
  .header-actions .history-actions { display: contents; }
  :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button { width: 100%; min-width: 0; min-height: 40px; padding: 6px 2px; }
  .header-actions #view-reset { min-width: 0; padding-inline: 0; font-size: 12px; letter-spacing: -.5px; }
  .export-menu summary > span { display: none; }
  .hint-star { width: 40px; height: 40px; margin-bottom: 12px; }
  .empty-hint p { font-size: 12px; }
}
@media (max-height: 460px) {
  .topbar { min-height: 50px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .hint-star { display: none; }
}
@media (prefers-reduced-motion: reduce) { .settings-panel[open] { animation: none; } }

/* Верхняя полоса — отдельная строка сетки, а не слой поверх рисунка.
   Кнопка не перекрывает штрихи; панель в этом режиме открывается поверх
   холста, не меняя его размер при каждом открытии. Embed использует ту же
   компоновку с первой загрузки; выход из fullscreen не возвращает бренд. */
body:is([data-fullscreen="true"], [data-embed="true"]) .topbar { min-height: 42px; padding: calc(4px + env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 4px max(8px, env(safe-area-inset-left)); gap: 0; flex-wrap: nowrap; }
body:is([data-fullscreen="true"], [data-embed="true"]) .brand,
body:is([data-fullscreen="true"], [data-embed="true"]) .empty-hint { display: none; }
body:is([data-fullscreen="true"], [data-embed="true"]) .header-actions { gap: 4px; }
/* Компактность относится только к кнопкам самой полосы, истории и summary.
   Кнопка внутри export-popover сохраняет размеры и контрастный фон primary. */
body:is([data-fullscreen="true"], [data-embed="true"]) :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button { min-height: 32px; min-width: 34px; padding: 6px; background: transparent; color: var(--text); }
body:is([data-fullscreen="true"], [data-embed="true"]) :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button > span:not(#view-zoom) { display: none; }
body:is([data-fullscreen="true"], [data-embed="true"]) :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button > svg { width: 18px; height: 18px; }
body:is([data-fullscreen="true"], [data-embed="true"]) .studio { margin-right: 0; padding: 0 0 env(safe-area-inset-bottom); }
body:is([data-fullscreen="true"], [data-embed="true"]) .workspace { grid-template-rows: minmax(0, 1fr); }
body:is([data-fullscreen="true"], [data-embed="true"]) .canvas-stack[data-shape="square"] { border-radius: 0; }
@media (pointer: coarse) {
  body:is([data-fullscreen="true"], [data-embed="true"]) :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button { min-height: 40px; min-width: 40px; }
}

@media (max-width: 560px) {
  body[data-fullscreen="true"] .header-actions { grid-template-columns: repeat(8,minmax(0,1fr)); }
  body:is([data-fullscreen="true"], [data-embed="true"]) :is(.header-actions, .header-actions > .history-actions, .header-actions > .export-menu) > .button { min-width: 0; padding-inline: 2px; }
}

/* На телефоне значок сброса расположен над процентом в той же ячейке.
   Даже 1600% помещается в девять кнопок, не сужая область касания соседей. */
@media (max-width: 560px) {
  .header-actions #view-reset { flex-direction: column; gap: 0; font-size: 10px; line-height: 12px; padding-block: 3px; }
  .header-actions #view-reset > svg { width: 16px; height: 16px; }
}
