:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #14161a;
  --muted: #6b7280;
  --line: #e6e8ec;
  --accent: #0a84ff;
  --accent-soft: #eaf4ff;
  --green: #12a150;
  --amber: #b45309;
  --radius: 16px;
  --shadow: 0 8px 30px rgba(20, 22, 26, 0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro SC", "PingFang SC", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

#app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* Sidebar */
#sidebar {
  width: 268px;
  flex: 0 0 268px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 12px;
}
.side-top { display: flex; }
.side-bottom { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.conv-list { overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.conv-item {
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-item:hover { background: #f2f3f5; }
.conv-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.status-line { font-size: 12px; color: var(--muted); line-height: 1.4; word-break: break-all; }

/* Main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  height: 56px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
#topbar .title { display: flex; flex-direction: column; min-width: 0; }
#topbar .title span { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#toggle-sidebar { display: inline-flex; }

/* Manual sidebar collapse (desktop) */
body.sidebar-collapsed #sidebar { display: none; }
body.sidebar-collapsed #main { margin: 0 auto; }

.banner {
  margin: 12px 16px 0;
  padding: 10px 14px;
  background: #fff7e6;
  border: 1px solid #ffe0a3;
  color: var(--amber);
  border-radius: 12px;
  font-size: 13px;
}
.hidden { display: none !important; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}
.msg { max-width: 84%; padding: 11px 14px; border-radius: 16px; line-height: 1.55; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.03); }
.msg.thinking { color: var(--muted); font-style: italic; }

/* ---------- Receipt / ticket style draft ---------- */
.receipt {
  --accent-type: #e5484d;
  align-self: flex-start;
  width: 100%;
  max-width: 400px;
  background: linear-gradient(180deg, #fffdf9 0%, #fffbf3 100%);
  border: 1px solid #eee3cf;
  border-radius: 18px;
  padding: 16px 18px 14px;
  box-shadow: 0 14px 34px rgba(90, 66, 22, 0.12), 0 1px 0 #fff inset;
  position: relative;
  overflow: hidden;
}
.receipt[data-type="withdrawal"] { --accent-type: #e5484d; }
.receipt[data-type="deposit"] { --accent-type: #12a150; }
.receipt[data-type="transfer"] { --accent-type: #0a84ff; }
.receipt::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 4px;
  background: var(--accent-type);
  opacity: .9;
}
.receipt-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.receipt-brand {
  font-size: 11px;
  letter-spacing: .14em;
  font-weight: 700;
  color: #a08a5e;
  text-transform: uppercase;
}
.receipt-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: #f1ede4;
  color: #8a7a58;
}
.receipt-badge.s-dry { background: #e7f1ff; color: #0a6fd8; }
.receipt-badge.s-ready { background: #fff3d6; color: #b47409; }
.receipt-badge.s-done { background: #e4f7ec; color: #0f8a45; }
.receipt-badge.s-fail { background: #fdeaea; color: #c53b3f; }
.receipt-copy {
  margin-left: auto;
  font-size: 12px;
  padding: 4px 9px;
  border: 1px solid #e6dcc6;
  border-radius: 9px;
  background: #fff;
  color: #8a7a58;
  cursor: pointer;
}
.receipt-copy:hover { background: #faf5ea; color: #6f5f3e; }
.receipt-copy:active { transform: scale(.95); }

.receipt-divider {
  position: relative;
  border-top: 1.5px dashed #e2d6bd;
  margin: 12px -18px;
}
.receipt-divider::before,
.receipt-divider::after {
  content: "";
  position: absolute;
  top: -8px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid #eee3cf;
}
.receipt-divider::before { left: -7px; }
.receipt-divider::after { right: -7px; }

.receipt-amount {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 6px 2px 10px;
}
.receipt-amount .sign {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-type);
  background: #f1ede4;
  padding: 3px 9px;
  border-radius: 999px;
}
.receipt[data-type="withdrawal"] .sign { background: #fdeaea; color: #e5484d; }
.receipt[data-type="deposit"] .sign { background: #e4f7ec; color: #12a150; }
.receipt[data-type="transfer"] .sign { background: #e7f1ff; color: #0a84ff; }
.receipt-amount .amt {
  font-size: 30px;
  font-weight: 750;
  letter-spacing: -0.5px;
  color: #2b2418;
  font-variant-numeric: tabular-nums;
}
.receipt-amount .cur { font-size: 14px; color: #a08a5e; font-weight: 600; }

.receipt-rows { display: flex; flex-direction: column; }
.receipt-row {
  display: grid;
  grid-template-columns: 64px 1fr 22px;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: none;
  border-bottom: 1px dashed #efe7d6;
  padding: 9px 2px;
  font-family: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  border-radius: 8px;
}
.receipt-row:last-child { border-bottom: none; }
.receipt-row:hover { background: #fdf8ee; }
.receipt-row .k { color: #a08a5e; font-size: 12.5px; }
.receipt-row .v { color: #2b2418; font-weight: 600; text-align: right; word-break: break-word; }
.receipt-row .pencil { color: #c9bda3; font-size: 12px; opacity: 0; transition: opacity .15s; text-align: right; }
.receipt-row:hover .pencil { opacity: 1; }

.receipt-issues { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.receipt-issues .issue { font-size: 13px; color: #b45309; background: #fff7e6; border: 1px solid #ffe3ac; border-radius: 10px; padding: 6px 10px; }
.receipt-risk { margin-top: 10px; font-size: 13px; color: #c53b3f; background: #fdeaea; border-radius: 10px; padding: 6px 10px; }
.receipt-note { margin-top: 10px; font-size: 12px; color: #a08a5e; }
.receipt-actions { display: flex; gap: 8px; margin-top: 6px; }
.primary-btn.block { flex: 1; justify-content: center; }

.receipt-result { margin-top: 4px; }
.receipt-result .result-title { color: #0f8a45; font-weight: 700; margin-bottom: 8px; font-size: 15px; }
.receipt-result .result-line {
  display: flex; justify-content: space-between; gap: 10px;
  font-size: 13.5px; padding: 4px 2px; border-bottom: 1px dashed #efe7d6;
}
.receipt-result .result-line:last-of-type { border-bottom: none; }
.receipt-result .result-line span { color: #a08a5e; }
.receipt-result .result-line strong { color: #2b2418; font-weight: 650; text-align: right; }

.mini-btn, .chip, .ghost-btn, .primary-btn, .icon-btn { font-family: inherit; cursor: pointer; border: none; }
.mini-btn { background: #f2f3f5; border-radius: 8px; padding: 4px 10px; font-size: 12px; color: var(--muted); }
.mini-btn:hover { background: #e8eaee; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 6px; }
.chip { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.chip:hover { background: #dcecfd; }
.primary-btn { background: var(--accent); color: #fff; border-radius: 12px; padding: 9px 16px; font-size: 14px; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center; }
.primary-btn:hover { background: #0071e3; }
.ghost-btn { background: #f2f3f5; color: var(--text); border-radius: 12px; padding: 9px 14px; font-size: 14px; text-decoration: none; display: inline-flex; align-items: center; }
.ghost-btn:hover { background: #e8eaee; }
.icon-btn { background: transparent; font-size: 20px; padding: 4px 8px; }

/* Composer */
.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(255,255,255,.9);
  backdrop-filter: blur(12px);
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}
#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 11px 14px;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
  outline: none;
  background: #fff;
  max-height: 160px;
}
#input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal-card { background: #fff; border-radius: 18px; padding: 20px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow); }
.modal-card h3 { margin: 0 0 6px; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.modal-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.modal-card input, .modal-card select { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-family: inherit; font-size: 14px; color: var(--text); }
.modal-card .row { display: flex; gap: 10px; }
.modal-card .row label { flex: 1; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }
.test-result { font-size: 13px; color: var(--muted); min-height: 18px; margin-bottom: 8px; }

/* Provider fixed info + model picker */
.fixed-info {
  background: #f6f8fb;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.fixed-row { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; color: var(--muted); }
.fixed-row code { color: var(--text); background: #eef1f6; padding: 1px 6px; border-radius: 6px; font-size: 12px; word-break: break-all; text-align: right; }
.model-picker { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 12px; }
.picker-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.current-model { font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.ghost-btn.small { padding: 5px 10px; font-size: 13px; border-radius: 9px; }
#s-model-select { width: 100%; }

/* In-page editor / confirm dialogs */
.modal-card textarea,
.modal-card #ed-input,
#ed-body select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: inherit;
  font-size: 15px;
  color: var(--text);
  background: #fff;
  outline: none;
}
#ed-body textarea { resize: vertical; line-height: 1.5; }
#ed-body select:focus, #ed-body input:focus, #ed-body textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.confirm-body { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.confirm-line { font-size: 14px; color: var(--text); line-height: 1.5; }
.confirm-line:first-child { color: var(--muted); }
.primary-btn.danger { background: #e5484d; }
.primary-btn.danger:hover { background: #cf3b40; }

/* Markdown in assistant messages */
.msg.md { line-height: 1.62; white-space: normal; }.msg.md > *:first-child { margin-top: 0; }
.msg.md > *:last-child { margin-bottom: 0; }
.msg.md p { margin: 0 0 8px; }
.msg.md h1, .msg.md h2, .msg.md h3, .msg.md h4 { margin: 14px 0 6px; line-height: 1.3; }
.msg.md h1 { font-size: 18px; }
.msg.md h2 { font-size: 16.5px; }
.msg.md h3 { font-size: 15.5px; }
.msg.md h4 { font-size: 15px; }
.msg.md ul, .msg.md ol { margin: 4px 0 10px; padding-left: 22px; }
.msg.md li { margin: 3px 0; }
.msg.md blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 10px 10px 0;
  color: #29435f;
}
.msg.md hr { border: none; border-top: 1px solid var(--line); margin: 12px 0; }
.msg.md code {
  background: #f1f3f6;
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg.md pre {
  background: #0f1720;
  color: #e6edf3;
  padding: 12px 14px;
  border-radius: 12px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg.md pre code { background: none; color: inherit; padding: 0; font-size: 12.5px; }
.msg.md strong { font-weight: 650; }
.md-table-wrap { overflow-x: auto; margin: 8px 0; }
.msg.md table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
.msg.md th, .msg.md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; white-space: nowrap; }
.msg.md th { background: #f7f8fa; font-weight: 600; }

/* Field lists rendered as mini receipts */
.msg.md .md-receipt {
  margin: 8px 0;
  background: linear-gradient(180deg, #fffdf9, #fffbf3);
  border: 1px solid #eee3cf;
  border-radius: 14px;
  padding: 10px 14px;
  box-shadow: 0 6px 18px rgba(90, 66, 22, 0.07);
}
.md-receipt-title { font-size: 13px; font-weight: 650; color: #6f5f3e; margin-bottom: 6px; }
.md-receipt-rows { display: flex; flex-direction: column; }
.md-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: baseline;
  padding: 6px 0;
  border-bottom: 1px dashed #efe7d6;
  font-size: 13.5px;
}
.md-row:last-child { border-bottom: none; }
.md-row .k { color: #a08a5e; white-space: nowrap; }
.md-row .v { color: #2b2418; font-weight: 600; text-align: right; word-break: break-word; }

/* Thinking ticker */
.thinking { display: flex; align-items: center; gap: 9px; color: var(--muted); font-style: normal; }
.thinking .spinner {
  width: 13px; height: 13px; flex: 0 0 auto;
  border: 2px solid #dbe7f5;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
.thinking .tip { animation: tipIn .35s ease-out; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes tipIn { from { opacity: .15; transform: translateY(2px); } }


/* Nicer modal interactions */
.modal { animation: fadeIn .16s ease-out; }
.modal-card { position: relative; animation: cardIn .2s cubic-bezier(.2,.9,.3,1.1); }
@keyframes fadeIn { from { opacity: 0; } }
@keyframes cardIn { from { opacity: 0; transform: translateY(12px) scale(.97); } }
.modal-close {
  position: absolute;
  top: 12px; right: 12px;
  width: 30px; height: 30px;
  border: none; border-radius: 50%;
  background: #f2f3f5; color: var(--muted);
  font-size: 18px; line-height: 1;
  cursor: pointer;
}
.modal-close:hover { background: #e8eaee; color: var(--text); }
.nice-select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 34px !important;
}
.segmented { display: flex; gap: 6px; background: #f2f3f5; padding: 4px; border-radius: 12px; }
.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 9px 8px;
  border-radius: 9px;
  font-family: inherit;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s, box-shadow .15s;
}
.seg-btn.active { background: #fff; color: var(--accent); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.chip:active { transform: scale(.96); }
.mini-btn:active, .ghost-btn:active, .primary-btn:active { transform: scale(.97); }




/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: rgba(20,22,26,.9);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 100;
  animation: pop .18s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translate(-50%, 8px); } }

/* Mobile / iPad */
@media (max-width: 820px) {
  #sidebar {
    position: fixed;
    z-index: 40;
    top: 0; bottom: 0; left: 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  #toggle-sidebar { display: inline-flex; }
  .msg { max-width: 92%; }
  .receipt { max-width: 100%; }
}
