:root {
  color-scheme: light;
  --bg: #efeff4; --surface: #ffffff; --sunken: #eeeef3; --thumb: #ffffff;
  --text: #000000; --hint: #6d6d72; --line: #e0e0e6;
  --accent: #3390ec; --accent-ink: #ffffff; --accent-soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --danger: #e53935;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { color-scheme: dark; --bg: #0f0f10; --surface: #1c1c1e; --sunken: #2c2c2e; --thumb: #636366; --text: #ffffff; --hint: #98989f; --line: #38383a; --accent: #4da3f7; }
}
:root[data-theme="dark"] { color-scheme: dark; --bg: #0f0f10; --surface: #1c1c1e; --sunken: #2c2c2e; --thumb: #636366; --text: #ffffff; --hint: #98989f; --line: #38383a; --accent: #4da3f7; }

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; background: var(--bg); color: var(--text); font: 400 16px/1.4 var(--font); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; max-width: 1100px; margin: 0 auto; padding: 14px 20px 6px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 19px; }
.brand-mark { width: 26px; height: 26px; border-radius: 50%; background: conic-gradient(#ff3e9d, #ffd36e, #35c8e8, #8768e8, #ff3e9d); -webkit-mask: radial-gradient(circle, transparent 7px, #000 8px); mask: radial-gradient(circle, transparent 7px, #000 8px); }
.topbar-note { color: var(--hint); font-size: 14px; }
.bot-link { color: var(--accent); text-decoration: none; font-weight: 600; white-space: nowrap; }
.is-telegram .topbar { display: none; }

.app-shell { display: grid; grid-template-columns: minmax(0, 1fr) 340px; grid-template-rows: auto 1fr; column-gap: 28px; max-width: 1100px; margin: 0 auto; padding: 8px 20px 48px; }
.rail { grid-column: 1 / -1; display: flex; gap: 2px; margin-bottom: 18px; padding: 2px; border-radius: 10px; background: var(--sunken); max-width: 460px; }
.rail-item { flex: 1; min-height: 34px; padding: 0 12px; border: 0; border-radius: 8px; background: none; color: var(--text); font-weight: 500; font-size: 14px; white-space: nowrap; }
.rail-item.is-active { background: var(--thumb); box-shadow: 0 1px 3px #0002; font-weight: 600; }

.workspace { grid-column: 1; grid-row: 2; min-width: 0; }
.workspace-heading { margin: 0 4px 10px; }
h1 { margin: 0; font-size: 22px; font-weight: 700; }
.tool-panel { overflow: hidden; border-radius: 12px; background: var(--surface); }

.upload-bar, .frame-upload { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.upload-bar { margin: 14px; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); transition: background .15s; }
.upload-bar.is-over { outline: 2px dashed var(--accent); }
.upload-icon, .frame-upload-icon { display: grid; flex: none; width: 38px; height: 38px; place-items: center; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-size: 19px; }
.overlay-icon { background: #8e6cf0; }
.upload-copy strong, .frame-upload strong { display: block; font-size: 16px; font-weight: 600; }
.upload-copy small, .frame-upload small { display: block; color: var(--hint); font-size: 13px; }
.upload-action, .frame-upload-action { margin-left: auto; color: var(--accent); font-weight: 600; white-space: nowrap; }

.crop-stage { position: relative; display: grid; min-height: 200px; max-height: 380px; place-items: center; overflow: hidden; margin: 0 14px; border-radius: 10px; background: repeating-conic-gradient(var(--sunken) 0 25%, var(--bg) 0 50%) 0 0 / 18px 18px; }
#crop-canvas { display: block; max-width: 100%; max-height: 380px; cursor: grab; touch-action: none; }
#crop-canvas:active { cursor: grabbing; }
.stage-hint { position: absolute; display: none; padding: 8px 12px; border-radius: 8px; background: #000a; color: #fff; font-size: 14px; pointer-events: none; }
.crop-stage.is-empty .stage-hint { display: block; }
.stage-meta { position: absolute; right: 8px; bottom: 8px; display: flex; gap: 4px; padding: 3px 8px; border-radius: 6px; background: #000a; color: #fff; font-size: 12px; }
#piece-count::before { content: ","; margin-right: 4px; margin-left: -4px; }

.crop-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 14px; padding: 14px; }
.control-group { display: grid; gap: 6px; }
.control-label, .palette-subhead, .style-label, .symbol-label { color: var(--hint); font-size: 13px; }
.segmented, .frame-source { display: flex; gap: 2px; padding: 2px; border-radius: 10px; background: var(--sunken); }
.segment, .source-option { min-height: 34px; padding: 0 14px; border: 0; border-radius: 8px; background: none; font-size: 14px; }
.source-option { flex: 1; }
.segment.is-selected, .source-option.is-selected { background: var(--thumb); box-shadow: 0 1px 3px #0002; font-weight: 600; }
select, .height-input { height: 38px; border: 0; border-radius: 10px; background: var(--sunken); }
select { padding: 0 26px 0 12px; }
.height-input { display: flex; align-items: center; padding-right: 12px; }
.height-input input { width: 64px; border: 0; outline: 0; padding: 0 0 0 12px; background: none; }
.height-input span { color: var(--hint); }

.panel-footer { display: flex; align-items: center; justify-content: flex-end; gap: 10px; padding: 12px 14px 14px; }
.primary-button, .secondary-button { min-height: 46px; padding: 0 20px; border: 0; border-radius: 12px; font-weight: 600; font-size: 16px; }
.primary-button { background: var(--accent); color: var(--accent-ink); }
.primary-button:hover { filter: brightness(.94); }
.primary-button:disabled { opacity: .4; cursor: not-allowed; }
.secondary-button { background: var(--accent-soft); color: var(--accent); }
.secondary-button:disabled { opacity: .4; cursor: not-allowed; }
.panel-footer .primary-button { width: 100%; }
.frame-footer .secondary-button { width: 100%; }
.download-results { padding: 4px 14px 16px; }
.results-heading { margin-bottom: 8px; color: var(--hint); font-size: 14px; }
.results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.result-tile { overflow: hidden; border-radius: 10px; background: var(--sunken); color: var(--text); text-decoration: none; font-size: 13px; text-align: center; }
.result-tile img { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; }
.result-tile span { display: block; padding: 6px 4px; color: var(--accent); }

.frame-source { margin: 14px 14px 0; }
.frame-workbench { display: grid; gap: 14px; margin: 12px 14px 0; padding: 12px; border-radius: 10px; background: var(--bg); }
.overlay-upload { padding-top: 12px; border-top: 1px solid var(--line); }
.frame-photo-name { overflow: hidden; color: var(--hint); font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.overlay-name { margin-left: 50px; }
.gif-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.export-actions { display: flex; gap: 8px; }
.export-actions .primary-button, .export-actions .secondary-button { min-height: 40px; padding: 0 14px; font-size: 15px; }
.speed-control, .zoom-control { display: grid; align-items: center; gap: 6px 12px; color: var(--hint); font-size: 13px; }
.speed-control { grid-template-columns: auto minmax(80px, 1fr) auto; }
.zoom-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.zoom-control { grid-template-columns: minmax(0, 1fr) auto; }
.zoom-control > span { grid-column: 1 / -1; }
input[type=range] { width: 100%; margin: 0; accent-color: var(--accent); }
output { min-width: 54px; color: var(--text); text-align: right; }
.frame-palette { display: grid; gap: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.palette-heading { display: flex; justify-content: space-between; font-weight: 600; }
.palette-heading span:last-child { color: var(--hint); font-weight: 400; }
.color-swatches { display: grid; grid-auto-flow: column; grid-template-rows: repeat(3, 34px); grid-auto-columns: 34px; gap: 8px; overflow-x: auto; padding: 4px 4px 10px; touch-action: pan-x; scrollbar-width: thin; }
.gradient-swatches { grid-auto-columns: 84px; }
.color-swatch, .gradient-swatch { position: relative; padding: 0; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); }
.color-swatch { width: 30px; height: 30px; border-radius: 50%; background: var(--swatch-color); }
.gradient-swatch { width: 78px; height: 30px; border-radius: 15px; background: var(--swatch-gradient); }
.color-swatch.is-selected, .gradient-swatch.is-selected { box-shadow: 0 0 0 3px var(--accent); }
.color-swatch.is-selected::after, .gradient-swatch.is-selected::after { position: absolute; inset: 0; content: "\2713"; color: #fff; font-size: 14px; font-weight: 700; line-height: 26px; text-align: center; text-shadow: 0 1px 3px #000a; }
.color-swatch:disabled { cursor: not-allowed; opacity: .35; }
.repeat-control { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--hint); }
.frame-status { color: var(--accent); font-size: 14px; }
.frames-grid, .png-frames { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; padding: 14px; }
.frame-option, .png-frame-option { display: grid; justify-items: center; gap: 6px; padding: 10px 4px 8px; border: 0; border-radius: 12px; background: var(--bg); font-size: 13px; text-align: center; }
.frame-option.is-selected, .png-frame-option.is-selected { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft); }
.frame-swatch { display: block; width: 72px; height: 72px; border-radius: 50%; }
.png-thumb { display: block; width: 72px; height: 72px; overflow: hidden; border-radius: 50%; background: var(--tg-grad, linear-gradient(180deg, #4facfe, #2b6fd6)); }
.png-thumb img { display: block; width: 100%; height: 100%; }

.bio-head { display: flex; align-items: baseline; justify-content: space-between; padding: 14px 14px 6px; }
.bio-label { font-weight: 600; }
.char-count { color: var(--hint); font-size: 14px; }
.char-count.is-over-limit { color: var(--danger); font-weight: 600; }
#bio-input { display: block; width: calc(100% - 28px); min-height: 100px; margin: 0 14px; padding: 12px; resize: vertical; border: 0; border-radius: 10px; background: var(--bg); font-size: 16px; line-height: 1.5; }
.style-label, .symbol-label { margin: 18px 14px 8px; }
.style-options { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; padding: 0 14px; }
.style-option { display: grid; justify-items: center; gap: 2px; padding: 9px 2px; border: 0; border-radius: 10px; background: var(--bg); }
.style-option.is-selected { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft); }
.style-option > span { font-size: 18px; }
.style-option small { color: var(--hint); font-size: 12px; }
.symbol-options { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px; }
.symbol-options button { width: 40px; height: 38px; border: 0; border-radius: 10px; background: var(--bg); font-size: 17px; }
.symbol-options button:hover { background: var(--accent-soft); }
.bio-output-label { margin: 20px 14px 8px; color: var(--hint); font-size: 13px; }
.bio-output { min-height: 58px; margin: 0 14px; padding: 12px; border-radius: 10px; background: var(--bg); line-height: 1.5; overflow-wrap: anywhere; }

.preview-column { grid-column: 2; grid-row: 2; position: sticky; top: 16px; align-self: start; }
.preview-heading { margin: 0 4px 10px; font-size: 14px; font-weight: 400; color: var(--hint); }
.phone-shell { overflow: hidden; border: 6px solid #111; border-radius: 34px; background: var(--bg); }
.tg-header { padding-bottom: 14px; background: var(--tg-grad, linear-gradient(180deg, #4facfe, #2b6fd6)); color: #fff; text-align: center; transition: background .3s; }
.phone-top { display: flex; height: 30px; align-items: center; justify-content: space-between; padding: 0 20px; font-size: 12px; font-weight: 600; }
.phone-status { display: flex; align-items: center; gap: 3px; }
.phone-status i { width: 4px; height: 8px; border-radius: 1px; background: #fff; }
.phone-status i:nth-child(2) { height: 10px; }
.phone-status i:last-child { width: 14px; height: 7px; border-radius: 2px; }
.tg-nav { display: flex; justify-content: space-between; padding: 2px 14px 0; font-size: 14px; }
.avatar-wrap { width: 96px; aspect-ratio: 1; margin: 2px auto 8px; overflow: hidden; border-radius: 50%; background: #0003; }
.avatar-preview { display: block; width: 100%; height: 100%; }
.tg-header h3 { margin: 0; padding: 0 12px; font-size: 21px; font-weight: 600; overflow-wrap: anywhere; }
.tg-status { margin-top: 2px; font-size: 13px; opacity: .8; }
.tg-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 14px 10px 0; }
.tg-actions span { display: grid; justify-items: center; gap: 3px; padding: 8px 0 6px; border-radius: 12px; background: #ffffff2e; font-size: 11px; }
.tg-card { margin: 10px 8px 0; border-radius: 12px; background: var(--surface); }
.tg-row { padding: 9px 14px; }
.tg-row + .tg-row { border-top: 1px solid var(--line); }
.tg-row small { color: var(--hint); font-size: 12px; }
.profile-bio { min-height: 18px; font-size: 15px; line-height: 1.35; overflow-wrap: anywhere; white-space: pre-wrap; }
.profile-username { color: var(--accent); font-size: 15px; }
.tg-tabs { display: flex; justify-content: space-around; margin: 10px 8px 0; padding: 9px 0; border-radius: 12px 12px 0 0; background: var(--surface); color: var(--hint); font-size: 14px; }
.tg-tabs .is-on { color: var(--accent); font-weight: 600; }
.profile-gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; margin: 0 8px; background: var(--surface); }
.profile-gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; }
.profile-gallery img:nth-child(1) { object-position: left; }
.profile-gallery img:nth-child(3) { object-position: right; }
.phone-bottom { display: grid; height: 22px; place-items: center; }
.phone-bottom span { width: 80px; height: 4px; border-radius: 3px; background: var(--text); opacity: .8; }
.profile-fields { display: grid; gap: 10px; margin-top: 14px; padding: 12px; border-radius: 12px; background: var(--surface); }
.profile-fields label { display: grid; gap: 4px; }
.profile-fields label > span { color: var(--hint); font-size: 13px; }
.profile-fields input { width: 100%; height: 40px; padding: 0 12px; border: 0; border-radius: 10px; background: var(--bg); }
.username-input { position: relative; }
.username-input b { position: absolute; top: 9px; left: 12px; color: var(--hint); font-weight: 400; }
.username-input input { padding-left: 28px; }

@media (max-width: 900px) {
  .topbar { padding-inline: 14px; }
  .topbar-note { display: none; }
  .app-shell { display: flex; flex-direction: column; padding: 6px 12px calc(40px + env(safe-area-inset-bottom)); }
  .rail { max-width: none; margin-bottom: 12px; }
  /* Шапка профиля становится закреплённой полоской с живой аватаркой, остальное — карточки под формой */
  .preview-column, .phone-shell { display: contents; }
  .preview-heading, .phone-top, .tg-nav, .tg-actions, .phone-bottom { display: none; }
  .tg-header { order: 1; position: sticky; top: 0; z-index: 20; display: grid; grid-template-columns: 64px 1fr; grid-template-areas: "av name" "av status"; align-content: center; column-gap: 14px; margin: 0 -12px 14px; padding: 10px 16px; text-align: left; box-shadow: 0 2px 10px #0003; }
  .tg-header .avatar-wrap { grid-area: av; width: 64px; margin: 0; }
  .tg-header h3 { grid-area: name; align-self: end; padding: 0; font-size: 18px; }
  .tg-status { grid-area: status; align-self: start; margin: 0; }
  .workspace { order: 2; }
  .tg-card { order: 3; margin: 14px 0 0; }
  .tg-tabs { order: 4; margin: 14px 0 0; }
  .profile-gallery { order: 5; margin: 0; overflow: hidden; border-radius: 0 0 12px 12px; }
  .profile-fields { order: 6; }
}
@media (max-width: 560px) {
  .segment, .source-option, .rail-item { min-height: 40px; }
  .frames-grid, .png-frames { grid-template-columns: repeat(3, minmax(0, 1fr)); padding: 10px; }
  .frame-swatch, .png-thumb { width: 60px; height: 60px; }
  .style-options { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .zoom-controls { grid-template-columns: 1fr; }
  .gif-actions { flex-direction: column; align-items: stretch; }
  .export-actions > * { flex: 1; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* Не даём широким внутренним блокам (полоса цветов, ползунки) растягивать панель */
.frame-workbench, .frame-palette { grid-template-columns: minmax(0, 1fr); }
.frame-workbench > *, .frame-palette > *, .tool-panel, .workspace { min-width: 0; }
.palette-heading { gap: 12px; }
.gif-actions > :first-child { min-width: 0; flex: 1 1 auto; }
.export-actions { flex: 0 0 auto; }
.speed-control output { min-width: 0; white-space: nowrap; }
