/* ===== Theme variables ===== */
[data-theme="dark"] {
    --bg-app:      #0b141a;
    --bg-header:   #1f2c34;
    --bg-input:    #1f2c34;
    --bg-user-msg: #005c4b;
    --bg-ai-msg:   #202c33;
    --text-main:   #e9edef;
    --text-sub:    #8696a0;
    --text-user:   #e9edef;
    --text-ai:     #e9edef;
    --border:      #2a3942;
    --date-bg:     #182229;
    --date-color:  #8696a0;
    --dialog-bg:   rgba(0,0,0,0.7);
}

[data-theme="light"] {
    --bg-app:      #efeae2;
    --bg-header:   #075e54;
    --bg-input:    #f0f2f5;
    --bg-user-msg: #d9fdd3;
    --bg-ai-msg:   #ffffff;
    --text-main:   #111b21;
    --text-sub:    #667781;
    --text-user:   #111b21;
    --text-ai:     #111b21;
    --border:      #d1d7db;
    --date-bg:     #ffffff;
    --date-color:  #54656f;
    --dialog-bg:   rgba(0,0,0,0.5);
}

:root {
    --accent:       #25d366;
    --accent-dark:  #128c7e;
    --send-btn:     #00a884;
    --error:        #ef4444;
    --warning:      #f59e0b;
    --success:      #22c55e;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

[v-cloak] { display: none; }

html { height: 100%; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-app);
    color: var(--text-main);
    /* Android は viewport meta の interactive-widget=resizes-content で
       レイアウトビューポート自体が縮むため height:100% で追従できる。
       dvh を併用すると高さの取り合いで入力欄が上下に発振するので使わない。
       iOS はこの meta を無視するため、キーボード表示中だけ JS が
       --app-height に visualViewport.height を書き込んで追従させる
       （app.js の syncIosViewport を参照）。未設定なら従来どおり 100%。 */
    height: var(--app-height, 100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#app {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

/* ===== Header ===== */
.header {
    background: var(--bg-header);
    padding: calc(0.6rem + env(safe-area-inset-top, 0px))
             calc(1rem + env(safe-area-inset-right, 0px))
             0.6rem
             calc(1rem + env(safe-area-inset-left, 0px));
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    z-index: 10;
}

.header h1 {
    font-size: 1.05rem;
    font-weight: 600;
    color: #fff;
    flex: 1;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.header-buttons {
    display: flex;
    gap: 0.4rem;
    align-items: center;
    flex-wrap: wrap;
    position: relative;
}

.header button {
    padding: 0.3rem 0.7rem;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.25);
    background: rgba(255,255,255,0.1);
    color: #fff;
    cursor: pointer;
    font-size: 0.78rem;
    white-space: nowrap;
    transition: background 0.15s;
}
.header button:hover { background: rgba(255,255,255,0.2); }

/* ===== Hamburger menu ===== */
.menu-toggle {
    display: flex !important;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 4px;
    width: 36px;
    height: 36px;
    padding: 0 !important;
    flex-shrink: 0;
}
.menu-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    background: #fff;
    border-radius: 1px;
}

.menu-dropdown {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    background: var(--bg-header);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 8px;
    min-width: 170px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    display: flex;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-4px);
    transition: opacity 0.15s, visibility 0.15s, transform 0.15s, max-height 0.15s;
    z-index: 100;
    overflow: hidden;
    /* 閉じている間も visibility:hidden のままレイアウトに残ると、この 550px 超の
       高さが #app のスクロール可能領域を作る。キーボード表示で #app が縮むと、
       ブラウザがフォーカス中の入力欄を見せようとして #app 自体をスクロールし
       （overflow:hidden でも UA のスクロールイントゥビューは効く）、ヘッダーと
       送信直後のメッセージが画面外へ持ち上がる。高さを 0 に畳んで解消する。 */
    max-height: 0;
}
.menu-dropdown.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    /* 開いているときも表示領域を超えない。項目が増えてもメニュー内で
       スクロールさせ、#app 側にスクロール領域を作らない。 */
    max-height: calc(var(--app-height, 100dvh) - 5rem);
    overflow-y: auto;
    /* 端まで来たスクロールを親へ伝播させない。iOS でこれが親へ抜けると、
       overflow:hidden の #app が UA に動かされてヘッダーごと画面外へ持ち上がる
       （app.js の visualViewport 対応にある症状と同じ経路）。 */
    overscroll-behavior: contain;
    /* 古い iOS WKWebView で慣性スクロールを効かせる */
    -webkit-overflow-scrolling: touch;
}
/* flex コンテナの子は既定で縮む（flex-shrink:1）。max-height で頭打ちに
   なったとき、項目が潰れて全部収まってしまうとスクロールが発生しない。
   縮ませないことで、はみ出した分を確実にスクロールに回す。 */
.menu-dropdown > * {
    flex-shrink: 0;
}
.menu-dropdown button {
    width: 100% !important;
    padding: 0.65rem 1rem !important;
    border: none !important;
    border-radius: 0 !important;
    background: transparent !important;
    text-align: left;
    font-size: 0.85rem !important;
}
.menu-dropdown button:hover {
    background: rgba(255,255,255,0.12) !important;
}

/* Menu section labels */
.menu-section-label {
    padding: 0.75rem 1rem 0.35rem;
    font-size: 0.7rem;
    font-weight: 600;
    color: rgba(255,255,255,0.5);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Model & Personality selectors in menu */
.menu-model-selector,
.menu-personality-selector {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.3rem;
    background: transparent !important;
    border: none !important;
    padding: 0 1rem 0.5rem;
}
.menu-model-selector button,
.menu-personality-selector button {
    flex: 0 1 auto;
    padding: 0.4rem 0.7rem !important;
    font-size: 0.75rem !important;
    border: 1px solid rgba(255,255,255,0.25) !important;
    background: rgba(255,255,255,0.08) !important;
    border-radius: 4px !important;
    text-align: center;
    white-space: nowrap;
}
.menu-model-selector button:not(:last-child),
.menu-personality-selector button:not(:last-child) {
    margin-right: 0.3rem;
}
.menu-model-selector button.active,
.menu-personality-selector button.active {
    background: var(--accent-dark) !important;
    border-color: var(--accent-dark) !important;
}

/* Menu divider */
.menu-divider {
    height: 1px;
    background: rgba(255,255,255,0.15);
    margin: 0.5rem 0;
}

/* Menu error section */
.menu-error-label {
    color: rgba(251, 146, 60, 0.9) !important;
}
.menu-error-box {
    margin: 0 0.75rem 0.5rem;
    padding: 0.6rem 0.75rem;
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid rgba(239, 68, 68, 0.4);
    border-radius: 6px;
    font-size: 0.78rem;
}
.menu-error-msg {
    color: rgba(255, 200, 200, 0.95);
    line-height: 1.4;
    word-break: break-all;
}
.menu-error-time {
    color: rgba(255,255,255,0.4);
    font-size: 0.7rem;
    margin-top: 0.3rem;
}
.menu-error-clear {
    margin-top: 0.5rem;
    width: 100% !important;
    padding: 0.3rem 0 !important;
    background: rgba(255,255,255,0.1) !important;
    border: 1px solid rgba(255,255,255,0.2) !important;
    border-radius: 4px !important;
    color: rgba(255,255,255,0.7) !important;
    font-size: 0.75rem !important;
    text-align: center !important;
    cursor: pointer;
}

/* Model selector inside header */
.model-selector,
.personality-selector {
    display: flex;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.25);
    border-radius: 6px;
    overflow: hidden;
}
.model-selector button,
.personality-selector button {
    border: none;
    border-radius: 0;
    padding: 0.3rem 0.6rem;
    font-size: 0.75rem;
    background: transparent;
}
.model-selector button.active,
.personality-selector button.active {
    background: var(--accent-dark);
}

/* ===== Chat container ===== */
#chat {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 0.75rem 1rem;
    padding-bottom: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    scroll-behavior: smooth;
}

#chat::-webkit-scrollbar { width: 5px; }
#chat::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; }

/* ===== Date separator ===== */
.date-label {
    text-align: center;
    margin: 0.75rem 0 0.5rem;
    user-select: none;
    -webkit-user-select: none;
}
.date-label span {
    background: var(--date-bg);
    color: var(--date-color);
    font-size: 0.72rem;
    padding: 0.2rem 0.65rem;
    border-radius: 10px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

/* ===== Message row ===== */
.msg-row {
    display: flex;
    align-items: flex-end;
    gap: 0.4rem;
    animation: fadeIn 0.15s ease-out;
}
.msg-row.user  { flex-direction: row-reverse; }
.msg-row.assistant { flex-direction: row; }
.msg-row.system { justify-content: center; }

/* ===== Bubble ===== */
.bubble {
    max-width: 72%;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    font-size: 0.9rem;
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-word;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,0.1);
    -webkit-touch-callout: none;
    user-select: none;
    -webkit-user-select: none;
}

.msg-row.user .bubble {
    background: var(--bg-user-msg);
    color: var(--text-user);
    border-bottom-right-radius: 2px;
}

.msg-row.assistant .bubble {
    background: var(--bg-ai-msg);
    color: var(--text-ai);
    border-bottom-left-radius: 2px;
}

.msg-row.system .bubble {
    background: var(--date-bg);
    color: var(--date-color);
    font-size: 0.78rem;
    font-style: italic;
    border-radius: 10px;
    padding: 0.2rem 0.8rem;
    max-width: 90%;
    box-shadow: none;
}

/* Message metadata (read status + timestamp) */
.msg-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 2px;
    flex-shrink: 0;
    align-self: flex-end;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.read-status {
    font-size: 0.6rem;
    color: var(--text-sub);
}

/* Timestamp */
.msg-time {
    font-size: 0.65rem;
    color: var(--text-sub);
    flex-shrink: 0;
    white-space: nowrap;
}

/* ===== Empty state ===== */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-sub);
    gap: 0.5rem;
    pointer-events: none;
}
.empty-state h2 { font-size: 1.1rem; font-weight: 500; }
.empty-state p  { font-size: 0.85rem; }

/* ===== Typing indicator ===== */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}
.typing-dots {
    background: var(--bg-ai-msg);
    padding: 0.6rem 0.9rem;
    border-radius: 8px;
    border-bottom-left-radius: 2px;
    display: flex;
    gap: 4px;
    align-items: center;
    box-shadow: 0 1px 1px rgba(0,0,0,0.1);
}
.typing-dot {
    width: 7px; height: 7px;
    background: var(--text-sub);
    border-radius: 50%;
    animation: typingBounce 1.2s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

/* ===== Input area ===== */
.input-container {
    flex-shrink: 0;
    background: var(--bg-input);
    padding: 0.6rem 0.75rem;
    padding-bottom: max(0.6rem, env(safe-area-inset-bottom, 0px));
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    border-top: 1px solid var(--border);
    z-index: 20;
}

.input-container textarea {
    flex: 1;
    background: var(--bg-ai-msg);
    color: var(--text-main);
    /* bg-ai-msg と bg-input の明暗差だけで枠を表現していたが、iOS(WKWebView)は
       広色域(P3)描画でこの僅かな差が視認しにくく、枠なしに見えてしまうため
       明示的な枠線を引いて両OSで見た目を揃える */
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 0.6rem 1rem;
    font-size: 0.9rem;
    line-height: 1.4;
    resize: none;
    max-height: 120px;
    min-height: 42px;
    font-family: inherit;
    outline: none;
    overflow-y: auto;
}
.input-container textarea::placeholder { color: var(--text-sub); }

.input-container button {
    background: var(--send-btn);
    border: none;
    border-radius: 50%;
    width: 42px; height: 42px;
    flex-shrink: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    transition: background 0.15s, transform 0.1s;
}
.input-container button:hover:not(.is-disabled) { background: var(--accent-dark); }
.input-container button:active:not(.is-disabled) { transform: scale(0.93); }
/* 送信ボタンは disabled 属性を使わない（送信でボタンが disabled 化すると
   iOS(WebKit) がタップ完了前にイベント配信を止め、textarea のフォーカスが
   奪われてキーボードが閉じるため）。見た目だけクラスで無効化する。 */
.input-container button.is-disabled { opacity: 0.45; cursor: not-allowed; }

/* ===== Toast ===== */
.toast {
    position: fixed;
    top: 4rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.75);
    color: #fff;
    padding: 0.5rem 1.1rem;
    border-radius: 20px;
    font-size: 0.82rem;
    z-index: 500;
    pointer-events: none;
    animation: fadeIn 0.2s ease;
}

/* ===== Modal ===== */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: var(--dialog-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}
.modal-backdrop.show { opacity: 1; visibility: visible; }

.modal {
    background: var(--bg-ai-msg);
    border-radius: 12px;
    padding: 1.5rem;
    max-width: 360px;
    width: 88%;
    transform: scale(0.92);
    transition: transform 0.2s;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}
.modal-backdrop.show .modal { transform: scale(1); }

.modal h2 { font-size: 1.05rem; color: var(--text-main); }
.modal p  { font-size: 0.85rem; color: var(--text-sub); line-height: 1.5; }

.modal .primary {
    width: 100%;
    padding: 0.65rem;
    background: var(--error);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.15s;
}
.modal .primary:hover { background: #dc2626; }

.modal .close-btn {
    width: 100%;
    padding: 0.65rem;
    background: transparent;
    color: var(--text-sub);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.15s;
}
.modal .close-btn:hover { background: var(--border); }

/* ===== サーバー設定モーダル（ネイティブアプリ） ===== */
.server-modal .primary { background: var(--accent-dark); }
.server-modal .primary:hover { background: var(--accent); }
.server-modal .primary:disabled { opacity: 0.5; cursor: default; }

.server-input {
    width: 100%;
    box-sizing: border-box;
    padding: 0.65rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text-main);
    font-size: 16px; /* iOSのフォーカス時自動ズームを防ぐ */
}
.server-input:focus { outline: none; border-color: var(--accent); }

.server-test { font-size: 0.8rem; }
.server-test.ok { color: var(--success) !important; }
.server-test.fail { color: var(--error) !important; }

/* ===== Spinner ===== */
.spinner {
    width: 18px; height: 18px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-5px); }
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ===== Context menu ===== */
.ctx-menu {
    position: fixed;
    z-index: 300;
    background: var(--bg-ai-msg);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.35);
    overflow: hidden;
    min-width: 110px;
    animation: fadeIn 0.12s ease;
}
.ctx-menu button {
    display: block;
    width: 100%;
    padding: 0.6rem 1rem;
    background: transparent;
    border: none;
    color: var(--text-main);
    font-size: 0.88rem;
    text-align: left;
    cursor: pointer;
    transition: background 0.12s;
}
.ctx-menu button:hover { background: var(--border); }
.ctx-menu button + button { border-top: 1px solid var(--border); }
.ctx-menu button.memorize-btn.memorized { color: #2eb872; font-weight: 600; }
.ctx-menu button.memorize-btn.unmemorized { color: #e05a4a; font-weight: 600; }
.ctx-menu button.memorize-btn:disabled { opacity: 0.6; cursor: wait; }

/* ===== Edit bubble ===== */
.bubble.editing {
    padding: 0.4rem 0.5rem;
}
.edit-textarea {
    width: 100%;
    background: transparent;
    border: none;
    color: inherit;
    font-size: 0.9rem;
    font-family: inherit;
    line-height: 1.55;
    resize: none;
    outline: none;
    max-height: 200px;
    min-height: 36px;
    overflow-y: auto;
}
.edit-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.4rem;
    margin-top: 0.4rem;
}
.edit-cancel-btn, .edit-confirm-btn {
    padding: 0.25rem 0.7rem;
    border-radius: 6px;
    font-size: 0.78rem;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background 0.12s;
}
.edit-cancel-btn {
    background: transparent;
    color: var(--text-sub);
    border-color: var(--border);
}
.edit-cancel-btn:hover { background: var(--border); }
.edit-confirm-btn {
    background: var(--send-btn);
    color: #fff;
    border-color: var(--send-btn);
}
.edit-confirm-btn:hover:not(:disabled) { background: var(--accent-dark); }
.edit-confirm-btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .bubble { max-width: 85%; }
    .header h1 { font-size: 0.95rem; }
    /* iOS Safari はフォーカスした入力欄の font-size が 16px 未満だと
       自動ズームするため、モバイルでは 16px 以上を保証してズームを防ぐ */
    .input-container textarea,
    .edit-textarea,
    input,
    select,
    textarea { font-size: 16px; }
}
