:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --text: #1f1e1d;
  --muted: #75716b;
  --line: #e7e3da;
  --accent: #c2603f;
  --on-accent: #ffffff;
  --user: #f3e7dd;
  --ja: #f4f1ea;
  --code: #f0ede5;
  --danger: #c0392b;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "Segoe UI", Roboto, sans-serif;
  --font-ja: -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1d;
    --surface: #2a2927;
    --text: #f3f1eb;
    --muted: #a19c93;
    --line: #3a3834;
    --accent: #e3906f;
    --on-accent: #1f1e1d;
    --user: #3a2d26;
    --ja: #242321;
    --code: #181716;
    --danger: #ff7b6b;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }

/* ---------- header ---------- */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 96px 1fr 96px;
  align-items: center;
  min-height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(6px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(18px);
  backdrop-filter: saturate(1.6) blur(18px);
  border-bottom: 1px solid var(--line);
}
.bar-left { display: flex; justify-content: flex-start; }
.bar-right { display: flex; justify-content: flex-end; }
.bar h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  min-width: 44px;
  height: 44px;
  padding: 0 10px;
  border: 0;
  border-radius: 12px;
  background: none;
  color: var(--accent);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:active { background: var(--line); }
#back { font-size: 34px; font-weight: 300; padding-bottom: 4px; }
#action { font-size: 17px; font-weight: 700; }
#action.on { background: var(--accent); color: var(--on-accent); }

main {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 max(14px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

/* ---------- chat list ---------- */
.search { padding-top: 12px; }
.search input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.search input:focus { border-color: var(--accent); }
.status { min-height: 1.2em; margin: 10px 4px 0; font-size: 13px; color: var(--muted); }
.status.error { color: var(--danger); }
.status.busy::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border: 2px solid var(--muted);
  border-top-color: transparent;
  border-radius: 50%;
  vertical-align: -1px;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.group { margin: 22px 4px 8px; font-size: 13px; font-weight: 600; color: var(--muted); letter-spacing: .02em; }
.session {
  display: block;
  margin-bottom: 8px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: inherit;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.session:active { background: var(--ja); }
.s-title { display: block; font-weight: 600; line-height: 1.35; }
.s-meta { display: flex; gap: 8px; margin-top: 4px; font-size: 13px; color: var(--muted); }
.s-proj { max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.s-meta time { margin-left: auto; white-space: nowrap; }
.empty { padding: 48px 12px; text-align: center; color: var(--muted); }

/* ---------- chat ---------- */
.chat-head { padding: 18px 4px 4px; }
.chat-title { margin: 0; font-size: 22px; line-height: 1.3; }
.chat-title-ja { margin: 6px 0 0; font-family: var(--font-ja); font-size: 14px; color: var(--muted); }
.chat-sub { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.hint { margin: 10px 0 0; font-size: 13px; color: var(--accent); }
.day { display: flex; align-items: center; gap: 10px; margin: 26px 0 6px; font-size: 12px; color: var(--muted); }
.day::before, .day::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 12px 0; }
.msg.user { align-items: flex-end; }
.bubble {
  max-width: 100%;
  padding: 11px 15px;
  border: 1px solid var(--line);
  border-radius: 18px 18px 18px 6px;
  background: var(--surface);
  cursor: pointer;
  overflow-wrap: anywhere;
  -webkit-tap-highlight-color: transparent;
}
.msg.user .bubble { max-width: 88%; border-color: transparent; border-radius: 18px 18px 6px 18px; background: var(--user); }
.bubble:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.bubble[aria-expanded="true"] { border-color: var(--accent); }
.bubble.static { cursor: default; }
.bubble.pending .en { font-family: var(--font-ja); font-size: 15px; }
.meta { margin: 4px 8px 0; font-size: 12px; color: var(--muted); }
.ja {
  margin-top: 10px;
  padding: 10px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 12px;
  background: var(--ja);
  font-family: var(--font-ja);
  font-size: 15px;
  line-height: 1.75;
}
.msg.user .ja { background: color-mix(in srgb, var(--bg) 70%, transparent); }
.ja-label { margin-bottom: 4px; font-size: 11px; font-weight: 700; letter-spacing: .06em; color: var(--accent); }
.badge {
  display: inline-block;
  margin-bottom: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--ja);
  color: var(--muted);
  font-family: var(--font);
  font-size: 11px;
  font-weight: 600;
}
.muted { color: var(--muted); }

/* ---------- rendered markdown ---------- */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .65em; }
.md h3, .md h4, .md h5, .md h6 { margin: .9em 0 .35em; font-size: 1em; line-height: 1.35; }
.md h3 { font-size: 1.08em; }
.md ul, .md ol { margin: 0 0 .65em; padding-left: 1.35em; }
.md li { margin: .2em 0; }
.md li.d1 { margin-left: 1.2em; }
.md li.d2 { margin-left: 2.4em; }
.md li.d3 { margin-left: 3.6em; }
.md blockquote { margin: 0 0 .65em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md code { padding: .1em .35em; border-radius: 5px; background: var(--code); font-family: var(--mono); font-size: .84em; }
.md pre {
  margin: 0 0 .65em;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--code);
  font-size: 13px;
  line-height: 1.5;
  overflow-x: auto;
  overflow-wrap: normal;
  -webkit-overflow-scrolling: touch;
}
.md pre code { padding: 0; background: none; font-size: inherit; white-space: pre; }
.md .table { margin: 0 0 .65em; overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; line-height: 1.45; }
.md th, .md td { padding: 5px 9px; border: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: normal; }
.md th { background: var(--ja); }
.md a, .md .ref { color: var(--accent); }
.md hr { margin: 1em 0; border: 0; border-top: 1px solid var(--line); }

/* ---------- setup + settings ---------- */
.setup { max-width: 420px; margin: 0 auto; padding: 48px 8px; text-align: center; }
.setup-icon { border-radius: 22px; }
.setup h2 { margin: 18px 0 8px; font-size: 24px; }
.setup p { color: var(--muted); }
.keyform { display: flex; flex-direction: column; gap: 10px; margin: 22px 0 8px; }
.keyform input {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}
.keyform button, .buttons button {
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  cursor: pointer;
}
.buttons { display: flex; flex-direction: column; gap: 10px; }
.buttons button { border: 1px solid var(--line); background: var(--surface); color: var(--accent); }
.buttons button.danger { color: var(--danger); }
.fine { margin-top: 24px; font-size: 12px; line-height: 1.6; color: var(--muted); }
.fine code { font-family: var(--mono); font-size: 11px; word-break: break-all; }
.card { margin-bottom: 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); overflow: hidden; }
.kv-row { display: flex; justify-content: space-between; gap: 12px; padding: 11px 14px; border-top: 1px solid var(--line); font-size: 15px; }
.kv-row:first-child { border-top: 0; }
.kv-row span:last-child { color: var(--muted); text-align: right; overflow-wrap: anywhere; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: 0 6px 24px rgb(0 0 0 / .25);
  transform: translateX(-50%);
}
.toast button { padding: 0; border: 0; background: none; color: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; }
