/* ============================================================
   AI ヘルプ機能 — ポメキャラクター (Codex Pets 流用)
   スプライトシート: 1536 x 1872 (8 列 × 9 行 / 1 セル 192 x 208)
   表示時は 1/2 スケール (96 x 104) で描画
   ============================================================ */

#pet-host {
    position: fixed;
    left: 0;
    top: 0;
    transform: translate3d(0, 0, 0);
    z-index: 9000;
    width: 96px;
    height: 104px;
    cursor: grab;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    will-change: transform;
}

#pet-host:active {
    cursor: grabbing;
}

#pet-host.dragging {
    cursor: grabbing;
    transition: none;
}

.pet-sprite {
    width: 100%;
    height: 100%;
    background-image: url('/assets/pets/pome-blue-merle.webp');
    background-repeat: no-repeat;
    background-size: 768px 936px; /* 1 セル = 96 x 104 表示 (元 192x208 を 1/2 倍) */
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    pointer-events: none;
}

:root[data-theme="dark"] .pet-sprite {
    background-image: url('/assets/pets/pome-black-tan.webp');
}

/* +/- トグルボタン (キャラ右上に重ねる) */
.pet-toggle-button {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid #ffffff;
    color: #ffffff;
    font-family: var(--font-sans, system-ui, sans-serif);
    font-weight: 700;
    font-size: 14px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.25);
    transition: transform 0.12s ease;
    z-index: 1;
    padding: 0;
    box-sizing: border-box;
}
.pet-toggle-button:hover {
    transform: scale(1.12);
}
.pet-toggle-button.hide {
    background: #c62828;  /* − ボタン: 赤 */
}
.pet-toggle-button.show {
    background: #2f7d32;  /* + ボタン: 緑 */
}

/* + ボタン単独表示時のホスト（キャラ非表示・+ボタンのみ） */
/* − ボタンと完全に同じサイズで統一 */
#pet-host.collapsed {
    width: 22px;
    height: 22px;
}
#pet-host.collapsed .pet-sprite {
    display: none;
}
#pet-host.collapsed .pet-toggle-button {
    top: 0;
    right: 0;
    width: 22px;
    height: 22px;
    font-size: 14px;
}

/* 吹き出し（Phase D-2 で使用） */
.pet-speech-bubble {
    position: absolute;
    bottom: calc(100% + 12px);
    right: -8px;
    min-width: 140px;
    max-width: 240px;
    background: var(--bg-card, #ffffff);
    color: var(--text-primary, #1a1a1a);
    border: 1px solid var(--border, #dfdbd2);
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 0.85rem;
    line-height: 1.45;
    font-family: var(--font-sans, system-ui, sans-serif);
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
    white-space: pre-wrap;
    pointer-events: none;
    animation: pet-bubble-pop 0.18s ease-out;
}
/* 三角形: ::before で枠線色を描画し、::after で背景色を 1px 上に重ねる */
.pet-speech-bubble::before {
    content: '';
    position: absolute;
    top: 100%;
    right: 21px;
    border: 9px solid transparent;
    border-top-color: var(--border, #dfdbd2);
}
.pet-speech-bubble::after {
    content: '';
    position: absolute;
    top: calc(100% - 1px);
    right: 22px;
    border: 8px solid transparent;
    border-top-color: var(--bg-card, #ffffff);
}
@keyframes pet-bubble-pop {
    from { opacity: 0; transform: translateY(6px) scale(0.95); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* モバイル: サイズはそのまま、ドラッグできるのでユーザーが好きな位置へ移動可 */

/* ============================================================
   チャット吹き出し (Phase D-2)
   - 「まだ準備中…」と同じ吹き出し風スタイル
   - ダーク: 黒系背景 / ライト: 白背景
   - 内部仕切り無しの一枚バブル
   ============================================================ */

#pet-chat-window {
    position: fixed;
    /* left/top は JS が設定 (_positionRelativeToPet) */
    left: -9999px;
    top:  -9999px;
    z-index: 9100;
    width: 320px;
    max-width: calc(100vw - 32px);
    height: 440px;
    max-height: calc(100vh - 48px);
    display: none;
    flex-direction: column;
    background: var(--bg-card, #ffffff);
    border: 1px solid var(--border, #dfdbd2);
    border-radius: 18px;
    box-shadow: var(--shadow-lg, 0 16px 40px rgba(0,0,0,0.25));
    font-family: var(--font-sans, system-ui, sans-serif);
    color: var(--text-primary, #1a1a1a);
}
/* ダークテーマで「まだ準備中…」と同じく黒系バブル */
:root[data-theme="dark"] #pet-chat-window {
    background: #1a1a1a;
    border-color: rgba(212, 175, 55, 0.4);
    color: #ededed;
}
#pet-chat-window.open {
    display: flex;
    animation: pet-chat-pop 0.18s ease-out;
}
#pet-chat-window.dragging {
    transition: none;
    user-select: none;
    cursor: grabbing;
}
@keyframes pet-chat-pop {
    from { opacity: 0; transform: translateY(12px) scale(0.96); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* 吹き出しテール — inline SVG 背景でブラウザ依存を排除。
   JS が top:${tailY}px をセット。中央寄せは transform で。 */
.pet-chat-tail {
    position: absolute;
    width: 18px;
    height: 32px;
    pointer-events: none;
    transform: translateY(-50%);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    z-index: 1;
}

/* バブル右端から右向き (バブルが左、ポメが右) */
/* SVG: 18x32 三角形 — fill 用 (closed) と stroke 用 (open, 2辺のみ) を分けて
   バブル接合側の縦線を描かない */
.pet-chat-tail.side-right {
    right: -17px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18 32' preserveAspectRatio='none'><path d='M0 0 L18 16 L0 32 Z' fill='%23ffffff'/><path d='M1 1 L18 16 L1 31' fill='none' stroke='%23dfdbd2' stroke-width='1' stroke-linejoin='miter'/></svg>");
}
:root[data-theme="dark"] .pet-chat-tail.side-right {
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18 32' preserveAspectRatio='none'><path d='M0 0 L18 16 L0 32 Z' fill='%231a1a1a'/><path d='M1 1 L18 16 L1 31' fill='none' stroke='rgba(212,175,55,0.4)' stroke-width='1' stroke-linejoin='miter'/></svg>");
}

/* バブル左端から左向き (バブルが右、ポメが左) */
.pet-chat-tail.side-left {
    left: -17px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18 32' preserveAspectRatio='none'><path d='M18 0 L0 16 L18 32 Z' fill='%23ffffff'/><path d='M17 1 L0 16 L17 31' fill='none' stroke='%23dfdbd2' stroke-width='1' stroke-linejoin='miter'/></svg>");
}
:root[data-theme="dark"] .pet-chat-tail.side-left {
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18 32' preserveAspectRatio='none'><path d='M18 0 L0 16 L18 32 Z' fill='%231a1a1a'/><path d='M17 1 L0 16 L17 31' fill='none' stroke='rgba(212,175,55,0.4)' stroke-width='1' stroke-linejoin='miter'/></svg>");
}

/* 上部ナビ (背景なし・仕切り線なし) — ポメ追従なのでドラッグ不要 */
.pet-chat-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px 6px;
    background: transparent;
    border: 0;
    user-select: none;
    flex-shrink: 0;
}
.pet-chat-back, .pet-chat-close {
    width: 26px;
    height: 26px;
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.7;
    cursor: pointer;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: opacity 0.12s ease, background 0.12s ease;
    flex-shrink: 0;
}
.pet-chat-back:hover, .pet-chat-close:hover {
    opacity: 1;
    background: rgba(0, 0, 0, 0.06);
}
:root[data-theme="dark"] .pet-chat-back:hover,
:root[data-theme="dark"] .pet-chat-close:hover {
    background: rgba(255, 255, 255, 0.08);
}
.pet-chat-close { font-size: 20px; line-height: 1; }
.pet-chat-title {
    font-family: var(--font-display, var(--font-serif, serif));
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--brand-gold, #bca072);
}
:root[data-theme="dark"] .pet-chat-title { color: #D4AF37; }
.pet-chat-usage {
    flex: 1;
    text-align: right;
    font-size: 0.72rem;
    opacity: 0.65;
}

.pet-chat-greeting {
    padding: 8px 16px;
    font-size: 0.9rem;
    line-height: 1.6;
    background: transparent;
    border: 0;
}
.pet-chat-greeting.hidden { display: none; }

.pet-chat-chips {
    padding: 8px 16px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    background: transparent;
    border: 0;
}
.pet-chat-chips.hidden { display: none; }
.pet-chat-chip {
    background: rgba(0, 0, 0, 0.04);
    color: inherit;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 14px;
    padding: 4px 10px;
    font-size: 0.78rem;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.12s ease;
}
:root[data-theme="dark"] .pet-chat-chip {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(212, 175, 55, 0.25);
}
.pet-chat-chip:hover {
    background: var(--brand-gold, #bca072);
    color: #ffffff;
    border-color: var(--brand-gold, #bca072);
}
:root[data-theme="dark"] .pet-chat-chip:hover {
    background: #D4AF37;
    color: #1a1a1a;
}

.pet-chat-thread {
    flex: 1;
    overflow-y: auto;
    padding: 6px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: transparent;
    min-height: 0;
}

.pet-msg {
    display: flex;
    flex-direction: column;
    max-width: 88%;
}
.pet-msg-user {
    align-self: flex-end;
    align-items: flex-end;
}
.pet-msg-ai {
    align-self: flex-start;
    align-items: flex-start;
}
.pet-msg-bubble {
    padding: 8px 12px;
    border-radius: 14px;
    font-size: 0.88rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}
.pet-msg-user .pet-msg-bubble {
    background: var(--brand-gold, #bca072);
    color: #ffffff;
    border-bottom-right-radius: 4px;
}
:root[data-theme="dark"] .pet-msg-user .pet-msg-bubble {
    background: #D4AF37;
    color: #1a1a1a;
}
.pet-msg-ai .pet-msg-bubble {
    background: rgba(0, 0, 0, 0.04);
    color: inherit;
    border: 0;
    border-bottom-left-radius: 4px;
}
:root[data-theme="dark"] .pet-msg-ai .pet-msg-bubble {
    background: rgba(255, 255, 255, 0.06);
}
.pet-msg-meta {
    margin-top: 3px;
    font-size: 0.7rem;
    color: var(--text-secondary, #6e6e6e);
}
.pet-msg-quote {
    border-left: 3px solid var(--brand-gold, #bca072);
    background: var(--bg-subtle, #f9f7f0);
    padding: 6px 9px;
    margin-bottom: 6px;
    border-radius: 4px;
    font-size: 0.82rem;
    color: var(--text-primary, #1a1a1a);
    line-height: 1.5;
}
:root[data-theme="dark"] .pet-msg-quote {
    background: rgba(212, 175, 55, 0.08);
    color: #EDEDED;
    border-left-color: #D4AF37;
}
.pet-msg-summary {
    font-size: 0.88rem;
}
.pet-msg-sources {
    margin-top: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}
.pet-msg-source-chip {
    font-size: 0.7rem;
    background: var(--bg-subtle, #f9f7f0);
    color: var(--text-secondary, #6e6e6e);
    border: 1px solid var(--border, #dfdbd2);
    border-radius: 8px;
    padding: 2px 6px;
}

/* ローディング (タイプ中インジケータ) */
.pet-chat-loading {
    padding: 4px 14px;
    background: transparent;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}
.pet-chat-loading.hidden { display: none; }
.pet-chat-typing {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    background: rgba(0, 0, 0, 0.04);
    border: 0;
    border-radius: 12px;
}
:root[data-theme="dark"] .pet-chat-typing {
    background: rgba(255, 255, 255, 0.06);
}
.pet-chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--brand-gold, #bca072);
    animation: pet-typing 1.2s ease-in-out infinite;
}
.pet-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.pet-chat-typing span:nth-child(3) { animation-delay: 0.30s; }
@keyframes pet-typing {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30%           { opacity: 1;   transform: translateY(-3px); }
}

/* 入力欄 (仕切り無し、バブル内部のように) */
.pet-chat-inputbar {
    display: flex;
    gap: 6px;
    padding: 8px 14px 12px;
    background: transparent;
    border: 0;
    align-items: flex-end;
    flex-shrink: 0;
}
#pet-chat-input {
    flex: 1 1 0;
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
    resize: none;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 18px;
    padding: 8px 14px;
    font-size: 0.88rem;
    font-family: inherit;
    line-height: 1.4;
    min-height: 36px;
    max-height: 120px;
    background: rgba(0, 0, 0, 0.03);
    color: inherit;
    outline: none;
    transition: border-color 0.12s ease;
}
:root[data-theme="dark"] #pet-chat-input {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(212, 175, 55, 0.3);
    color: #ededed;
}
#pet-chat-input:focus {
    border-color: var(--brand-gold, #bca072);
}
:root[data-theme="dark"] #pet-chat-input:focus {
    border-color: #D4AF37;
}
.pet-chat-send {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 0;
    background: var(--brand-gold, #bca072);
    color: #ffffff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.12s ease;
    flex-shrink: 0;
}
:root[data-theme="dark"] .pet-chat-send {
    background: #D4AF37;
    color: #1a1a1a;
}
.pet-chat-send:hover:not(:disabled) {
    transform: scale(1.06);
}
.pet-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* スマホ */
@media (max-width: 640px) {
    #pet-chat-window {
        right: 12px;
        bottom: 12px;
        left: 12px;
        width: auto;
        max-width: none;
        height: calc(100vh - 24px);
    }
}
