* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-page: #f4f7f4;
  --bg-white: #ffffff;
  --bg-sidebar: #fafff5;
  --bg-hover: #edf7ed;
  --bg-active: #dff0df;
  --bg-input: #f8faf8;

  --green-50: #f0fdf0;
  --green-100: #dcfce7;
  --green-200: #bbf7d0;
  --green-300: #86efac;
  --green-400: #4ade80;
  --green-500: #22c55e;
  --green-600: #16a34a;
  --green-700: #15803d;
  --green-800: #166534;

  --grad-main: linear-gradient(135deg, #22c55e 0%, #16a34a 50%, #15803d 100%);
  --grad-light: linear-gradient(135deg, #86efac 0%, #4ade80 100%);
  --grad-subtle: linear-gradient(135deg, #f0fdf0 0%, #dcfce7 100%);
  --grad-sidebar: linear-gradient(180deg, #f0fdf4 0%, #ffffff 100%);

  --text-primary: #1a2e1a;
  --text-secondary: #3d5a3d;
  --text-muted: #7a9a7a;
  --text-on-green: #ffffff;

  --border: #d4e8d4;
  --border-light: #e8f5e8;
  --shadow-sm: 0 1px 3px rgba(22, 163, 74, 0.08);
  --shadow-md: 0 4px 12px rgba(22, 163, 74, 0.1);
  --shadow-lg: 0 8px 30px rgba(22, 163, 74, 0.12);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
  --font-mono: "SF Mono", "Fira Code", "JetBrains Mono", monospace;
  --radius: 8px;
  --radius-lg: 12px;
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: var(--font-sans);
  font-size: 14px;
  background: var(--bg-page);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

.app { display: flex; height: 100vh; height: 100dvh; }

/* === Sidebar === */
.sidebar {
  width: 340px;
  min-width: 340px;
  background: var(--grad-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 12px rgba(22, 163, 74, 0.04);
  transition: margin-left .25s ease, opacity .25s ease;
}

.sidebar.hidden {
  margin-left: -340px;
  opacity: 0;
  pointer-events: none;
}

.sidebar-collapsed {
  width: 48px;
  min-width: 48px;
  background: var(--grad-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 8px;
}

.btn-expand-sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--green-700);
}

.btn-expand-sidebar:hover { color: var(--green-500); }

.expand-logo {
  font-size: 18px;
  line-height: 1;
}

.expand-arrow {
  font-size: 10px;
}

.btn-toggle {
  width: 30px; height: 30px;
  background: var(--bg-white);
  color: var(--green-600);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: all .15s;
}

.btn-toggle:hover { background: var(--green-50); }

.sidebar-header {
  padding: 16px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-light);
}

.sidebar-header h1 {
  font-size: 17px;
  font-weight: 800;
  background: var(--grad-main);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 0.5px;
}

.header-actions {
  display: flex; gap: 6px; align-items: center;
}

.sidebar-header button,
.sidebar-header .btn-settings,
.sidebar-header .btn-toggle {
  width: 30px; height: 30px;
  background: var(--bg-white);
  color: var(--green-600);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  text-decoration: none;
  transition: all .15s;
  box-shadow: var(--shadow-sm);
}

.sidebar-header button:hover,
.sidebar-header .btn-settings:hover,
.sidebar-header .btn-toggle:hover {
  background: var(--green-50);
  border-color: var(--green-400);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

#tree { flex: 1; overflow-y: auto; padding: 6px 8px; }

.tree-empty {
  padding: 24px 12px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

/* Tree items */
.tree-item {
  display: flex; align-items: center;
  padding: 10px 12px;
  margin-bottom: 2px;
  cursor: pointer;
  border-radius: var(--radius);
  transition: all .12s;
}

.tree-item:hover {
  background: var(--bg-hover);
}

.tree-item.active {
  background: var(--green-100);
  box-shadow: inset 3px 0 0 var(--green-500);
}

.tree-item-label {
  flex: 1;
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-item-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 8px;
}

/* === Main === */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.main-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-white);
  font-size: 14px;
  font-weight: 600;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: var(--shadow-sm);
}

.header-meta {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

.messages {
  flex: 1; overflow-y: auto; padding: 28px;
  background: var(--bg-page);
}

.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%;
  gap: 14px;
}

.empty-state-icon {
  width: 64px; height: 64px;
  background: var(--grad-light);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px;
  color: var(--green-800);
  font-weight: 700;
  box-shadow: var(--shadow-md);
}

.empty-state-text {
  font-size: 14px;
  color: var(--text-muted);
}

.input-area {
  padding: 16px 28px;
  border-top: 1px solid var(--border-light);
  background: var(--bg-white);
  display: flex; gap: 10px;
  box-shadow: 0 -2px 8px rgba(22, 163, 74, 0.03);
}

.input-area textarea {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 14px;
  color: var(--text-primary);
  font: inherit;
  resize: none; outline: none;
  transition: all .15s;
}

.input-area textarea:focus {
  border-color: var(--green-400);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.12);
}

.input-area textarea::placeholder { color: var(--text-muted); }

.input-area button {
  padding: 10px 24px;
  background: var(--grad-main);
  color: var(--text-on-green);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  transition: all .15s;
  box-shadow: var(--shadow-sm);
}

.input-area button:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

/* === Dialogs === */
dialog {
  background: var(--bg-white);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0; max-width: 520px; width: 90%;
  box-shadow: var(--shadow-lg);
}

dialog::backdrop { background: rgba(0, 0, 0, .3); backdrop-filter: blur(2px); }

.dialog-inner {
  padding: 28px;
  display: flex; flex-direction: column; gap: 16px;
}

.dialog-inner h2 {
  font-size: 17px;
  font-weight: 700;
  color: var(--text-primary);
}

.dialog-inner label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12px; color: var(--text-secondary);
  font-weight: 600;
}

.dialog-inner input[type="url"],
.dialog-inner input[type="text"] {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text-primary);
  font: inherit; font-size: 14px;
  outline: none;
  transition: all .15s;
}

.dialog-inner input:focus {
  border-color: var(--green-400);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1);
}

.dialog-inner input::placeholder { color: var(--text-muted); }

.dialog-actions {
  display: flex; justify-content: flex-end; gap: 10px;
  margin-top: 8px;
}

.dialog-actions button {
  padding: 10px 20px; border-radius: var(--radius);
  font-size: 13px; font-weight: 600;
  transition: all .15s;
}

.dialog-actions button[type="button"] {
  background: var(--bg-white);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.dialog-actions button[type="button"]:hover {
  background: var(--bg-hover);
  border-color: var(--green-300);
}

.dialog-actions button[type="submit"] {
  background: var(--grad-main);
  color: var(--text-on-green);
  border: none;
  box-shadow: var(--shadow-sm);
}

.dialog-actions button[type="submit"]:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.probe-status { font-size: 12px; min-height: 18px; font-weight: 600; }
.probe-status.ok { color: var(--green-600); }
.probe-status.err { color: #dc2626; }
.probe-status.loading { color: var(--text-muted); }

/* Model list in dialog */
.model-list h3 {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  font-weight: 600;
}

.model-check {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 0; font-size: 13px;
}

.model-check input[type="checkbox"] {
  accent-color: var(--green-500);
  width: 16px; height: 16px;
  flex-shrink: 0;
}

.add-model-manual {
  display: flex; gap: 8px; margin-top: 10px;
}

.add-model-manual input { flex: 1; }

.add-model-manual button {
  padding: 8px 14px;
  background: var(--bg-white);
  color: var(--green-700);
  border: 1px solid var(--green-300);
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 600;
  transition: all .15s;
}

.add-model-manual button:hover {
  background: var(--green-50);
  border-color: var(--green-500);
}

/* === Provider management page === */
.btn-add-provider-page {
  padding: 8px 18px;
  background: var(--grad-main);
  color: var(--text-on-green);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
  transition: all .15s;
}

.btn-add-provider-page:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.provider-content {
  flex: 1; overflow-y: auto; padding: 28px;
  display: flex; flex-direction: column; gap: 18px;
  background: var(--bg-page);
}

.empty-hint {
  color: var(--text-muted);
  font-size: 14px;
  padding: 40px;
  text-align: center;
}

.provider-card {
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: all .15s;
  box-shadow: var(--shadow-sm);
}

.provider-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--green-300);
}

.provider-card-header {
  padding: 16px 18px;
  display: flex; justify-content: space-between; align-items: center;
  background: var(--grad-subtle);
  border-bottom: 1px solid var(--border-light);
}

.provider-card-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--green-800);
}

.provider-card-url {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 3px;
  font-family: var(--font-mono);
}

.provider-card-actions { display: flex; gap: 8px; }

.provider-card-actions button {
  padding: 6px 12px;
  background: var(--bg-white);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 500;
  transition: all .15s;
}

.provider-card-actions button:hover {
  background: var(--bg-hover);
  border-color: var(--green-300);
  color: var(--green-700);
}

.provider-card-models { padding: 8px 0; }

.model-row {
  display: flex; align-items: center;
  padding: 8px 18px; font-size: 13px;
  transition: background .1s;
}

.model-row:hover { background: var(--bg-hover); }

.model-id {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-primary);
}

.model-display {
  color: var(--text-muted);
  margin-left: 10px;
  font-size: 12px;
}

.model-del {
  margin-left: auto;
  font-size: 16px;
  color: var(--text-muted);
  background: none; border: none; cursor: pointer;
  padding: 0 6px;
  transition: color .15s;
  line-height: 1;
}

.model-del:hover { color: #dc2626; }

.btn-add-model-row {
  display: block; width: 100%;
  padding: 8px 18px;
  text-align: left;
  font-size: 13px;
  color: var(--green-600);
  background: none; border: none; cursor: pointer;
  font-weight: 600;
  transition: all .1s;
}

.btn-add-model-row:hover {
  background: var(--bg-hover);
  color: var(--green-700);
}

/* Provider tree in sidebar (providers page) */
.provider-group { margin-bottom: 2px; }

.provider-header {
  display: flex; align-items: center;
  padding: 8px 12px; cursor: pointer;
  font-size: 13px; font-weight: 600;
  color: var(--text-secondary);
  border-radius: var(--radius);
  transition: all .1s;
}

.provider-header:hover { background: var(--bg-hover); }

.provider-header .arrow {
  margin-right: 6px; font-size: 10px;
  transition: transform .15s;
  color: var(--text-muted);
}

.provider-header.collapsed .arrow { transform: rotate(-90deg); }
.provider-header .name { flex: 1; }

.provider-models { overflow: hidden; }
.provider-models.collapsed { display: none; }

.provider-models .model-item {
  padding: 5px 12px 5px 30px;
  font-size: 12px;
  color: var(--text-primary);
  border-radius: var(--radius);
  margin: 1px 4px;
}

.provider-models .model-item:hover { background: var(--bg-hover); }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--green-200); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--green-300); }

/* === Chat: SVG graph in sidebar === */
.tree-divider {
  height: 1px;
  background: var(--border);
  margin: 8px 12px;
}

.graph-wrap {
  padding: 8px 4px;
  overflow-x: auto;
  overflow-y: hidden;
}

.graph-wrap svg {
  display: block;
  min-width: 100%;
}

.graph-wrap circle {
  transition: fill .15s;
}

.graph-wrap circle:hover {
  r: 7;
}

.graph-wrap text:hover {
  fill: #1a1a1a !important;
  font-weight: 600;
}

/* === Chat: Messages === */
.messages {
  padding: 24px 28px;
}

.msg {
  display: flex;
  margin-bottom: 20px;
  gap: 12px;
}

.msg-user { justify-content: flex-end; }
.msg-assistant { justify-content: flex-start; }

.msg-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.msg-user .msg-label { text-align: right; }

.msg-body {
  font-size: 15px;
  line-height: 1.8;
  color: #2d2d2d;
  word-break: break-word;
}

.msg-user .msg-body {
  background: var(--green-50);
  border: 1px solid var(--green-200);
  border-radius: 16px 16px 4px 16px;
  padding: 10px 16px;
  color: #1a1a1a;
}

.msg-assistant .msg-body {
  background: #f5f5f5;
  border: 1px solid #e0e0e0;
  border-radius: 16px 16px 16px 4px;
  padding: 12px 16px;
}

.msg-loading .msg-body { color: var(--text-muted); font-style: italic; }
.msg-error .msg-body { color: #dc2626; font-size: 13px; }

.msg-content { max-width: 90%; }

.highlight-flash {
  animation: flash-bg 1.2s ease;
}

@keyframes flash-bg {
  0%, 100% { background: transparent; }
  20% { background: rgba(34, 197, 94, 0.12); }
}

/* Fold/expand */
.msg-fold-btn {
  display: inline-block;
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 8px;
  margin-top: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: #fafafa;
  user-select: none;
}

.msg-fold-btn:hover { background: #eee; color: #555; }

.msg-body.folded {
  max-height: 80px;
  overflow: hidden;
  position: relative;
}

.msg-body.folded::after {
  content: "";
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 40px;
  background: linear-gradient(transparent, #f5f5f5);
  pointer-events: none;
}

/* Markdown rendered content — restrained gray tones */
.msg-body h2 {
  font-size: 15px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 18px 0 6px;
  padding: 6px 10px;
  background: #eee;
  border-left: 3px solid #999;
  border-radius: 0 var(--radius) var(--radius) 0;
  cursor: pointer;
  transition: all .12s;
}

.msg-body h2:hover {
  background: #e0e0e0;
  border-left-color: #666;
}

.msg-body h2::before {
  content: "▸ ";
  color: #999;
  font-size: 12px;
}

.msg-body p { margin: 6px 0; color: #333; }
.msg-body ul, .msg-body ol { margin: 6px 0; padding-left: 20px; }
.msg-body li { margin: 3px 0; color: #333; }
.msg-body strong { color: #1a1a1a; }
.msg-body em { color: #555; }

.msg-body code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: #eee;
  padding: 2px 5px;
  border-radius: 3px;
  color: #333;
}

.msg-body pre {
  background: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: var(--radius);
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
}

.msg-body pre code { background: none; padding: 0; color: #222; }

.msg-body blockquote {
  border-left: 3px solid #ccc;
  padding-left: 12px;
  color: #666;
  margin: 10px 0;
}

.msg-body h3 {
  font-size: 14px;
  font-weight: 600;
  color: #222;
  margin: 12px 0 4px;
}

/* === Login page === */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--grad-subtle);
}

.login-card {
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 36px;
  width: 360px;
  box-shadow: var(--shadow-lg);
}

.login-title {
  font-size: 24px;
  font-weight: 800;
  background: var(--grad-main);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
  margin-bottom: 4px;
}

.login-sub {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.login-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.login-card input[type="email"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--bg-input);
  color: var(--text-primary);
  outline: none;
  transition: border-color .15s;
  margin-bottom: 16px;
}

.login-card input:focus {
  border-color: var(--green-400);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1);
}

.login-actions {
  display: flex;
  gap: 10px;
}

.login-actions button {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
}

.btn-primary {
  background: var(--grad-main);
  color: white;
  border: none;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover { box-shadow: var(--shadow-md); }

.btn-secondary {
  background: var(--bg-white);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  border-color: var(--green-300);
  color: var(--green-700);
}

.login-status {
  text-align: center;
  font-size: 14px;
  color: var(--text-secondary);
  margin: 12px 0;
}

#btn-retry {
  margin: 12px auto 0;
  display: block;
  padding: 8px 24px;
}

.login-links {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 16px;
}

.login-links a {
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
  transition: color .15s;
}

.login-links a:hover {
  color: var(--green-600);
  text-decoration: underline;
}

/* === Mobile === */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 100;
    width: 85vw;
    min-width: 280px;
    max-width: 380px;
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    transform: translateX(-100%);
    transition: transform .25s ease;
  }

  .sidebar.visible {
    transform: translateX(0);
  }

  .sidebar-collapsed { display: none !important; }

  .main-header { padding: 10px 12px; }

  .main-header-title-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 0;
  }

  .btn-mobile-menu {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px; height: 32px;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 16px;
    color: var(--text-secondary);
    cursor: pointer;
    flex-shrink: 0;
  }

  .btn-mobile-menu:hover { background: var(--bg-hover); }

  .main-header-title-text {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .main-header-title-text .brand { color: var(--green-600); }

  .msg-content { max-width: 95%; }

  .messages { padding: 16px; }

  .input-area {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  .sidebar-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.3);
    z-index: 99;
  }

  .sidebar-overlay.visible { display: block; }

  .provider-sidebar-hidden .sidebar { display: none; }
  .provider-sidebar-hidden .app { display: block; }
  .provider-sidebar-hidden .main { width: 100%; }

  .provider-card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .provider-card-actions {
    width: 100%;
    display: flex;
    gap: 8px;
  }

  .provider-card-actions button { flex: 1; }

  .btn-add-provider-page {
    padding: 8px 12px;
    font-size: 12px;
  }

  .dialog-inner { padding: 20px; }
  .dialog-inner input[type="url"],
  .dialog-inner input[type="text"] { font-size: 16px; }

  .login-card {
    width: 90vw;
    padding: 28px 24px;
  }
}

@media (max-width: 480px) {
  .sidebar {
    width: 100vw;
    max-width: 100vw;
  }

  .msg-body { font-size: 14px; }
}
