/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-dark: #0f0f13;
    --bg-sidebar: #16161d;
    --bg-main: #1a1a24;
    --bg-card: #21212e;
    --bg-input: #2a2a3a;
    --bg-hover: #2e2e40;
    --border: #2e2e40;
    --border-light: #3a3a50;
    --text-primary: #e8e8f0;
    --text-secondary: #9999b0;
    --text-muted: #666680;
    --accent: #7c6bf0;
    --accent-hover: #6a58e0;
    --accent-glow: rgba(124, 107, 240, 0.15);
    --success: #4ade80;
    --warning: #fbbf24;
    --danger: #f87171;
    --info: #60a5fa;
    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
    --shadow: 0 2px 12px rgba(0,0,0,0.3);
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-display: 'Playfair Display', Georgia, serif;
}

/* ===== Light Theme ===== */
[data-theme="light"] {
    --bg-dark: #f5f5f7;
    --bg-sidebar: #ffffff;
    --bg-main: #f0f0f3;
    --bg-card: #ffffff;
    --bg-input: #f5f5f7;
    --bg-hover: #e8e8ed;
    --border: #d5d5dc;
    --border-light: #c5c5d0;
    --text-primary: #1a1a2e;
    --text-secondary: #555570;
    --text-muted: #8888a0;
    --accent: #6c5ce7;
    --accent-hover: #5a4bd6;
    --accent-glow: rgba(108, 92, 231, 0.1);
    --shadow: 0 2px 12px rgba(0,0,0,0.08);
}
[data-theme="light"] .message.user .msg-avatar {
    background: #e8e8ed;
    color: #555570;
}
[data-theme="light"] .message.ai .msg-avatar {
    background: linear-gradient(135deg, #6c5ce7, #a78bfa);
    color: #fff;
}

html, body {
    height: 100%;
    font-family: var(--font-sans);
    background: var(--bg-dark);
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.6;
    overflow: hidden;
}

/* ===== App Layout ===== */
#app {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* ===== Sidebar ===== */
#sidebar {
    width: 240px;
    min-width: 240px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 0;
}

.sidebar-header {
    padding: 24px 20px 20px;
}

.logo {
    font-family: var(--font-display);
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.5px;
}
.logo span {
    color: var(--accent);
}

.sidebar-nav {
    flex: 1;
    padding: 0 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-footer {
    padding: 10px;
    border-top: 1px solid var(--border);
}

.nav-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: 14px;
    font-family: var(--font-sans);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.15s;
    width: 100%;
    text-align: left;
}
.nav-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}
.nav-btn.active {
    background: var(--accent-glow);
    color: var(--accent);
}

/* ===== Main Content ===== */
#main-content {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-main);
}

.view {
    display: none;
    padding: 32px 40px;
    max-width: 960px;
    margin: 0 auto;
    animation: fadeIn 0.2s ease;
}
.view.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.view-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 28px;
    gap: 16px;
}
.view-header h2 {
    font-family: var(--font-display);
    font-size: 26px;
    font-weight: 600;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border: none;
    border-radius: var(--radius);
    font-family: var(--font-sans);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.btn-primary {
    background: var(--accent);
    color: #fff;
}
.btn-primary:hover {
    background: var(--accent-hover);
}
.btn-primary:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
.btn-ghost {
    background: none;
    color: var(--text-secondary);
}
.btn-ghost:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}
.btn-outline {
    background: none;
    color: var(--text-secondary);
    border: 1px dashed var(--border-light);
}
.btn-outline:hover {
    color: var(--accent);
    border-color: var(--accent);
}
.btn-sm {
    padding: 6px 10px;
    font-size: 13px;
}
.btn-lg {
    padding: 14px 28px;
    font-size: 16px;
}
.btn-danger {
    background: none;
    color: var(--danger);
}
.btn-danger:hover {
    background: rgba(248,113,113,0.1);
}

/* ===== Cards Grid ===== */
.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
}

.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}
.card:hover {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), var(--shadow);
    transform: translateY(-1px);
}
.session-card .card-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
}
.session-card .card-avatar svg {
    width: 20px;
    height: 20px;
}

.card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}
.card-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent-glow);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 600;
    color: var(--accent);
    flex-shrink: 0;
}
.card-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}
.card-subtitle {
    font-size: 12px;
    color: var(--text-muted);
}
.card-body {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.5;
}
.card-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid var(--border);
}

.card-tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 100px;
    font-size: 11px;
    font-weight: 500;
    background: var(--accent-glow);
    color: var(--accent);
}

/* ===== Empty State ===== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    color: var(--text-muted);
    text-align: center;
}
.empty-state svg {
    margin-bottom: 16px;
    opacity: 0.4;
}
.empty-state h3 {
    font-size: 18px;
    margin-bottom: 8px;
    color: var(--text-secondary);
}

/* ===== Forms ===== */
.form-container {
    max-width: 680px;
}

.form-section {
    margin-bottom: 32px;
}
.form-section h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.form-group {
    margin-bottom: 16px;
}
.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

input[type="text"],
input[type="number"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 14px;
    transition: border-color 0.15s;
}
input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
}
textarea {
    resize: vertical;
}
select {
    cursor: pointer;
}

.input-with-btn {
    display: flex;
    gap: 8px;
}
.input-with-btn input {
    flex: 1;
}

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

/* ===== Photo Analyze Zone ===== */
.photo-analyze-zone {
    border: 2px dashed var(--border-light);
    border-radius: var(--radius);
    padding: 20px;
    text-align: center;
    margin-bottom: 12px;
    transition: all 0.2s;
    background: var(--bg-input);
    position: relative;
}
.photo-analyze-zone.dragover {
    border-color: var(--accent);
    background: var(--accent-glow);
}
.photo-drop-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    color: var(--text-muted);
}
.photo-drop-content svg {
    opacity: 0.5;
}
.photo-drop-content p {
    font-size: 13px;
}
.link-btn {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    font-size: 13px;
    font-family: var(--font-sans);
    text-decoration: underline;
    padding: 0;
}
.link-btn:hover {
    color: var(--accent-hover);
}
.photo-url-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    width: 100%;
    max-width: 400px;
}
.photo-url-row input {
    flex: 1;
    padding: 6px 10px;
    font-size: 12px;
}
.photo-preview {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}
.photo-preview img {
    max-width: 200px;
    max-height: 200px;
    border-radius: var(--radius);
    object-fit: cover;
}
.btn-analyze-photo {
    width: 100%;
    max-width: 220px;
    justify-content: center;
}
.photo-remove {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--danger);
    color: #fff;
    border: none;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}
.photo-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    color: var(--text-secondary);
    font-size: 13px;
}

/* ===== NPC Form Block ===== */
.npc-block {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    position: relative;
}
.npc-block .form-row {
    margin-bottom: 0;
}
.npc-remove {
    position: absolute;
    top: 8px;
    right: 8px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
}
.npc-remove:hover {
    color: var(--danger);
    background: rgba(248,113,113,0.1);
}

/* ===== Game Launch ===== */
.launch-container {
    max-width: 680px;
}
.launch-step {
    margin-bottom: 32px;
}
.launch-step h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 14px;
    color: var(--text-secondary);
}
.launch-select-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 10px;
}
.launch-card {
    padding: 14px 16px;
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.15s;
}
.launch-card:hover {
    border-color: var(--border-light);
}
.launch-card.selected {
    border-color: var(--accent);
    background: var(--accent-glow);
}
.launch-card .card-title {
    font-size: 14px;
}
.launch-card .card-subtitle {
    font-size: 12px;
}
.launch-actions {
    text-align: center;
    padding-top: 20px;
}

/* ===== Play View (Chat) ===== */
#view-play {
    padding: 0;
    max-width: none;
    height: 100vh;
    display: none;
    flex-direction: column;
}
#view-play.active {
    display: flex;
}

.play-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-sidebar);
    flex-shrink: 0;
}
.play-title {
    flex: 1;
}
.play-title h2 {
    font-family: var(--font-display);
    font-size: 18px;
    font-weight: 600;
}
.play-subtitle {
    font-size: 12px;
    color: var(--text-muted);
}

.play-body {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Message bubbles */
.message {
    display: flex;
    gap: 12px;
    max-width: 85%;
    animation: msgIn 0.25s ease;
}
@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

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

.msg-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
}
.message.ai .msg-avatar {
    background: linear-gradient(135deg, #7c6bf0, #a78bfa);
    color: #fff;
}
.message.user .msg-avatar {
    background: var(--bg-hover);
    color: var(--text-secondary);
}

.msg-content {
    padding: 12px 16px;
    border-radius: var(--radius-lg);
    line-height: 1.65;
    font-size: 14px;
}
.message.ai .msg-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-primary);
}
.message.user .msg-content {
    background: var(--accent);
    color: #fff;
}

/* Game status blocks in messages */
.game-status-block {
    margin-top: 14px;
    padding: 12px 14px;
    background: var(--bg-input);
    border-left: 3px solid var(--accent);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    font-size: 13px;
    line-height: 1.6;
}
.game-status-block strong {
    color: var(--accent);
}

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 14px 18px;
}
.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* Message actions (delete, regenerate) */
.message {
    position: relative;
}
.msg-actions {
    position: absolute;
    top: 4px;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}
.message:hover .msg-actions {
    opacity: 1;
}
.message.ai .msg-actions {
    right: 4px;
}
.message.user .msg-actions {
    left: 4px;
}
.msg-action-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    padding: 0;
    line-height: 1;
}
.msg-action-btn:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}
.msg-delete:hover {
    background: #e74c3c;
    border-color: #e74c3c;
}
.msg-regen {
    position: relative;
}
.regen-menu {
    display: none;
    position: absolute;
    top: 34px;
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 8px 0;
    min-width: 200px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.3);
    z-index: 100;
}
.regen-menu.open {
    display: block;
}
.regen-menu-title {
    padding: 6px 14px;
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}
.regen-menu button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 8px 14px;
    border: none;
    background: none;
    color: var(--text-primary);
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
}
.regen-menu button:hover {
    background: var(--accent);
    color: white;
}

/* Chat input */
.chat-input-area {
    display: flex;
    gap: 10px;
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    background: var(--bg-sidebar);
    align-items: flex-end;
}
.chat-input-area textarea {
    flex: 1;
    padding: 12px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 14px;
    resize: none;
    max-height: 120px;
    line-height: 1.5;
}
.chat-input-area textarea:focus {
    outline: none;
    border-color: var(--accent);
}
.btn-send {
    padding: 12px 14px;
    border-radius: var(--radius-lg);
    flex-shrink: 0;
}
.btn-photo {
    padding: 12px 14px;
    border-radius: var(--radius-lg);
    flex-shrink: 0;
    background: var(--bg-input);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}
.btn-photo:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}
.btn-photo:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Scene photo thumbnail */
.scene-photo img {
    max-width: 200px !important;
    max-height: 200px;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    object-fit: cover;
}
.scene-photo img:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

/* Lightbox */
.photo-lightbox {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.9);
    z-index: 9999;
    justify-content: center;
    align-items: center;
    cursor: zoom-out;
}
.photo-lightbox.active {
    display: flex;
}
.photo-lightbox img {
    max-width: 90vw;
    max-height: 90vh;
    border-radius: 8px;
    object-fit: contain;
}

/* ===== Game Panel (right sidebar) ===== */
.game-panel {
    width: 280px;
    min-width: 280px;
    background: var(--bg-sidebar);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 20px 16px;
    transition: margin-right 0.3s;
}
.game-panel.hidden {
    display: none;
}

.panel-section {
    margin-bottom: 24px;
}
.panel-section h4 {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 10px;
}
.panel-content {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.6;
}

.context-tag {
    display: inline-block;
    padding: 4px 10px;
    background: var(--bg-hover);
    border-radius: 100px;
    font-size: 12px;
    font-weight: 500;
    margin-right: 6px;
    margin-bottom: 6px;
}

.npc-card-mini {
    padding: 10px 12px;
    background: var(--bg-card);
    border-radius: var(--radius);
    margin-bottom: 8px;
    border: 1px solid var(--border);
}
.npc-card-mini .npc-name {
    font-weight: 600;
    font-size: 13px;
    color: var(--text-primary);
}
.npc-card-mini .npc-desc {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
}
.npc-card-mini .npc-attitude {
    display: inline-block;
    margin-top: 4px;
    padding: 1px 8px;
    border-radius: 100px;
    font-size: 11px;
    font-weight: 600;
}
.attitude-positive { background: rgba(74,222,128,0.15); color: var(--success); }
.attitude-negative { background: rgba(248,113,113,0.15); color: var(--danger); }
.attitude-neutral { background: rgba(153,153,176,0.15); color: var(--text-muted); }
.attitude-interested { background: rgba(96,165,250,0.15); color: var(--info); }
.attitude-attraction { background: rgba(251,191,36,0.15); color: var(--warning); }

/* Player info in panel */
.player-stat {
    font-size: 12px;
    margin-bottom: 4px;
}
.player-stat strong {
    color: var(--text-primary);
}

/* ===== Settings ===== */
#view-settings .form-container {
    max-width: 500px;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #sidebar {
        width: 60px;
        min-width: 60px;
    }
    .sidebar-header {
        padding: 16px 10px;
    }
    .logo {
        font-size: 0;
    }
    .logo span {
        font-size: 18px;
    }
    .nav-btn {
        justify-content: center;
        padding: 10px;
        font-size: 0;
    }
    .nav-btn svg {
        margin: 0;
    }
    .view {
        padding: 20px 16px;
    }
    .form-row {
        grid-template-columns: 1fr;
    }
    .game-panel {
        display: none;
    }
    .chat-messages {
        padding: 16px;
    }
    .message {
        max-width: 95%;
    }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--border-light);
}
