/* ============================================
   CHAT TERMINAL CSS
   Fullscreen chat layout with voice control
   Pairs with chat-terminal.html
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; width: 100%; overflow: hidden; }

/* ── Dark ── */
:root, [data-theme="dark"] {
    --bg-primary: #09090b;
    --bg-secondary: #0f0f12;
    --bg-tertiary: #18181b;
    --bg-card: #1c1c20;
    --bg-card-hover: #232328;
    --bg-elevated: #1e1e23;
    --bg-input: #18181b;
    --bg-input-focus: #232328;
    --text-primary: #fafafa;
    --text-secondary: #a1a1aa;
    --text-muted: #52525b;
    --text-inverse: #09090b;
    --border-primary: #27272a;
    --border-secondary: #1f1f23;
    --border-focus: #52525b;
    --shadow-sm: 0 1px 3px rgba(0,0,0,.4);
    --shadow-md: 0 4px 16px rgba(0,0,0,.5);
    --shadow-lg: 0 8px 32px rgba(0,0,0,.6);
    --accent: #e4e4e7;
    --accent-dim: rgba(255,255,255,.06);
    --success-color: #4ade80;
    --error-color: #f87171;
    --warning-color: #fbbf24;
    --overlay-bg: rgba(0,0,0,.7);
    --modal-bg: #171717;
    --user-msg-bg: #27272a;
    --user-msg-text: #fafafa;
    --assistant-msg-bg: #18181b;
    --voice-active: #4ade80;
    --voice-listening: #60a5fa;
    --voice-speaking: #a78bfa;
    --voice-thinking: #fbbf24;
    --background-light: #1e1e23;
    --background-dark: #09090b;
    --background-card: #1c1c20;
    --primary-color: #e4e4e7;
    --shadow-color: rgba(0,0,0,.6);
}

/* ── Light ── */
[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #fafafa;
    --bg-tertiary: #f4f4f5;
    --bg-card: #ffffff;
    --bg-card-hover: #f4f4f5;
    --bg-elevated: #ffffff;
    --bg-input: #f4f4f5;
    --bg-input-focus: #ffffff;
    --text-primary: #09090b;
    --text-secondary: #71717a;
    --text-muted: #a1a1aa;
    --text-inverse: #fafafa;
    --border-primary: #e4e4e7;
    --border-secondary: #f4f4f5;
    --border-focus: #a1a1aa;
    --shadow-sm: 0 1px 3px rgba(0,0,0,.06);
    --shadow-md: 0 4px 16px rgba(0,0,0,.08);
    --shadow-lg: 0 8px 32px rgba(0,0,0,.1);
    --accent: #18181b;
    --accent-dim: rgba(0,0,0,.04);
    --success-color: #22c55e;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    --overlay-bg: rgba(0,0,0,.3);
    --modal-bg: #ffffff;
    --user-msg-bg: #18181b;
    --user-msg-text: #fafafa;
    --assistant-msg-bg: #f4f4f5;
    --voice-active: #22c55e;
    --voice-listening: #3b82f6;
    --voice-speaking: #8b5cf6;
    --voice-thinking: #f59e0b;
    --background-light: #ffffff;
    --background-dark: #fafafa;
    --background-card: #ffffff;
    --primary-color: #18181b;
    --shadow-color: rgba(0,0,0,.1);
}

body {
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── App container ── */
.ct-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 860px;
    margin: 0 auto;
}

/* ── Header ── */
.ct-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-primary);
    background: var(--bg-primary);
    flex-shrink: 0;
    z-index: 10;
}
.ct-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}
.ct-avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-primary);
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; color: var(--text-muted); flex-shrink: 0;
}
.ct-name {
    font-size: 15px;
    font-weight: 700;
    line-height: 1.2;
    color: var(--text-primary);
}
.ct-status-row {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 5px;
}
.ct-dot {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--text-muted);
    transition: background .3s;
}
.ct-dot.listening  { background: #10B981; animation: ctDotP 1.5s infinite; }
.ct-dot.speaking   { background: #3B82F6; animation: ctDotP 1s infinite; }
.ct-dot.thinking   { background: #F59E0B; animation: ctDotP .8s infinite; }
.ct-dot.connected  { background: #22C55E; }
.ct-dot.connecting { background: #6366F1; animation: ctDotP 1s infinite; }
.ct-dot.muted      { background: #6B7280; }

@keyframes ctDotP {
    0%,100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.5); opacity: .5; }
}

.ct-header-right {
    display: flex;
    align-items: center;
    gap: 6px;
}
.ct-hbtn {
    width: 36px; height: 36px; border-radius: 10px;
    border: 1px solid var(--border-primary);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 16px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all .2s;
}
.ct-hbtn:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
    border-color: var(--border-focus);
}

/* Theme toggle icon swap */
.ct-theme-toggle .bi-sun-fill { display: none; }
.ct-theme-toggle .bi-moon-fill { display: block; }
[data-theme="light"] .ct-theme-toggle .bi-sun-fill { display: block; }
[data-theme="light"] .ct-theme-toggle .bi-moon-fill { display: none; }

/* ── Voice status bar ── */
.ct-voicebar {
    display: none;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-primary);
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    flex-shrink: 0;
    transition: all .3s;
}
.ct-voicebar.visible { display: flex; }
.ct-voicebar .ct-vbd {
    width: 8px; height: 8px; border-radius: 50%;
    background: currentColor;
    animation: ctDotP 1.5s infinite;
}
.ct-voicebar.listening {
    color: #10B981;
    background: rgba(16, 185, 129, 0.08);
    border-bottom-color: rgba(16, 185, 129, 0.2);
}
.ct-voicebar.speaking {
    color: #3B82F6;
    background: rgba(59, 130, 246, 0.08);
    border-bottom-color: rgba(59, 130, 246, 0.2);
}
.ct-voicebar.thinking {
    color: #F59E0B;
    background: rgba(245, 158, 11, 0.08);
    border-bottom-color: rgba(245, 158, 11, 0.2);
}
.ct-voicebar.thinking .ct-vbd {
    animation: ctDotP 0.8s infinite;
}
.ct-voicebar.muted {
    color: #6B7280;
    background: rgba(107, 114, 128, 0.08);
    border-bottom-color: rgba(107, 114, 128, 0.2);
}
.ct-voicebar.connecting {
    color: #6366F1;
    background: rgba(99, 102, 241, 0.08);
    border-bottom-color: rgba(99, 102, 241, 0.2);
}

.ct-vb-end {
    margin-left: auto;
    background: none; border: none;
    color: var(--text-muted);
    font-size: 12px; font-family: inherit; font-weight: 600;
    cursor: pointer;
    padding: 4px 10px;
    border-radius: 8px;
    transition: all .2s;
}
.ct-vb-end:hover {
    color: var(--error-color);
    background: rgba(248,113,113,.1);
}

/* ── Chat area ── */
.ct-chat {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    scroll-behavior: smooth;
}
.ct-chat::-webkit-scrollbar { width: 4px; }
.ct-chat::-webkit-scrollbar-track { background: transparent; }
.ct-chat::-webkit-scrollbar-thumb { background: var(--border-primary); border-radius: 2px; }

/* ── Messages ── */
.ct-msg {
    max-width: 82%;
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.6;
    animation: ctMsgIn .25s ease;
    word-wrap: break-word;
}
@keyframes ctMsgIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}
.ct-msg-user {
    align-self: flex-end;
    background: var(--user-msg-bg);
    color: var(--user-msg-text);
    border-bottom-right-radius: 4px;
}
.ct-msg-assistant {
    align-self: flex-start;
    background: var(--assistant-msg-bg);
    color: var(--text-primary);
    border: 1px solid var(--border-primary);
    border-bottom-left-radius: 4px;
}
.ct-msg-time {
    font-size: 10px;
    color: var(--text-muted);
    margin-top: 4px;
    opacity: .6;
}
.ct-msg-user .ct-msg-time {
    text-align: right;
    color: rgba(255,255,255,.4);
}

/* Typing dots */
.ct-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 14px 18px;
    align-self: flex-start;
    background: var(--assistant-msg-bg);
    border: 1px solid var(--border-primary);
    border-radius: 18px;
    border-bottom-left-radius: 4px;
}
.ct-typing span {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--text-muted);
    animation: ctTyping .6s ease-in-out infinite;
}
.ct-typing span:nth-child(2) { animation-delay: .15s; }
.ct-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes ctTyping {
    0%,100% { transform: translateY(0); opacity: .4; }
    50% { transform: translateY(-4px); opacity: 1; }
}

/* Empty state */
.ct-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 12px;
    color: var(--text-muted);
}

.ct-empty-start {
    background: none;
    border: 2px solid var(--border-primary);
    border-radius: 24px;
    width: 80px; height: 80px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    margin-bottom: 8px;
    position: relative;
    transition: all .3s;
}

.ct-empty-start:hover {
    border-color: var(--border-focus);
    background: var(--accent-dim);
    transform: scale(1.05);
}

.ct-empty-start:active {
    transform: scale(0.97);
}

.ct-empty-icon {
    width: 64px; height: 64px;
    border-radius: 20px;
    background: var(--accent-dim);
    display: flex; align-items: center; justify-content: center;
    font-size: 28px;
    color: var(--text-muted);
    transition: all .3s;
}

.ct-empty-start:hover .ct-empty-icon {
    color: var(--text-secondary);
}

.ct-empty-spinner {
    display: none;
    position: absolute;
    inset: -2px;
}

.ct-spinner {
    width: 100%; height: 100%;
    border: 2px solid transparent;
    border-top-color: #10B981;
    border-right-color: #10B981;
    border-radius: 24px;
    animation: ctEmptySpin 0.8s linear infinite;
}

@keyframes ctEmptySpin { to { transform: rotate(360deg); } }

/* Connecting state */
.ct-empty-start.connecting .ct-empty-icon i {
    display: none;
}
.ct-empty-start.connecting .ct-empty-icon::after {
    content: '';
    width: 24px; height: 24px;
    border: 3px solid var(--border-primary);
    border-top-color: #10B981;
    border-radius: 50%;
    animation: ctEmptySpin 0.7s linear infinite;
}
.ct-empty-start.connecting .ct-empty-spinner {
    display: block;
}
.ct-empty-start.connecting {
    border-color: #10B981;
    pointer-events: none;
}

.ct-empty h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
}
.ct-empty p {
    font-size: 13px;
    max-width: 280px;
    line-height: 1.5;
}

/* ── Input bar ── */
.ct-inputbar {
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border-primary);
    background: var(--bg-primary);
    flex-shrink: 0;
    display: flex;
    align-items: flex-end;
    gap: 10px;
}
.ct-inputwrap {
    flex: 1;
    display: flex;
    align-items: flex-end;
    background: var(--bg-input);
    border: 1px solid var(--border-primary);
    border-radius: 22px;
    padding: 4px 6px 4px 18px;
    transition: all .2s;
    min-height: 48px;
}
.ct-inputwrap:focus-within {
    border-color: var(--border-focus);
    background: var(--bg-input-focus);
    box-shadow: 0 0 0 3px var(--accent-dim);
}
.ct-inputwrap textarea {
    flex: 1; border: none; background: transparent;
    color: var(--text-primary);
    font-size: 14px; font-family: inherit;
    outline: none; resize: none;
    line-height: 1.5;
    max-height: 120px;
    padding: 8px 0;
    min-height: 20px;
}
.ct-inputwrap textarea::placeholder { color: var(--text-muted); }

.ct-send {
    width: 38px; height: 38px; min-width: 38px;
    border-radius: 50%; border: none;
    background: var(--accent);
    color: var(--text-inverse);
    font-size: 16px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all .2s;
    opacity: .3;
    pointer-events: none;
}
.ct-send.active { opacity: 1; pointer-events: auto; }
.ct-send.active:hover { transform: scale(1.06); }

/* ── Voice button ── */
.ct-voice {
    width: 48px; height: 48px; min-width: 48px;
    border-radius: 50%;
    border: 2px solid var(--border-primary);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 20px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all .25s;
    position: relative;
    overflow: visible;
}
.ct-voice:hover {
    border-color: var(--border-focus);
    color: var(--text-primary);
    background: var(--bg-card-hover);
}

.ct-voice.connecting { border-color: var(--text-muted); }

.ct-voice.listening {
    border-color: #10B981;
    color: #10B981;
    box-shadow: 0 0 0 4px rgba(16,185,129,.15);
    animation: ctVPGreen 2s ease-in-out infinite;
}
@keyframes ctVPGreen {
    0%,100% { box-shadow: 0 0 0 4px rgba(16,185,129,.12); }
    50% { box-shadow: 0 0 0 8px rgba(16,185,129,.04); }
}

.ct-voice.speaking {
    border-color: #3B82F6;
    color: #3B82F6;
    box-shadow: 0 0 0 4px rgba(59,130,246,.15);
}

.ct-voice.thinking {
    border-color: #F59E0B;
    color: #F59E0B;
    box-shadow: 0 0 0 4px rgba(245,158,11,.15);
    animation: ctVYellow 1.2s ease-in-out infinite;
}
@keyframes ctVYellow {
    0%,100% { box-shadow: 0 0 0 4px rgba(245,158,11,.12); }
    50% { box-shadow: 0 0 0 8px rgba(245,158,11,.04); }
}

.ct-voice.muted {
    border-color: #6B7280;
    color: #6B7280;
    opacity: .8;
}

.ct-voice.connected {
    border-color: #22C55E;
    color: #22C55E;
}

/* Voice wave bars */
.ct-vw {
    display: none;
    align-items: center;
    justify-content: center;
    gap: 2px;
    height: 20px;
}
.ct-vw span {
    width: 3px;
    border-radius: 2px;
    background: currentColor;
    animation: ctWave .5s ease-in-out infinite;
}
.ct-vw span:nth-child(1) { height: 6px; animation-delay: 0s; }
.ct-vw span:nth-child(2) { height: 12px; animation-delay: .1s; }
.ct-vw span:nth-child(3) { height: 16px; animation-delay: .2s; }
.ct-vw span:nth-child(4) { height: 12px; animation-delay: .3s; }
.ct-vw span:nth-child(5) { height: 6px; animation-delay: .4s; }
@keyframes ctWave {
    0%,100% { transform: scaleY(.5); }
    50% { transform: scaleY(1.3); }
}
.ct-voice.listening.voice-active .ct-vi { display: none; }
.ct-voice.listening.voice-active .ct-vw { display: flex; }

/* ── Loading screen ── */
.loading-screen {
    position: fixed; inset: 0;
    background: var(--bg-primary);
    display: flex; align-items: center; justify-content: center;
    z-index: 9999;
    transition: opacity .4s;
}
.loading-screen.hidden { opacity: 0; pointer-events: none; }
.loading-spinner {
    width: 40px; height: 40px;
    border: 3px solid var(--border-primary);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: ctSpin .8s linear infinite;
}
@keyframes ctSpin { to { transform: rotate(360deg); } }

/* ── Error toast ── */
.error-toast {
    position: fixed; top: 24px; left: 50%; transform: translateX(-50%);
    background: var(--error-color); color: white;
    padding: 14px 24px; border-radius: 12px;
    font-size: 14px; font-weight: 500;
    z-index: 10000; max-width: 90%; text-align: center;
    box-shadow: var(--shadow-lg);
    animation: ctSlideD .3s ease;
}
@keyframes ctSlideD {
    from { transform: translateX(-50%) translateY(-16px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ── Language overlay ── */
.ct-lang-overlay {
    display: none;
    position: fixed; inset: 0;
    background: var(--overlay-bg);
    z-index: 100;
    align-items: flex-end;
    justify-content: center;
}
.ct-lang-overlay.visible { display: flex; }
.ct-lang-sheet {
    background: var(--bg-elevated);
    border-radius: 20px 20px 0 0;
    width: 100%; max-width: 860px;
    padding: 24px;
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    animation: ctSheetUp .25s ease;
}
@keyframes ctSheetUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}
.ct-lang-sheet h3 {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 16px;
    color: var(--text-primary);
}
.ct-lo {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-radius: 12px;
    cursor: pointer;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    transition: background .15s;
}
.ct-lo:hover { background: var(--accent-dim); }
.ct-lo-sel { background: var(--accent-dim); }
.ct-lo-sel::after {
    content: '✓';
    margin-left: auto;
    font-weight: 700;
    color: var(--voice-active);
}

/* ── Hide original layout elements that main.js might inject ── */
#mobile-control-bar,
#mobile-transcript-display,
.chat-toggle,
#mainWrapper,
.main-wrapper {
    display: none !important;
}

/* ── Scrollbar ── */
* { scrollbar-width: thin; scrollbar-color: var(--border-primary) transparent; }

/* ── Mobile ── */
@media (max-width: 600px) {
    .ct-header { padding: 10px 14px; }
    .ct-chat { padding: 14px; }
    .ct-inputbar { padding: 10px 12px; }
    .ct-msg { max-width: 88%; }
}

/* ── Selection ── */
::selection {
    background: var(--accent-dim);
    color: var(--text-primary);
}