:root {
    --bg: #09090b;
    --side: #0f0f12;
    --panel: #16161c;
    --elev: #1c1c24;
    --line: rgba(255, 255, 255, .08);
    --blue: #3d6bff;
    --blue2: #5b86ff;
    --text: #f4f4f5;
    --muted: #8b8b98;
    --danger: #f07178;
    --r: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; height: 100dvh; margin: 0; }
* {
    scrollbar-width: thin;
    scrollbar-color: #3a4468 transparent;
}
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: #0c0c10; border-radius: 10px; }
*::-webkit-scrollbar-thumb {
    background: linear-gradient(180deg, #4a5a88, #2c3554);
    border-radius: 10px;
    border: 2px solid #0c0c10;
}
*::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #5b86ff, #3d6bff); }
*::-webkit-scrollbar-corner { background: transparent; }

button, input, textarea { font: inherit; color: inherit; font-size: 16px; }
button { cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
body {
    font-family: Inter, ui-sans-serif, system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    letter-spacing: -.01em;
}
body.is-out { overflow: hidden; }
[hidden] { display: none !important; }

.login {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 30;
    place-items: center;
    background:
        radial-gradient(900px 500px at 50% -10%, rgba(61, 107, 255, .22), transparent 55%),
        var(--bg);
}
body.is-out .login { display: grid; }
body.is-in .login { display: none; }
.gate {
    width: min(380px, calc(100% - 32px));
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.logo {
    width: 190px;
    height: auto;
    margin: 0 auto;
    display: block;
}

.field { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.field-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--muted);
    padding-left: 4px;
}
.field-box {
    display: flex;
    align-items: center;
    gap: 6px;
    background: linear-gradient(180deg, #1a1a22, #14141a);
    border: 1px solid var(--line);
    border-radius: var(--r);
    padding: 2px 4px 2px 12px;
    min-height: 48px;
    transition: border-color .15s, box-shadow .15s;
}
.field-box.tall { align-items: stretch; padding: 8px 10px; min-height: 64px; }
.field-box.ghost-box {
    background: transparent;
    border-color: transparent;
    min-height: 40px;
    padding-left: 8px;
}
.field-box:focus-within {
    border-color: rgba(61, 107, 255, .7);
    box-shadow: 0 0 0 4px rgba(61, 107, 255, .16);
}
.ghost-box:focus-within { background: #14141c; }
.field-input {
    flex: 1;
    width: 100%;
    background: transparent;
    border: 0;
    outline: none;
    padding: 10px 4px;
    min-width: 0;
    appearance: none;
    resize: none;
    font-size: 16px;
}
.field-input::placeholder { color: #6a6a78; }
.field-eye {
    background: transparent;
    border: 0;
    color: var(--muted);
    width: 40px; height: 40px;
    border-radius: 10px;
    display: grid; place-items: center;
}
.field-eye:hover { color: #fff; background: rgba(255,255,255,.04); }
.field-inline { flex: 1; }
.field-grow { flex: 1; }

.btn-primary, .btn-new, .send-btn {
    border: 0;
    border-radius: var(--r);
    font-weight: 600;
    background: linear-gradient(180deg, var(--blue2), var(--blue));
    color: #fff;
    box-shadow: 0 8px 24px rgba(61, 107, 255, .28);
    transition: transform .12s, filter .12s;
}
.btn-primary { padding: 13px 16px; }
.btn-primary:hover, .btn-new:hover, .send-btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn-primary:disabled, .send-btn:disabled { opacity: .55; transform: none; }
.btn-ghost {
    background: #16161e;
    border: 1px solid var(--line);
    color: var(--muted);
    border-radius: 10px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 500;
}
.btn-ghost:hover { color: #fff; border-color: #3a3a48; }
.btn-ghost.danger { color: var(--danger); border-color: rgba(240, 113, 120, .28); background: rgba(240, 113, 120, .06); }
.err { color: var(--danger); font-size: 13px; text-align: center; margin: 0; }

.app { display: none; grid-template-columns: 268px 1fr; height: 100%; height: 100dvh; }
body.is-in .app { display: grid; }
.side-head { display: none; }
.menu-btn { display: none; }
.side-mask { display: none; }
.side {
    background: var(--side);
    border-right: 1px solid var(--line);
    display: flex; flex-direction: column;
    padding: 14px 12px; gap: 12px;
}
.btn-new {
    display: flex; align-items: center; justify-content: center;
    gap: 8px; height: 42px; width: 100%;
}
.btn-new span { font-size: 18px; line-height: 1; }
.chat-list { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-list button {
    text-align: left; background: transparent; border: 0;
    color: #c9c9d1; padding: 9px 10px; border-radius: 10px;
    font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-list button:hover { background: var(--elev); color: #fff; }
.chat-list button.active {
    background: #1a2238; color: #fff;
    box-shadow: inset 0 0 0 1px rgba(61, 107, 255, .35);
}
.side-foot {
    display: flex; align-items: center; justify-content: space-between;
    gap: 8px; padding-top: 10px; border-top: 1px solid var(--line);
}
.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: #243056; color: var(--blue2);
    display: grid; place-items: center;
    font-size: 13px; font-weight: 700; flex: 0 0 auto;
}
.icon-btn {
    background: #16161e; border: 1px solid var(--line);
    color: var(--muted); width: 36px; height: 36px; border-radius: 10px;
    display: grid; place-items: center;
}
.icon-btn:hover { color: #fff; border-color: #444; }

.main {
    display: grid;
    grid-template-rows: 56px 1fr auto;
    min-width: 0;
    overflow: hidden;
    background: linear-gradient(180deg, #0c0c10, #09090b);
}
.top {
    display: flex; align-items: center; gap: 12px;
    padding: 0 20px; border-bottom: 1px solid var(--line);
}
.chat-title { font-weight: 600; font-size: 15px; padding: 6px 4px; }

.thread {
    overflow: auto;
    min-width: 0;
    padding: 28px 7vw 16px;
    display: flex; flex-direction: column; gap: 22px;
    background-image:
        linear-gradient(rgba(255,255,255,.015) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255,255,255,.015) 1px, transparent 1px);
    background-size: 48px 48px;
}
.thread:empty::after {
    content: "Введите описание кадра";
    margin: auto; color: #5c5c68; font-size: 15px;
}
.msg { max-width: 880px; width: 100%; min-width: 0; align-self: center; }
.bubble {
    display: inline-block; max-width: 100%;
    background: var(--panel); border: 1px solid var(--line);
    border-radius: 14px; padding: 10px 14px;
    font-size: 14px; line-height: 1.5; white-space: pre-wrap;
    overflow-wrap: anywhere; word-break: break-word;
}
.msg.user { text-align: right; }
.msg.user .bubble { background: #182038; border-color: rgba(61, 107, 255, .25); text-align: left; }
.gallery {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px; margin-top: 12px;
    max-width: 100%;
    min-width: 0;
}
@media (min-width: 861px) {
    .gallery { grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)); }
}
.g-item {
    position: relative;
    border-radius: 14px;
    overflow: hidden;
    border: 1px solid var(--line);
    background: #111;
    cursor: zoom-in;
    max-width: 100%;
    min-width: 0;
}
.g-item img {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
}
.g-tools {
    position: absolute;
    right: 8px; bottom: 8px;
    display: flex; gap: 6px;
}
.g-tools button {
    width: 40px; height: 40px;
    border: 0; border-radius: 12px;
    background: rgba(12, 12, 18, .78);
    color: #fff;
    display: grid; place-items: center;
    backdrop-filter: blur(8px);
}
.g-tools button:hover { background: var(--blue); }

.lightbox {
    position: fixed; inset: 0; z-index: 80;
    display: grid;
    place-items: center;
    background: rgba(4, 4, 8, .92);
    backdrop-filter: blur(12px);
}
.lightbox[hidden] { display: none !important; }
.lb-x {
    position: absolute; top: max(12px, env(safe-area-inset-top)); right: 12px; z-index: 2;
    width: 44px; height: 44px; border: 0; border-radius: 12px;
    background: rgba(255,255,255,.08); color: #fff;
    display: grid; place-items: center;
}
.lightbox img {
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 140px);
    width: auto;
    height: auto;
    margin: auto;
    object-fit: contain;
    border-radius: 12px;
    user-select: none;
}
.lb-nav {
    position: absolute; top: 50%; transform: translateY(-50%);
    width: 48px; height: 64px; border: 0; border-radius: 14px;
    background: rgba(255,255,255,.08); color: #fff;
    font-size: 32px; line-height: 1;
}
.lb-nav#lb-prev { left: 12px; }
.lb-nav#lb-next { right: 12px; }
.lb-nav[hidden] { display: none !important; }
.lb-bar {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    display: flex; align-items: center; justify-content: center; gap: 16px;
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
    background: linear-gradient(transparent, rgba(0,0,0,.55));
}
.lb-bar .btn-primary { min-width: 160px; }
#lb-count { color: var(--muted); font-size: 13px; }
.status {
    font-size: 13px; color: var(--blue2); margin-top: 8px;
    display: flex; align-items: center; gap: 10px;
}
.status .ring {
    width: 16px; height: 16px; border-radius: 50%;
    border: 2px solid rgba(91, 134, 255, .25);
    border-top-color: var(--blue2);
    animation: spin .8s linear infinite;
    flex: 0 0 auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loader {
    position: fixed; inset: 0; z-index: 95;
    display: grid; place-items: center;
    background: rgba(6, 6, 10, .62);
    backdrop-filter: blur(8px);
}
.loader[hidden] { display: none !important; }
.loader-ring {
    width: 48px; height: 48px; border-radius: 50%;
    border: 3px solid rgba(91, 134, 255, .2);
    border-top-color: var(--blue2);
    animation: spin .8s linear infinite;
    margin: 0 auto 14px;
}
.loader p { margin: 0; color: #e8e8f0; font-size: 14px; text-align: center; }

.toast {
    position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
    z-index: 110;
    max-width: calc(100% - 24px);
    background: #2a1518;
    border: 1px solid rgba(240, 113, 120, .55);
    color: #ffd4d6;
    padding: 12px 18px;
    border-radius: 12px;
    font-size: 14px;
    box-shadow: 0 12px 40px rgba(0,0,0,.45);
    text-align: center;
}

.composer { padding: 0 7vw 22px; min-width: 0; }
.sheet {
    max-width: 880px; margin: 0 auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 12px;
    box-shadow: 0 16px 50px rgba(0, 0, 0, .35);
}
.settings { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }

.dd { position: relative; }
.dd-btn {
    display: flex; align-items: center; gap: 8px;
    min-height: 36px; padding: 0 10px 0 12px;
    background: linear-gradient(180deg, #22222c, #191920);
    border: 1px solid var(--line);
    border-radius: 999px;
    color: #e8e8f0;
    font-size: 12.5px; font-weight: 500;
}
.dd-btn:hover { border-color: #3d4560; }
.dd.open .dd-btn {
    border-color: rgba(61, 107, 255, .7);
    box-shadow: 0 0 0 3px rgba(61, 107, 255, .16);
}
.dd-val { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dd-frame-slot { display: grid; place-items: center; flex: 0 0 auto; }
.dd-chev { opacity: .55; transition: transform .15s; }
.dd.open .dd-chev { transform: rotate(180deg); }
.dd-menu {
    display: none;
    position: absolute; left: 0; bottom: calc(100% + 8px);
    min-width: 220px; max-height: 260px; overflow: auto;
    padding: 6px;
    background: #14141c;
    border: 1px solid rgba(255,255,255,.1);
    border-radius: 14px;
    box-shadow: 0 18px 50px rgba(0,0,0,.5);
    z-index: 40;
}
.dd-menu-sizes { min-width: 248px; max-height: 340px; }
.dd.open .dd-menu { display: block; }
.dd-item {
    display: flex; align-items: center; justify-content: space-between;
    gap: 10px;
    width: 100%; text-align: left;
    background: transparent; border: 0;
    color: #dddde6; padding: 9px 10px; border-radius: 10px;
    font-size: 13px;
}
.dd-item:hover { background: #1e1e28; }
.dd-item.is-on { background: #1a2238; color: #fff; }
.dd-item-text {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-width: 0;
}
.dd-item small { color: var(--muted); font-size: 11px; }
.size-frame {
    width: 34px;
    height: 34px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    background:
        linear-gradient(180deg, rgba(255,255,255,.04), transparent),
        #0b0b10;
    border: 1px dashed rgba(255, 255, 255, .14);
    border-radius: 6px;
}
.size-frame.is-sm {
    width: 20px;
    height: 20px;
    border-radius: 4px;
}
.size-frame-inner {
    display: block;
    max-width: 100%;
    max-height: 100%;
    background: linear-gradient(165deg, rgba(120, 158, 255, .92), rgba(61, 107, 255, .55));
    border: 1px solid rgba(180, 205, 255, .45);
    border-radius: 3px;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, .25);
}
.dd-item.is-on .size-frame-inner {
    background: linear-gradient(165deg, #8eb0ff, var(--blue));
    border-color: rgba(255, 255, 255, .45);
}

.switch {
    display: flex; align-items: center; gap: 8px;
    background: transparent; border: 0;
    color: var(--muted); font-size: 12.5px; padding: 4px 4px 4px 2px;
}
.switch-track {
    width: 38px; height: 22px; border-radius: 999px;
    background: #2a2a34; border: 1px solid var(--line);
    position: relative; flex: 0 0 auto;
    transition: background .15s, border-color .15s;
}
.switch-knob {
    position: absolute; top: 2px; left: 2px;
    width: 16px; height: 16px; border-radius: 50%;
    background: #c8c8d4;
    transition: transform .18s cubic-bezier(.2,.8,.2,1), background .15s;
}
.switch[aria-pressed="true"] { color: #fff; }
.switch[aria-pressed="true"] .switch-track {
    background: linear-gradient(180deg, var(--blue2), var(--blue));
    border-color: transparent;
}
.switch[aria-pressed="true"] .switch-knob { transform: translateX(16px); background: #fff; }

.send { display: flex; flex-direction: column; gap: 8px; }
.send-row { display: grid; grid-template-columns: 44px 1fr 128px; gap: 10px; align-items: stretch; }
.attach-btn { height: 100%; min-height: 64px; width: 44px; }
.attach-row {
    display: flex; align-items: center; gap: 10px;
    background: var(--elev); border: 1px solid var(--line);
    border-radius: 12px; padding: 6px 8px;
}
.attach-row img {
    width: 48px; height: 48px; object-fit: cover; border-radius: 8px;
    max-width: 48px;
}
.attach-row span { flex: 1; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.send-btn { margin: 0; height: 100%; min-height: 64px; }
.spin { opacity: .65; }

.modal {
    position: fixed; inset: 0; z-index: 50;
    display: grid; place-items: center;
    background: rgba(6, 6, 10, .62);
    backdrop-filter: blur(8px);
}
.modal-card {
    width: min(360px, calc(100% - 32px));
    background: #16161e;
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 22px;
    box-shadow: 0 24px 70px rgba(0,0,0,.5);
}
.modal-text { margin: 0 0 18px; font-size: 15px; line-height: 1.45; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 860px) {
    body.is-in .app { grid-template-columns: 1fr; }
    body.is-in.menu-open { overflow: hidden; }
    .menu-btn { display: grid; flex: 0 0 auto; }
    .side-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 4px;
    }
    .side-mask {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 44;
        background: rgba(0, 0, 0, .55);
    }
    body.menu-open .side-mask { display: block; }
    .side {
        position: fixed;
        z-index: 45;
        inset: 0 auto 0 0;
        width: min(86vw, 320px);
        transform: translateX(-105%);
        transition: transform .22s ease;
        padding-top: calc(12px + env(safe-area-inset-top));
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
        box-shadow: 12px 0 40px rgba(0,0,0,.4);
    }
    body.menu-open .side { transform: none; }
    .chat-list button { min-height: 48px; font-size: 15px; padding: 12px; }
    .btn-new, .icon-btn, .dd-btn, .btn-ghost, .btn-primary { min-height: 44px; }
    .icon-btn { width: 44px; height: 44px; }
    .top { padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); }
    .thread {
        padding: 16px 14px 12px;
        -webkit-overflow-scrolling: touch;
    }
    .composer {
        padding: 0 10px calc(10px + env(safe-area-inset-bottom));
    }
    .sheet { padding: 10px; border-radius: 16px; }
    .settings {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
        margin-bottom: 8px;
    }
    .dd { flex: 0 0 auto; }
    .dd-val { max-width: 42vw; }
    .dd-menu {
        position: fixed;
        left: 12px;
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom));
        top: auto;
        min-width: 0;
        max-height: min(50vh, 360px);
        z-index: 70;
    }
    .dd-item { min-height: 48px; font-size: 15px; }
    .size-frame { width: 38px; height: 38px; }
    .switch { min-height: 44px; flex: 0 0 auto; white-space: nowrap; }
    .send-row { grid-template-columns: 44px 1fr; }
    .send-btn { grid-column: 1 / -1; min-height: 52px; width: 100%; }
    .login { padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom); }
    .gate { width: min(380px, 100%); }
    .modal-actions { flex-direction: column-reverse; }
    .modal-actions .btn-primary,
    .modal-actions .btn-ghost { width: 100%; min-height: 48px; }
    .toast {
        bottom: calc(16px + env(safe-area-inset-bottom));
        width: calc(100% - 24px);
        text-align: center;
    }
    .lightbox img {
        max-width: calc(100vw - 16px);
        width: auto;
        height: auto;
        max-height: calc(100dvh - 160px);
    }
}
