:root {
    --bg: #131417;
    --surface: #1b1d22;
    --surface-2: #22242b;
    --border: #33353d;
    --text: #f2f2f4;
    --text-muted: #97999f;
    --accent: #7c6cf0;
    --accent-hover: #8f81f3;
    --success: #22c55e;
    --danger: #ef4444;
    --danger-hover: #f56565;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

h1, h2, h3 {
    margin: 0 0 12px;
    color: var(--text);
}

a {
    color: var(--accent);
}

p {
    color: var(--text-muted);
    line-height: 1.5;
}

label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

input[type="text"],
input[type="password"],
input[type="number"],
textarea,
select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.15s ease;
}

input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--accent);
}

textarea {
    min-height: 90px;
    resize: vertical;
}

button {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease;
}

button:hover {
    background: var(--accent-hover);
}

button:active {
    transform: scale(0.98);
}

button:disabled {
    background: var(--surface-2);
    color: var(--text-muted);
    cursor: default;
    transform: none;
}

button.secondary {
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text);
}

button.secondary:hover {
    background: var(--surface);
}

button.danger {
    background: transparent;
    border: 1px solid var(--danger);
    color: var(--danger);
}

button.danger:hover {
    background: var(--danger);
    color: #fff;
}

/* Page shell */
header.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

header.app-header h1 {
    font-size: 18px;
    font-weight: 700;
}

/* Brand */
.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--text);
}

.brand-mark {
    width: 32px;
    height: 32px;
    flex-shrink: 0;
}

.brand-name {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.01em;
    white-space: nowrap;
}

.app-nav {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
}

.app-nav a {
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    text-decoration: none;
    font-size: 14px;
    font-weight: 600;
}

.app-nav a:hover {
    background: var(--surface-2);
    color: var(--text);
}

.app-nav a.current {
    background: var(--surface-2);
    color: var(--text);
}

@media (max-width: 700px) {
    .brand-name { display: none; }
    header.app-header { padding: 10px 14px; }
}

.app-header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

main.app-main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 28px 24px 60px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
}

.section {
    margin-bottom: 28px;
}

/* Nav avatar dropdown */
.nav-avatar-container {
    position: relative;
}

.nav-avatar-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 13px;
    border: none;
    cursor: pointer;
    padding: 0;
}

.nav-dropdown {
    display: none;
    flex-direction: column;
    position: absolute;
    right: 0;
    top: 46px;
    min-width: 200px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    z-index: 30;
}

.nav-dropdown.open {
    display: flex;
}

.nav-dropdown a,
.nav-dropdown button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 11px 16px;
    background: none;
    color: var(--text);
    border: none;
    border-radius: 0;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
}

.nav-dropdown a:hover,
.nav-dropdown button:hover {
    background: var(--surface);
}

.nav-dropdown hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 4px 0;
}

/* Voice/persona card grid */
.voice-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
}

.voice-card {
    position: relative;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: border-color 0.15s ease, transform 0.1s ease;
}

.voice-card:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.voice-card.active {
    border-color: var(--success);
}

.voice-card-image {
    width: 100%;
    height: 140px;
    background: var(--surface-2) center/cover no-repeat;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 13px;
}

.voice-card-body {
    padding: 12px;
}

.voice-card-name {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.voice-card-badge {
    display: inline-block;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    padding: 3px 8px;
    border-radius: 999px;
    background: rgba(34, 197, 94, 0.15);
    color: var(--success);
}

.voice-card-badge.global {
    background: rgba(124, 108, 240, 0.15);
    color: var(--accent);
}

.voice-card-owner {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.voice-card-actions {
    display: flex;
    gap: 6px;
    margin-top: 10px;
}

.voice-card-actions button {
    flex: 1;
    padding: 6px 8px;
    font-size: 12px;
}

.add-voice-card {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 210px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: border-color 0.15s ease, color 0.15s ease;
}

.add-voice-card:hover {
    border-color: var(--accent);
    color: var(--accent);
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 50;
    overflow-y: auto;
}

.modal-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin: 5vh auto;
    padding: 24px;
    width: 90%;
    max-width: 560px;
}

.modal-content .close {
    float: right;
    color: var(--text-muted);
    font-size: 22px;
    cursor: pointer;
    line-height: 1;
}

.modal-content .close:hover {
    color: var(--text);
}

.form-group {
    margin-bottom: 16px;
}

.slider-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.slider-value {
    min-width: 34px;
    text-align: right;
    color: var(--text-muted);
    font-size: 13px;
}

.slider {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: var(--surface-2);
    outline: none;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

/* Image picker grid (inside modal) */
.image-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    max-height: 260px;
    overflow-y: auto;
}

.image-upload {
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 12px;
    cursor: pointer;
}

.image-upload:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.image-container {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 2px solid transparent;
}

.image-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    display: block;
}

.image-container.selected {
    border-color: var(--success);
}

.checkmark {
    display: none;
    position: absolute;
    top: 4px;
    right: 4px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--success);
    color: #fff;
    font-size: 12px;
    text-align: center;
    line-height: 20px;
}

.image-container.selected .checkmark {
    display: block;
}

.delete-icon {
    display: none;
    position: absolute;
    top: 4px;
    left: 4px;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    width: 20px;
    height: 20px;
    text-align: center;
    line-height: 20px;
    font-size: 11px;
    cursor: pointer;
}

.image-container:hover .delete-icon {
    display: block;
}

/* Toasts */
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 100;
}

.toast {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    color: var(--text);
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    font-size: 14px;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    max-width: 320px;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
}

.toast.toast-success {
    border-left-color: var(--success);
}

.toast.toast-error {
    border-left-color: var(--danger);
}

/* Login page */
.login-shell {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}

.login-card {
    width: 100%;
    max-width: 340px;
    text-align: center;
}

.login-card input {
    margin-bottom: 12px;
}

.login-card button {
    width: 100%;
}

#errorMessage {
    color: var(--danger);
    font-size: 13px;
    margin-top: 10px;
}

/* Listen-along user picker */
.user-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.user-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 14px 8px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: border-color 0.15s ease, transform 0.1s ease;
}

.user-card:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.user-card.selected {
    border-color: var(--success);
}

.user-avatar {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
}

.user-avatar-fallback {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 20px;
}

.user-name {
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    word-break: break-word;
}

/* Account page avatar control */
.avatar-row {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 12px;
}

.avatar-preview {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    object-fit: cover;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 22px;
    flex-shrink: 0;
}

/* Nav avatar showing an uploaded picture instead of initials */
.nav-avatar-btn.has-image {
    background-size: cover;
    background-position: center;
    font-size: 0;
}

/* ---- Analytics ---- */
/* Chart roles. Slots are the validated dark-mode categorical order (blue, orange, aqua),
   checked against this app's own surface rather than assumed. */
.viz-root {
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --grid: #2c2c2a;
    --axis: #383835;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 14px;
    margin-bottom: 24px;
}

.stat-tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
}

.stat-label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.stat-value {
    font-size: 30px;
    font-weight: 700;
    line-height: 1.1;
}

.stat-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.chart-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 20px;
}

.chart-card h2 {
    font-size: 15px;
    margin-bottom: 4px;
}

.chart-note {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0 0 16px;
}

/* Horizontal bars: magnitude by category. Value sits outside the bar as a direct label,
   so identity never depends on colour alone. */
.bar-row {
    display: grid;
    grid-template-columns: minmax(80px, 132px) 1fr auto;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
    font-size: 13px;
}

.bar-name {
    color: var(--text-secondary, var(--text));
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.bar-track {
    background: var(--surface-2);
    border-radius: 4px;
    height: 18px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    background: var(--series-1);
    border-radius: 0 4px 4px 0;
    min-width: 2px;
}

.bar-fill.alt { background: var(--series-2); }

.bar-value {
    color: var(--text);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-width: 34px;
    text-align: right;
}

/* Column chart for time series. 2px gap between columns is the surface spacer. */
.column-chart {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    height: 140px;
    padding-top: 8px;
    border-bottom: 1px solid var(--axis);
}

.column {
    flex: 1;
    min-width: 3px;
    background: var(--series-1);
    border-radius: 3px 3px 0 0;
    min-height: 2px;
    position: relative;
}

.column.quiet { background: var(--surface-2); }

.column-axis {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 6px;
}

.empty-note {
    color: var(--text-muted);
    font-size: 13px;
}

/* ---- Listen page: mock stream + chat ---- */
.stream-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) clamp(260px, 24vw, 360px);
    gap: 18px;
    align-items: start;
}

.stream-stage {
    position: relative;
    aspect-ratio: 16 / 9;
    /* Keeps the frame usable on short laptop screens, where a full-width 16:9 stage
       would otherwise push the captions below the fold. */
    max-height: min(64vh, 620px);
    margin-inline: auto;
    width: 100%;
    background: #0b0c10;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

/* The stage is a column: the character gets the upper area, captions get a reserved band
   underneath. Nothing is absolutely positioned here, so the text can never cover the
   character the way a free-floating overlay would. */
.listen-page .stream-stage {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-end;
    padding: 14px;
    gap: 10px;
}

.listen-page .stream-stage #imageContainer {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.listen-page .stream-stage #character {
    max-height: 100%;
    height: auto;
    width: auto;
    max-width: 55%;
    object-fit: contain;
}

/* The OBS overlay lets the caption box fill the whole browser source so it can be
   reshaped by dragging the source. This page isn't a browser source — the box sits in a
   reserved band under the character — so the fill behaviour is undone here. */
.listen-page .stream-stage #subtitleBox {
    position: static;
    inset: auto;
    transform: none;
    align-self: center;
    width: 100%;
    height: auto;
    max-width: 100%;
    min-width: 0;
    /* Scales with the stage rather than a fixed px size, so it stays readable on a laptop
       without swallowing the frame on a small window. */
    font-size: clamp(13px, 1.55vw, 21px);
    max-height: 46%;
    overflow-y: auto;
}

.listen-page .stream-stage #subtitleBox.theme-game {
    width: 100%;
    height: auto;
    max-width: 100%;
    padding: 16px 18px 18px;
    font-size: clamp(13px, 1.55vw, 21px);
}

.listen-page .stream-stage #subtitleBox.theme-clean {
    width: 100%;
    height: auto;
}

.listen-page .stream-stage #subtitleBox.theme-game #speakerName {
    position: static;
    align-self: flex-start;
    display: inline-block;
    font-size: 11px;
    margin-bottom: 8px;
}

/* The RPG continue-marker is a stream flourish; in the preview frame it just crowds
   the text, so it's dropped here. */
.listen-page .stream-stage #subtitleBox.theme-game::after { content: none; }

.stream-stage .stage-idle {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 14px;
    text-align: center;
    padding: 20px;
}

.stream-chat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
    /* Matches the stage height so the two columns line up rather than one running long. */
    max-height: min(64vh, 620px);
}

.stream-chat-header {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.stream-chat-body {
    overflow-y: auto;
    padding: 10px 14px;
    flex: 1;
}

.stream-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 18px;
}

.stream-controls .user-grid {
    display: flex;
    gap: 8px;
    margin: 0;
    flex-wrap: wrap;
    grid-template-columns: none;
}

.stream-controls .user-card {
    flex-direction: row;
    gap: 8px;
    padding: 6px 12px 6px 6px;
}

.stream-controls .user-avatar {
    width: 30px;
    height: 30px;
    font-size: 12px;
}

.stream-controls .user-name {
    font-size: 13px;
}

@media (max-width: 860px) {
    .stream-layout {
        grid-template-columns: minmax(0, 1fr);
    }
    .stream-chat { max-height: 46vh; }
}

/* Persona memory panel */
.memory-note {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    margin-bottom: 10px;
    font-size: 14px;
    line-height: 1.5;
}

.memory-note-header {
    font-weight: 600;
    margin-bottom: 4px;
}

.memory-history {
    max-height: 380px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    background: var(--surface-2);
}

.memory-entry {
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
    line-height: 1.45;
}

.memory-entry:last-child {
    border-bottom: none;
}

.memory-entry.bot .memory-who {
    color: var(--accent);
}

.memory-who {
    font-weight: 600;
    margin-right: 8px;
}

.translate-btn {
    margin-top: 6px;
    padding: 3px 10px;
    font-size: 12px;
    font-weight: 600;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    border-radius: var(--radius-sm);
}

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

.translate-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

/* English rendering shown beneath the original in the log, so the transcript keeps what
   was actually said as well as what it means. */
.memory-translation {
    margin-top: 4px;
    padding-left: 10px;
    border-left: 2px solid var(--border);
    color: var(--text-muted);
    font-style: italic;
}

.memory-when {
    font-size: 11px;
    color: var(--text-muted);
}

.persona-notes {
    margin-bottom: 20px;
}

.persona-notes h3 {
    margin-bottom: 8px;
    font-size: 15px;
    color: var(--accent);
}

select[multiple] {
    padding: 6px;
}

select[multiple] option {
    padding: 5px 8px;
    border-radius: 4px;
}

/* Admin table */
table.admin-table {
    width: 100%;
    border-collapse: collapse;
}

table.admin-table th,
table.admin-table td {
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}

table.admin-table th {
    color: var(--text-muted);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

table.admin-table td button {
    padding: 5px 10px;
    font-size: 12px;
    margin-right: 4px;
}
