:root {
    --bg: #111;
    --surface: #1a1a1a;
    --surface2: #222;
    --border: #2e2e2e;
    --orange: #ff6a00;
    --orange2: #ff9900;
    --orange-dim: #7a3200;
    --orange-glow: rgba(255, 106, 0, 0.15);
    --text: #f5e6d0;
    --text-dim: #8a7a6a;
    --text-muted: #3a3028;
    --r: 8px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: "Nunito", sans-serif;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

body::before {
    content: "";
    position: fixed;
    inset: 0;
    background-image:
        linear-gradient(rgba(255, 106, 0, 0.04) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 106, 0, 0.04) 1px, transparent 1px);
    background-size: 32px 32px;
    pointer-events: none;
    z-index: 0;
    animation: gridDriftX 60s ease-in-out infinite alternate, gridDriftY 43s ease-in-out infinite alternate;
}

@keyframes gridDriftX {
    0% { background-position-x: 0; }
    100% { background-position-x: 1024px; }
}

@keyframes gridDriftY {
    0% { background-position-y: 0; }
    100% { background-position-y: 1024px; }
}

button,
input,
select {
    font: inherit;
}

.debug-toggle,
.debug-console {
    position: fixed;
    z-index: 5000;
}

.debug-toggle {
    top: 12px;
    right: 12px;
    border: 1px solid var(--orange);
    background: rgba(17, 17, 17, 0.95);
    color: var(--orange2);
    border-radius: 999px;
    padding: 8px 12px;
    font-size: 0.75rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

.debug-console {
    top: 56px;
    right: 12px;
    width: min(420px, calc(100vw - 24px));
    max-height: min(62vh, 560px);
    display: flex;
    flex-direction: column;
    background: rgba(10, 10, 10, 0.97);
    border: 1px solid var(--orange);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.5);
}

.debug-console.is-hidden,
.debug-toggle.is-hidden {
    display: none;
}

.debug-console-top,
.debug-console-actions {
    display: flex;
    align-items: center;
}

.debug-console-top {
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    background: rgba(255, 106, 0, 0.12);
    border-bottom: 1px solid rgba(255, 106, 0, 0.22);
}

.debug-console-top strong {
    color: var(--text);
    font-size: 0.8rem;
}

.debug-console-actions {
    gap: 8px;
}

.debug-console-button {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-dim);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.72rem;
    cursor: pointer;
}

.debug-console-body {
    overflow: auto;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.72rem;
    line-height: 1.45;
    color: var(--text);
}

.debug-entry {
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    padding: 8px 9px;
    background: rgba(255, 255, 255, 0.03);
    white-space: pre-wrap;
    word-break: break-word;
}

.debug-entry strong {
    color: var(--orange2);
}

.topbar {
    position: relative;
    z-index: 50;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 11px 20px;
    background: rgba(17, 17, 17, 0.96);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
}

.topbar-left,
.topbar-right,
.chat-header,
.chat-header-actions,
.reply-bar,
.reply-bar-info,
.group-row,
.msg-info {
    display: flex;
    align-items: center;
}

.topbar-left,
.topbar-right,
.chat-header-actions {
    gap: 12px;
}

.app-title,
.sidebar-title,
.chat-title,
.btn-send,
.btn-cancel-reply,
.auth-kicker,
.auth-title {
    font-family: "Press Start 2P", monospace;
}

.app-title {
    font-size: 0.68rem;
    color: var(--orange);
    text-shadow: 0 0 10px var(--orange);
}

.app-subtitle,
.auth-copy,
.auth-status {
    font-size: 0.73rem;
    color: var(--text-dim);
    margin-top: 2px;
    font-weight: 700;
}

.topbar-link,
.user-pill,
.icon-button,
.sidebar-toggle,
.auth-field input,
.auth-field select,
.auth-submit,
.chat-input {
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: var(--surface);
    color: var(--text-dim);
}

.topbar-link,
.user-pill {
    padding: 7px 12px;
    font-size: 0.78rem;
    font-weight: 700;
}

.topbar-link,
.icon-button,
.sidebar-toggle,
.sidebar-close,
.btn-send,
.btn-cancel-reply,
.group-item,
.auth-submit {
    cursor: pointer;
    transition: 0.15s;
}

.topbar-link:hover,
.user-pill:hover,
.icon-button:hover,
.auth-submit:hover {
    border-color: var(--orange);
    color: var(--orange);
}

.user-pill {
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-avatar,
.msg-avatar {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    background: linear-gradient(135deg, var(--orange), var(--orange2));
    color: #1b0f06;
    font-weight: 800;
}

.user-avatar {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    font-size: 0.62rem;
}

.sidebar-toggle {
    display: none;
    padding: 8px 12px;
    color: var(--text);
    font-size: 1.2rem;
}

.sidebar-close {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
}

.chat-layout {
    display: flex;
    flex: 1;
    min-height: 0;
    position: relative;
    z-index: 1;
}

.sidebar {
    width: 320px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header,
.chat-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-title {
    font-size: 0.68rem;
    color: var(--text);
}

.sidebar-search {
    padding: 14px 14px 10px;
    border-bottom: 1px solid var(--border);
}

.sidebar-search-input,
.chat-input,
.auth-field input,
.auth-field select {
    width: 100%;
    min-width: 0;
    background: var(--bg);
    color: var(--text);
    outline: none;
}

.sidebar-search-input,
.auth-field input,
.auth-field select {
    padding: 11px 13px;
    font-size: 0.9rem;
}

.sidebar-search-input:focus,
.chat-input:focus,
.auth-field input:focus,
.auth-field select:focus {
    border-color: var(--orange);
    box-shadow: 0 0 10px var(--orange-glow);
}

.groups-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.group-item {
    width: 100%;
    border: 1px solid transparent;
    background: transparent;
    color: inherit;
    text-align: left;
    padding: 15px;
    border-radius: var(--r);
    margin-bottom: 8px;
}

.group-item:hover {
    background: var(--surface2);
}

.group-item.active {
    background: var(--orange-glow);
    border-color: var(--orange);
}

.group-main,
.chat-main,
.active-chat,
.msg-content,
.chat-header-main,
.empty-state {
    min-width: 0;
}

.group-main,
.msg-content,
.empty-state,
.auth-card {
    display: flex;
    flex-direction: column;
}

.group-main,
.msg-content {
    gap: 4px;
}

.group-row {
    justify-content: space-between;
    gap: 10px;
}

.group-name {
    font-weight: 800;
    font-size: 0.95rem;
    color: var(--text);
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.group-time,
.group-desc,
.chat-desc,
.msg-time,
.reply-preview-text,
.auth-copy,
.auth-status,
.empty-state p {
    color: var(--text-dim);
}

.group-time {
    flex-shrink: 0;
    font-size: 0.7rem;
    font-weight: 700;
}

.group-desc {
    font-size: 0.75rem;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.group-badge {
    min-width: 22px;
    height: 22px;
    border-radius: 999px;
    display: grid;
    place-items: center;
    background: var(--orange);
    color: #fff;
    font-size: 0.68rem;
    font-weight: 800;
    padding: 0 6px;
    flex-shrink: 0;
}

.group-badge.is-muted {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-dim);
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: rgba(0, 0, 0, 0.2);
}

.active-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-header {
    background: var(--surface);
    display: flex;
    justify-content: space-between;
    gap: 14px;
}

.chat-title {
    font-size: 0.76rem;
    color: var(--orange);
    line-height: 1.4;
}

.chat-desc {
    font-size: 0.85rem;
    font-weight: 600;
    margin-top: 6px;
    overflow-wrap: anywhere;
}

.icon-button {
    width: 36px;
    height: 36px;
    font-size: 1rem;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.messages-container.is-empty {
    justify-content: center;
}

.message {
    display: flex;
    gap: 12px;
    max-width: 85%;
}

.message.own {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    border: 1px solid var(--border);
    font-size: 0.68rem;
}

.message.own .msg-content {
    align-items: flex-end;
}

.msg-info {
    gap: 8px;
    font-size: 0.75rem;
    font-weight: 800;
}

.msg-author {
    color: var(--orange2);
}

.msg-time {
    font-weight: 600;
    font-size: 0.65rem;
}

.msg-bubble,
.empty-state {
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.msg-bubble {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.92rem;
    line-height: 1.4;
    color: var(--text);
    overflow-wrap: anywhere;
}

.empty-state {
    margin: auto;
    width: min(560px, 100%);
    padding: 22px;
    border-radius: 12px;
    gap: 10px;
}

.message.own .msg-bubble {
    background: var(--orange-glow);
    border-color: var(--orange);
}

.empty-state-title,
.auth-title {
    color: var(--text);
}

.reply-bar {
    background: var(--surface2);
    padding: 8px 15px;
    border-top: 1px solid var(--border);
    justify-content: space-between;
    gap: 14px;
    font-size: 0.8rem;
}

.reply-bar-info {
    gap: 8px;
    color: var(--text-dim);
    min-width: 0;
}

.reply-preview-text {
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-cancel-reply {
    background: transparent;
    border: none;
    color: var(--orange2);
    font-size: 0.5rem;
    flex-shrink: 0;
}

.chat-input-area {
    padding: 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 12px;
}

.chat-input {
    flex: 1;
    padding: 12px 15px;
    font-size: 0.95rem;
}

.btn-send {
    background: var(--orange);
    color: #fff;
    border: none;
    border-radius: var(--r);
    padding: 0 25px;
    font-size: 0.6rem;
    text-transform: uppercase;
}

.btn-send:hover {
    background: var(--orange2);
    transform: translateY(-2px);
    box-shadow: 0 4px 15px var(--orange-glow);
}

.btn-send:disabled,
.chat-input:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.auth-screen {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(0, 0, 0, 0.52);
    backdrop-filter: blur(10px);
}

.settings-screen {
    position: fixed;
    inset: 0;
    z-index: 180;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(8px);
}

.auth-screen.is-hidden {
    display: none;
}

.settings-screen.is-hidden {
    display: none;
}

.auth-card {
    width: min(420px, 100%);
    gap: 14px;
    padding: 24px;
    background: rgba(26, 26, 26, 0.96);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
}

.settings-card {
    width: min(460px, 100%);
    gap: 14px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    background: rgba(26, 26, 26, 0.98);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
}

.settings-header,
.settings-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.settings-close {
    border: none;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 1.1rem;
}

.auth-kicker {
    font-size: 0.52rem;
    color: var(--orange2);
}

.auth-title {
    font-size: 0.78rem;
    line-height: 1.5;
}

.auth-field {
    display: flex;
    flex-direction: column;
    gap: 7px;
    font-size: 0.82rem;
    font-weight: 700;
}

.auth-field.is-hidden,
.auth-field-extra.is-hidden {
    display: none;
}

.auth-submit {
    min-height: 42px;
}

.auth-switch {
    border: 0;
    background: transparent;
    color: var(--orange2);
    font-size: 0.8rem;
    font-weight: 800;
    text-align: left;
    padding-top: 2px;
    cursor: pointer;
}

.auth-switch:hover {
    color: var(--text);
}

.auth-status {
    min-height: 18px;
}

@media (max-width: 768px) {
    .topbar {
        padding: 10px;
    }

    .app-subtitle {
        display: none;
    }

    .topbar-right {
        gap: 8px;
    }

    .topbar-link,
    .user-pill {
        padding: 6px 8px;
        font-size: 0.7rem;
    }

    .sidebar-toggle,
    .sidebar-close {
        display: block;
    }

    .sidebar {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 1000;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 10px 0 30px rgba(0, 0, 0, 0.8);
        width: 85%;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .chat-header {
        padding: 12px 14px;
    }

    .chat-title {
        font-size: 0.64rem;
    }

    .chat-desc {
        font-size: 0.74rem;
    }

    .messages-container {
        padding: 15px 10px;
    }

    .message {
        max-width: 95%;
        gap: 8px;
    }

    .msg-avatar {
        width: 28px;
        height: 28px;
    }

    .msg-bubble,
    .empty-state {
        font-size: 0.85rem;
    }

    .chat-input-area {
        padding: 10px;
        gap: 8px;
    }

    .chat-input {
        padding: 10px;
        font-size: 0.85rem;
    }

    .btn-send {
        padding: 0 15px;
        font-size: 0.5rem;
    }
}

::-webkit-scrollbar {
    width: 5px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}
