/* ============ FableClone ============ */
:root {
  --bg: #eeece2;
  --bg-panel: #f5f4ed;
  --bg-input: #ffffff;
  --bg-user: #e8e5d8;
  --bg-hover: #e4e1d3;
  --text: #3d3929;
  --text-soft: #73705f;
  --accent: #d97757;
  --accent-hover: #c4643f;
  --border: #d9d6c8;
  --danger: #c0392b;
  --code-bg: #282c34;
  --shadow: 0 2px 12px rgba(0,0,0,.08);
  --radius: 14px;
}
[data-theme="dark"] {
  --bg: #262624;
  --bg-panel: #1f1f1d;
  --bg-input: #30302e;
  --bg-user: #33322d;
  --bg-hover: #3a3936;
  --text: #eae8e0;
  --text-soft: #a29f93;
  --accent: #d97757;
  --accent-hover: #e28b6d;
  --border: #45443f;
  --code-bg: #1b1d22;
  --shadow: 0 2px 12px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Pretendard", -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
#app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 264px; min-width: 264px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 12px;
  gap: 10px;
  transition: margin-left .2s ease;
}
#sidebar.collapsed { margin-left: -276px; }
#btn-expand {
  position: fixed; top: 12px; left: 8px; z-index: 20;
}
.sidebar-header { display: flex; align-items: center; justify-content: space-between; }
.logo { font-weight: 700; font-size: 17px; letter-spacing: -.2px; }
#convo-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.convo-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: var(--text-soft); font-size: 13.5px;
  border: none; background: none; text-align: left; width: 100%;
}
.convo-item:hover { background: var(--bg-hover); color: var(--text); }
.convo-item.active { background: var(--bg-hover); color: var(--text); font-weight: 600; }
.convo-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo-item .del {
  visibility: hidden; border: none; background: none; cursor: pointer;
  color: var(--text-soft); font-size: 13px; padding: 2px 4px; border-radius: 4px;
}
.convo-item:hover .del { visibility: visible; }
.convo-item .del:hover { color: var(--danger); }
.sidebar-footer { display: flex; gap: 6px; }
.list-label {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11.5px; color: var(--text-soft); text-transform: none;
  padding: 2px 4px 0; font-weight: 600; letter-spacing: .3px;
}
.icon-btn.small { font-size: 13px; padding: 2px 6px; }
#sync-status { font-size: 11.5px; color: var(--text-soft); padding: 2px 4px; min-height: 15px; }
#sync-status.error { color: var(--danger); }

/* ---------- Buttons ---------- */
.primary-btn {
  background: var(--accent); color: #fff; border: none;
  padding: 10px 14px; border-radius: 10px; cursor: pointer;
  font-size: 14px; font-weight: 600;
}
.primary-btn:hover { background: var(--accent-hover); }
.ghost-btn {
  background: none; border: 1px solid var(--border); color: var(--text-soft);
  padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.ghost-btn:hover { background: var(--bg-hover); color: var(--text); }
.danger-btn {
  background: none; border: 1px solid var(--danger); color: var(--danger);
  padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.danger-btn:hover { background: var(--danger); color: #fff; }
.icon-btn {
  background: none; border: none; cursor: pointer; font-size: 16px;
  color: var(--text-soft); padding: 6px 8px; border-radius: 8px; line-height: 1;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.send { color: var(--accent); font-size: 18px; }
.icon-btn.send:disabled { opacity: .35; cursor: default; }
.icon-btn.stop { color: var(--danger); font-size: 18px; }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 18px; border-bottom: 1px solid var(--border);
  background: var(--bg);
}
#model-label {
  background: var(--bg-panel); color: var(--text-soft);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; font-size: 13px; font-weight: 600;
}
#usage-info { margin-left: auto; font-size: 12px; color: var(--text-soft); }

#chat-scroll { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
#chat { max-width: 820px; margin: 0 auto; padding: 24px 20px 12px; }

/* ---------- Empty state ---------- */
#empty-state {
  max-width: 560px; margin: 12vh auto 0; text-align: center; padding: 0 20px;
  color: var(--text-soft);
}
#empty-state .empty-logo { font-size: 48px; }
#empty-state h1 {
  font-family: Georgia, "Nanum Myeongjo", serif; font-weight: 500;
  color: var(--text); font-size: 30px; margin: 12px 0 8px;
}
#empty-state p { margin: 4px 0; font-size: 14.5px; }
#empty-state a { color: var(--accent); }
#empty-state.hidden { display: none; }

/* ---------- Messages ---------- */
.msg { display: flex; gap: 12px; margin-bottom: 22px; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; margin-top: 2px;
}
.msg.user .avatar { background: var(--bg-user); }
.msg.assistant .avatar { background: var(--accent); color: #fff; }
.msg .bubble { min-width: 0; flex: 1; }
.msg.user .bubble {
  background: var(--bg-user); border-radius: var(--radius);
  padding: 10px 14px; white-space: pre-wrap; word-break: break-word;
}
.msg .role-label { font-size: 12px; color: var(--text-soft); margin-bottom: 4px; }
.msg .files { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.msg .files img {
  max-width: 180px; max-height: 180px; border-radius: 10px;
  border: 1px solid var(--border); object-fit: cover; cursor: pointer;
}
.file-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; font-size: 13px; color: var(--text);
  cursor: pointer; max-width: 260px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}
.file-chip:hover { border-color: var(--accent); color: var(--accent); }

/* markdown content */
.md { line-height: 1.65; word-break: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: .6em 0; }
.md h1, .md h2, .md h3 { margin: 1em 0 .4em; line-height: 1.3; }
.md ul, .md ol { margin: .5em 0; padding-left: 1.4em; }
.md li { margin: .2em 0; }
.md blockquote {
  margin: .6em 0; padding: 4px 12px; border-left: 3px solid var(--accent);
  color: var(--text-soft); background: var(--bg-panel); border-radius: 4px;
}
.md table { border-collapse: collapse; margin: .8em 0; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; font-size: 14px; }
.md th { background: var(--bg-panel); }
.md code:not(pre code) {
  background: var(--bg-hover); padding: 2px 6px; border-radius: 5px;
  font-size: 13px; font-family: "Cascadia Code", Consolas, monospace;
}
.md a { color: var(--accent); }
.md hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

/* code blocks */
.codeblock { margin: .8em 0; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
.codeblock .code-head {
  display: flex; align-items: center; justify-content: space-between;
  background: #21252b; color: #9da5b4; font-size: 12px; padding: 6px 12px;
  font-family: Consolas, monospace;
}
.codeblock .copy-btn {
  background: none; border: none; color: #9da5b4; cursor: pointer; font-size: 12px;
}
.codeblock .copy-btn:hover { color: #fff; }
.codeblock pre {
  margin: 0; padding: 12px 14px; overflow-x: auto; background: var(--code-bg);
  font-size: 13px; line-height: 1.5;
}
.codeblock pre code { font-family: "Cascadia Code", Consolas, monospace; background: none; }

/* thinking block */
.thinking {
  margin-bottom: 10px; border: 1px dashed var(--border); border-radius: 10px;
  background: var(--bg-panel); font-size: 13.5px; color: var(--text-soft);
}
.thinking summary { cursor: pointer; padding: 8px 12px; user-select: none; }
.thinking .think-body { padding: 0 14px 10px; white-space: pre-wrap; line-height: 1.6; }

/* streaming cursor & status */
.cursor { display: inline-block; width: 7px; height: 15px; background: var(--accent);
  vertical-align: text-bottom; animation: blink 1s steps(1) infinite; border-radius: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.msg-error {
  color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  padding: 10px 14px; border-radius: 10px; font-size: 14px; margin-bottom: 20px;
}
.msg-actions { margin-top: 6px; display: flex; gap: 8px; }
.msg-actions button {
  background: none; border: none; color: var(--text-soft); cursor: pointer;
  font-size: 12px; padding: 3px 6px; border-radius: 6px;
}
.msg-actions button:hover { background: var(--bg-hover); color: var(--text); }

/* ---------- Composer ---------- */
#composer-wrap { max-width: 820px; margin: 0 auto; width: 100%; padding: 0 20px 14px; }
#attach-preview { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
#attach-preview:empty { display: none; }
#attach-preview .chip {
  position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--border);
}
#attach-preview .chip.doc {
  width: auto; height: auto; min-width: 90px; max-width: 220px;
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-panel); padding: 8px 26px 8px 10px; font-size: 12.5px;
}
#attach-preview .chip .doc-icon { font-size: 16px; }
#attach-preview .chip .doc-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text);
}
#attach-preview .chip img { width: 100%; height: 100%; object-fit: cover; }
#attach-preview .chip button {
  position: absolute; top: 2px; right: 2px; border: none; border-radius: 50%;
  width: 18px; height: 18px; font-size: 11px; cursor: pointer;
  background: rgba(0,0,0,.55); color: #fff; line-height: 1;
}
#composer {
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 16px; padding: 8px 10px; box-shadow: var(--shadow);
}
#composer:focus-within { border-color: var(--accent); }
#input {
  flex: 1; border: none; outline: none; resize: none; background: none;
  color: var(--text); font-family: inherit; font-size: 15px; line-height: 1.5;
  max-height: 200px; padding: 6px 4px;
}
#composer-hint {
  text-align: center; font-size: 11.5px; color: var(--text-soft); margin-top: 8px;
  opacity: .8;
}

/* ---------- Modal ---------- */
#modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
#modal-backdrop[hidden] { display: none; }
#settings-modal {
  background: var(--bg-panel); border-radius: 16px; padding: 24px;
  width: min(520px, 92vw); max-height: 88vh; overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0,0,0,.3);
}
#settings-modal h2 { margin: 0 0 16px; font-size: 19px; }
.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 13.5px; font-weight: 600; margin-bottom: 6px; }
.field small { display: block; color: var(--text-soft); font-size: 12px; margin-top: 5px; }
.field small a { color: var(--accent); }
.field input[type="password"], .field input[type="text"], .field input[type="number"],
.field select, .field textarea {
  width: 100%; background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  font-size: 14px; font-family: inherit;
}
.field.checkbox { display: flex; align-items: flex-start; gap: 8px; }
.field.checkbox input { margin-top: 3px; }
.field.checkbox span { font-weight: 400; font-size: 13.5px; margin: 0; }
.key-row { display: flex; gap: 6px; }
.field-error { color: var(--danger); font-size: 13px; margin-top: 4px; }
.key-row input { flex: 1; }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 20px; }
.modal-actions .spacer { flex: 1; }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  #sidebar { position: fixed; z-index: 30; height: 100%; box-shadow: var(--shadow); }
  #sidebar.collapsed { margin-left: -280px; }
  .msg .images img { max-width: 120px; max-height: 120px; }
}

/* live relay */
.live-note {
  font-size: 12px; color: var(--accent); margin-bottom: 6px; font-weight: 600;
}

/* ---------- Drag & drop ---------- */
#drop-overlay {
  position: fixed; inset: 0; z-index: 60; display: none;
  align-items: center; justify-content: center; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 14%, rgba(0, 0, 0, .35));
}
#drop-overlay.show { display: flex; }
.drop-box {
  border: 3px dashed var(--accent); border-radius: 20px; padding: 36px 56px;
  font-size: 20px; font-weight: 700; color: #fff; background: rgba(0, 0, 0, .5);
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-soft); }
