:root {
  color-scheme: light dark;
  --paper: #f4f7f9;
  --ink: #1c2833;
  --sub: #55636f;
  --mint: #8fdcc2;
  --lilac: #bdb0f5;
  --surface: #fff;
  --line: #dce5ec;
  --user: #d5f3e8;
  --error: #b22c22;
  --focus: #2f6fd0;
  --code: #eaf0f4;
}

/* 既定は端末設定に従う。ヘッダーのボタンで data-theme を付けると、そちらが優先される */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --paper: #15232b;
    --ink: #edf3f7;
    --sub: #b3c2cc;
    --surface: #20333e;
    --line: #38505d;
    --user: #214a40;
    --error: #ff9b90;
    --focus: #a7c9ff;
    --code: #17272f;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #15232b;
  --ink: #edf3f7;
  --sub: #b3c2cc;
  --surface: #20333e;
  --line: #38505d;
  --user: #214a40;
  --error: #ff9b90;
  --focus: #a7c9ff;
  --code: #17272f;
}

:root[data-theme="light"] {
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.6 system-ui, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", Meiryo, sans-serif;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button, select {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 7px 12px;
}
button { cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; opacity: .5; }
button:not(:disabled):active { transform: translateY(1px); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.primary { background: var(--mint); color: #142e25; border-color: transparent; font-weight: 700; }
.app {
  max-width: 840px;
  height: 100%;
  height: 100dvh;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.header {
  padding: max(10px, env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid var(--line);
}
.brand-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#speech-toggle { font-size: 12px; padding: 7px 8px; }
.brand { margin-right: auto; color: var(--ink); text-decoration: none; font-size: 21px; font-weight: 750; white-space: nowrap; }
.brand span { font-size: 16px; font-weight: 500; color: var(--sub); }
.history-label { display: block; margin-top: 8px; font-size: 12px; color: var(--sub); }
.header select { width: 100%; min-width: 0; text-overflow: ellipsis; }
.message-scroll { min-height: 0; overflow-y: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch; padding: 24px 16px; }
.welcome { text-align: center; padding: 5vh 8px 24px; color: var(--sub); }
.welcome-mark { display: inline-grid; place-items: center; width: 64px; height: 64px; border-radius: 22px; background: var(--lilac); color: #1c2833; font-size: 32px; font-weight: 800; transform: rotate(-6deg); }
.welcome h1 { color: var(--ink); font-size: clamp(22px, 5vw, 28px); margin: 24px 0 8px; }
.welcome p { margin: 0; }
.messages { display: flex; flex-direction: column; gap: 20px; }
.message { display: flex; flex-direction: column; align-items: flex-start; max-width: 92%; align-self: flex-start; min-width: 0; }
.message.user { align-items: flex-end; align-self: flex-end; }
.message-label { color: var(--sub); font-size: 12px; padding: 0 6px 4px; }
.bubble { padding: 12px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px 18px 18px 4px; min-width: 48px; max-width: 100%; overflow-wrap: anywhere; }
.user .bubble { background: var(--user); border-radius: 18px 18px 4px 18px; }
.message-text { white-space: pre-wrap; }
.bubble p { margin: 0 0 12px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { line-height: 1.4; margin: 16px 0 8px; }
.bubble h1 { font-size: 24px; }
.bubble h2 { font-size: 21px; }
.bubble h3 { font-size: 18px; }
.bubble > :first-child { margin-top: 0; }
.bubble ul, .bubble ol { padding-left: 24px; margin: 8px 0; }
.bubble code { background: var(--code); border-radius: 5px; padding: 2px 4px; font: .9em/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; }
.bubble pre { max-width: 100%; overflow-x: auto; background: var(--code); padding: 12px; border-radius: 10px; white-space: pre; }
.bubble pre code { padding: 0; border-radius: 0; }
.bubble img { display: block; max-width: 100%; max-height: 360px; width: auto; height: auto; border-radius: 10px; margin-top: 8px; object-fit: contain; }
.message-error { color: var(--error); white-space: pre-wrap; }
.pending { color: var(--sub); }
.composer-area { position: sticky; bottom: 0; padding: 8px 16px max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--paper); }
.notices { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.login-providers { display: flex; flex-wrap: wrap; gap: 8px; }
#status { flex: 1; margin: 0; font-size: 14px; color: var(--sub); overflow-wrap: anywhere; }
#status.error, .storage-status { color: var(--error); }
.storage-status { font-size: 13px; margin: 4px 0 8px; }
.composer { padding: 8px; border-radius: 20px; border: 1px solid var(--line); background: var(--surface); }
textarea { display: block; width: 100%; resize: none; border: 0; background: transparent; padding: 8px; font-size: 16px; line-height: 24px; min-height: 44px; max-height: 160px; overflow-y: auto; }
textarea:focus-visible { outline-offset: 0; border-radius: 10px; }
.composer-actions, .attach-actions { display: flex; gap: 6px; align-items: center; }
.composer-actions { justify-content: space-between; margin-top: 4px; }
.attach-actions button { border-color: transparent; background: var(--paper); padding: 6px 10px; }
.interim { margin: 4px 8px; font-size: 14px; color: var(--sub); }
.attachments { display: flex; gap: 10px; padding: 6px; overflow-x: auto; }
.attachment { position: relative; flex: 0 0 88px; padding-top: 14px; }
.attachment img { display: block; width: 88px; height: 88px; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); }
.attachment button { position: absolute; right: -4px; top: 0; width: 44px; height: 44px; border-radius: 50%; padding: 0; font-size: 24px; box-shadow: 0 2px 5px #0002; }
#microphone.listening { background: var(--lilac); color: #1c2833; animation: pulse 1.25s ease-in-out infinite; }
.footnote { text-align: center; margin: 4px 0 0; font-size: 11px; color: var(--sub); }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px #bdb0f555; opacity: .7; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; } }
@media (max-width: 600px) {
  .notices { flex-direction: column; align-items: stretch; }
  .login-providers { flex-direction: column; }
}
@media (max-width: 374px) {
  .header, .composer-area { padding-left: 10px; padding-right: 10px; }
  .brand-row { gap: 6px; }
  .brand-row button { padding-left: 8px; padding-right: 8px; }
  .message-scroll { padding-left: 10px; padding-right: 10px; }
}
