:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #8b93a5;
  --accent: #6c8cff;
  --accent-2: #5674f0;
  --danger: #ff6b6b;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  border-radius: 10px; padding: 9px 16px; font: inherit; cursor: pointer;
  text-decoration: none; white-space: nowrap; transition: background .15s, border-color .15s;
}
.btn:hover { border-color: #3b4252; background: #252a35; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.icon { padding: 6px 10px; font-size: 18px; }
.btn.block { width: 100%; }
.btn.danger { color: var(--danger); }
.muted { color: var(--muted); }
.error-text { color: var(--danger); margin: 0; }

/* Login */
.login-view { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px 28px; display: grid; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card input {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 11px 12px; font: inherit; outline: none;
}
.login-card input:focus { border-color: var(--accent); }

/* Layout */
.app { display: flex; height: 100%; }
.sidebar {
  width: 270px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.sidebar-head, .sidebar-foot { padding: 12px; }
.sidebar-foot { border-top: 1px solid var(--border); }
.conv-list { list-style: none; margin: 0; padding: 0 8px; flex: 1; overflow-y: auto; }
.conv-item {
  display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px;
  cursor: pointer; margin-bottom: 2px;
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: #262c3a; }
.conv-thumb {
  width: 40px; height: 40px; border-radius: 8px; flex-shrink: 0; object-fit: cover;
  background: var(--panel-2); border: 1px solid var(--border);
}
.conv-meta { min-width: 0; flex: 1; }
.conv-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-sub { font-size: 12px; color: var(--muted); }
.conv-sub.busy { color: var(--accent); }
.sidebar-mask { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px;
  border-bottom: 1px solid var(--border); min-height: 56px;
}
#toggle-sidebar { display: none; }
.conv-title { margin: 0; font-size: 16px; font-weight: 600; flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; }
.messages > * { max-width: 860px; margin-left: auto; margin-right: auto; }
.empty-state { text-align: center; color: var(--muted); padding-top: 12vh; }
.empty-state h3 { color: var(--text); font-size: 22px; margin-bottom: 8px; }
.empty-state p { margin: 4px 0; }
.empty-state .small-note { font-size: 13px; margin-top: 18px; opacity: .8; }
.local-note { font-size: 12px; color: var(--muted); margin: 0 0 8px; text-align: center; }

.msg { margin-bottom: 22px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble {
  max-width: 85%; padding: 10px 14px; border-radius: 14px; white-space: pre-wrap; word-break: break-word;
}
.msg.user .bubble { background: #2b3550; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); }
.msg-meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Markdown in assistant replies */
.md { white-space: normal; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { margin: 1em 0 .5em; line-height: 1.35; }
.md h1 { font-size: 1.35em; }
.md h2 { font-size: 1.22em; }
.md h3 { font-size: 1.1em; }
.md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .7em; padding-left: 1.5em; }
.md li { margin: .2em 0; }
.md li > p { margin: 0; }
.md li > ul, .md li > ol, .md li > .md-code { margin: .3em 0; }
.md a { color: var(--accent); text-decoration: none; word-break: break-all; }
.md a:hover { text-decoration: underline; }
.md code { font-family: Consolas, "SFMono-Regular", Menlo, monospace; font-size: .9em; background: var(--panel-2); border: 1px solid var(--border); border-radius: 5px; padding: .1em .35em; }
.md blockquote { margin: 0 0 .7em; padding: .3em .9em; border-left: 3px solid var(--accent); border-radius: 0 6px 6px 0; background: rgba(108, 140, 255, .07); color: #c3c8d4; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md-code { margin: 0 0 .7em; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: #0b0d11; }
.md-code-head { display: flex; justify-content: space-between; align-items: center; padding: 3px 6px 3px 12px; font-size: 12px; color: var(--muted); background: var(--panel-2); border-bottom: 1px solid var(--border); }
.md-copy { border: 0; background: none; color: var(--muted); font: inherit; cursor: pointer; padding: 2px 6px; border-radius: 5px; }
.md-copy:hover { color: var(--text); background: #2a2f3a; }
.md-code pre { margin: 0; padding: 10px 12px; overflow-x: auto; }
.md-code code { background: none; border: 0; padding: 0; font-size: 13px; white-space: pre; }
.md-table { overflow-x: auto; margin: 0 0 .7em; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--panel-2); font-weight: 600; white-space: nowrap; }
.msg-error { color: var(--danger); font-size: 13px; margin-top: 6px; white-space: pre-wrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; justify-content: flex-end; }
.chip-img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; }
.chip-label { font-size: 12px; color: var(--muted); align-self: center; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; margin-top: 10px; }
.shot { position: relative; border-radius: var(--radius); overflow: hidden; background: var(--panel); border: 1px solid var(--border); }
.shot img { display: block; width: 100%; height: auto; cursor: zoom-in; }
.shot-tag {
  position: absolute; left: 8px; top: 8px; background: rgba(0,0,0,.6); color: #fff;
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
}
.shot-actions { display: flex; gap: 6px; padding: 8px; }
.shot-actions .btn { flex: 1; }

.working { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .9s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Composer */
.composer {
  border-top: 1px solid var(--border); padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
}
.composer > * { max-width: 860px; margin-left: auto; margin-right: auto; }
.composer textarea {
  display: block; width: 100%; resize: none; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; font: inherit;
  outline: none; min-height: 52px; max-height: 220px;
}
.composer textarea:focus { border-color: var(--accent); }
.composer-bar { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }
.select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  padding: 5px 8px; font: inherit; font-size: 13px;
}
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.attach { position: relative; }
.attach img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.attach.base img { border-color: var(--accent); }
.attach .x {
  position: absolute; right: -6px; top: -6px; width: 20px; height: 20px; border-radius: 50%;
  border: none; background: #000; color: #fff; cursor: pointer; font-size: 12px; line-height: 20px; padding: 0;
}
.attach .cap { position: absolute; left: 0; right: 0; bottom: 0; font-size: 11px; text-align: center;
  background: rgba(0,0,0,.65); color: #fff; border-radius: 0 0 8px 8px; }
.drop-hint { outline: 2px dashed var(--accent); outline-offset: -6px; }
.mode-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.mode-tabs:empty { display: none; }
.mode-tab {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
  border-radius: 999px; padding: 4px 14px; font: inherit; font-size: 13px; cursor: pointer;
}
.mode-tab:hover { color: var(--text); }
.mode-tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.mode-tab.soon { opacity: .6; }
.mode-tab.soon::after { content: '即将上线'; font-size: 10px; margin-left: 4px; vertical-align: 1px; }
.mode-tab.soon.active { opacity: 1; }
.composer textarea:disabled { opacity: .6; cursor: not-allowed; }

.attach.doc {
  display: flex; flex-direction: column; justify-content: center; gap: 2px;
  height: 64px; max-width: 180px; padding: 6px 12px 6px 10px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
}
.doc-name { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-size { font-size: 11px; color: var(--muted); }
.doc-chip {
  font-size: 12px; padding: 4px 10px; border-radius: 8px; max-width: 220px; align-self: center;
  background: var(--panel-2); border: 1px solid var(--border);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.history-picker {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 10px 10px; margin-bottom: 8px;
}
.picker-head { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.picker-grid { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.picker-item {
  position: relative; flex-shrink: 0; width: 72px; height: 72px; padding: 0; cursor: pointer;
  border: 2px solid transparent; border-radius: 8px; background: none; overflow: hidden;
}
.picker-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.picker-item:hover { border-color: var(--border); }
.picker-item.picked { border-color: var(--accent); }
.picker-item .cap {
  position: absolute; left: 0; right: 0; bottom: 0; font-size: 11px; text-align: center;
  background: rgba(0,0,0,.65); color: #fff;
}
.picker-item.picked .cap { background: var(--accent); }

/* Lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 16px; gap: 12px;
}
.lightbox img { max-width: 100%; max-height: calc(100% - 60px); object-fit: contain; border-radius: 8px; }
.lightbox-bar { display: flex; gap: 8px; }

/* Account */
.link-btn { background: none; border: none; color: var(--accent); font: inherit; font-size: 13px; cursor: pointer; padding: 0; }
.link-btn:hover { text-decoration: underline; }
.account-chip {
  width: 100%; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px;
  color: var(--text); font: inherit; cursor: pointer; margin-bottom: 8px;
}
.account-chip:hover { border-color: #3b4252; }
.account-name { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-balance { font-size: 12px; color: var(--accent); }
.account-balance.low { color: var(--danger); }
.foot-actions { display: flex; gap: 8px; }
.foot-actions > * { flex: 1; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 40;
  display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; overflow-y: auto;
}
.modal-card {
  width: min(900px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 20px 22px 24px;
}
.modal-card h4 { margin: 22px 0 10px; font-size: 15px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h3 { margin: 0; font-size: 18px; }
.small-text { font-size: 12px; line-height: 1.7; margin: 10px 0 0; }

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.stat-row:empty { display: none; }
.stat {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px;
  display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 18px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-value.accent { color: var(--accent); }
.stat-sub { font-size: 12px; color: var(--muted); }

.section-head { display: flex; align-items: center; gap: 8px; margin: 22px 0 10px; }
.section-head h4 { margin: 0; flex: 1; }
.section-head + .stat-row { margin-bottom: 10px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 7px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 500; background: var(--panel-2); }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover td { background: rgba(255,255,255,.02); }
.table td.num, .table th.num { text-align: right; }

.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.input {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 7px 10px; font: inherit; font-size: 13px; outline: none; min-width: 0;
}
.input:focus { border-color: var(--accent); }

/* Admin */
.admin { min-height: 100%; }
.admin-top { position: sticky; top: 0; background: var(--bg); z-index: 5; }
.small-text-inline { font-size: 13px; }
.tabs { display: flex; gap: 4px; padding: 0 16px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  background: none; border: none; border-bottom: 2px solid transparent; color: var(--muted);
  font: inherit; padding: 10px 14px; cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.admin-main { max-width: 1200px; margin: 0 auto; padding: 4px 16px 40px; }
.sub-title { margin: 22px 0 10px; font-size: 14px; }
.spaced { margin-top: 12px; }
.row-actions { display: flex; gap: 4px; }
.row-actions .btn.small { padding: 3px 8px; font-size: 12px; }
.pos { color: var(--text); }
.neg { color: var(--danger); }
.code { font-family: ui-monospace, Consolas, monospace; background: var(--panel-2); padding: 2px 6px; border-radius: 6px; letter-spacing: .5px; }
.price-form { display: grid; gap: 16px; margin-top: 18px; max-width: 760px; }
.price-group {
  border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px 10px; margin: 0;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px 16px;
}
.price-group legend { padding: 0 6px; font-weight: 600; }
.price-group label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.price-group .small-text { grid-column: 1 / -1; margin: 2px 0 0; }

@media (max-width: 760px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 30; transform: translateX(-100%);
    transition: transform .2s;
  }
  .app.sidebar-open .sidebar { transform: none; }
  .app.sidebar-open .sidebar-mask { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
  #toggle-sidebar { display: inline-flex; }
  .bubble { max-width: 92%; }
  .gallery { grid-template-columns: 1fr; }
  .composer-bar { flex-wrap: wrap; }
}
