:root {
  /* Light Theme */
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel2: #f1f5f9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary2: #3b82f6;
  --danger: #ef4444;
  --warn: #f59e0b;
  --good: #10b981;
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --font-system: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
}

[data-theme="dark"] {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel2: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #334155;
  --primary: #3b82f6;
  --primary2: #60a5fa;
  --danger: #f87171;
  --warn: #fbbf24;
  --good: #34d399;
  --shadow: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.5);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-system);
  background: var(--bg);
  color: var(--text);
  overflow: hidden; /* App shell handles scrolling */
}

h1, h2, h3, h4 {
  margin: 0 0 0.5rem 0;
}

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.mono { font-family: var(--mono); }
.hidden { display: none !important; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.2s;
  gap: 0.5rem;
  font-size: 0.9375rem;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary2); }
.btn-secondary { background: var(--panel2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-ghost { background: transparent; color: var(--text); }
.btn-ghost:hover { background: var(--panel2); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.8125rem; }

/* Forms */
.form { display: flex; flex-direction: column; gap: 1rem; }
.field { display: flex; flex-direction: column; gap: 0.25rem; }
.field span { font-size: 0.875rem; font-weight: 500; }
.field input, .field select, .field textarea {
  padding: 0.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 1rem;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--primary);
  ring: 2px solid var(--primary);
}
.field.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; cursor: pointer; }

/* Login Page */
.page-login {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-shell { width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 2rem; }
.login-header { display: flex; justify-content: space-between; align-items: center; }
.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand-mark {
  background: var(--primary);
  color: white;
  padding: 0.5rem;
  border-radius: 4px;
  font-weight: bold;
}
.brand-name h1 { font-size: 1.25rem; margin: 0; }
.brand-name p { font-size: 0.875rem; margin: 0; }

.login-card {
  background: var(--panel);
  padding: 2rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.alert { padding: 0.75rem; border-radius: var(--radius); font-size: 0.875rem; margin-top: 1rem; }
.alert-danger { background: rgba(239, 68, 68, 0.1); border: 1px solid var(--danger); color: var(--danger); }

.login-hint { margin-top: 1rem; font-size: 0.8125rem; }
.login-footer { text-align: center; font-size: 0.75rem; }

/* Dashboard Layout */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  height: 60px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  z-index: 100;
}

.topbar .left, .topbar .right { display: flex; align-items: center; gap: 0.75rem; }
.brand-inline { display: flex; align-items: center; gap: 0.5rem; font-weight: bold; }
.brand-mark.small { padding: 0.25rem; font-size: 0.75rem; }

.global-search {
  display: flex;
  width: 400px;
  background: var(--panel2);
  border-radius: var(--radius);
  padding: 2px;
  border: 1px solid var(--border);
}
.global-search input {
  flex: 1;
  background: transparent;
  border: none;
  padding: 0.5rem 1rem;
  color: var(--text);
  font-size: 0.9375rem;
}
.global-search input:focus { outline: none; }

.main { display: flex; flex: 1; overflow: hidden; }

.sidebar {
  width: 260px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.sidebar .panel h3 { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 0.75rem; }

.sidebar .panel-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.sidebar .panel-header-row h3 {
  margin-bottom: 0 !important;
}

.btn-xs {
  padding: 2px 4px;
  font-size: 0.75rem;
  line-height: 1;
}

.clickable { cursor: pointer; }
.clickable:hover { background: var(--panel2) !important; }

.content { flex: 1; overflow-y: auto; background: var(--bg); position: relative; }

/* Components */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  padding: 1rem;
}

.kpi-card {
  background: var(--panel);
  padding: 1.25rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: transform 0.1s;
}
.kpi-card:hover { transform: translateY(-2px); }
.kpi-card .value { font-size: 1.5rem; font-weight: bold; margin: 0.25rem 0; }
.kpi-card .delta { font-size: 0.75rem; font-weight: 500; }
.kpi-card .delta.up { color: var(--good); }
.kpi-card .delta.down { color: var(--danger); }

.dash-row { display: flex; gap: 1rem; padding: 0 1rem 1rem 1rem; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; }
.panel.grow { flex: 1; }
.panel.side { width: 320px; }
.panel-header { padding: 1rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.panel-header h3 { margin: 0; font-size: 1rem; }

.tile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; padding: 1rem; }
.chart-tile { background: var(--panel2); border-radius: var(--radius); padding: 1rem; min-height: 200px; display: flex; flex-direction: column; }

.feed { padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem; overflow-y: auto; max-height: 400px; }
.feed-item { display: flex; flex-direction: column; gap: 0.125rem; border-left: 2px solid var(--border); padding-left: 0.75rem; font-size: 0.875rem; }

.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; padding: 1rem; }
.chip {
  background: var(--panel2);
  padding: 0.375rem 0.75rem;
  border-radius: 20px;
  font-size: 0.8125rem;
  cursor: pointer;
  border: 1px solid var(--border);
}
.chip:hover { background: var(--border); }

/* Suggested Questions List */
.suggested-list { display: flex; flex-direction: column; padding: 0.5rem; }
.suggested-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.suggested-row:last-child { border-bottom: none; }
.suggested-row .actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.suggested-row .question-text { flex-grow: 1; font-size: 0.9375rem; cursor: pointer; transition: color 0.2s; line-height: 1.4; }
.suggested-row .question-text:hover { color: var(--primary); }

.btn-icon-only {
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 1.25rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Icon scaling */
.report-icon, .task-icon, .chat-icon, .search-icon, .btn-icon-only {
  font-size: 1.2rem;
  line-height: 1;
}

/* Desktop View */
.desktop-view { display: flex; flex-direction: column; height: 100%; position: relative; }
.desktop-toolbar {
  padding: 0.5rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.desktop-toolbar .left, .desktop-toolbar .right { display: flex; align-items: center; gap: 1rem; }

.desktop { flex: 1; position: relative; overflow: hidden; background-image: radial-gradient(var(--border) 1px, transparent 1px); background-size: 20px 20px; }

.taskbar {
  height: 40px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  gap: 0.5rem;
}

.sql-log {
  position: relative;
  background: var(--panel2);
  border-top: 2px solid var(--border);
  max-height: 300px;
  overflow-y: auto;
  flex-shrink: 0;
}

.sql-log-content {
  padding: 1rem 1rem 2.5rem 1rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
}

.sql-log-entry {
  margin-bottom: 0.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
  opacity: 0.9;
}

.sql-log-entry:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.sql-log-clear {
  position: absolute;
  bottom: 0.5rem;
  right: 0.5rem;
  z-index: 10;
  opacity: 0.8;
}
.sql-log-clear:hover { opacity: 1; }

.task-pill {
  background: var(--panel2);
  border: 1px solid var(--border);
  padding: 0.25rem 0.75rem;
  border-radius: 4px;
  font-size: 0.8125rem;
  cursor: pointer;
  max-width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.task-pill:hover { background: var(--border); }
.task-pill.active { border-color: var(--primary); background: var(--border); box-shadow: 0 0 0 1px var(--primary); }

/* Windows */
.win {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-width: 320px;
  min-height: 200px;
  overflow: hidden;
  z-index: 10;
}

.win.maximized {
  top: 0 !important; left: 0 !important;
  width: 100% !important; height: 100% !important;
  border-radius: 0;
}

.win-titlebar {
  background: var(--panel2);
  padding: 0.5rem 0.75rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: grab;
  user-select: none;
}
.win-title { display: flex; align-items: center; gap: 0.5rem; font-weight: 600; font-size: 0.875rem; }
.win-controls { display: flex; gap: 0.25rem; }
.win-btn {
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 4px; border: none; background: transparent;
  color: var(--text); cursor: pointer; font-size: 0.75rem;
}
.win-btn:hover { background: var(--border); }
.win-btn.danger:hover { background: var(--danger); color: white; }

.win-header { padding: 0.75rem; border-bottom: 1px solid var(--border); background: var(--panel); }
.win-prompt {
  width: 100%;
  resize: none;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  padding: 0.5rem;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.875rem;
  margin-bottom: 0.5rem;
}
.win-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.win-meta { margin-top: 0.5rem; display: flex; justify-content: space-between; }

.win-body { flex: 1; overflow: auto; padding: 1rem; position: relative; }

.win-resize-handle {
  position: absolute;
  bottom: 0; right: 0;
  width: 16px; height: 16px;
  cursor: nwse-resize;
}

/* Chat Window */
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.chat-msg {
  max-width: 85%;
  padding: 0.75rem;
  border-radius: var(--radius);
  font-size: 0.9375rem;
  line-height: 1.4;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--primary);
  color: white;
}
.chat-msg.assistant {
  align-self: flex-start;
  background: var(--panel2);
  border: 1px solid var(--border);
}
.chat-role {
  font-size: 0.75rem;
  font-weight: bold;
  margin-bottom: 0.25rem;
  opacity: 0.8;
}
.chat-input-wrap {
  padding: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
}
.chat-input {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.5rem;
  resize: none;
  font-family: inherit;
  font-size: 0.9375rem;
  height: 40px;
}

/* Data Display */
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th { text-align: left; padding: 0.5rem; border-bottom: 2px solid var(--border); color: var(--muted); }
td { padding: 0.5rem; border-bottom: 1px solid var(--border); }
tr:hover td { background: var(--panel2); }

.summary-text { line-height: 1.5; }
.summary-bullets { margin: 1rem 0; padding-left: 1.5rem; }
.summary-bullets li { margin-bottom: 0.5rem; }

/* Menus */
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  top: 100%; right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 1000;
  min-width: 160px;
  padding: 0.5rem 0;
}
.menu-item {
  display: block; width: 100%; text-align: left;
  padding: 0.5rem 1rem; border: none; background: transparent;
  color: var(--text); cursor: pointer; font-size: 0.875rem;
}
.menu-item:hover { background: var(--panel2); }
.menu-item.danger { color: var(--danger); }

/* Progress bar */
.progress-container { width: 100%; background: var(--panel2); height: 8px; border-radius: 4px; overflow: hidden; margin: 1rem 0; }
.progress-bar { background: var(--primary); height: 100%; transition: width 0.3s; }

/* Custom Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* Charts (pure HTML bars) */
.bar-chart { display: flex; align-items: flex-end; gap: 4px; height: 100px; width: 100%; padding-top: 20px; }
.bar-item { flex: 1; background: var(--primary); border-radius: 2px 2px 0 0; position: relative; min-width: 10px; }
.bar-item:hover { background: var(--primary2); }
.bar-label { position: absolute; bottom: -20px; left: 50%; transform: translateX(-50%); font-size: 10px; white-space: nowrap; color: var(--muted); }

/* Toast */
.toast-container { position: fixed; bottom: 2rem; right: 2rem; z-index: 9999; pointer-events: none; }
.toast {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  padding: 0.75rem 1.5rem; border-radius: 40px; box-shadow: var(--shadow);
  margin-top: 0.5rem; pointer-events: auto; animation: toastIn 0.3s forwards;
}
@keyframes toastIn { from { transform: translateY(100%); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: min(90vw, 800px);
  height: min(80vh, 700px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-body {
  padding: 1rem;
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.modal-body textarea {
  flex: 1;
  width: 100%;
  resize: none;
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  font-family: var(--mono);
  font-size: 0.9rem;
}

.modal-footer {
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  background: var(--panel);
}

/* Sidebar Lists & General List Items */
.list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.list-item {
  padding: 0.5rem;
  border-radius: 6px;
  font-size: 0.875rem;
  transition: background 0.2s;
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
}

.list-item .title {
  font-weight: 500;
  color: var(--text);
}

.list-item .meta {
  font-size: 0.75rem;
  color: var(--muted);
}

.text-truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.report-icon {
  margin-right: 0.5rem;
}

.text-center { text-align: center; }
.p-2 { padding: 0.5rem; }
.ml-2 { margin-left: 0.5rem; }
.x-small { font-size: 0.75rem; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-weight: 500;
  font-size: 0.75rem;
  background: var(--panel2);
  border: 1px solid var(--border);
  color: var(--muted);
}
.badge-secondary { background: var(--panel2); border-color: var(--border); }
