/* ---------- tokens ---------- */
:root {
  --bg: #fbfbf9;
  --panel: #ffffff;
  --ink: #15171a;
  --ink-2: #3d4249;
  --muted: #767b83;
  --line: #e5e4de;
  --line-2: #efeee9;
  --user: #f3f2ed;
  --code-bg: #f4f3ee;
  --accent: #0e7a58;
  --accent-soft: #e7f3ee;
  --danger: #b4322d;
  --radius: 10px;
  --r-sm: 7px;
  --shadow: 0 1px 2px rgba(20, 20, 20, .05), 0 10px 30px rgba(20, 20, 20, .06);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}
:root[data-theme="dark"] {
  --bg: #0c0e0e;
  --panel: #121515;
  --ink: #e8ebe8;
  --ink-2: #c3c8c4;
  --muted: #878f8b;
  --line: #232826;
  --line-2: #1a1e1d;
  --user: #191d1c;
  --code-bg: #171b1a;
  --accent: #35b287;
  --accent-soft: #12312a;
  --danger: #e06b64;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .35);
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 14px/1.62 ui-sans-serif, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- login gate ---------- */
.gate {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: var(--bg); z-index: 50; padding: 20px;
}
.gate-card {
  width: 100%; max-width: 330px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 24px 22px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: -.01em; font-size: 15px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.gate-card p { margin: 0; font-size: 12.5px; }
input[type="password"], input[type="text"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--bg); outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.err { color: var(--danger); font-size: 12px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px; border-radius: 8px; font-size: 13px; font-weight: 500;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { background: var(--line-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn {
  width: 28px; height: 28px; display: inline-grid; place-items: center; border-radius: 7px;
  color: var(--muted); font-size: 13px;
}
.icon-btn:hover { background: var(--line-2); color: var(--ink); }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 236px 1fr; height: 100dvh; }

/* rail */
.rail {
  border-right: 1px solid var(--line); background: var(--panel);
  display: flex; flex-direction: column; min-height: 0;
}
.rail-head { padding: 10px; border-bottom: 1px solid var(--line-2); }
.rail-head .btn { width: 100%; justify-content: flex-start; font-weight: 500; }
.convs { overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 1px; flex: 1; min-height: 0; }
.conv {
  display: flex; align-items: center; gap: 8px; padding: 7px 9px; border-radius: var(--r-sm);
  font-size: 13px; color: var(--ink-2); text-align: left; width: 100%;
}
.conv:hover { background: var(--line-2); }
.conv.on { background: var(--accent-soft); color: var(--ink); }
.conv .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .x { opacity: 0; font-size: 12px; color: var(--muted); }
.conv:hover .x { opacity: 1; }
.rail-foot { padding: 8px 10px; border-top: 1px solid var(--line-2); display: flex; align-items: center; gap: 6px; font-size: 12px; }

/* main */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  height: 46px; flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  padding: 0 14px; border-bottom: 1px solid var(--line); background: var(--bg);
  position: sticky; top: 0; z-index: 5;
}
.topbar .title { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }
.model-tag {
  display: inline-flex; align-items: center; height: 24px; padding: 0 9px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--panel);
  font-size: 11.5px; color: var(--muted); letter-spacing: -.005em; white-space: nowrap;
  user-select: none;
}

/* messages */
.messages { flex: 1; overflow-y: auto; overflow-x: hidden; min-height: 0; scroll-behavior: smooth; }
.thread { max-width: 760px; margin: 0 auto; padding: 18px 20px 8px; display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.msg { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.msg-head { display: flex; align-items: center; gap: 7px; font-size: 11px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.msg-head .who { color: var(--ink-2); }
.msg-body { font-size: 14px; color: var(--ink); min-width: 0; overflow-wrap: break-word; }
.msg.user .msg-body {
  background: var(--user); border: 1px solid var(--line-2); border-radius: 12px;
  padding: 10px 13px; align-self: flex-start; max-width: 100%;
}
.msg-body > *:first-child { margin-top: 0; }
.msg-body > *:last-child { margin-bottom: 0; }
.msg-body p { margin: 0 0 .7em; }
.msg-body ul, .msg-body ol { margin: 0 0 .7em; padding-left: 1.35em; }
.msg-body li { margin: .18em 0; }
.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4 { margin: 1.1em 0 .45em; line-height: 1.3; font-size: 1em; font-weight: 650; }
.msg-body h1 { font-size: 1.18em; } .msg-body h2 { font-size: 1.1em; } .msg-body h3 { font-size: 1.02em; }
.msg-body code { font-family: var(--mono); font-size: .87em; background: var(--code-bg); padding: .12em .38em; border-radius: 5px; }
.msg-body pre {
  position: relative; background: var(--code-bg); border: 1px solid var(--line-2); border-radius: 9px;
  padding: 11px 12px; overflow-x: auto; margin: 0 0 .75em; font-size: 12.5px; line-height: 1.55;
}
.msg-body pre code { background: none; padding: 0; font-size: inherit; }
.msg-body blockquote { margin: 0 0 .7em; padding-left: 12px; border-left: 2px solid var(--line); color: var(--ink-2); }
.msg-body table { border-collapse: collapse; font-size: 13px; margin: 0 0 .8em; display: block; overflow-x: auto; }
.msg-body th, .msg-body td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.msg-body th { background: var(--line-2); font-weight: 600; }
.msg-body img { max-width: 100%; border-radius: 8px; }
.msg-body hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.copy-code {
  position: absolute; top: 6px; right: 6px; font-size: 11px; color: var(--muted);
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 2px 7px; opacity: 0;
  transition: opacity .15s;
}
pre:hover .copy-code { opacity: 1; }

/* images in messages */
.imgs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 2px; max-width: 100%; }
.img-card { position: relative; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--panel); max-width: 100%; }
.img-card img { display: block; max-width: min(380px, 100%); max-height: 380px; width: auto; height: auto; }
.img-card .acts {
  position: absolute; inset: auto 0 0 0; display: flex; gap: 6px; padding: 7px;
  background: linear-gradient(to top, rgba(0,0,0,.55), transparent); opacity: 0; transition: opacity .15s;
}
.img-card:hover .acts { opacity: 1; }
.img-card .acts button {
  font-size: 11.5px; color: #fff; background: rgba(0,0,0,.42); border: 1px solid rgba(255,255,255,.28);
  border-radius: 6px; padding: 3px 8px; backdrop-filter: blur(4px);
}
.img-card .acts button:hover { background: rgba(0,0,0,.62); }

/* status / typing */
.pending { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--muted); animation: bob 1.1s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .16s; } .dots i:nth-child(3) { animation-delay: .32s; }
@keyframes bob { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px; }
@keyframes blink { 50% { opacity: 0; } }

/* composer */
.composer-wrap { flex: 0 0 auto; padding: 8px 20px 12px; background: linear-gradient(to top, var(--bg) 65%, transparent); }
.composer { max-width: 760px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); transition: border-color .15s; }
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.composer textarea {
  width: 100%; border: 0; outline: none; resize: none; background: transparent;
  padding: 11px 13px 4px; max-height: 180px; line-height: 1.55; font-size: 14px;
  display: block;
}
.composer-bar { display: flex; align-items: center; gap: 6px; padding: 6px 8px 8px; flex-wrap: wrap; }
.seg { display: inline-flex; background: var(--line-2); border-radius: 8px; padding: 2px; }
.seg button { font-size: 12px; padding: 3px 10px; border-radius: 6px; color: var(--muted); font-weight: 500; }
.seg button.on { background: var(--panel); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.07); }
.img-opts { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; max-width: 100%; min-width: 0; }
.img-opts select {
  font-size: 11.5px; color: var(--ink-2); border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); padding: 3px 5px; outline: none; max-width: 96px; flex: 0 0 auto;
}

/* strength slider */
.strength { display: flex; align-items: center; gap: 7px; min-width: 0; }
.strength input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 108px; height: 16px;
  background: transparent; outline: none; cursor: pointer; flex: 0 0 auto; margin: 0;
}
.strength input[type="range"]::-webkit-slider-runnable-track {
  height: 3px; border-radius: 999px;
  background: linear-gradient(to right, var(--accent) 0 var(--pct, 66%), var(--line) var(--pct, 66%) 100%);
}
.strength input[type="range"]::-moz-range-track {
  height: 3px; border-radius: 999px;
  background: linear-gradient(to right, var(--accent) 0 var(--pct, 66%), var(--line) var(--pct, 66%) 100%);
}
.strength input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 11px; height: 11px; margin-top: -4px;
  border-radius: 50%; background: var(--panel); border: 2px solid var(--accent);
  box-shadow: 0 1px 2px rgba(0,0,0,.12); transition: transform .12s;
}
.strength input[type="range"]::-moz-range-thumb {
  width: 11px; height: 11px; border-radius: 50%; background: var(--panel);
  border: 2px solid var(--accent); box-shadow: 0 1px 2px rgba(0,0,0,.12);
}
.strength input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.15); }
.strength input[type="range"]:focus-visible { outline: 2px solid var(--accent-soft); outline-offset: 2px; border-radius: 4px; }
.strength-read { display: inline-flex; align-items: baseline; gap: 5px; min-width: 0; }
.strength-read b { font-size: 11.5px; font-weight: 600; color: var(--ink); white-space: nowrap; }
.strength-read span { font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-note { font-size: 11.5px; color: var(--muted); }
.refs { max-width: 760px; margin: 0 auto 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.ref {
  display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--ink-2);
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 4px 2px 3px;
}
.ref img { width: 20px; height: 20px; object-fit: cover; border-radius: 50%; }
.ref button { color: var(--muted); font-size: 11px; padding: 0 4px; }
.hint { max-width: 760px; margin: 6px auto 0; font-size: 11px; color: var(--muted); text-align: center; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 9px;
  font-size: 12.5px; z-index: 90; box-shadow: var(--shadow); max-width: 90vw;
}
.toast.warn { background: var(--danger); color: #fff; }

/* empty */
.empty { max-width: 760px; margin: 0 auto; padding: 64px 20px; text-align: center; color: var(--muted); }
.empty h2 { font-size: 15px; color: var(--ink); font-weight: 600; margin: 0 0 6px; }
.empty p { margin: 0; font-size: 13px; }
.empty .keys { margin-top: 14px; font-size: 12px; display: inline-flex; gap: 14px; }
kbd { font-family: var(--mono); font-size: 10.5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; background: var(--panel); color: var(--ink-2); }

/* mobile */
.backdrop { display: none; }
@media (max-width: 560px) {
  .img-opts {
    order: 10; flex: 1 0 100%; flex-wrap: nowrap; overflow: hidden;
    padding-bottom: 1px;
  }
  .strength { flex: 1 1 auto; gap: 8px; min-width: 0; }
  .strength input[type="range"] { width: 100%; flex: 1 1 auto; }
  .strength-read span { display: none; }
  .strength-read b { font-size: 12px; }
  .img-opts select { flex: 0 0 auto; max-width: 76px; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .rail {
    position: fixed; z-index: 40; inset: 0 auto 0 0; width: 250px;
    transform: translateX(-102%); transition: transform .18s ease; box-shadow: var(--shadow);
  }
  .rail.open { transform: none; }
  .backdrop.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 39; }
  .thread { padding: 14px 14px 6px; }
  .composer-wrap { padding: 6px 10px 10px; }
}
