:root {
    --bg: #0c0c10;
    --sidebar: #121218;
    --card: #1a1a22;
    --border: #2a2a35;
    --text: #f0f0f5;
    --text-muted: #8a8a9a;
    --accent: #6c5ce7;
    --accent-hover: #7f71ef;
    --danger: #e74c3c;
    --success: #2ecc71;
    --warning: #f39c12;
    --blue: #3498db;
    --radius: 12px;
    --chat-bg: linear-gradient(180deg, #0f0f13 0%, #1a1a22 100%);
    --msg-mine: #3d2f8c;
    --msg-other: #1e1e28;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); }

/* AUTH */
.auth-screen {
    position: fixed; inset: 0;
    display: flex; align-items: center; justify-content: center;
    background: radial-gradient(ellipse at 30% 20%, #1a1530 0%, var(--bg) 60%);
    z-index: 100;
    padding: 16px;
}
.auth-box {
    width: 100%; max-width: 360px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 32px 24px;
    text-align: center;
}
.auth-logo {
    width: 56px; height: 56px;
    margin: 0 auto 16px;
    background: linear-gradient(135deg, var(--accent), #a29bfe);
    border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
}
.auth-logo svg { width: 28px; height: 28px; color: #fff; }
.auth-box h1 { font-size: 24px; font-weight: 700; margin-bottom: 4px; }
.subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 24px; }
.auth-err { color: var(--danger); font-size: 13px; min-height: 20px; margin-bottom: 8px; }
.input-group {
    display: flex; align-items: center;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 12px;
    overflow: hidden;
}
.input-group .prefix {
    padding: 0 4px 0 14px;
    color: var(--text-muted);
    font-size: 15px;
}
.input-group input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text);
    padding: 12px 14px;
    font-size: 15px;
}
.auth-btn {
    width: 100%;
    padding: 13px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 4px;
}
.auth-btn:hover { background: var(--accent-hover); }
.auth-switch { margin-top: 18px; font-size: 14px; color: var(--text-muted); }
.auth-switch a { color: var(--accent); cursor: pointer; margin-left: 6px; }

/* FROZEN */
.frozen-banner {
    position: fixed; top: 0; left: 0; right: 0;
    background: #5c3a00;
    color: #ffeaa7;
    z-index: 90;
    padding: 10px 16px;
    font-size: 13px;
}
.frozen-content { display: flex; align-items: center; gap: 10px; justify-content: center; }
.frozen-content svg { width: 18px; height: 18px; flex-shrink: 0; }

/* LAYOUT */
.app-layout {
    display: flex;
    height: 100%;
    height: 100dvh;
}
.sidebar {
    width: 320px;
    min-width: 280px;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100%;
}
.sidebar-header { padding: 12px; border-bottom: 1px solid var(--border); }
.nav-tabs { display: flex; gap: 8px; }
.nav-tab {
    flex: 1;
    padding: 14px 10px;
    background: var(--card);
    border: 2px solid var(--border);
    color: var(--text);
    font-size: 15px;
    font-weight: 800;
    letter-spacing: 0.02em;
    border-radius: 12px;
    cursor: pointer;
    text-transform: none;
    box-shadow: 0 2px 0 rgba(0,0,0,0.25);
}
.nav-tab:hover { border-color: var(--accent); color: #fff; }
.nav-tab.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    box-shadow: 0 3px 0 #4a3db8;
}

.search-container { padding: 10px 12px; position: relative; }
.search-input-wrap {
    display: flex; align-items: center; gap: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0 12px;
}
.search-input-wrap svg { width: 16px; height: 16px; color: var(--text-muted); flex-shrink: 0; }
.search-input-wrap input {
    flex: 1; background: transparent; border: none; outline: none;
    color: var(--text); padding: 10px 0; font-size: 14px;
}
.search-result {
    position: absolute; left: 12px; right: 12px; top: 100%;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    z-index: 20;
    margin-top: 4px;
    overflow: hidden;
}
.search-hit {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px; cursor: pointer;
}
.search-hit:hover { background: var(--bg); }
.search-miss { padding: 12px; color: var(--text-muted); font-size: 13px; }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.chat-item {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 14px; cursor: pointer;
}
.chat-item:hover, .chat-item.active { background: var(--card); }
.avatar, .avatar-sm {
    width: 44px; height: 44px;
    border-radius: 50%;
    background: var(--accent);
    background-size: cover;
    background-position: center;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 16px; color: #fff;
    flex-shrink: 0;
}
.avatar-sm { width: 36px; height: 36px; font-size: 14px; }
.chat-info { flex: 1; min-width: 0; }
.chat-name { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.global-tag {
    font-size: 10px; font-weight: 500;
    background: var(--accent); color: #fff;
    padding: 1px 6px; border-radius: 6px;
}
.verified-badge { color: #3498db; vertical-align: middle; flex-shrink: 0; }

.sidebar-footer {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px;
    border-top: 1px solid var(--border);
    cursor: pointer;
}
.sidebar-footer:hover { background: var(--card); }
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 14px; font-weight: 600; }
.user-status { font-size: 12px; color: var(--success); }

/* MAIN */
.main-content { flex: 1; display: flex; flex-direction: column; height: 100%; min-width: 0; }
.view { flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.view.hidden { display: none !important; }

.chat-empty {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    color: var(--text-muted); gap: 8px;
}
.empty-icon svg { width: 48px; height: 48px; opacity: 0.4; }
.chat-empty h2 { font-size: 18px; color: var(--text); }
.chat-empty p { font-size: 14px; }

.chat-area { flex: 1; display: flex; flex-direction: column; height: 100%; }
.chat-header {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--sidebar);
}
.chat-header-info { flex: 1; cursor: pointer; min-width: 0; }
.chat-title { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 4px; }
.chat-status { font-size: 12px; color: var(--text-muted); }

.chat-messages {
    flex: 1; overflow-y: auto;
    padding: 16px;
    background: var(--chat-bg);
    display: flex; flex-direction: column; gap: 8px;
}
.msg-row { display: flex; gap: 8px; max-width: 85%; }
.msg-row.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: var(--accent);
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: 600; color: #fff;
    flex-shrink: 0; cursor: pointer; overflow: hidden;
}
.msg-avatar img { width: 100%; height: 100%; object-fit: cover; }
.msg-bubble {
    background: var(--msg-other);
    border-radius: 14px;
    padding: 8px 12px;
    max-width: 100%;
}
.msg-row.mine .msg-bubble { background: var(--msg-mine); }
.msg-author {
    font-size: 12px; font-weight: 600; margin-bottom: 2px;
    cursor: pointer; color: var(--accent);
    display: flex; align-items: center; gap: 4px;
}
.msg-login { font-weight: 400; color: var(--text-muted); font-size: 11px; }
.msg-text { font-size: 14px; line-height: 1.4; word-break: break-word; white-space: pre-wrap; }
.msg-time { font-size: 10px; color: var(--text-muted); margin-top: 4px; text-align: right; }
.msg-img { max-width: 220px; border-radius: 10px; display: block; cursor: pointer; }

.chat-input-area {
    display: flex; align-items: center; gap: 6px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    background: var(--sidebar);
}
.chat-input-area input {
    flex: 1; background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 10px 16px;
    color: var(--text); outline: none; font-size: 14px;
}
.icon-btn {
    width: 40px; height: 40px;
    border: none; background: transparent;
    color: var(--text-muted);
    border-radius: 50%;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--card); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }
.send-btn { color: var(--accent); }
.back-btn { display: none; }

/* PAGE HEADER */
.page-header {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--sidebar);
}
.page-header h2 { flex: 1; font-size: 16px; font-weight: 600; }
.btn-primary {
    background: var(--accent); color: #fff;
    border: none; border-radius: 8px;
    padding: 8px 14px; font-size: 13px; font-weight: 600;
    cursor: pointer;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
    background: transparent; color: var(--text-muted);
    border: 1px solid var(--border); border-radius: 8px;
    padding: 8px 14px; font-size: 13px; cursor: pointer;
}
.btn-red, .btn-green, .btn-blue, .btn-orange {
    border: none; border-radius: 6px;
    padding: 6px 10px; font-size: 12px; font-weight: 500;
    cursor: pointer; color: #fff;
}
.btn-red { background: var(--danger); }
.btn-green { background: var(--success); }
.btn-blue { background: var(--blue); }
.btn-orange { background: var(--warning); color: #1a1a1a; }

/* EDITOR */
.editor-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    max-width: 700px;
    margin: 0 auto;
    width: 100%;
}
.editor-title {
    width: 100%;
    background: transparent;
    border: none;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    font-size: 28px;
    font-weight: 700;
    padding: 10px 0;
    outline: none;
    margin-bottom: 20px;
}
.editor-toolbar { display: flex; gap: 8px; margin-bottom: 15px; flex-wrap: wrap; }
.editor-toolbar button {
    padding: 8px 12px;
    background: var(--sidebar);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
}
.editor-toolbar button:hover { background: var(--card); }
.editor-content {
    min-height: 400px;
    padding: 20px;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    border-radius: 12px;
    outline: none;
    font-size: 16px;
    line-height: 1.6;
}
.editor-content:empty:before {
    content: attr(data-placeholder);
    color: var(--text-muted);
}
.editor-content img { max-width: 100%; border-radius: 8px; margin: 8px 0; }
.publish-result { margin-top: 16px; font-size: 14px; color: var(--success); line-height: 1.5; }
.publish-result a { color: var(--accent); cursor: pointer; word-break: break-all; }

/* BG */
.bg-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px; padding: 16px; overflow-y: auto;
}
.bg-card {
    border: 2px solid var(--border);
    border-radius: 12px; overflow: hidden;
    cursor: pointer;
}
.bg-card.selected { border-color: var(--accent); }
.bg-preview { height: 80px; }
.bg-name { padding: 8px; font-size: 13px; text-align: center; background: var(--card); }
.admin-quick { display: flex; flex-wrap: wrap; gap: 8px; }

/* PROFILE */
.profile-view-container { flex: 1; overflow-y: auto; padding: 24px 16px; }
.profile-card { text-align: center; max-width: 360px; margin: 0 auto; }
.profile-avatar-lg {
    width: 96px; height: 96px; border-radius: 50%;
    background: var(--accent); background-size: cover; background-position: center;
    margin: 0 auto 16px;
    display: flex; align-items: center; justify-content: center;
    font-size: 36px; font-weight: 700; color: #fff;
}
.profile-card h2 { font-size: 22px; display: flex; align-items: center; justify-content: center; gap: 6px; }
.profile-login { color: var(--text-muted); font-size: 14px; margin: 4px 0 12px; }
.profile-about { font-size: 14px; color: var(--text-muted); margin-bottom: 12px; }
.official-badge {
    display: inline-block;
    background: rgba(52, 152, 219, 0.15);
    color: #3498db;
    font-size: 12px; font-weight: 600;
    padding: 4px 10px; border-radius: 20px;
    margin-bottom: 16px;
}
.profile-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.admin-profile-actions {
    display: flex; flex-wrap: wrap; gap: 6px;
    justify-content: center; margin-top: 12px;
}

/* ADMIN */
.admin-content { flex: 1; overflow-y: auto; padding: 12px; }
.admin-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 10px;
}
.admin-user { font-size: 14px; margin-bottom: 8px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.admin-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.status-tag {
    font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 500;
}
.status-active { background: rgba(46, 204, 113, 0.15); color: var(--success); }
.status-frozen { background: rgba(243, 156, 18, 0.15); color: var(--warning); }
.status-blocked { background: rgba(231, 76, 60, 0.15); color: var(--danger); }

/* MODAL */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex; align-items: center; justify-content: center;
    z-index: 200; padding: 16px;
}
.modal-box {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 24px;
    width: 100%; max-width: 420px;
    max-height: 80vh; overflow-y: auto;
    position: relative;
}
.modal-close {
    position: absolute; top: 12px; right: 14px;
    background: none; border: none; color: var(--text-muted);
    font-size: 22px; cursor: pointer;
}
.article-meta { font-size: 12px; color: var(--text-muted); margin: 8px 0 12px; }
.article-body { font-size: 14px; line-height: 1.6; }
.article-body img { max-width: 100%; border-radius: 8px; margin: 8px 0; }

/* TOAST */
.toast {
    position: fixed; bottom: 24px; left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 10px 20px;
    border-radius: 10px;
    font-size: 14px;
    z-index: 300;
    opacity: 0;
    transition: all 0.25s ease;
    pointer-events: none;
}
.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ========== RESPONSIVE ========== */

/* Tablet */
@media (max-width: 1024px) and (min-width: 769px) {
    .sidebar {
        width: 280px;
        min-width: 260px;
    }
    .msg-row { max-width: 78%; }
    .editor-container { padding: 16px; }
    .editor-title { font-size: 24px; }
}

/* Mobile + small tablet */
@media (max-width: 768px) {
    .app-layout {
        position: relative;
        overflow: hidden;
    }

    .sidebar {
        width: 100%;
        min-width: 0;
        max-width: none;
        position: absolute;
        inset: 0;
        z-index: 30;
        border-right: none;
        transition: transform 0.25s ease, visibility 0.25s;
        transform: translateX(0);
        visibility: visible;
        background: var(--sidebar);
    }

    .main-content {
        position: absolute;
        inset: 0;
        z-index: 10;
        width: 100%;
        min-width: 0;
    }

    /* Hide sidebar when chat / panel is open */
    body.chat-active .sidebar {
        transform: translateX(-105%);
        visibility: hidden;
        pointer-events: none;
    }
    body.chat-active .main-content {
        z-index: 40;
    }

    .back-btn {
        display: flex !important;
        flex-shrink: 0;
    }

    .chat-header {
        padding: 8px 10px;
        padding-top: max(8px, env(safe-area-inset-top));
        gap: 8px;
    }
    .chat-input-area {
        padding: 8px 10px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        gap: 4px;
    }
    .chat-input-area input {
        padding: 10px 14px;
        font-size: 16px; /* prevent iOS zoom */
    }
    .icon-btn {
        width: 44px;
        height: 44px;
        min-width: 44px;
    }
    .icon-btn svg { width: 22px; height: 22px; }

    .chat-messages {
        padding: 12px 10px;
        gap: 6px;
    }
    .msg-row { max-width: 92%; }
    .msg-img { max-width: min(220px, 70vw); }

    .page-header {
        padding: 10px 12px;
        padding-top: max(10px, env(safe-area-inset-top));
        gap: 8px;
    }
    .page-header h2 { font-size: 15px; }

    .editor-container {
        padding: 12px;
        max-width: none;
    }
    .editor-title {
        font-size: 22px;
        margin-bottom: 12px;
    }
    .editor-content {
        min-height: 240px;
        padding: 14px;
        font-size: 16px;
    }

    .profile-view-container {
        padding: 16px 12px;
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }
    .profile-avatar-lg {
        width: 80px;
        height: 80px;
        font-size: 30px;
    }
    .profile-card h2 { font-size: 18px; }

    .admin-content {
        padding: 10px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
    .admin-card { padding: 10px; }

    .sidebar-header { padding: 10px; padding-top: max(10px, env(safe-area-inset-top)); }
    .nav-tab {
        padding: 12px 8px;
        font-size: 14px;
    }
    .search-container { padding: 8px 10px; }
    .sidebar-footer {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }

    .chat-empty h2 { font-size: 16px; }
    .chat-empty p { font-size: 13px; padding: 0 16px; text-align: center; }

    .modal-box {
        max-width: 100%;
        margin: 0 8px;
        padding: 20px 16px;
        max-height: 85vh;
    }

    .auth-box {
        padding: 28px 20px;
        max-width: 100%;
    }
    .auth-box h1 { font-size: 22px; }

    /* Force views full screen on mobile */
    #createView.active,
    #profileView.active,
    #adminView.active,
    #modView.active {
        position: absolute;
        inset: 0;
        z-index: 45;
        background: var(--bg);
    }
    #chatView.active {
        position: absolute;
        inset: 0;
        z-index: 15;
    }
}

/* Very small phones */
@media (max-width: 380px) {
    .nav-tab { font-size: 13px; padding: 11px 6px; }
    .msg-bubble { padding: 7px 10px; }
    .msg-text { font-size: 13px; }
    .chat-title { font-size: 14px; }
}

.chat-item.panel-item .avatar {
    background: linear-gradient(135deg, #6c5ce7, #a29bfe);
}
.chat-item.panel-item:nth-of-type(2) .avatar {
    background: linear-gradient(135deg, #f39c12, #e67e22);
}
.admin-tag { background: #6c5ce7 !important; }
.mod-tag { background: #f39c12 !important; color: #1a1a1a !important; }

.msg-row.mine .msg-text { color: #fff; }

/* snowflake spin */
@keyframes snow-wobble {
    0% { transform: rotate(-18deg); }
    50% { transform: rotate(18deg); }
    100% { transform: rotate(-18deg); }
}
.snow-spin {
    display: inline-block;
    animation: snow-wobble 1.2s ease-in-out infinite;
    transform-origin: center;
}

.ghost-av, .ghost-av-sm {
    background: #2a2a35 !important;
    font-size: 18px !important;
}
.profile-avatar-lg.ghost-av {
    font-size: 42px !important;
    display: flex; align-items: center; justify-content: center;
}
.ghost-av-sm { font-size: 16px; }

.msg-undelivered {
    margin-top: 6px;
    font-size: 11px;
    color: #e74c3c;
    display: flex;
    align-items: center;
    gap: 4px;
}
.undelivered-bang {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px; height: 14px;
    border-radius: 50%;
    background: #e74c3c;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    flex-shrink: 0;
}

.article-404 {
    text-align: center;
    padding: 40px 16px;
}
.article-404-code {
    font-size: 72px;
    font-weight: 800;
    line-height: 1;
    margin-bottom: 16px;
    color: var(--text);
}
.article-404-text {
    font-size: 18px;
    font-weight: 700;
    line-height: 1.4;
    color: var(--text);
}

.report-meta { font-size: 13px; line-height: 1.6; }
.report-meta a { color: var(--accent); word-break: break-all; }
.frozen-status { color: var(--text-muted); font-size: 13px; margin-bottom: 12px; }
.frozen-item .chat-name { color: #9ec9ff; }

/* force views (desktop / base) */
#createView.active,
#bgView.active,
#profileView.active,
#adminView.active,
#modView.active {
    display: flex !important;
    position: relative;
    z-index: 25;
    background: var(--bg);
}
#chatView.active { display: flex !important; }

/* Mobile overrides must come last so they win */
@media (max-width: 768px) {
    #createView.active,
    #profileView.active,
    #adminView.active,
    #modView.active {
        position: absolute !important;
        inset: 0;
        z-index: 45;
        background: var(--bg);
    }
    #chatView.active {
        position: absolute !important;
        inset: 0;
        z-index: 15;
    }
    body.chat-active .sidebar {
        transform: translateX(-105%);
        visibility: hidden;
        pointer-events: none;
    }
    body.chat-active .main-content {
        z-index: 40;
    }
    .back-btn { display: flex !important; }
}
