:root {
  --bg: #f6f5f1; --paper: #ffffff; --ink: #1d2523; --muted: #6a736f; --line: #e3e1da;
  --accent: #1f6b5c; --accent-ink: #ffffff; --accent-soft: #e4f0ec;
  --mask: #fff1c7; --mask-ink: #7a5600; --keep: #eef0ee; --block: #fde4e1; --block-ink: #a12a1b;
  --radius: 10px;
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic UI", Meiryo, sans-serif;
  color: var(--ink); font-size: 15px; line-height: 1.7;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #151918; --paper: #1d2321; --ink: #e8ece9; --muted: #9aa49f; --line: #2f3734;
    --accent: #5fb39f; --accent-ink: #0d1412; --accent-soft: #23332e;
    --mask: #4a3c12; --mask-ink: #f5d98a; --keep: #2a302d; --block: #4a1f1a; --block-ink: #ffb4a8; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--ink); }
button { cursor: pointer; border: 1px solid transparent; border-radius: 8px; padding: 7px 14px; background: none; }
.primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.primary:disabled { opacity: .5; cursor: default; }
.ghost { border-color: var(--line); background: var(--paper); }
.ghost:hover { border-color: var(--accent); }
.wide { width: 100%; margin-top: 8px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono, code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.error { color: var(--block-ink); background: var(--block); padding: 8px 12px; border-radius: 8px; }
.grow { flex: 1; }

.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; letter-spacing: .04em; }
.brand.big { font-size: 24px; justify-content: center; }
.mark { display: inline-grid; place-items: center; width: 1.7em; height: 1.7em; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-size: .85em; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 32px 28px; text-align: center; }
.login-card form { text-align: left; display: grid; gap: 12px; margin-top: 16px; }

.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px; padding: 10px 20px; background: var(--paper); border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; }
.tabs button { color: var(--muted); }
.tabs button.on { color: var(--ink); background: var(--accent-soft); font-weight: 600; }
.who { margin-left: auto; display: flex; align-items: center; gap: 10px; position: relative; }
.badge { font-size: 12px; padding: 2px 10px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge.bad { background: var(--block); color: var(--block-ink); }
.menu { position: absolute; right: 0; top: 110%; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 6px; display: grid; min-width: 180px; box-shadow: 0 8px 24px rgba(0,0,0,.08); }
.menu button { text-align: left; }
.menu button:hover { background: var(--accent-soft); }

.pane { padding: 20px; }
.narrow { max-width: 960px; margin: 0 auto; }
.chat { display: grid; grid-template-columns: 230px 1fr 260px; gap: 18px; max-width: 1440px; margin: 0 auto; height: calc(100vh - 57px); }
.conv { display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 4px 2px 12px; }
.side { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; overflow-y: auto; }
.side h3 { margin: 0 0 4px; font-size: 14px; }
.mapping { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.mapping li { display: flex; gap: 8px; align-items: baseline; font-size: 13px; }
.mapping .tok { font-family: ui-monospace, Menlo, monospace; background: var(--mask); color: var(--mask-ink); border-radius: 4px; padding: 0 5px; white-space: nowrap; }

.hist { display: flex; flex-direction: column; gap: 6px; min-height: 0; overflow-y: auto; padding-right: 2px; }
.hist-head { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 12px; }
.tiny { padding: 0 8px; font-size: 13px; line-height: 1.6; }
.hist-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.hist-list li { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 5px 8px; cursor: pointer; font-size: 13.5px; }
.hist-list li:hover { background: var(--paper); }
.hist-list li.on { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.hist { overflow-x: hidden; }
.hist-list li { min-width: 0; }
.hist-list .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-list .sub { font-size: 11px; color: var(--muted); font-weight: 400; white-space: nowrap; }
.hist-list .x { visibility: hidden; padding: 0 4px; color: var(--muted); }
.hist-list li:hover .x { visibility: visible; }
.proj-bar { display: flex; gap: 10px; align-items: center; background: var(--accent-soft); border-radius: 10px; padding: 6px 12px; font-size: 13.5px; }
.proj-bar button { font-size: 12px; padding: 2px 10px; }
.danger { color: var(--block-ink); }
.empty { text-align: center; padding: 6vh 12px 12px; }
.empty h1 { font-size: 22px; margin: 0 0 6px; }
.templates { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-top: 22px; text-align: left; }
.tpl { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; text-align: left; }
.tpl:hover { border-color: var(--accent); }
.tpl small { color: var(--accent); display: block; font-size: 11.5px; }

.msg { margin: 14px 0; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 78%; padding: 10px 14px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; }
.bubble.md { white-space: normal; }
.bubble.md h4 { margin: 12px 0 4px; font-size: 15px; }
.bubble.md h4:first-child, .bubble.md p:first-child { margin-top: 0; }
.bubble.md p { margin: 6px 0; }
.bubble.md ul, .bubble.md ol { margin: 6px 0; padding-left: 1.4em; }
.bubble.md table { border-collapse: collapse; margin: 8px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.bubble.md th, .bubble.md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.bubble.md .msg-tools, .bubble.md .raw { white-space: normal; }
.bubble.md .raw { white-space: pre-wrap; }
.msg.user .bubble { background: var(--accent-soft); }
.msg.ai .bubble { background: var(--paper); border: 1px solid var(--line); }
.msg-tools { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.msg-tools button { font-size: 12px; padding: 3px 10px; }
.raw { margin-top: 8px; padding: 8px 10px; background: var(--bg); border-radius: 8px; font-size: 12.5px; white-space: pre-wrap; }
.raw b { display: block; color: var(--muted); font-weight: 600; }
.thinking { color: var(--muted); font-size: 13px; }
.dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .2s; } .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.msg.user { flex-direction: column; align-items: flex-end; }
.note { font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.save { position: relative; }
.save summary { list-style: none; cursor: pointer; font-size: 12px; padding: 3px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); margin: 0; }
.save summary::-webkit-details-marker { display: none; }
.save-menu { position: absolute; z-index: 6; bottom: 110%; left: 0; display: grid; gap: 2px; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 6px; min-width: 170px; box-shadow: 0 8px 24px rgba(0,0,0,.12); }
.save-menu button { text-align: left; border: none; }
.msg-tools .primary { font-size: 12.5px; padding: 3px 12px; }
.tpl.own { border-color: var(--accent); }
.tpl-admin { list-style: none; padding: 0; display: grid; gap: 6px; }
.tpl-admin li { display: flex; justify-content: space-between; gap: 8px; background: var(--bg); border-radius: 8px; padding: 8px 10px; font-size: 13.5px; }
.tpl-admin li small { color: var(--accent); }
.att-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; width: 100%; }
.attached { flex-direction: column; align-items: stretch; }
.attached p { margin: 4px 0 0; }
.attached textarea { margin-top: 6px; font-size: 13px; }

.compose { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.compose textarea { border: none; padding: 4px 6px; }
.compose textarea:focus { outline: none; }
.compose-bar { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.file { color: var(--ink); font-size: 14px; padding: 7px 14px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.attached { font-size: 13px; padding: 4px 8px; margin-bottom: 6px; background: var(--bg); border-radius: 6px; display: flex; gap: 8px; align-items: center; }
.attached button { padding: 0 6px; }

.preview { background: var(--paper); border: 2px solid var(--accent); border-radius: 12px; padding: 14px 16px; margin-bottom: 10px; max-height: 62vh; overflow-y: auto; }
.pv-head { display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; margin-bottom: 8px; }
.pv-block { border-top: 1px solid var(--line); padding: 10px 0; }
.pv-label { font-size: 13px; margin-bottom: 6px; }
.pv-text { white-space: pre-wrap; padding: 8px 10px; background: var(--bg); border-radius: 8px; }
.pv-text.material { max-height: 220px; overflow-y: auto; }
.pv-text.material { margin-top: 6px; font-size: 13px; }
.pv-text.material::before { content: "添付ファイル"; display: block; font-size: 11px; color: var(--muted); }
mark { border-radius: 4px; padding: 0 3px; cursor: pointer; background: var(--mask); color: var(--mask-ink); }
mark::after { content: " → " attr(data-label); font-size: 11px; opacity: .8; }
mark.keep { background: var(--keep); color: var(--ink); text-decoration: none; }
mark.keep::after { content: " そのまま"; }
mark.block { background: var(--block); color: var(--block-ink); cursor: default; text-decoration: line-through; }
mark.block::after { content: " 送れません"; text-decoration: none; }
.blocked { background: var(--block); color: var(--block-ink); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; }
.blocked button { margin-top: 8px; background: var(--block-ink); color: var(--paper); font-weight: 600; }
.pv-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.switch { display: flex; gap: 6px; align-items: center; color: var(--ink); font-size: 13px; margin: 6px 0; }
.switch input { width: auto; margin: 0; }
details summary { cursor: pointer; font-size: 13px; color: var(--accent); margin: 4px 0; }
details textarea { font-size: 13px; font-family: ui-monospace, Menlo, monospace; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font-size: 12px; background: var(--accent-soft); color: var(--accent); border-radius: 99px; padding: 1px 10px; }

.card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.card h3 { margin: 0 0 8px; font-size: 15px; display: flex; gap: 8px; align-items: center; }
.form { display: grid; gap: 10px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid2 .card { margin-bottom: 16px; }
.inline { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.inline.wrap { flex-wrap: wrap; }
.inline input:not([type=checkbox]), .inline select { width: auto; flex: 1; min-width: 120px; }
.inline .switch { flex: none; }
.tags { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.tags li { background: var(--bg); border-radius: 6px; padding: 2px 4px 2px 10px; font-size: 13px; display: flex; gap: 4px; align-items: center; }
.tags li small { color: var(--muted); }
.tags button { padding: 0 6px; color: var(--muted); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.table.small td { font-size: 12.5px; }
.mem { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; background: var(--paper); }
.mem-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.mem-head b { font-size: 15px; }
.mem p { margin: 6px 0; white-space: pre-wrap; }
.mem .badge.wait { background: var(--mask); color: var(--mask-ink); }
.mem.off { opacity: .6; }

dialog { border: 1px solid var(--line); border-radius: 14px; padding: 20px 22px; background: var(--paper); color: var(--ink); max-width: 420px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog img { display: block; margin: 8px auto; width: 200px; height: 200px; background: #fff; border-radius: 8px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-size: 14px; z-index: 20; max-width: calc(100% - 32px); }

/* hamburger and drawers */
.icon-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; padding: 0; border-radius: 10px; color: var(--ink); }
.icon-btn:hover { background: var(--accent-soft); }
.chat.nohist { grid-template-columns: 1fr 260px; }
.chat.nohist .hist { display: none; }
.side-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-close, .map-btn { display: none; }
.file { display: inline-flex; gap: 6px; align-items: center; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 25; opacity: 0; pointer-events: none; transition: opacity .2s; }

@media (max-width: 860px) {
  .top { gap: 6px; padding: 6px 10px; flex-wrap: nowrap; }
  .brand-name, #provider-badge { display: none; }
  .tabs button { padding: 6px 10px; font-size: 14px; }
  .who { margin-left: auto; }
  #user-btn { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 6px 10px; font-size: 13px; }
  input, select, textarea { font-size: 16px; }   /* keeps iPhone from zooming in */

  .pane { padding: 14px; }
  .chat { display: flex; flex-direction: column; gap: 0; padding: 0; height: calc(100vh - 51px); height: calc(100dvh - 51px); }
  .chat.nohist { display: flex; }
  .conv { flex: 1; padding: 8px 10px 10px; min-height: 0; }
  .messages { padding-bottom: 8px; }

  /* history: slides in from the left */
  .hist, .chat.nohist .hist { display: flex; position: fixed; top: 0; bottom: 0; left: 0; width: 84vw; max-width: 320px; z-index: 30;
    background: var(--bg); padding: 16px 14px; transform: translateX(-102%); transition: transform .22s ease; box-shadow: 4px 0 24px rgba(0,0,0,.2); }
  body.nav-open .hist { transform: none; }
  .hist-list li { padding: 9px 8px; font-size: 15px; }
  .hist-list .x { visibility: visible; }

  /* hidden-values panel: slides up from the bottom */
  .side { position: fixed; left: 0; right: 0; bottom: 0; max-height: 65vh; z-index: 30; border-radius: 16px 16px 0 0;
    transform: translateY(102%); transition: transform .22s ease; box-shadow: 0 -4px 24px rgba(0,0,0,.2); padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  body.map-open .side { transform: none; }
  .sheet-close { display: inline-grid; }
  .map-btn { display: inline-flex; gap: 4px; align-items: center; font-size: 13px; padding: 6px 10px; }
  .map-btn b { color: var(--accent); }
  body.nav-open .scrim, body.map-open .scrim { opacity: 1; pointer-events: auto; }

  .empty { padding: 3vh 4px 8px; }
  .empty h1 { font-size: 19px; }
  .empty > p { font-size: 13px; }
  .templates { grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
  .tpl { padding: 8px 10px; font-size: 13.5px; line-height: 1.5; }
  .bubble { max-width: 94%; padding: 9px 12px; }
  .msg { margin: 10px 0; }
  .proj-bar { font-size: 12.5px; flex-wrap: wrap; gap: 4px 8px; padding: 6px 10px; }
  .proj-bar .muted { display: none; }

  .compose { padding: 8px; border-radius: 14px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  .compose textarea { min-height: 44px; }
  .compose-bar { gap: 6px; }
  .file { padding: 6px 10px; }
  .file .long { display: none; }
  .preview { max-height: 58vh; padding: 12px; }
  .pv-head .muted { display: none; }
  .pv-actions { position: sticky; bottom: -12px; background: var(--paper); padding: 8px 0 4px; margin-top: 4px; }

  .grid2, .row { grid-template-columns: 1fr; }
  .table { display: block; overflow-x: auto; }
  dialog { width: calc(100% - 24px); max-width: none; padding: 16px; }
  .toast { bottom: calc(90px + env(safe-area-inset-bottom)); }
}
.compose.drop { outline: 2px dashed var(--accent); outline-offset: 2px; }
.move-menu { position: fixed; z-index: 40; max-height: 60vh; overflow-y: auto; right: auto; width: max-content; min-width: 200px; max-width: 300px; }
.move-menu .muted { padding: 2px 10px 4px; }
.move-menu button:disabled { opacity: .45; cursor: default; }
.hist-list li.drop { outline: 2px dashed var(--accent); background: var(--accent-soft); }
.hist-list li[draggable] { cursor: pointer; }
.mini { font-size: 12.5px; padding: 5px 8px; width: auto; border-radius: 8px; }
.toggle { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; cursor: pointer; background: var(--paper); }
.toggle input { width: auto; margin: 0; }
.toggle:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.status { font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.sources { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 12.5px; }
.sources ol { margin: 4px 0 0; padding-left: 1.4em; }
.sources a { color: var(--accent); word-break: break-all; }
.model-tag { align-self: center; margin-left: 4px; }
.chip.web { background: #e7f1fb; color: #1f5f9e; }
fieldset.models { border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px 4px; margin: 0; display: grid; gap: 6px; }
fieldset.models legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
@media (max-width: 860px) { .toggle span { font-size: 0; } .toggle span::before { content: "🌐"; font-size: 14px; } .mini { max-width: 110px; } }
.row-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.row-actions button { font-size: 12px; padding: 3px 8px; }
dialog .inline button { white-space: nowrap; }
.otp-link { display: none; text-align: center; margin: 4px auto 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; color: var(--accent); text-decoration: none; font-size: 14px; }
@media (max-width: 860px) { .otp-link { display: block; } dialog img { width: 160px; height: 160px; } #twofa-form { flex-wrap: wrap; } #twofa-form input { flex-basis: 100%; } }
#dlg-invite { width: min(560px, calc(100% - 24px)); max-width: none; }
#inv-url { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
.badge.wait { background: var(--mask); color: var(--mask-ink); }
