:root {
    --bg: #111216;
    --panel: #181a20;
    --line: #262932;
    --text: #e8e9ee;
    --muted: #8b8f9c;
    --accent: #7c6cf0;
    --accent-text: #ffffff;
    --bubble-in: #1f222b;
    --bubble-out: #4b3fc0;
    --bubble-out-text: #f4f2ff;
    --code-bg: #0c0d10;
    --danger: #e5646b;
    --radius: 18px;
    color-scheme: dark;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg: #f3f3f6;
        --panel: #ffffff;
        --line: #e1e2e8;
        --text: #17181c;
        --muted: #6b6f7b;
        --bubble-in: #ffffff;
        --bubble-out: #6a5ae6;
        --code-bg: #f0f0f4;
        color-scheme: light;
    }
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
    margin: 0;
}

body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

[hidden] {
    display: none !important;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

/* ---------- вход: чёрная дыра ---------- */

/* Космос всегда тёмный, независимо от темы */
.login {
    position: fixed;
    inset: 0;
    overflow: hidden;
    background: #030307;
    color: #f3e9df;
}

.hole-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

.hole-button {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border: 0;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.hole-button:focus-visible {
    outline: 1px dashed rgba(255, 200, 160, 0.5);
    outline-offset: 6px;
}

.login.leaving .hole-button {
    pointer-events: none;
}

.hole-hint {
    position: absolute;
    left: 0;
    right: 0;
    bottom: max(14%, calc(env(safe-area-inset-bottom) + 48px));
    margin: 0;
    text-align: center;
    font-size: 12px;
    letter-spacing: 0.35em;
    text-transform: uppercase;
    color: rgba(255, 214, 180, 0.42);
    animation: hint 3.2s ease-in-out infinite;
    pointer-events: none;
}

@keyframes hint {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
}

.login-card {
    position: absolute;
    left: 50%;
    top: 50%;
    width: min(320px, calc(100% - 32px));
    display: grid;
    gap: 12px;
    padding: 22px;
    border-radius: 22px;
    background: rgba(8, 8, 14, 0.55);
    border: 1px solid rgba(255, 180, 130, 0.16);
    box-shadow: 0 0 60px rgba(255, 120, 50, 0.12), inset 0 0 30px rgba(255, 140, 70, 0.04);
    backdrop-filter: blur(14px) saturate(1.2);
    -webkit-backdrop-filter: blur(14px) saturate(1.2);
    /* Выплывает из горизонта */
    transform: translate(-50%, -50%) scale(0.15);
    opacity: 0;
    filter: blur(6px);
    transition: transform 0.55s cubic-bezier(0.2, 0.9, 0.25, 1.15), opacity 0.4s ease, filter 0.5s ease;
}

.login-card.open {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
    filter: none;
}

/* После верного пароля форма падает обратно в дыру */
.login.leaving .login-card {
    transform: translate(-50%, -50%) scale(0.05) rotate(25deg);
    opacity: 0;
    transition: transform 0.5s cubic-bezier(0.6, 0, 0.9, 0.4), opacity 0.45s ease-in;
}

.login-card.shake {
    animation: shake 0.42s;
}

@keyframes shake {
    0%, 100% { translate: 0; }
    20% { translate: -9px 0; }
    40% { translate: 8px 0; }
    60% { translate: -5px 0; }
    80% { translate: 3px 0; }
}

.login-card input,
.login-card button {
    width: 100%;
    padding: 12px 14px;
    border-radius: 12px;
    font: inherit;
}

.login-card input {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 200, 160, 0.18);
    color: #f6ede4;
    text-align: center;
    letter-spacing: 0.15em;
}

.login-card input::placeholder {
    color: rgba(255, 220, 190, 0.38);
    letter-spacing: 0.05em;
}

.login-card input:focus {
    outline: none;
    border-color: rgba(255, 170, 110, 0.65);
    box-shadow: 0 0 0 3px rgba(255, 140, 70, 0.15);
}

.login-card button {
    border: 0;
    background: linear-gradient(135deg, #ffb36b, #ff6a3d);
    color: #1a0c05;
    font-weight: 700;
    letter-spacing: 0.04em;
}

.login-card button:hover {
    filter: brightness(1.08);
}

.login-error {
    min-height: 1.4em;
    margin: 0;
    color: #ff8a7a;
    font-size: 14px;
    text-align: center;
}

.login-error:empty {
    display: none;
}

/* Чат проявляется из темноты после входа */
.app.arriving {
    animation: arrive 0.7s ease-out;
}

@keyframes arrive {
    from { opacity: 0; filter: blur(8px); transform: scale(0.97); }
    to { opacity: 1; filter: none; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
    .hole-hint { animation: none; opacity: 0.8; }
    .login-card, .login.leaving .login-card { transition: none; }
    .login-card.shake, .app.arriving { animation: none; }
}

/* ---------- каркас ---------- */

.app {
    height: 100dvh;
    display: grid;
    grid-template-rows: auto 1fr auto;
    max-width: 860px;
    margin: 0 auto;
    position: relative;
}

.top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    padding-top: max(10px, env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
    background: var(--panel);
}

.who {
    display: flex;
    align-items: center;
    gap: 12px;
}

.avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 700;
}

.name {
    font-weight: 600;
}

.status {
    font-size: 13px;
    color: var(--muted);
    min-height: 1.2em;
}

.icon-btn {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    flex: none;
}

.icon-btn:hover {
    background: var(--bubble-in);
    color: var(--text);
}

/* ---------- лента ---------- */

.feed {
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 12px 16px 4px;
}

.more {
    text-align: center;
    color: var(--muted);
    font-size: 13px;
    padding: 8px;
}

.day {
    text-align: center;
    color: var(--muted);
    font-size: 12px;
    margin: 16px 0 8px;
}

.msg {
    display: flex;
    margin: 4px 0;
}

.msg.user {
    justify-content: flex-end;
}

.msg.system {
    justify-content: center;
}

.bubble {
    max-width: min(78%, 620px);
    padding: 8px 12px 6px;
    border-radius: var(--radius);
    background: var(--bubble-in);
    border: 1px solid var(--line);
    overflow-wrap: anywhere;
}

.msg.user .bubble {
    background: var(--bubble-out);
    color: var(--bubble-out-text);
    border-color: transparent;
    border-bottom-right-radius: 6px;
}

.msg.assistant .bubble {
    border-bottom-left-radius: 6px;
}

.msg.pending {
    opacity: 0.6;
}

.bubble.error {
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
    font-size: 14px;
    display: flex;
    gap: 12px;
    align-items: center;
}

.retry {
    border: 1px solid currentColor;
    background: transparent;
    border-radius: 999px;
    padding: 2px 12px;
    color: inherit;
    white-space: nowrap;
    flex: none;
}

.text p {
    margin: 0 0 6px;
}

.text p:last-child {
    margin-bottom: 0;
}

.text ul, .text ol {
    margin: 4px 0 6px;
    padding-left: 22px;
}

.text blockquote {
    margin: 4px 0 6px;
    padding-left: 10px;
    border-left: 3px solid var(--accent);
    color: var(--muted);
}

.text a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.msg.assistant .text a {
    color: var(--accent);
}

.text code {
    font: 13px/1.4 ui-monospace, "Cascadia Mono", Consolas, monospace;
    background: var(--code-bg);
    padding: 1px 5px;
    border-radius: 5px;
}

.msg.user .text code {
    background: rgba(0, 0, 0, 0.25);
}

.text pre {
    margin: 6px 0;
    padding: 10px 12px;
    background: var(--code-bg);
    border-radius: 10px;
    overflow-x: auto;
}

.text pre code {
    padding: 0;
    background: none;
    white-space: pre;
}

.spoiler {
    background: currentColor;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
}

.spoiler.open {
    background: rgba(127, 127, 127, 0.2);
}

.meta {
    display: block;
    text-align: right;
    font-size: 11px;
    opacity: 0.6;
    margin-top: 2px;
}

.tools {
    display: grid;
    gap: 2px;
    margin-bottom: 4px;
}

.tools:empty {
    display: none;
}

.tool {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tool::before {
    content: "› ";
}

.images {
    display: grid;
    gap: 4px;
    margin: 2px -6px 6px;
    grid-template-columns: 1fr;
}

.images.n2, .images.n4 {
    grid-template-columns: 1fr 1fr;
}

.images.n3 {
    grid-template-columns: 1fr 1fr 1fr;
}

.images img {
    width: 100%;
    max-height: 340px;
    object-fit: cover;
    border-radius: 12px;
    display: block;
    cursor: zoom-in;
    background: var(--code-bg);
}

.images:not(.n1) img {
    aspect-ratio: 1;
}

.dots {
    display: inline-flex;
    gap: 4px;
    padding: 6px 0;
}

.dots i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted);
    animation: blink 1.2s infinite ease-in-out;
}

.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
    0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

/* ---------- ввод ---------- */

.composer {
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: var(--panel);
}

.row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
}

#input {
    flex: 1;
    min-width: 0;
    resize: none;
    border: 1px solid var(--line);
    border-radius: 20px;
    padding: 9px 14px;
    background: var(--bg);
    color: var(--text);
    font: inherit;
    max-height: 200px;
    overflow-y: hidden;
}

#input:focus {
    outline: none;
    border-color: var(--accent);
}

.send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 0;
    background: var(--accent);
    color: var(--accent-text);
    display: grid;
    place-items: center;
    flex: none;
}

.send:disabled {
    opacity: 0.5;
    cursor: default;
}

.attachments {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.attachments:not(:empty) {
    margin-bottom: 8px;
}

.thumb {
    position: relative;
    width: 64px;
    height: 64px;
}

.thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 10px;
}

.thumb .remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 0;
    background: var(--text);
    color: var(--bg);
    line-height: 1;
    font-size: 15px;
}

.drop {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    border: 2px dashed var(--accent);
    border-radius: 16px;
    font-size: 18px;
    pointer-events: none;
}

.viewer {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.9);
    display: grid;
    place-items: center;
    cursor: zoom-out;
    z-index: 10;
}

.viewer img {
    max-width: 100vw;
    max-height: 100dvh;
    object-fit: contain;
}

.viewer-link {
    position: fixed;
    bottom: max(16px, env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    padding: 8px 16px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    text-decoration: none;
    font-size: 14px;
    cursor: pointer;
}

/* ---------- арты в сообщениях ---------- */

.art {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    margin: 6px -6px 4px;
    width: min(520px, 70vw);
}

.art.n1 { grid-template-columns: 1fr; width: min(320px, 70vw); }
.art.n2 { grid-template-columns: repeat(2, 1fr); width: min(400px, 70vw); }
.art.n3 { grid-template-columns: repeat(3, 1fr); }

.art-card {
    position: relative;
    display: block;
    aspect-ratio: 3 / 4;
    border-radius: 10px;
    overflow: hidden;
    background: var(--code-bg);
}

.art.n1 .art-card {
    aspect-ratio: auto;
}

.art-card img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.art.n1 .art-card img {
    height: auto;
    max-height: 420px;
    object-fit: contain;
}

.art-card:hover img {
    filter: brightness(1.08);
}

.rating {
    position: absolute;
    top: 4px;
    left: 4px;
    font-size: 10px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 4px;
    color: #fff;
    background: rgba(0, 0, 0, 0.55);
}

.rating.r-E { background: #c0392b; }
.rating.r-Q { background: #d68910; }

.play {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 28px;
    text-shadow: 0 0 8px rgba(0, 0, 0, 0.8);
}

@media (max-width: 560px) {
    .art { width: 72vw; grid-template-columns: repeat(2, 1fr); }
    .art.n1 { width: 72vw; }
}

/* ---------- память ---------- */

.top-actions {
    display: flex;
    gap: 4px;
}

.panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(440px, 100%);
    background: var(--panel);
    border-left: 1px solid var(--line);
    box-shadow: -12px 0 32px rgba(0, 0, 0, 0.25);
    display: grid;
    grid-template-rows: auto 1fr;
    z-index: 5;
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px 10px 16px;
    padding-top: max(10px, env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
}

.panel-head h2 {
    margin: 0;
    font-size: 17px;
}

.panel-head .icon-btn {
    font-size: 24px;
}

.panel-body {
    overflow-y: auto;
    padding: 4px 16px 24px;
}

.panel-body h3 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin: 18px 0 8px;
}

.panel-row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin: 8px 0;
}

.panel-row .hint {
    margin-right: auto;
}

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

.btn {
    border: 1px solid var(--line);
    background: var(--bg);
    border-radius: 10px;
    padding: 6px 12px;
    white-space: nowrap;
}

.btn:hover:not(:disabled) {
    border-color: var(--accent);
}

.btn.primary {
    background: var(--accent);
    border-color: transparent;
    color: var(--accent-text);
}

.btn:disabled {
    opacity: 0.5;
    cursor: default;
}

.panel textarea,
.panel input,
.panel select {
    font: inherit;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px 10px;
}

.panel textarea {
    width: 100%;
    resize: vertical;
}

.panel textarea:focus,
.panel input:focus,
.panel select:focus {
    outline: none;
    border-color: var(--accent);
}

.persona-name {
    width: 100%;
    margin-bottom: 8px;
}

.filters .search {
    flex: 1;
    min-width: 0;
}

.kind-filter {
    max-width: 150px;
}

.journal {
    display: grid;
    gap: 6px;
}

.journal-item {
    font-size: 13px;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 6px 10px;
}

.journal-item summary {
    cursor: pointer;
}

.journal-item .tool {
    white-space: normal;
    margin-top: 4px;
}

.journal-item.checkin {
    display: flex;
    gap: 6px;
    align-items: baseline;
}

.journal-item.checkin span:nth-child(2) {
    flex: 1;
}

.decision {
    font-weight: 600;
}

.door-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 2px 0;
}

.d-wrote { color: #6cc788; }
.d-silent { color: var(--muted); }
.d-skipped { color: #d68910; }

.memory-list {
    display: grid;
    gap: 8px;
    margin-top: 6px;
}

.memory {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px 10px;
}

.memory-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
}

.memory-text {
    margin: 6px 0 2px;
    overflow-wrap: anywhere;
}

.memory-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

.link-btn {
    border: 0;
    background: none;
    padding: 0;
    font-size: 12px;
    color: var(--muted);
}

.link-btn:hover {
    color: var(--text);
}

.link-btn.danger:hover {
    color: var(--danger);
}

.chip {
    font-size: 11px;
    padding: 1px 8px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--chip) 22%, transparent);
    color: color-mix(in srgb, var(--chip) 75%, var(--text));
    --chip: var(--muted);
}

.kind-fact { --chip: #5aa9e6; }
.kind-preference { --chip: #e6a35a; }
.kind-event { --chip: #6cc788; }
.kind-observation { --chip: #b27cf0; }
.kind-self { --chip: #e66c9c; }

.dots-imp {
    font-size: 9px;
    letter-spacing: 1px;
    color: var(--accent);
}

.memory-edit {
    border: 1px solid var(--accent);
    border-radius: 12px;
    padding: 8px;
}

.memory-edit .panel-row {
    flex-wrap: wrap;
}

.memory-edit select {
    flex: 1;
    min-width: 0;
}

.form-error {
    color: var(--danger);
    font-size: 13px;
}

.form-error:empty {
    display: none;
}

@media (max-width: 560px) {
    .bubble {
        max-width: 88%;
    }
}
