/* ブラウザ通話ページの見た目。スマートフォンとPCの両方で使えるようにする。 */

:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --text: #1f2933;
    --muted: #6b7280;
    --line: #e5e7eb;
    --accent: #1d4ed8;
    --bot: #eef2ff;
    --user: #dcfce7;
    --danger: #b91c1c;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", Meiryo, sans-serif;
    font-size: 16px;
    line-height: 1.6;
}

.app {
    max-width: 720px;
    margin: 0 auto;
    padding: 16px;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/*
 * 利用者向けページ（index.html）: 画面の高さに収め、チャット欄だけをスクロールさせる。
 * 通話ボタンはページをスクロールしなくても常に画面の下に見える。
 */
.app.app-fit {
    /* 100vh はスマートフォンのアドレスバーの裏まで含むため、対応していれば 100dvh を使う。
       非対応の古いブラウザは index.html のスクリプトが実際の高さを設定する。 */
    height: 100vh;
    min-height: 0;
    /* ホームバーやノッチに通話ボタンが隠れないようにする（viewport-fit=cover のため）。 */
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

@supports (height: 100dvh) {
    .app.app-fit {
        height: 100dvh;
    }
}

/* チャット欄以外は高さを保ち、残りの高さをチャット欄が使う。 */
.app-fit > * {
    flex: none;
}

.app-fit > .chat {
    flex: 1 1 auto;
    min-height: 0;
    /* チャット欄の端までスクロールしても、ページ全体を動かさない。 */
    overscroll-behavior: contain;
}

/* 長い案内（マイクを許可する手順など）でも、チャット欄と通話ボタンを画面の外へ押し出さない。 */
.app-fit > .notice {
    max-height: 25%;
    overflow-y: auto;
}

/* 横向きのスマートフォンなど高さが足りない画面では、余白と説明文を省いてチャット欄を広く取る。 */
@media (max-height: 520px) {
    .app.app-fit {
        gap: 8px;
        padding-top: calc(8px + env(safe-area-inset-top));
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
    }

    .app-fit header.app-header {
        padding: 8px 16px;
    }

    .app-fit header.app-header h1 {
        margin: 0;
    }

    .app-fit .status-bar {
        padding: 6px 16px;
    }

    .app-fit header.app-header p,
    .app-fit .hint {
        display: none;
    }
}

header.app-header {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
}

header.app-header h1 {
    margin: 0 0 4px;
    font-size: 18px;
}

header.app-header p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

/* 通話の状態 */
.status-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 16px;
    font-size: 14px;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #9ca3af;
    flex: none;
}

.status-dot.connecting { background: #f59e0b; }
.status-dot.speaking   { background: var(--accent); }
.status-dot.listening  { background: #16a34a; animation: pulse 1.2s infinite; }
.status-dot.ended      { background: #9ca3af; }

@keyframes pulse {
    0%   { opacity: 1; }
    50%  { opacity: 0.3; }
    100% { opacity: 1; }
}

/* チャット */
.chat {
    flex: 1;
    min-height: 320px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
    overflow-y: auto;
}

.chat-empty {
    color: var(--muted);
    font-size: 14px;
    text-align: center;
    margin-top: 32px;
}

.chat-row {
    display: flex;
    flex-direction: column;
    margin-bottom: 12px;
}

.chat-row-bot  { align-items: flex-start; }
.chat-row-user { align-items: flex-end; }

.bubble {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    white-space: pre-wrap;
    word-break: break-word;
}

/*
  背景色を付けた語（threeR-ai-bot の表示文字設定。webcall.js）。文字の高さだけだと色が細く見えるため、マーカーのように少し広げる。
  語が折り返したときも行ごとに角を丸める（Safari は -webkit- 付きの指定だけを解釈する）。
*/
.bubble .highlight-marker {
    padding: 0.1em 0.2em;
    border-radius: 0.25em;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
}

.bubble-bot {
    background: var(--bot);
    border-top-left-radius: 4px;
}

.bubble-user {
    background: var(--user);
    border-top-right-radius: 4px;
}

/* 接続先が受け取った発話（誤変換を直した場合の元の認識結果・当てはめた値）。 */
.bubble-note {
    margin-top: 4px;
    font-size: 12px;
    color: var(--muted);
}

/* 電話では SMS で送る案内。読み上げと区別し、URL・電話番号を押してもらう場所として見せる。 */
.bubble-guide {
    background: var(--panel);
    border: 1px solid #c7d2fe;
    border-left: 4px solid var(--accent);
    border-top-left-radius: 4px;
}

.bubble a {
    color: var(--accent);
    text-decoration: underline;
    /* 長い URL でも吹き出しからはみ出さないよう、どこでも折り返す。 */
    overflow-wrap: anywhere;
}

/* 返信のボタン。既存のチャットボットのクイックリプライと同じく、案内の吹き出しの中に縦に並べる。 */
.bubble-with-replies {
    /* 短い質問でもボタンが押しやすい幅になるよう、吹き出しを広げる。 */
    width: 85%;
}

.bubble-replies {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 10px;
    white-space: normal;
}

.reply-button {
    display: block;
    width: 100%;
    min-height: 44px;
    padding: 8px 12px;
    border: 1px solid var(--accent);
    border-radius: 10px;
    background: var(--panel);
    color: var(--accent);
    font: inherit;
    font-size: 15px;
    line-height: 1.4;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.reply-button:hover:not(:disabled),
.reply-button:focus-visible {
    background: var(--accent);
    color: #fff;
}

.reply-button:disabled {
    opacity: 0.45;
    cursor: default;
}

/* 電話番号は途中で折り返すと読み違えやすいため、1行にまとめる。 */
.bubble a[href^="tel:"] {
    white-space: nowrap;
    overflow-wrap: normal;
}

.bubble-meta {
    color: var(--muted);
    font-size: 11px;
    margin-top: 2px;
}

/* 操作 */
.controls {
    display: flex;
    gap: 12px;
}

button.call-button {
    flex: 1;
    padding: 16px;
    font-size: 17px;
    font-weight: 700;
    color: #fff;
    background: #16a34a;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    min-height: 56px;
}

button.call-button:hover:not(:disabled) { background: #15803d; }

button.call-button.hangup { background: var(--danger); }
button.call-button.hangup:hover:not(:disabled) { background: #991b1b; }

button.call-button:disabled {
    background: #9ca3af;
    cursor: not-allowed;
}

.notice {
    background: #fef3c7;
    border: 1px solid #fcd34d;
    border-radius: 10px;
    padding: 10px 14px;
    font-size: 14px;
}

.notice.hidden { display: none; }

.hint {
    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

/* テストページ用 */
.test-panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
    font-size: 14px;
}

.test-panel h2 {
    margin: 0 0 8px;
    font-size: 15px;
}

.test-panel label {
    display: block;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 4px;
}

.test-panel input[type="text"] {
    width: 100%;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 14px;
}

.log {
    margin-top: 12px;
    max-height: 180px;
    overflow-y: auto;
    background: #111827;
    color: #e5e7eb;
    border-radius: 8px;
    padding: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 12px;
    white-space: pre-wrap;
}
