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

:root {
    --bg: #0f0a1a;
    --surface: #1a1228;
    --surface-2: #241a36;
    --border: rgba(255, 255, 255, 0.08);
    --text: #f5f0ff;
    --muted: rgba(245, 240, 255, 0.5);
    --accent: #f093fb;
    --accent-2: #f5576c;
    --own-bubble: linear-gradient(135deg, #667eea, #764ba2);
    --other-bubble: #2a2038;
    --online: #2ed573;
    --offline: #ffa502;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

.app {
    height: 100%;
    max-width: 720px;
    margin: 0 auto;
    background: var(--surface);
    box-shadow: 0 0 60px rgba(0, 0, 0, 0.4);
}

.screen {
    display: none;
    height: 100%;
    flex-direction: column;
}

.screen.active {
    display: flex;
}

/* Setup */
.setup-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 32px 24px;
    text-align: center;
    background:
        radial-gradient(circle at 50% 0%, rgba(240, 147, 251, 0.15), transparent 55%),
        var(--bg);
}

.app-icon {
    font-size: 4rem;
    margin-bottom: 16px;
    filter: drop-shadow(0 8px 24px rgba(240, 147, 251, 0.35));
}

.app-title {
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 8px;
}

.app-tagline {
    color: var(--muted);
    font-size: 0.95rem;
    margin-bottom: 32px;
}

.setup-label {
    display: block;
    width: 100%;
    max-width: 320px;
    text-align: left;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 8px;
}

.setup-input {
    width: 100%;
    max-width: 320px;
    padding: 14px 16px;
    border-radius: 14px;
    border: 2px solid rgba(240, 147, 251, 0.25);
    background: rgba(0, 0, 0, 0.35);
    color: var(--text);
    font-size: 1rem;
    margin-bottom: 16px;
}

.setup-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(240, 147, 251, 0.15);
}

.btn {
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-weight: 700;
    transition: transform 0.15s, opacity 0.15s;
}

.btn:active {
    transform: scale(0.97);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    width: 100%;
    max-width: 320px;
    padding: 14px 24px;
    border-radius: 14px;
    font-size: 1rem;
    color: #fff;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    box-shadow: 0 8px 24px rgba(245, 87, 108, 0.35);
}

.btn-primary:hover:not(:disabled) {
    box-shadow: 0 12px 32px rgba(245, 87, 108, 0.45);
}

.back-link {
    margin-top: 24px;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
}

.back-link:hover {
    color: var(--accent);
}

/* Header */
.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    color: var(--muted);
    text-decoration: none;
    font-size: 1.2rem;
    flex-shrink: 0;
}

.header-back:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
}

.header-info {
    flex: 1;
    min-width: 0;
}

.header-title {
    font-size: 1rem;
    font-weight: 700;
}

.header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--muted);
    margin-top: 2px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted);
    flex-shrink: 0;
}

.status-dot.status-online {
    background: var(--online);
    box-shadow: 0 0 8px rgba(46, 213, 115, 0.6);
}

.status-dot.status-offline {
    background: var(--offline);
}

.header-user {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px 6px 6px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.04);
    color: var(--text);
    cursor: pointer;
    max-width: 140px;
}

.header-user:hover {
    background: rgba(255, 255, 255, 0.08);
}

.user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    flex-shrink: 0;
}

.user-name {
    font-size: 0.8rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Messages */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background:
        radial-gradient(circle at 100% 0%, rgba(102, 126, 234, 0.08), transparent 40%),
        var(--bg);
    -webkit-overflow-scrolling: touch;
}

.chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    text-align: center;
    gap: 12px;
    padding: 40px 20px;
}

.chat-empty span {
    font-size: 3rem;
    opacity: 0.5;
}

.chat-empty p {
    line-height: 1.5;
    font-size: 0.95rem;
}

.chat-day {
    align-self: center;
    padding: 6px 14px;
    margin: 12px 0 8px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 600;
}

.chat-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 85%;
    margin-bottom: 4px;
}

.chat-row-own {
    align-self: flex-end;
    flex-direction: row-reverse;
    max-width: 80%;
}

.chat-row-grouped {
    margin-top: -2px;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 700;
    background: var(--other-bubble);
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.msg-avatar-spacer {
    width: 32px;
    flex-shrink: 0;
}

.msg-bubble-wrap {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.chat-row-own .msg-bubble-wrap {
    align-items: flex-end;
}

.msg-author {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--accent);
    padding-left: 4px;
}

.msg-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.45;
    word-break: break-word;
    white-space: pre-wrap;
    background: var(--other-bubble);
    border: 1px solid var(--border);
}

.msg-bubble-emoji {
    font-size: 2.4rem;
    line-height: 1.2;
    padding: 8px 12px;
    letter-spacing: 0.05em;
}

.msg-bubble-custom {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
}

.custom-emoji-img {
    display: inline-block;
    vertical-align: middle;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    object-fit: contain;
}

.custom-emoji-missing {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 6px;
}

.chat-row-own .msg-bubble {
    background: var(--own-bubble);
    border: none;
    border-bottom-right-radius: 6px;
}

.chat-row:not(.chat-row-own):not(.chat-row-grouped) .msg-bubble {
    border-bottom-left-radius: 6px;
}

.msg-time {
    font-size: 0.68rem;
    color: var(--muted);
    padding: 0 4px;
}

/* Compose */
.chat-compose {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    padding-bottom: max(12px, var(--safe-bottom));
    background: var(--surface-2);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    position: relative;
}

.btn-emoji {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.05);
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: pointer;
    transition: background 0.15s, transform 0.15s;
}

.btn-emoji:hover,
.btn-emoji.active {
    background: rgba(240, 147, 251, 0.15);
    border-color: rgba(240, 147, 251, 0.35);
}

.emoji-picker {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: calc(100% + 8px);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.35);
    overflow: hidden;
    z-index: 20;
}

.emoji-picker.hidden {
    display: none;
}

.emoji-tabs {
    display: flex;
    gap: 4px;
    padding: 8px 8px 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.emoji-tab {
    border: none;
    background: transparent;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 8px 10px;
    border-radius: 8px 8px 0 0;
    cursor: pointer;
    white-space: nowrap;
    font-family: inherit;
}

.emoji-tab.active {
    color: var(--accent);
    background: rgba(240, 147, 251, 0.1);
}

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 2px;
    padding: 8px;
    max-height: 180px;
    overflow-y: auto;
}

.emoji-btn {
    border: none;
    background: transparent;
    font-size: 1.45rem;
    line-height: 1;
    padding: 8px 4px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.12s, transform 0.12s;
}

.emoji-btn:hover {
    background: rgba(255, 255, 255, 0.08);
    transform: scale(1.15);
}

.emoji-btn:active {
    transform: scale(0.95);
}

.emoji-btn-custom {
    padding: 4px;
}

.emoji-create-btn {
    background: rgba(240, 147, 251, 0.12);
    border: 2px dashed rgba(240, 147, 251, 0.45);
    font-size: 1.6rem;
}

.custom-emoji-cell {
    position: relative;
}

.emoji-delete-btn {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 18px;
    height: 18px;
    border: none;
    border-radius: 50%;
    background: #ff4757;
    color: #fff;
    font-size: 0.85rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
}

.custom-emoji-cell:hover .emoji-delete-btn {
    opacity: 1;
}

.emoji-grid-hint {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--muted);
    font-size: 0.82rem;
    padding: 16px 8px;
}

/* Emoji creator */
.emoji-creator {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.emoji-creator.hidden {
    display: none;
}

.emoji-creator-panel {
    width: 100%;
    max-width: 360px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.creator-title {
    font-size: 1.2rem;
    margin-bottom: 4px;
}

.creator-hint {
    color: var(--muted);
    font-size: 0.82rem;
    margin-bottom: 16px;
}

.creator-canvas-wrap {
    display: flex;
    justify-content: center;
    margin-bottom: 14px;
}

#creatorCanvas {
    width: 192px;
    height: 192px;
    border-radius: 16px;
    border: 2px solid var(--border);
    background:
        linear-gradient(45deg, rgba(255,255,255,0.06) 25%, transparent 25%),
        linear-gradient(-45deg, rgba(255,255,255,0.06) 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, rgba(255,255,255,0.06) 75%),
        linear-gradient(-45deg, transparent 75%, rgba(255,255,255,0.06) 75%);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
    background-color: transparent;
    touch-action: none;
    cursor: crosshair;
}

.creator-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

.creator-name {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.25);
    color: var(--text);
    font-family: inherit;
    margin-bottom: 12px;
}

.creator-tools {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 12px;
}

.creator-colors {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.creator-color {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
}

.creator-color.active {
    border-color: #fff;
    box-shadow: 0 0 0 2px var(--accent);
}

.creator-brushes {
    display: flex;
    gap: 8px;
}

.creator-tool {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.05);
    font-size: 1.1rem;
    cursor: pointer;
}

.creator-tool.active {
    border-color: var(--accent);
    background: rgba(240, 147, 251, 0.15);
}

.creator-share {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 16px;
    cursor: pointer;
}

.creator-actions {
    display: flex;
    gap: 10px;
}

.btn-secondary {
    flex: 1;
    padding: 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text);
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.creator-actions .btn-primary {
    flex: 1.4;
    max-width: none;
}

.compose-input {
    flex: 1;
    padding: 12px 16px;
    border-radius: 24px;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.25);
    color: var(--text);
    font-size: 1rem;
    font-family: inherit;
}

.compose-input:focus {
    outline: none;
    border-color: rgba(240, 147, 251, 0.4);
    box-shadow: 0 0 0 3px rgba(240, 147, 251, 0.1);
}

.btn-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    flex-shrink: 0;
    box-shadow: 0 4px 16px rgba(245, 87, 108, 0.35);
}

.btn-send:hover:not(:disabled) {
    box-shadow: 0 6px 20px rgba(245, 87, 108, 0.5);
}

@media (max-width: 480px) {
    .app {
        max-width: none;
        box-shadow: none;
    }

    .chat-row {
        max-width: 92%;
    }

    .header-user .user-name {
        display: none;
    }

    .header-user {
        padding: 6px;
        max-width: none;
    }

    .emoji-grid {
        grid-template-columns: repeat(6, 1fr);
    }
}
