:root {
  --bg: #ffffff;
  --surface: #f6f7f9;
  --surface-2: #eef0f4;
  --line: #e7e9ee;
  --line-soft: #f0f1f5;
  --fg: #14161a;
  --fg-2: #454b57;
  --muted: #8b909c;
  --accent: #3d5afe;
  --accent-soft: #eceffe;
  --ok: #14a06a;
  --warn: #b7791f;
  --err: #dc2f42;
  --r: 14px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Noto Sans JP", system-ui, sans-serif;
  --shadow: 0 1px 2px rgba(16, 18, 24, .04), 0 8px 28px rgba(16, 18, 24, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0c0f;
    --surface: #131519;
    --surface-2: #1b1e24;
    --line: #22252c;
    --line-soft: #191c21;
    --fg: #e9eaee;
    --fg-2: #b3b8c4;
    --muted: #7b818f;
    --accent: #7d95ff;
    --accent-soft: #191d33;
    --ok: #3ecf8e;
    --warn: #e0a63c;
    --err: #ff6b7a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 36px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
body { overscroll-behavior-y: none; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); text-underline-offset: 2px; }
svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.muted { color: var(--muted); }
.sm { font-size: 13.5px; }
.xs { font-size: 12px; }
.strong { font-weight: 650; }
.warn { color: var(--warn); }
.error { color: var(--err); font-size: 13px; margin: 10px 0 0; white-space: pre-wrap; }
.ok { color: var(--ok); }
.center { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.spacer { flex: 1; }

/* -------------------------------- atoms -------------------------------- */
.btn {
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 9px 16px;
  border-radius: 11px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 550;
  transition: background .15s, border-color .15s, transform .06s;
}
.btn:active { transform: scale(.985); }
.btn:hover { background: var(--surface); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.07); background: var(--accent); }
.btn.quiet { border-color: transparent; background: transparent; color: var(--muted); }
.btn.quiet:hover { background: var(--surface); color: var(--fg); }
.btn.danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 35%, transparent); }
.btn.block { width: 100%; }
.btn.lg { padding: 12px 18px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.link-btn {
  background: none; border: none; padding: 6px 2px; text-align: left;
  color: var(--muted); cursor: pointer; font-size: 13px; border-radius: 8px;
}
.link-btn:hover { color: var(--fg); }

.icon-btn {
  border: none; background: transparent; cursor: pointer; color: var(--fg-2);
  width: 36px; height: 36px; border-radius: 10px;
  display: inline-grid; place-items: center; flex: none;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--surface); color: var(--fg); }
.icon-btn:active { transform: scale(.94); }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--bg);
  border-radius: 999px; padding: 6px 12px; font-size: 13px;
  cursor: pointer; white-space: nowrap; color: var(--fg-2);
  transition: background .15s, border-color .15s, color .15s;
}
.chip:hover { background: var(--surface); }
.chip.sm { padding: 4px 10px; font-size: 12px; }
.chip.toggle[data-on="true"] { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--accent); }
.chip.model { font-weight: 600; color: var(--fg); max-width: 40vw; overflow: hidden; }
.chip.model span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.input, input[type="text"], input[type="email"], input[type="password"], input[type="search"], input[type="number"], select, textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 11px;
  padding: 10px 13px; width: 100%; outline: none; font-size: 15px;
  transition: border-color .15s, box-shadow .15s;
}
.input.sm, select.sm { padding: 8px 11px; font-size: 13.5px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.field > span { color: var(--muted); }
.toggle-row { display: flex; align-items: center; gap: 8px; }
.toggle-row input { width: auto; accent-color: var(--accent); }

.code-input { text-align: center; letter-spacing: .5em; font-size: 22px; font-family: var(--mono); padding: 14px; }
.code-input.wide { letter-spacing: .12em; font-size: 18px; }

.progress { position: fixed; top: 0; left: 0; right: 0; height: 2px; z-index: 200; background: transparent; overflow: hidden; }
.progress i { position: absolute; inset: 0; width: 40%; background: linear-gradient(90deg, transparent, var(--accent), transparent); animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(350%); } }

/* -------------------------------- login -------------------------------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(400px, 100%); }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 28px; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: -.01em; }
.brand p { margin: 0; }
.brand-mark { width: 36px; height: 36px; border-radius: 12px; background: linear-gradient(145deg, var(--accent), #a06bff); flex: none; }
.qr { display: grid; place-items: center; background: #fff; padding: 14px; border-radius: 14px; }
.qr svg { width: 208px; height: 208px; display: block; }
.secret { display: block; font-family: var(--mono); font-size: 12px; word-break: break-all; background: var(--surface); padding: 10px; border-radius: 10px; margin-top: 8px; }
.codes { font-family: var(--mono); background: var(--surface); padding: 14px; border-radius: 12px; font-size: 13px; margin: 0; }

/* --------------------------------- app --------------------------------- */
.app { display: grid; grid-template-columns: 268px 1fr; height: 100dvh; }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 29; }

.sidebar {
  background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0; z-index: 30;
}
.side-top { padding: 14px 12px 10px; display: flex; flex-direction: column; gap: 8px; }
.rooms { flex: 1; overflow-y: auto; padding: 4px 8px; display: flex; flex-direction: column; gap: 1px; }
.room {
  padding: 9px 11px; border-radius: 10px; cursor: pointer;
  display: flex; flex-direction: column; gap: 1px; border: 1px solid transparent;
}
.room:hover { background: var(--surface-2); }
.room.active { background: var(--bg); border-color: var(--line); box-shadow: var(--shadow); }
.room .t { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room .m { font-size: 11px; color: var(--muted); display: flex; gap: 8px; overflow: hidden; }
.side-bottom { padding: 10px 14px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); display: flex; flex-direction: column; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line-soft); background: var(--bg);
  position: sticky; top: 0; z-index: 10;
}
.menu { display: none; }
.room-title {
  background: transparent; border: 1px solid transparent; font-weight: 600; font-size: 15px;
  padding: 6px 8px; flex: 1; min-width: 0;
}
.room-title:hover { border-color: var(--line); }
.room-title:focus { box-shadow: none; }

/* ------------------------------ messages ------------------------------- */
.thread { flex: 1; overflow-y: auto; padding: 24px 16px 8px; scroll-behavior: smooth; }
.msg { max-width: 720px; margin: 0 auto 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--surface-2); border-radius: 18px 18px 4px 18px;
  padding: 10px 15px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .who { display: flex; align-items: center; gap: 8px; font-size: 11.5px; color: var(--muted); margin-bottom: 8px; }
.msg.assistant .who .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
.body { overflow-wrap: anywhere; }
.body > *:first-child { margin-top: 0; }
.body > *:last-child { margin-bottom: 0; }
.body h1, .body h2, .body h3 { font-size: 1.05em; margin: 1.4em 0 .5em; letter-spacing: -.01em; }
.body p { margin: 0 0 .9em; }
.body ul, .body ol { padding-left: 1.3em; margin: 0 0 .9em; }
.body li { margin: .2em 0; }
.body pre {
  background: var(--surface); border: 1px solid var(--line-soft); border-radius: 12px;
  padding: 14px; overflow-x: auto; font-family: var(--mono); font-size: 13px; line-height: 1.55;
}
.body code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: 1.5px 5px; border-radius: 6px; }
.body pre code { background: none; padding: 0; }
.body blockquote { margin: 0 0 .9em; padding-left: 14px; border-left: 2px solid var(--line); color: var(--fg-2); }
.body table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13.5px; margin-bottom: .9em; }
.body th, .body td { border: 1px solid var(--line); padding: 6px 11px; text-align: left; }
.body th { background: var(--surface); }
.body img { max-width: 100%; border-radius: 12px; }
.body hr { border: none; border-top: 1px solid var(--line); margin: 1.4em 0; }

.caret { display: inline-block; width: 7px; height: 1.05em; background: var(--accent); vertical-align: -2px; border-radius: 1px; animation: caret 1s steps(2) infinite; }
@keyframes caret { 50% { opacity: 0; } }

.status {
  display: inline-flex; align-items: center; gap: 9px; margin: 2px 0 10px;
  font-size: 12.5px; color: var(--fg-2);
  background: var(--surface); border-radius: 999px; padding: 6px 14px 6px 12px;
}
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); animation: bounce 1.15s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 70%, 100% { transform: translateY(0); opacity: .45; } 35% { transform: translateY(-4px); opacity: 1; } }
.status .el { color: var(--muted); font-variant-numeric: tabular-nums; }

.think {
  margin: 0 0 12px; border-left: 2px solid var(--line); padding: 2px 0 2px 12px;
  color: var(--muted); font-size: 13px;
}
.think .peek {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; white-space: pre-wrap;
  mask-image: linear-gradient(to bottom, #000 40%, transparent);
}
.think[open] .peek { -webkit-line-clamp: unset; mask-image: none; max-height: 320px; overflow-y: auto; }
.think summary { cursor: pointer; list-style: none; font-size: 12px; margin-bottom: 4px; }
.think summary::-webkit-details-marker { display: none; }

.notice { font-size: 12.5px; color: var(--warn); margin-bottom: 10px; display: flex; gap: 6px; }
.cites { margin-top: 12px; display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; }
.cites .h { color: var(--muted); font-size: 11.5px; }
.cites a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gen-images { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.gen-images img { max-width: min(320px, 100%); border-radius: 12px; cursor: zoom-in; }
.msg-err { color: var(--err); font-size: 13px; margin-top: 10px; white-space: pre-wrap; }
.foot { margin-top: 10px; font-size: 11.5px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.acts { display: flex; gap: 2px; margin-left: auto; }
.acts .icon-btn { width: 30px; height: 30px; }
.msg .acts { opacity: 0; transition: opacity .15s; }
.msg:hover .acts, .msg:focus-within .acts { opacity: 1; }

.empty { max-width: 460px; margin: 12vh auto; text-align: center; color: var(--muted); }
.empty h2 { font-size: 17px; color: var(--fg); margin: 0 0 8px; letter-spacing: -.01em; }
.empty p { font-size: 13.5px; margin: 0 0 6px; }

/* ------------------------------ composer -------------------------------- */
.composer-wrap {
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, transparent);
  position: sticky; bottom: 0;
}
.attachments { max-width: 720px; margin: 0 auto 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.att { display: inline-flex; gap: 7px; align-items: center; background: var(--surface); border-radius: 10px; padding: 5px 8px; font-size: 12px; }
.att img { width: 26px; height: 26px; object-fit: cover; border-radius: 6px; }
.att button { background: none; border: none; cursor: pointer; color: var(--muted); padding: 0 2px; }

.toolbar { max-width: 720px; margin: 0 auto 8px; display: flex; gap: 6px; align-items: center; overflow-x: auto; scrollbar-width: none; }
.toolbar::-webkit-scrollbar { display: none; }

.composer {
  max-width: 720px; margin: 0 auto; position: relative;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px;
  padding: 5px 6px; transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 45%, transparent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer .flat { width: 34px; height: 34px; margin-bottom: 1px; }
.composer .flat:hover { background: var(--surface-2); }
.composer textarea {
  border: none; background: transparent; padding: 9px 2px; resize: none;
  min-height: 40px; max-height: 40vh; box-shadow: none !important; border-radius: 0;
}
.composer textarea:focus { box-shadow: none; }
.send {
  width: 36px; height: 36px; border-radius: 50%; border: none; flex: none;
  background: var(--accent); color: #fff; cursor: pointer;
  display: grid; place-items: center; transition: opacity .15s, transform .06s;
}
.send:active { transform: scale(.92); }
.send:disabled { opacity: .3; cursor: not-allowed; }
.send.stop { background: var(--fg); }
#composer-hint { max-width: 720px; margin: 8px auto 0; min-height: 1em; }
.rec { color: var(--err) !important; animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { opacity: .45; } }

/* -------------------------------- sheets -------------------------------- */
.sheet {
  position: fixed; inset: 0; background: rgba(10, 12, 18, .45); z-index: 60;
  display: grid; place-items: center; padding: 20px;
  animation: fade .16s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet-card {
  background: var(--bg); border: 1px solid var(--line); border-radius: 18px;
  width: min(520px, 100%); max-height: 86dvh; display: flex; flex-direction: column;
  overflow: hidden; box-shadow: var(--shadow); animation: pop .18s cubic-bezier(.2, .9, .3, 1.1);
}
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: .6; } }
.sheet-card.lg { width: min(940px, 100%); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 14px 18px; border-bottom: 1px solid var(--line-soft); }
.sheet-head h2 { font-size: 15px; margin: 0; }
.sheet-body { padding: 18px; overflow-y: auto; }
.sheet-foot { padding: 10px 18px; border-top: 1px solid var(--line-soft); }
.filters { padding: 12px 16px; border-bottom: 1px solid var(--line-soft); display: flex; flex-direction: column; gap: 8px; }
.filter-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; align-items: center; }
.filter-row::-webkit-scrollbar { display: none; }
.filter-row select { width: auto; min-width: 118px; flex: none; }

.model-list { overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.model-row { display: flex; justify-content: space-between; gap: 12px; padding: 11px 13px; border-radius: 12px; cursor: pointer; border: 1px solid transparent; }
.model-row:hover { background: var(--surface); }
.model-row.active { border-color: color-mix(in srgb, var(--accent) 45%, transparent); background: var(--accent-soft); }
.model-row .name { font-size: 13.5px; font-weight: 600; }
.model-row .meta { font-size: 11.5px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; margin-top: 2px; }
.model-row .price { font-size: 11.5px; color: var(--fg-2); text-align: right; white-space: nowrap; font-family: var(--mono); flex: none; }
.tag { font-size: 10.5px; border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; color: var(--muted); }

.tabs { display: flex; gap: 2px; padding: 8px 12px; border-bottom: 1px solid var(--line-soft); overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { background: none; border: none; border-radius: 9px; padding: 7px 13px; cursor: pointer; font-size: 13px; color: var(--muted); white-space: nowrap; }
.tab.active { background: var(--surface-2); color: var(--fg); font-weight: 550; }

.seg { display: flex; background: var(--surface); border-radius: 12px; padding: 3px; }
.seg-btn { flex: 1; background: none; border: none; padding: 8px; border-radius: 9px; cursor: pointer; font-size: 13.5px; color: var(--muted); }
.seg-btn.active { background: var(--bg); color: var(--fg); box-shadow: var(--shadow); font-weight: 550; }
.drop { display: grid; place-items: center; border: 1.5px dashed var(--line); border-radius: 14px; padding: 26px 16px; cursor: pointer; font-size: 13.5px; color: var(--muted); text-align: center; }
.drop:hover { border-color: var(--accent); color: var(--fg); }

table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td { border-bottom: 1px solid var(--line-soft); padding: 8px; text-align: left; }
table.data th { color: var(--muted); font-weight: 600; font-size: 11.5px; position: sticky; top: 0; background: var(--bg); }
table.data td.num { text-align: right; font-family: var(--mono); font-size: 12.5px; }
.scroll-x { overflow-x: auto; }
.card { border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 12px; font-size: 14px; }
.kv { display: grid; grid-template-columns: minmax(110px, auto) 1fr; gap: 7px 16px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + env(safe-area-inset-bottom));
  background: var(--fg); color: var(--bg); padding: 11px 18px; border-radius: 12px;
  z-index: 100; font-size: 13px; max-width: min(90vw, 460px); box-shadow: var(--shadow);
  animation: pop .18s cubic-bezier(.2, .9, .3, 1.1);
}
.toast.err { background: var(--err); color: #fff; }

/* ------------------------------- mobile -------------------------------- */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(84vw, 320px);
    transform: translateX(-101%); transition: transform .24s cubic-bezier(.3, .8, .3, 1);
    padding-top: env(safe-area-inset-top);
  }
  .sidebar.open { transform: none; box-shadow: 0 0 60px rgba(0, 0, 0, .45); }
  .menu { display: inline-grid; }
  .thread { padding: 18px 14px 4px; }
  .msg { margin-bottom: 22px; }
  .composer-wrap { padding: 6px 12px calc(10px + env(safe-area-inset-bottom)); }
  .chip.model { max-width: 34vw; }

  .sheet { padding: 0; place-items: end stretch; }
  .sheet-card, .sheet-card.lg {
    width: 100%; max-height: 92dvh; border-radius: 20px 20px 0 0; border-bottom: none;
    animation: up .24s cubic-bezier(.2, .9, .3, 1);
    padding-bottom: env(safe-area-inset-bottom);
  }
  @keyframes up { from { transform: translateY(24px); opacity: .7; } }
  .sheet-head { padding-top: 16px; }
  .sheet-head::before {
    content: ''; position: absolute; top: 7px; left: 50%; transform: translateX(-50%);
    width: 38px; height: 4px; border-radius: 2px; background: var(--line);
  }
  .sheet-head { position: relative; }
  table.data { font-size: 12px; }
}

@media (hover: none) {
  .msg .acts { opacity: 1; }
}

.acts .icon-btn.done { color: var(--ok); }
.acts .icon-btn.busy { color: var(--accent); animation: pulse 1.2s infinite; }
