@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+JP:wght@400;700&family=Yuji+Syuku&display=swap');

body { font-family: 'Noto Serif JP', serif; }

.app-shell {
    min-height: 100dvh;
}

.chat-bg {
    background-image: url('https://www.transparenttextures.com/patterns/paper.png');
}

/* チャットエリア（mainタグ）の調整 */
#chat-area {
    /* ヘッダーのすぐ下に文字が来ないよう、上部に大きな余白を作る */
    padding-top: 2rem;

    /* ライトモードは古い羊皮紙のようなセピア調 */
    background-color: #e8d8b3 !important;
    background-image: url('https://www.transparenttextures.com/patterns/paper.png');
    background-blend-mode: multiply;
    -webkit-overflow-scrolling: touch;
}

#user-input {
    max-height: 35dvh;
}

@media (max-width: 768px) {
    body {
        overscroll-behavior-y: contain;
    }

    .app-shell {
        max-width: 100%;
        box-shadow: none;
    }

    #chat-area {
        padding-top: 1rem;
    }
}

/* ユーザーの発言：力強い筆跡 */
.user-msg {
    color: #1f1306;
    font-weight: 700;
    padding-left: 1rem;
    border-left: 4px solid #7b4f24;
    margin-bottom: 2rem;
}

.dark .user-msg {
    color: #f1f5f9;
    border-left-color: #94a3b8;
}

/* AIの返答：流麗な記録 */
.ai-msg {
    color: #2f2111;
    padding-left: 1rem;
    margin-bottom: 2rem;
    line-height: 2;
}

.dark .ai-msg {
    color: #cbd5e1;
}

.reveal-fade-in {
    animation: revealFadeIn 180ms ease-out;
}

@keyframes revealFadeIn {
    from { opacity: 0; transform: translateY(2px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 署名の追加（書物の題字風） */
.user-msg::before,
.ai-msg::before {
    display: block;
    margin-bottom: 0.68rem;
    white-space: pre;
    font-family: 'Yuji Syuku', 'Noto Serif JP', serif;
    font-size: 0.92rem;
    font-weight: 700;
    letter-spacing: 0.08rem;
    color: #4a2c12;
    line-height: 1.3;
}

.user-msg::before { content: "✦ " attr(data-signature) "\A"; }
.ai-msg::before { content: "✦ Codex\A"; }

.dark .user-msg::before,
.dark .ai-msg::before {
    color: #f8fafc;
}

/* 最後のメッセージ下だけ過剰な余白を抑える */
#chat-area > div:last-child .user-msg,
#chat-area > div:last-child .ai-msg {
    margin-bottom: 0.35rem;
}


/* 編集中のスタイルも「紙に書き込んでいる」風に */
[contenteditable="true"]:focus {
    outline: none;
    background-color: rgba(92, 58, 26, 0.08);
    border-left: 3px solid #8b5e34 !important;
}

.dark [contenteditable="true"]:focus {
    background-color: rgba(30, 41, 59, 0.9);
}

/* ダークモード時の背景調整（既存のものを補強） */
.dark #chat-area {
    background-color: #0f172a !important;
    background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png');
}

.emoji-icon-btn {
    border: 1px solid rgba(184, 157, 116, 0.8);
    border-radius: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    line-height: 1;
    background: rgba(255, 255, 255, 0.45);
    transition: transform 0.15s ease, background 0.2s ease;
}

.attach-menu-btn {
    border: 1px solid rgba(184, 157, 116, 0.8);
    border-radius: 9999px;
    width: 2.55rem;
    height: 2.55rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.45rem;
    line-height: 1;
    background: rgba(255, 255, 255, 0.55);
    color: #40260f;
    transition: transform 0.15s ease, background 0.2s ease;
}

.attach-menu-btn:active {
    transform: scale(0.94);
}

.dark .attach-menu-btn {
    border-color: #475569;
    background: rgba(30, 41, 59, 0.8);
    color: #f8fafc;
}

.emoji-icon-btn:active {
    transform: scale(0.94);
}

.dark .emoji-icon-btn {
    border-color: #475569;
    background: rgba(30, 41, 59, 0.8);
}

.preset-toggle-btn {
    border: 1px solid #b89d74;
    border-radius: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    line-height: 1;
    background: rgba(255, 255, 255, 0.55);
    transition: all 0.2s ease;
}

.preset-toggle-btn.is-open {
    background: #7b4f24;
    transform: translateX(2px);
}

.dark .preset-toggle-btn {
    color: #f8fafc;
    border-color: #475569;
    background: rgba(30, 41, 59, 0.8);
}

.dark .preset-toggle-btn.is-open {
    background: #334155;
}


.preset-toggle-btn {
    position: relative;
    z-index: 40;
}

.system-preset-backdrop {
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(15, 23, 42, 0.44);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

.system-preset-backdrop.is-open {
    opacity: 1;
    pointer-events: auto;
}

.system-preset-panel {
    position: fixed;
    left: 0;
    top: 0;
    width: min(84vw, 24rem);
    height: 100dvh;
    overflow-y: auto;
    z-index: 30;
    border-radius: 0 1.25rem 1.25rem 0;
    background: #e8d8b3;
    border-right: 1px solid rgba(184, 157, 116, 0.7);
    box-shadow: 0 20px 48px rgba(74, 44, 18, 0.22);
    transform: translateX(-105%);
    transition: transform 0.28s ease;
    padding-top: calc(env(safe-area-inset-top) + 6.7rem);
}

.dark .system-preset-panel {
    background: #1e293b;
    border-color: rgba(71, 85, 105, 0.9);
    box-shadow: 0 20px 48px rgba(2, 6, 23, 0.55);
}

.system-preset-panel.is-open {
    transform: translateX(0);
}


.persona-tab-btn {
    border: 1px solid #b89d74;
    border-radius: 9999px;
    padding: 0.25rem 0.8rem;
    font-size: 0.85rem;
    color: #40260f;
    background: rgba(255, 255, 255, 0.35);
    transition: all 0.15s ease;
    white-space: nowrap;
}

.persona-tab-btn.active {
    background: #7b4f24;
    border-color: #7b4f24;
    color: #fff;
    box-shadow: 0 0 0 2px rgba(123, 79, 36, 0.22);
    transform: translateY(-1px) scale(1.03);
}

.persona-tab-btn:active {
    transform: scale(0.96);
}

.dark .persona-tab-btn {
    color: #f8fafc;
    border-color: #475569;
    background: rgba(30, 41, 59, 0.72);
}

.dark .persona-tab-btn.active {
    background: #2563eb;
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.28);
}


.system-persona-tabs {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
}

.preset-sidebar-group {
    border: 1px solid rgba(184, 157, 116, 0.65);
    border-radius: 0.8rem;
    background: rgba(255, 255, 255, 0.35);
    overflow: visible;
}

.dark .preset-sidebar-group {
    border-color: rgba(71, 85, 105, 0.85);
    background: rgba(15, 23, 42, 0.45);
}

.preset-sidebar-group summary {
    list-style: none;
    cursor: pointer;
    padding: 0.65rem 0.8rem;
    font-size: 0.88rem;
    font-weight: 700;
    color: #4a2c12;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.preset-sidebar-group summary::marker,
.preset-sidebar-group summary::-webkit-details-marker {
    display: none;
}

.dark .preset-sidebar-group summary {
    color: #f8fafc;
}

.preset-sidebar-chevron {
    transition: transform 0.2s ease;
}

.preset-sidebar-group[open] .preset-sidebar-chevron {
    transform: rotate(180deg);
}

.preset-sidebar-list {
    padding: 0.2rem 0.6rem 0.7rem;
}

.persona-row {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
}
.item-menu { position: relative; }
.item-menu-toggle {
    list-style: none;
    cursor: pointer;
    color: #8b7355;
    padding: 0.15rem 0.35rem;
}
.dark .item-menu-toggle {
    color: #cbd5e1;
}
.item-menu-pop {
    position: absolute;
    right: 0;
    top: 1.7rem;
    min-width: 8.5rem;
    background: #0f172a;
    border: 1px solid #334155;
    border-radius: 0.6rem;
    padding: 0.25rem;
    display: none;
    z-index: 70;
}
.item-menu.is-open .item-menu-pop { display: block; }
.item-menu-pop button {
    width: 100%;
    text-align: left;
    color: #f8fafc;
    font-size: 0.82rem;
    padding: 0.35rem 0.5rem;
    border-radius: 0.4rem;
}
.item-menu-pop button:hover { background: #1e293b; }

.persona-row .persona-tab-btn {
    margin-inline: auto;
}

.persona-delete-btn {
    margin-left: auto;
    padding: 0.25rem 0.55rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    border: 1px solid #fda4af;
    color: #e11d48;
    background: transparent;
}

.dark .persona-delete-btn {
    border-color: #fb7185;
    color: #fecdd3;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateX(-12px); }
    to { opacity: 1; transform: translateX(0); }
}

.settings-action-btn {
    transition: transform 0.12s ease, filter 0.2s ease, box-shadow 0.22s ease;
}

.settings-action-btn:active,
.settings-action-btn.is-pressed {
    transform: scale(0.96);
    filter: brightness(1.08);
    box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.35);
}

.dark .settings-action-btn:active,
.dark .settings-action-btn.is-pressed {
    box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.22);
}

.settings-screen {
    display: flex;
    flex-direction: column;
}

.settings-screen-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: calc(env(safe-area-inset-top) + 0.9rem) 1rem 0.8rem;
    border-bottom: 1px solid #cbd5e1;
}

.settings-nav-btn {
    padding: 0.5rem 0.8rem;
    border-radius: 0.65rem;
    background: #e2e8f0;
    color: #0f172a;
    font-weight: 700;
}

.settings-screen-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.settings-view-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: #1e293b;
}

.settings-nav-card {
    width: 100%;
    text-align: left;
    padding: 1rem;
    border-radius: 1rem;
    border: 1px solid #cbd5e1;
    background: #ffffff;
    color: #0f172a;
    font-weight: 700;
}

.settings-nav-card-secondary {
    background: #1e293b;
    color: white;
}

.dark .settings-screen-header {
    border-color: #334155;
}

.dark .settings-nav-btn {
    background: #334155;
    color: #f8fafc;
}

.dark .settings-view-title {
    color: #f8fafc;
}

.dark .settings-nav-card {
    border-color: #475569;
    background: #1e293b;
    color: #f8fafc;
}

.preset-panel-tools {
    padding: 0.15rem 0.05rem 0.45rem;
}

.preset-search-input {
    width: 100%;
    border: 1px solid rgba(148, 163, 184, 0.55);
    border-radius: 0.85rem;
    padding: 0.45rem 0.65rem;
    font-size: 0.82rem;
    color: #f8fafc;
    background: rgba(15, 23, 42, 0.65);
}

.preset-search-input::placeholder {
    color: #94a3b8;
}

.scroll-to-bottom-btn {
    position: absolute;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom) + 6.2rem + var(--composer-grow-offset, 0px) + var(--composer-preview-offset, 0px));
    width: 2.6rem;
    height: 2.6rem;
    border-radius: 9999px;
    border: 1px solid rgba(123, 79, 36, 0.5);
    background: rgba(255, 255, 255, 0.9);
    color: #40260f;
    font-size: 1.2rem;
    font-weight: 700;
    box-shadow: 0 8px 20px rgba(64, 38, 15, 0.18);
    z-index: 45;
    opacity: 0;
    transform: translate(-50%, 8px);
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.scroll-to-bottom-btn.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
    pointer-events: auto;
}

.dark .scroll-to-bottom-btn {
    background: rgba(30, 41, 59, 0.92);
    color: #f8fafc;
    border-color: rgba(148, 163, 184, 0.6);
    box-shadow: 0 8px 20px rgba(2, 6, 23, 0.45);
}

.image-preview-list {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    overflow-x: auto;
    padding-bottom: 0.25rem;
}

.image-preview-item {
    position: relative;
    flex: 0 0 auto;
}

.image-preview-thumb {
    width: 3.5rem;
    height: 3.5rem;
    object-fit: cover;
    border-radius: 0.5rem;
    border: 1px solid rgba(148, 163, 184, 0.6);
    background: rgba(255, 255, 255, 0.8);
}

.image-preview-remove-btn {
    position: absolute;
    top: -0.3rem;
    right: -0.3rem;
    width: 1rem;
    height: 1rem;
    border-radius: 9999px;
    background: rgba(15, 23, 42, 0.9);
    color: #fff;
    font-size: 0.75rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.file-preview-list {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    max-height: 7.5rem;
    overflow-y: auto;
    padding-right: 0.2rem;
}

.file-preview-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    border: 1px solid rgba(139, 94, 52, 0.35);
    background: rgba(255, 255, 255, 0.52);
    border-radius: 0.6rem;
    padding: 0.38rem 0.45rem;
}

.file-preview-label {
    font-size: 0.72rem;
    line-height: 1.3;
    color: #40260f;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-preview-remove-btn {
    width: 1rem;
    height: 1rem;
    border-radius: 9999px;
    background: rgba(15, 23, 42, 0.9);
    color: #fff;
    font-size: 0.75rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
}

.dark .file-preview-item {
    border-color: rgba(148, 163, 184, 0.4);
    background: rgba(15, 23, 42, 0.45);
}

.dark .file-preview-label {
    color: #e2e8f0;
}

.message-attachment-tray {
    margin-top: 0.45rem;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.35rem;
}

.message-attachment-item {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    max-width: 15rem;
    padding: 0.25rem 0.45rem;
    border-radius: 0.6rem;
    border: 1px solid rgba(139, 94, 52, 0.35);
    background: rgba(255, 255, 255, 0.5);
    font-size: 0.72rem;
}

.message-attachment-thumb {
    width: 1.4rem;
    height: 1.4rem;
    border-radius: 0.32rem;
    object-fit: cover;
}

.message-attachment-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.dark .message-attachment-item {
    border-color: rgba(148, 163, 184, 0.4);
    background: rgba(15, 23, 42, 0.45);
    color: #e2e8f0;
}

.ai-msg p { margin: 0.35rem 0; }
.ai-msg h1, .ai-msg h2, .ai-msg h3 { font-weight: 700; line-height: 1.4; margin: 0.65rem 0 0.35rem; }
.ai-msg h1 { font-size: 1.2rem; }
.ai-msg h2 { font-size: 1.1rem; }
.ai-msg h3 { font-size: 1rem; }
.ai-msg ul { list-style: disc; margin: 0.4rem 0 0.4rem 1.3rem; padding: 0; }
.ai-msg ol { list-style: decimal; margin: 0.4rem 0 0.4rem 1.4rem; padding: 0; }
.ai-msg li { margin: 0.15rem 0; }
.ai-msg .task-list-item {
    list-style: none;
    margin-left: -1.1rem;
    display: flex;
    align-items: flex-start;
    gap: 0.45rem;
}
.ai-msg .task-list-item input[type="checkbox"] {
    margin-top: 0.32rem;
}
.ai-msg blockquote {
    margin: 0.5rem 0;
    padding: 0.2rem 0 0.2rem 0.8rem;
    border-left: 3px solid rgba(100, 116, 139, 0.55);
    color: rgba(51, 65, 85, 0.92);
}
.ai-msg hr {
    border: none;
    border-top: 1px solid rgba(100, 116, 139, 0.45);
    margin: 0.65rem 0;
}
.ai-msg table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.55rem 0;
    font-size: 0.95em;
}
.ai-msg th, .ai-msg td {
    border: 1px solid rgba(100, 116, 139, 0.35);
    padding: 0.35rem 0.45rem;
    vertical-align: top;
}
.ai-msg th {
    font-weight: 700;
    background: rgba(51, 65, 85, 0.08);
}
.ai-msg code { background: rgba(51, 65, 85, 0.12); border-radius: 0.3rem; padding: 0.08rem 0.3rem; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
.ai-msg pre { background: rgba(15, 23, 42, 0.08); border: 1px solid rgba(100, 116, 139, 0.25); border-radius: 0.5rem; padding: 0.65rem; overflow-x: auto; }
.ai-msg pre code { background: transparent; border-radius: 0; padding: 0; }
.ai-msg a { color: #1d4ed8; text-decoration: underline; }
.ai-msg del { opacity: 0.82; }
.dark .ai-msg code { background: rgba(148, 163, 184, 0.22); }
.dark .ai-msg pre { background: rgba(15, 23, 42, 0.75); border-color: rgba(148, 163, 184, 0.35); }
.dark .ai-msg a { color: #93c5fd; }
.dark .ai-msg blockquote {
    border-left-color: rgba(148, 163, 184, 0.55);
    color: rgba(226, 232, 240, 0.9);
}
.dark .ai-msg hr {
    border-top-color: rgba(148, 163, 184, 0.4);
}
.dark .ai-msg th, .dark .ai-msg td {
    border-color: rgba(148, 163, 184, 0.35);
}
.dark .ai-msg th {
    background: rgba(148, 163, 184, 0.14);
}
