:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: rgba(15, 23, 42, 0.12);
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
  --primary: #2563eb;
  --primary-2: #1d4ed8;
  --danger: #dc2626;
  --radius: 14px;
  --radius-sm: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji",
    "Segoe UI Emoji";
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --panel: #0f172a;
    --panel-2: #0b1426;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: rgba(226, 232, 240, 0.14);
    --shadow: 0 18px 48px rgba(0, 0, 0, 0.45);
    --primary: #3b82f6;
    --primary-2: #2563eb;
    --danger: #f87171;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--sans);
  background: radial-gradient(900px 600px at 10% -10%, rgba(59, 130, 246, 0.18), transparent 60%),
    radial-gradient(900px 600px at 90% 0%, rgba(124, 58, 237, 0.14), transparent 55%),
    var(--bg);
  color: var(--text);
}
.app {
  display: flex;
  min-height: 100vh;
  position: relative;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-logo {
  width: 34px;
  height: 34px;
}
.brand-title {
  font-weight: 750;
  letter-spacing: 0.2px;
}
.brand-subtitle {
  font-size: 12px;
  color: var(--muted);
  margin-top: 1px;
}

.main {
  flex: 1;
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.view {
  height: 100%;
  display: none;
  width: 100%;
  overflow: hidden;
  flex: 1;
}
.view.is-active {
  display: flex;
}

#viewChat {
  gap: 12px;
  padding: 12px;
}

.sidebar {
  width: 300px;
  min-width: 260px;
  max-width: 340px;
  border-right: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  z-index: 2;
  height: 100vh;
}
.sidebar.is-collapsed {
  width: 64px;
  min-width: 64px;
  max-width: 64px;
  padding: 8px 6px;
}
.sidebar-top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}
.icon-btn {
  min-width: 0;
  padding: 8px 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 750;
}
.sidebar.is-collapsed .brand {
  display: none;
}
.sidebar-inner {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 10px;
}
.sidebar.is-collapsed .sidebar-inner {
  display: none;
}
.sidebar-menu {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}
.menu-item {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  padding: 10px 10px;
  cursor: pointer;
  font-weight: 650;
  text-align: center;
}
.menu-item.is-active {
  border-color: rgba(37, 99, 235, 0.5);
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
}
.sidebar.is-collapsed .sidebar-menu,
.sidebar.is-collapsed .sidebar-top {
  padding: 8px 6px;
}
.sidebar.is-collapsed .sidebar-menu {
  grid-template-columns: 1fr;
}
.sidebar.is-collapsed .menu-item {
  padding: 8px 6px;
  font-size: 13px;
}
.sidebar-head {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.sidebar-head-actions {
  margin-left: auto;
  display: inline-flex;
  gap: 6px;
}
.sidebar-body {
  flex: 1;
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sidebar-section {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  background: var(--panel);
}
.sidebar-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 8px;
  color: var(--muted);
}
.sidebar-section.hint {
  border: 1px dashed var(--border);
  background: var(--panel-2);
}

.sidebar-floating-toggle {
  position: fixed;
  top: 16px;
  left: 12px;
  z-index: 11;
  display: none;
}

.sidebar-head {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.sidebar-head-actions {
  margin-left: auto;
  display: inline-flex;
  gap: 6px;
}
.sidebar-body {
  flex: 1;
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sidebar-section {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  background: var(--panel);
}
.sidebar-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 8px;
  color: var(--muted);
}

.sidebar-foot {
  padding: 12px;
  border-top: 1px solid var(--border);
  background: var(--panel-2);
}
.sidebar-actions {
  margin-top: 8px;
  display: grid;
  gap: 8px;
}
.sidebar-section.hint {
  border: 1px dashed var(--border);
  background: var(--panel-2);
}

.hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.chat-list {
  flex: 1;
  overflow: auto;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-section .chat-list {
  max-height: 50vh;
}

.chat-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  cursor: pointer;
}
.chat-item:hover {
  border-color: rgba(37, 99, 235, 0.35);
}
.chat-item.is-active {
  border-color: rgba(37, 99, 235, 0.7);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}
.chat-item-title {
  font-weight: 650;
  font-size: 14px;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-item-meta {
  font-size: 12px;
  color: var(--muted);
}

.panel {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}

.panel-title {
  font-weight: 750;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.panel-tools {
  margin-left: auto;
  display: inline-flex;
  gap: 8px;
  align-items: center;
}
.chat-tools {
  display: none;
}

.messages {
  flex: 1;
  overflow: auto;
  padding: 18px 14px 14px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 0 14px 0;
  align-items: flex-start;
}
.msg.user {
  align-items: flex-end;
}
.msg-meta {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.msg.user .msg-meta {
  justify-content: flex-end;
}
.msg.assistant .msg-meta {
  justify-content: flex-start;
}
.msg-role {
  font-weight: 750;
}
.msg-bubble {
  max-width: 920px;
  padding: 12px 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user .msg-bubble {
  background: rgba(37, 99, 235, 0.1);
  border-color: rgba(37, 99, 235, 0.22);
  align-self: flex-end;
}
.msg.assistant .msg-bubble {
  background: rgba(124, 58, 237, 0.08);
  border-color: rgba(124, 58, 237, 0.18);
  align-self: flex-start;
}

.msg-bubble code {
  font-family: var(--mono);
  font-size: 0.92em;
  background: rgba(148, 163, 184, 0.16);
  padding: 0.14em 0.36em;
  border-radius: 8px;
}
.msg-bubble pre {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.45;
  background: rgba(2, 6, 23, 0.9);
  color: #e2e8f0;
  padding: 12px;
  border-radius: 12px;
  overflow: auto;
  margin: 10px 0;
  border: 1px solid rgba(226, 232, 240, 0.14);
}
.msg-bubble pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  color: inherit;
}
.msg-bubble a {
  color: var(--primary);
  text-decoration: none;
}
.msg-bubble a:hover {
  text-decoration: underline;
}

.cursor {
  display: inline-block;
  width: 0.6ch;
  margin-left: 2px;
  color: var(--muted);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink {
  50% {
    opacity: 0;
  }
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--panel-2);
  padding: 12px;
  display: grid;
  gap: 10px;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
}
.composer-left {
  min-width: 150px;
}
.composer-left .select {
  width: 100%;
}

.textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 10px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  outline: none;
  resize: none;
  min-height: 60px;
  max-height: 160px;
  overflow-y: auto;
}
.textarea:focus {
  border-color: rgba(37, 99, 235, 0.6);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16);
}

.composer-actions {
  display: inline-flex;
  gap: 8px;
  align-items: center;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 9px 12px;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 650;
}
.btn:hover {
  border-color: rgba(37, 99, 235, 0.35);
}
.btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}
.btn.secondary {
  background: var(--panel-2);
}
.btn.primary {
  border-color: rgba(37, 99, 235, 0.35);
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: white;
}
.btn.primary:hover {
  filter: brightness(1.02);
}
.btn.danger {
  border-color: rgba(220, 38, 38, 0.35);
  background: rgba(220, 38, 38, 0.12);
  color: var(--text);
}
a.btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.select,
.input {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 10px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  outline: none;
}
.select:focus,
.input:focus {
  border-color: rgba(37, 99, 235, 0.6);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: white;
  padding: 10px 14px;
  border-radius: 999px;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.35);
  z-index: 50;
  max-width: min(720px, calc(100vw - 24px));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
}
.modal[hidden] {
  display: none !important;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 23, 0.55);
}
.modal-dialog {
  position: relative;
  width: min(740px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}
.modal-head,
.modal-foot {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-2);
}
.modal-foot {
  border-bottom: 0;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}
.modal-title {
  font-weight: 800;
}
.modal-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.label {
  font-weight: 750;
}
.help {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}
.radio-group {
  display: grid;
  gap: 8px;
}
.radio {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
}

.btn-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.file-btn {
  position: relative;
  overflow: hidden;
}

.draw {
  margin: 12px;
  height: calc(100% - 24px);
}
.draw-body {
  padding: 14px;
  overflow: auto;
}
.draw-grid {
  margin-top: 12px;
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr 1fr;
}
@media (max-width: 980px) {
  .app {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    max-width: none;
    height: auto;
    box-shadow: none;
  }
  .sidebar-inner {
    height: auto;
  }
  .sidebar.is-collapsed {
    width: 100%;
    min-width: 100%;
    max-width: 100%;
  }
  .sidebar-floating-toggle {
    top: 70px;
  }
  .main {
    height: auto;
  }
  #viewChat {
    flex-direction: column;
  }
  .composer {
    grid-template-columns: 1fr;
    align-items: stretch;
  }
  .composer-left,
  .composer-actions {
    width: 100%;
    display: flex;
  }
  .composer-left {
    justify-content: flex-start;
  }
  .composer-actions {
    justify-content: flex-end;
  }
  .textarea {
    min-height: 90px;
  }
  .draw-grid {
    grid-template-columns: 1fr;
  }
}

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  overflow: hidden;
}
.card-title {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  font-weight: 750;
}
.card-body {
  padding: 12px;
}
.muted {
  color: var(--muted);
}
.image-preview {
  display: grid;
  place-items: center;
  height: 220px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  color: var(--muted);
}
.image-preview-img {
  width: 100%;
  max-height: 420px;
  object-fit: contain;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.18);
}
.image-actions {
  display: inline-flex;
  gap: 8px;
  margin-bottom: 12px;
}

.spinner {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  border: 2px solid rgba(148, 163, 184, 0.45);
  border-top-color: rgba(37, 99, 235, 0.95);
  animation: spin 0.85s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
