/* MiniChat — 차분한 회청색 바탕, 파란 포인트. 답변마다 '어느 모델이 답했는지'를 조용히 보여준다. */
:root {
  --bg: #eceff3;
  --panel: #f6f8fa;
  --raised: #ffffff;
  --ink: #17202c;
  --ink-2: #566274;
  --line: #d3d9e2;
  --accent: #2b4fd6;
  --accent-ink: #ffffff;
  --user-bg: #dde5f7;
  --code-bg: #e5e9ef;
  --warn: #b3261e;
  --mono: ui-monospace, "JetBrains Mono", "D2Coding", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a24;
    --panel: #19212d;
    --raised: #1f2836;
    --ink: #e6ebf3;
    --ink-2: #9aa6b8;
    --line: #2b3546;
    --accent: #7d97ff;
    --accent-ink: #0c1226;
    --user-bg: #253254;
    --code-bg: #0f151e;
    --warn: #ff8a80;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
[hidden] { display: none !important; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15.5px/1.7 "Pretendard Variable", Pretendard, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 버튼 ---------- */
.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  border-radius: 9px;
  padding: 0.55rem 1.05rem;
  font-weight: 600;
}
.primary:hover { filter: brightness(1.06); }
.primary.wide { width: 100%; }
.ghost {
  background: transparent;
  border: 0;
  border-radius: 7px;
  padding: 0.35rem 0.6rem;
  color: var(--ink-2);
}
.ghost:hover { background: var(--line); color: var(--ink); }
.ghost.small { font-size: 13px; padding: 0.2rem 0.5rem; }
.icon-btn {
  display: none;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 0.4rem;
  color: var(--ink-2);
}

/* ---------- 로그인 ---------- */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
.login-card {
  width: min(22rem, 100%);
  display: grid;
  gap: 0.9rem;
  padding: 1.6rem;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.login-card h1 { margin: 0 0 0.2rem; font-size: 22px; letter-spacing: -0.01em; }
.login-card label { display: grid; gap: 0.3rem; font-size: 14px; color: var(--ink-2); }
.login-card input {
  padding: 0.6rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--ink);
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.error { margin: 0; min-height: 1.3em; color: var(--warn); font-size: 14px; }

/* ---------- 전체 레이아웃 ---------- */
.app {
  display: grid;
  grid-template-columns: 17.5rem minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.rail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.rail-top { padding: calc(0.75rem + env(safe-area-inset-top)) 0.75rem 0.5rem; }
.conv-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0.25rem 0; }
.rail-empty { margin: 1rem; color: var(--ink-2); font-size: 14px; }
.rail-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem calc(0.5rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.who { font-size: 14px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.conv { display: flex; align-items: center; gap: 0.15rem; margin: 1px 0.5rem; border-radius: 8px; }
.conv:hover { background: var(--line); }
.conv.active { background: var(--raised); box-shadow: inset 2px 0 0 var(--accent); }
.conv-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: 0;
  padding: 0.45rem 0.6rem;
}
.conv-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14.5px; line-height: 1.5; }
.conv-time { display: block; font-size: 12px; color: var(--ink-2); line-height: 1.4; }
.conv-act { display: none; flex: none; }
.conv:hover .conv-act, .conv:focus-within .conv-act, .conv.active .conv-act { display: flex; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: calc(0.5rem + env(safe-area-inset-top)) 1rem 0;
}
.presets { display: flex; gap: 1.25rem; flex: 1; min-width: 0; }
.presets button {
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 0.4rem 0.05rem;
  color: var(--ink-2);
  font-weight: 500;
}
.presets button[aria-checked="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 650; }

.quota { display: flex; align-items: center; gap: 0.5rem; font-size: 12.5px; color: var(--ink-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.quota-bar { width: 3.5rem; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.quota-fill { display: block; height: 100%; width: calc(var(--p, 0) * 100%); background: var(--accent); }
.quota.low { color: var(--warn); }
.quota.low .quota-fill { background: var(--warn); }

.chain { padding: 0.15rem 1rem 0.5rem; font-size: 12.5px; color: var(--ink-2); border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.chain .lab { margin-right: 0.35rem; }
.chain .name { color: var(--ink); margin-right: 0.9rem; }
.chain .paid { margin-left: 0.25rem; color: var(--warn); }

.scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.messages { max-width: 46rem; margin: 0 auto; padding: 1.5rem 1rem 2rem; }
.empty { margin: 12vh 0 0; text-align: center; color: var(--ink-2); }

/* ---------- 메시지 ---------- */
.msg { margin: 0 0 1.7rem; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .body {
  max-width: 88%;
  padding: 0.55rem 0.9rem;
  background: var(--user-bg);
  border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant { padding-left: 1rem; border-left: 2px solid var(--line); }
.msg.assistant.streaming { border-left-color: var(--accent); }
.msg.error { padding-left: 1rem; border-left: 2px solid var(--warn); color: var(--warn); }
.msg.error p { margin: 0 0 0.4rem; }

.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; margin-top: 0.35rem; min-height: 1.2rem; font-size: 12.5px; color: var(--ink-2); }
.meta .fb { padding: 0 0.4rem; border: 1px solid currentColor; border-radius: 4px; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.85rem; overflow-wrap: anywhere; }
.md h3, .md h4, .md h5, .md h6 { margin: 1.3rem 0 0.5rem; line-height: 1.4; letter-spacing: -0.01em; }
.md h3 { font-size: 19px; }
.md h4 { font-size: 17px; }
.md h5, .md h6 { font-size: 15.5px; }
.md ul, .md ol { margin: 0 0 0.85rem; padding-left: 1.35rem; }
.md li { margin: 0.15rem 0; }
.md li > ul, .md li > ol { margin: 0.2rem 0 0.2rem; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.2rem 0; }
.md blockquote { margin: 0 0 0.85rem; padding: 0.1rem 0 0.1rem 0.9rem; border-left: 3px solid var(--line); color: var(--ink-2); }
.md a { color: var(--accent); }
.md code { font-family: var(--mono); font-size: 0.9em; background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 4px; }

.code { margin: 0 0 0.95rem; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--code-bg); }
.code-bar { display: flex; align-items: center; justify-content: space-between; padding: 0.15rem 0.35rem 0.15rem 0.75rem; background: var(--panel); border-bottom: 1px solid var(--line); font-size: 12.5px; color: var(--ink-2); }
.code .copy { background: none; border: 0; border-radius: 6px; padding: 0.2rem 0.55rem; color: var(--ink-2); font-size: 12.5px; }
.code .copy:hover { background: var(--line); color: var(--ink); }
.code pre { margin: 0; padding: 0.75rem 0.9rem; overflow-x: auto; tab-size: 4; }
.code pre code { padding: 0; background: none; font-size: 13.5px; line-height: 1.6; }

.table-wrap { margin: 0 0 0.95rem; overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14.5px; font-variant-numeric: tabular-nums; }
.md th, .md td { padding: 0.4rem 0.8rem; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.md th { background: var(--panel); font-weight: 600; }

.msg.streaming .body > :last-child::after {
  content: "";
  display: inline-block;
  width: 0.5em;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.18em;
  background: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

/* ---------- 입력창 ---------- */
.composer-wrap { padding: 0.5rem 1rem calc(0.75rem + env(safe-area-inset-bottom)); }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  max-width: 46rem;
  margin: 0 auto;
  padding: 0.45rem;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.composer textarea {
  flex: 1;
  min-width: 0;
  max-height: 220px;
  padding: 0.4rem 0.5rem;
  background: transparent;
  border: 0;
  outline: none;
  resize: none;
}
.composer textarea::placeholder { color: var(--ink-2); }

.sr-copy { position: fixed; top: -1000px; opacity: 0; }
.scrim { position: fixed; inset: 0; z-index: 19; background: rgb(0 0 0 / 0.45); }

/* ---------- 좁은 화면 ---------- */
@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .icon-btn { display: inline-flex; }
  .rail {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(84vw, 20rem);
    transform: translateX(-100%);
    transition: transform 0.2s ease, visibility 0s linear 0.2s;
    visibility: hidden;
  }
  .app.rail-open .rail { transform: none; visibility: visible; transition: transform 0.2s ease, visibility 0s; }
  .conv-act { display: flex; }
  .quota-bar { display: none; }
  .bar { gap: 0.4rem; padding-left: 0.5rem; }
}

@media (prefers-reduced-motion: reduce) {
  .rail { transition: none; }
  .msg.streaming .body > :last-child::after { animation: none; }
}
