/* ==========================================
   Chat Mode — Voice Practice Overlay
   Desktop only. 和の感性 design system.
   Premium polish: 13-point upgrade.
   ========================================== */

/* ---- Custom Properties (driven by JS) ---- */
.chat-mode-container {
    --rms: 0;
    --voice-color: #C9A96E;
    --breath-duration: 3.5s;
}

/* ---- Overlay — Shoji entrance ---- */

.chat-mode-overlay {
    position: fixed;
    inset: 0;
    z-index: 5000;
    background: #FAFAF7;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    clip-path: inset(50% 50% 50% 50%);
    transition: clip-path 0.5s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-mode-overlay.visible {
    opacity: 1;
    clip-path: inset(0% 0% 0% 0%);
}

/* Session warmth — golden tint that builds over time */
.chat-mode-overlay::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, rgba(201,169,110,0.02), rgba(201,169,110,0.05));
    opacity: var(--session-warmth, 0);
    pointer-events: none;
    transition: opacity 8s ease;
}

/* Ambient state tint — subtle radial glow colored by state */
.chat-mode-overlay::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at 50% 25%, var(--ambient-tint, transparent) 0%, transparent 70%);
    pointer-events: none;
    transition: background 1.2s ease;
}

/* Depth separation — grid blurs behind during shoji entrance */
body.chat-mode .grid-container,
body.chat-mode .flashcard-container,
body.chat-mode .bottom-action-bar,
body.chat-mode .premium-progress-wrapper {
    filter: blur(3px);
    opacity: 0.6;
    transition: filter 0.5s ease, opacity 0.5s ease;
}

/* ---- Container ---- */

.chat-mode-container {
    position: relative;
    width: 100%;
    max-width: 640px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 20px 32px 16px;
    box-sizing: border-box;
}

/* ---- Header ---- */

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 0 4px;
    flex-shrink: 0;
}

/* ---- Session Timer (with heartbeat) ---- */

.chat-session-timer {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    color: #57534e;
    opacity: 0.45;
    letter-spacing: 0.03em;
    min-width: 3em;
}

.chat-session-timer:empty {
    display: none;
}

.chat-session-timer:not(:empty) {
    animation: timerHeartbeat 4s ease-in-out infinite;
}

@keyframes timerHeartbeat {
    0%, 100% { opacity: 0.45; }
    50% { opacity: 0.6; }
}

/* ---- Close Button ---- */

.chat-close-btn {
    background: none;
    border: none;
    font-size: 16px;
    color: #57534e;
    cursor: pointer;
    padding: 4px 8px;
    line-height: 1;
    border-radius: 4px;
    transition: color 0.2s, opacity 0.2s, transform 0.1s;
    flex-shrink: 0;
    margin-left: auto;
    opacity: 0.35;
}

.chat-close-btn:hover {
    color: #2C2C2C;
    opacity: 0.8;
}

.chat-close-btn:active {
    transform: scale(0.92);
}

/* ---- Voice Visualization: Refined Orb ---- */
/* Calmed, slower, muted. Stone-gray idle, indigo on voice. */

.voice-viz {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 80px;
    position: relative;
    margin: 8px 0 4px;
    opacity: 0;
    animation: vizFadeIn 0.6s ease 0.25s forwards;
}

@keyframes vizFadeIn {
    to { opacity: 1; }
}

/* ---- Orb ---- */

.voice-viz-orb {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 38%, #d6d3cd, #a8a29e);
    opacity: calc(0.3 + var(--rms) * 0.25);
    transform: scale(calc(1 + var(--rms) * 0.04));
    transition: background 1s ease,
                opacity 0.3s ease,
                transform 0.8s ease;
    position: relative;
    z-index: 2;
}

/* ---- Inner wash (soft glow, voice-reactive) ---- */

.voice-viz-ring {
    position: absolute;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 38%, #a8a29e, #78716c);
    opacity: calc(0.03 + var(--rms) * 0.12);
    transform: scale(calc(1.3 + var(--rms) * 0.4));
    filter: blur(calc(6px + var(--rms) * 6px));
    transition: background 1s ease,
                opacity 0.3s ease,
                transform 0.4s ease,
                filter 0.3s ease;
    z-index: 1;
}

/* ---- Outer ring (hide — not needed for refined orb) ---- */

.voice-viz-ring-outer {
    display: none;
}

/* ---- State: Connecting (slow pulse) ---- */

.chat-mode-container.state-connecting .voice-viz-orb {
    animation: vizPulse 3s ease-in-out infinite;
}

@keyframes vizPulse {
    0%, 100% { opacity: 0.2; }
    50% { opacity: 0.4; }
}

/* ---- State: Connected idle (slow breathing) ---- */

.chat-mode-container.state-connected .voice-viz-orb {
    animation: vizBreath var(--breath-duration, 7s) ease-in-out infinite;
}

@keyframes vizBreath {
    0%, 100% { transform: scale(1); opacity: 0.3; }
    50% { transform: scale(1.015); opacity: 0.38; }
}

/* ---- State: Ready (gentle swell) ---- */

.chat-mode-container.state-ready .voice-viz-orb {
    animation: vizReadyPulse 0.6s ease-out forwards;
}
.chat-mode-container.state-ready .voice-viz-ring {
    animation: vizReadyWash 0.7s ease-out forwards;
}

@keyframes vizReadyPulse {
    0% { opacity: 0.3; }
    40% { opacity: 0.55; }
    100% { opacity: 0.38; }
}

@keyframes vizReadyWash {
    0% { opacity: 0; transform: scale(1); }
    40% { opacity: 0.1; transform: scale(1.4); filter: blur(10px); }
    100% { opacity: 0.03; transform: scale(1.3); filter: blur(6px); }
}

/* ---- State: Speaking / User speaking (driven by --rms) ---- */

.chat-mode-container.state-speaking .voice-viz-orb,
.chat-mode-container.state-user-speaking .voice-viz-orb {
    animation: none;
}
.chat-mode-container.state-speaking .voice-viz-ring,
.chat-mode-container.state-user-speaking .voice-viz-ring {
    animation: none;
}

/* ---- State Colors ---- */

/* Agent speaking — indigo gradient */
.chat-mode-container.state-speaking .voice-viz-orb {
    background: radial-gradient(circle at 40% 38%, #7d8fa8, #3D5A80);
}
.chat-mode-container.state-speaking .voice-viz-ring {
    background: radial-gradient(circle at 40% 38%, #7d8fa8, #3D5A80);
}

/* User speaking — warm stone gradient */
.chat-mode-container.state-user-speaking .voice-viz-orb {
    background: radial-gradient(circle at 40% 38%, #a8a29e, #57534e);
}
.chat-mode-container.state-user-speaking .voice-viz-ring {
    background: radial-gradient(circle at 40% 38%, #a8a29e, #57534e);
}

/* Connected idle — stone gray (default gradient, no override needed) */

/* Connecting — slightly warmer stone */
.chat-mode-container.state-connecting .voice-viz-orb {
    background: radial-gradient(circle at 40% 38%, #d6d3cd, #a8a29e);
}

/* Error — kintsugi gold */
.chat-mode-container.state-error .voice-viz-orb {
    background: radial-gradient(circle at 40% 38%, #d4b886, #C9A96E);
}

/* ---- Viz Label ---- */

.voice-viz-label {
    flex-shrink: 0;
    text-align: center;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    color: #57534e;
    opacity: 0;
    height: 18px;
    line-height: 18px;
    margin-bottom: 8px;
    transition: opacity 0.3s ease;
    animation: vizFadeIn 0.4s ease 0.45s forwards;
}

/* Once conversation starts, label fades permanently */
.voice-viz-label.hidden {
    opacity: 0 !important;
    animation: none;
}

/* ---- Transcript Area (with top edge fade) ---- */

.chat-transcript {
    flex: 1;
    overflow-y: auto;
    scroll-behavior: smooth;
    padding: 16px 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 0;
    opacity: 0;
    animation: vizFadeIn 0.4s ease 0.6s forwards;
    -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 28px, black 100%);
    mask-image: linear-gradient(to bottom, transparent 0%, black 28px, black 100%);
}

/* Subtle scrollbar */
.chat-transcript::-webkit-scrollbar {
    width: 3px;
}
.chat-transcript::-webkit-scrollbar-track {
    background: transparent;
}
.chat-transcript::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.08);
    border-radius: 3px;
}

.chat-empty-state {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #57534e;
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    opacity: 0.5;
}

/* ---- Scope (session header with hairline) ---- */

.chat-scope-line {
    font-family: 'Inter', sans-serif;
    font-size: 10px;
    color: #57534e;
    opacity: 0.4;
    letter-spacing: 0.02em;
    text-align: center;
    padding: 0 0 12px;
    border-bottom: 1px solid rgba(87, 83, 78, 0.08);
    margin-bottom: 4px;
}

/* ---- Messages (variable rhythm) ---- */

.chat-message {
    display: flex;
    flex-direction: column;
}

/* Extra spacing when speaker changes (role label visible) */
.chat-message:has(> .chat-message-role:not(.role-hidden)) {
    margin-top: 14px;
}
.chat-message:first-child {
    margin-top: 0;
}

/* Agent message — washi paper warmth */
.chat-message.agent {
    padding: 6px 10px;
    border-left: 2px solid rgba(201, 169, 110, 0.2);
    background: rgba(201, 169, 110, 0.03);
    border-radius: 0 4px 4px 0;
    animation: agentFadeIn 0.4s ease;
}

/* User message — margin notes */
.chat-message.user {
    padding: 4px 10px 4px 0;
    border-right: 1.5px solid rgba(87, 83, 78, 0.12);
    animation: userFadeIn 0.3s ease;
}

@keyframes agentFadeIn {
    from {
        opacity: 0;
        transform: translateY(3px) translateX(-4px);
    }
    to {
        opacity: 1;
        transform: translateY(0) translateX(0);
    }
}

@keyframes userFadeIn {
    from {
        opacity: 0;
        transform: translateY(3px) translateX(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0) translateX(0);
    }
}

/* ---- Message Text ---- */

.chat-message-text {
    font-family: 'Klee One', 'Noto Sans JP', sans-serif;
    font-size: 17px;
    line-height: 1.8;
    color: #2C2C2C;
    word-break: break-word;
}

.chat-message.user .chat-message-text {
    color: #57534e;
    font-size: 15px;
}

/* Inline Japanese highlighting — faint gold tint */
.jp-highlight {
    background: rgba(201, 169, 110, 0.08);
    border-radius: 2px;
    padding: 0 2px;
}

/* Furigana ruby annotations */
.jp-highlight ruby {
    ruby-align: center;
}
.jp-highlight rt {
    font-size: 0.55em;
    color: #57534e;
    opacity: 0.8;
    font-family: 'Noto Sans JP', sans-serif;
}

/* ---- Role Label ---- */

.chat-message-role {
    font-family: 'Inter', sans-serif;
    font-size: 9px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #57534e;
    opacity: 0.55;
    margin-bottom: 2px;
}

/* Suppress consecutive same-speaker labels */
.chat-message-role.role-hidden {
    display: none;
}

/* ---- Screen reader only ---- */

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ---- Hide app chrome when voice overlay is active ---- */

body.chat-mode .hero-top-bar,
body.chat-mode .app-branding-topleft,
body.chat-mode .grid-filter-bar,
body.chat-mode .grid-search-bar,
body.chat-mode .nav-tools-row,
body.chat-mode .intro-mode-indicator {
    display: none !important;
}

/* ---- Desktop Only ---- */

@media (max-width: 768px) {
    .chat-mode-overlay {
        display: none !important;
    }
}

/* Narrower desktops */
@media (min-width: 769px) and (max-width: 1024px) {
    .chat-mode-container {
        max-width: 100%;
        padding: 20px 24px 16px;
    }
}

/* ---- Reduced Motion ---- */

@media (prefers-reduced-motion: reduce) {
    .chat-mode-overlay {
        transition: opacity 0.3s ease;
        clip-path: none;
    }
    .chat-mode-overlay.visible {
        clip-path: none;
    }
    .chat-message,
    .chat-message.agent,
    .chat-message.user {
        animation: none;
    }
    .voice-viz {
        animation: none;
        opacity: 1;
    }
    .voice-viz::after {
        display: none;
    }
    .voice-viz-orb {
        transition: background 0.4s ease;
        transform: none !important;
        animation: none !important;
    }
    .voice-viz-orb::after {
        display: none;
    }
    .voice-viz-ring,
    .voice-viz-ring-outer {
        display: none;
    }
    .voice-viz-label {
        animation: none;
        opacity: 0.5;
    }
    .chat-transcript {
        animation: none;
        opacity: 1;
        scroll-behavior: auto;
    }
    .chat-session-timer:not(:empty) {
        animation: none;
    }
    .chat-mode-container.state-connecting .voice-viz-orb,
    .chat-mode-container.state-connected .voice-viz-orb,
    .chat-mode-container.state-ready .voice-viz-orb {
        animation: none;
    }
    body.chat-mode .grid-container,
    body.chat-mode .flashcard-container,
    body.chat-mode .bottom-action-bar,
    body.chat-mode .premium-progress-wrapper {
        filter: none;
        opacity: 1;
        transition: none;
    }
}
