/* ── Linear-inspired design system ────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap');

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

:root {
  /* Surfaces */
  --bg-base:      #0F0F0F;
  --bg-elevated:  #161618;
  --bg-overlay:   #1C1C1E;
  --bg-hover:     rgba(255,255,255,0.04);
  --bg-active:    rgba(255,255,255,0.07);

  /* Borders */
  --border:       rgba(255,255,255,0.07);
  --border-light: rgba(255,255,255,0.04);
  --border-focus: rgba(94,106,210,0.75);

  /* Text */
  --text:         #EDEDEF;
  --text-sec:     #9898A1;
  --text-muted:   #4E4E57;

  /* Accent — Linear purple */
  --accent:       #5E6AD2;
  --accent-hover: #6872D9;
  --accent-muted: rgba(94,106,210,0.14);
  --accent-light: #8D95E8;

  /* Status */
  --danger:       #E5484D;
  --danger-muted: rgba(229,72,77,0.12);
  --success:      #30A46C;
  --success-muted:rgba(48,164,108,0.12);
  --warning:      #F76B15;

  /* Geometry */
  --radius:    6px;
  --radius-sm: 4px;
  --radius-lg: 10px;

  /* Elevation */
  --shadow:    0 0 0 1px var(--border), 0 4px 20px rgba(0,0,0,0.5);
  --shadow-lg: 0 0 0 1px var(--border), 0 12px 48px rgba(0,0,0,0.7);

  --ease: 0.12s ease;
}

html, body {
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg-base);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-light); text-decoration: none; }
a:hover { color: var(--text); }

/* ── Typography ────────────────────────────────────────────── */
h1 { font-size: 1.3rem; font-weight: 600; letter-spacing: -0.02em; }
h2 { font-size: 1.05rem; font-weight: 600; letter-spacing: -0.015em; }
h3 { font-size: .9rem;  font-weight: 600; letter-spacing: -0.01em; }

/* ── Layout ────────────────────────────────────────────────── */
.container { max-width: 1100px; margin: 0 auto; padding: 0 1.5rem; }
.stack > * + * { margin-top: .85rem; }
.row { display: flex; gap: .75rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1 1 0; }

/* ── Top bar ───────────────────────────────────────────────── */
.topbar {
  height: 48px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-base);
  position: sticky; top: 0; z-index: 40;
}
.topbar-logo {
  display: flex; align-items: center; gap: .5rem;
  font-weight: 600; font-size: .9rem; letter-spacing: -0.01em;
  color: var(--text); min-width: 0;
}
.topbar-logo .icon {
  width: 20px; height: 20px;
  background: var(--accent);
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: white; flex-shrink: 0;
}
.topbar-right {
  display: flex; align-items: center; gap: .5rem; font-size: .8rem; color: var(--text-sec);
}
.topbar-tenant {
  background: var(--accent); color: white; font-size: .65rem; font-weight: 600;
  padding: 2px 6px; border-radius: 4px; text-transform: uppercase;
}

/* Hamburger — hidden on desktop, shown on mobile */
.sidebar-toggle {
  display: none;
  align-items: center; justify-content: center;
  width: 28px; height: 28px; flex-shrink: 0;
  background: none; border: none; cursor: pointer;
  color: var(--text-sec);
  transition: color var(--ease);
}
.sidebar-toggle:hover { color: var(--text); }

/* ── Page body split ───────────────────────────────────────── */
.app-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: calc(100vh - 48px);
}

/* ── Sidebar ───────────────────────────────────────────────── */
.sidebar {
  border-right: 1px solid var(--border);
  padding: 1rem .75rem;
  background: var(--bg-base);
  display: flex; flex-direction: column; gap: 1.25rem;
}

/* Backdrop — only visible on mobile when drawer is open */
.sidebar-backdrop {
  display: none;
  position: fixed; top: 48px; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  z-index: 89;
}
.sidebar-backdrop.open { display: block; }
.sidebar-section-label {
  font-size: .7rem; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-muted);
  padding: 0 .5rem; margin-bottom: .35rem;
}

/* ── Main content area ─────────────────────────────────────── */
.main-content {
  padding: 1.5rem;
  background: var(--bg-base);
}

/* ── Card ──────────────────────────────────────────────────── */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
}
.card-title {
  font-size: .8rem; font-weight: 600;
  letter-spacing: -0.005em; color: var(--text-sec);
  text-transform: uppercase; letter-spacing: .05em;
  margin-bottom: .85rem;
}

/* ── Form ──────────────────────────────────────────────────── */
label {
  display: block; font-size: .75rem; font-weight: 500;
  color: var(--text-sec); margin-bottom: .3rem;
}
input[type=text], input[type=email], input[type=password], select, textarea {
  width: 100%; padding: .5rem .7rem;
  background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text); font-family: inherit; font-size: .875rem;
  transition: border-color var(--ease), box-shadow var(--ease);
  outline: none;
}
input::placeholder, textarea::placeholder { color: var(--text-muted); }
input:focus, select:focus, textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(94,106,210,0.15);
}
select { appearance: none; cursor: pointer; }
textarea { resize: vertical; min-height: 72px; }

/* ── Buttons ───────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .45rem 1rem; border-radius: var(--radius);
  font-family: inherit; font-size: .8rem; font-weight: 500;
  cursor: pointer; border: none; transition: background var(--ease), opacity var(--ease);
  white-space: nowrap;
}
.btn:disabled { opacity: .4; cursor: not-allowed; }

.btn-primary {
  background: var(--accent); color: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.1);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-active); color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: rgba(255,255,255,0.1); }

.btn-danger {
  background: var(--danger); color: #fff;
}
.btn-danger:hover:not(:disabled) { background: #ee5559; }

.btn-ghost {
  background: transparent; color: var(--text-sec);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--bg-hover); color: var(--text); }

.btn-sm { padding: .3rem .7rem; font-size: .75rem; }
.btn-xs { padding: .2rem .55rem; font-size: .7rem; }

/* ── Alerts ────────────────────────────────────────────────── */
.alert {
  padding: .6rem .85rem; border-radius: var(--radius);
  font-size: .8rem; line-height: 1.5; white-space: pre-line;
}
.alert-error   { background: var(--danger-muted);  color: #f87171; border: 1px solid rgba(229,72,77,.25); }
.alert-success { background: var(--success-muted); color: #4ade80; border: 1px solid rgba(48,164,108,.25); }
.alert-info    { background: var(--accent-muted);  color: var(--accent-light); border: 1px solid rgba(94,106,210,.25); }

/* ── Modal ─────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--bg-elevated);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%; max-width: 380px;
  padding: 2rem;
}
.modal-logo {
  text-align: center; margin-bottom: 1.5rem;
}
.modal-logo .icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  background: var(--accent); border-radius: var(--radius);
  font-size: 16px; font-weight: 800; color: white;
  margin-bottom: .75rem;
}
.modal h2 { text-align: center; margin-bottom: 1.5rem; font-size: 1rem; }

/* ── Tabs ──────────────────────────────────────────────────── */
.tabs {
  display: flex; background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 3px;
  margin-bottom: 1.25rem;
}
.tab {
  flex: 1; text-align: center;
  padding: .35rem .75rem; border-radius: var(--radius-sm);
  font-size: .8rem; font-weight: 500; cursor: pointer;
  color: var(--text-muted);
  transition: background var(--ease), color var(--ease);
}
.tab.active { background: var(--bg-hover); color: var(--text); }

/* ── Sidebar nav list ──────────────────────────────────────── */
.ds-list { list-style: none; display: flex; flex-direction: column; gap: 1px; }
.ds-list li {
  display: flex; align-items: center; justify-content: space-between;
  padding: .45rem .6rem; border-radius: var(--radius-sm);
  cursor: pointer; font-size: .825rem; color: var(--text-sec);
  transition: background var(--ease), color var(--ease);
}
.ds-list li:hover { background: var(--bg-hover); color: var(--text); }
.ds-list li.selected { background: var(--bg-active); color: var(--text); }
.ds-badge {
  font-size: .65rem; font-weight: 600;
  background: var(--accent-muted); color: var(--accent-light);
  padding: .1rem .4rem; border-radius: 999px;
  border: 1px solid rgba(94,106,210,0.2);
}

.ds-badge-demo {
  background: rgba(45,180,100,0.12); color: #4ade80;
  border-color: rgba(45,180,100,0.2);
}

.export-menu-item {
  display: block; width: 100%; padding: .5rem .875rem;
  background: none; border: none; cursor: pointer;
  color: var(--text-sec); font-size: .825rem; text-align: left;
  transition: background var(--ease), color var(--ease);
}
.export-menu-item:hover { background: var(--bg-hover); color: var(--text); }

.prompt-history {
  margin: 0 1.25rem .5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.prompt-history-toggle {
  display: flex; align-items: center; gap: .5rem;
  width: 100%; padding: .5rem .875rem;
  background: none; border: none; cursor: pointer;
  color: var(--text-sec); font-size: .8rem; text-align: left;
  transition: background var(--ease), color var(--ease);
}
.prompt-history-toggle:hover { background: var(--bg-hover); color: var(--text); }
.prompt-history-count {
  margin-left: auto;
  background: var(--bg-hover); border-radius: 999px;
  padding: .05rem .45rem; font-size: .75rem; color: var(--text-muted);
}
.prompt-history-entry {
  display: grid;
  grid-template-columns: 4.5rem 1fr auto;
  align-items: baseline;
  gap: .5rem;
  padding: .35rem .875rem;
  border-top: 1px solid var(--border);
  font-size: .8rem;
}
.prompt-history-role {
  font-size: .7rem; font-weight: 600; letter-spacing: .03em;
  text-transform: uppercase; padding: .1rem .35rem;
  border-radius: 4px; text-align: center;
}
.role-create { background: rgba(99,102,241,.15); color: #818cf8; }
.role-refine { background: rgba(45,180,100,.12); color: #4ade80; }
.prompt-history-text { color: var(--text-sec); line-height: 1.45; }
.prompt-history-time { color: var(--text-muted); white-space: nowrap; font-size: .75rem; }

.recent-list { list-style: none; display: flex; flex-direction: column; gap: 1px; }
.recent-list li {
  padding: .4rem .5rem; border-radius: var(--radius-sm);
  font-size: .825rem;
  transition: background var(--ease);
}
.recent-list li:hover { background: var(--bg-hover); }
.recent-list a { color: var(--text-sec); }
.recent-list a:hover { color: var(--text); }

/* ── Add data source form (inline) ────────────────────────── */
.ds-form {
  background: var(--bg-overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .85rem;
  margin-top: .5rem;
}
.ds-form .stack > * + * { margin-top: .6rem; }

/* ── Welcome prompt area ───────────────────────────────────── */
.prompt-section {
  display: flex; flex-direction: column; gap: 1.25rem;
}
.prompt-section h2 {
  font-size: .875rem; color: var(--text); margin-bottom: -.5rem;
}

/* ── Sample prompts ────────────────────────────────────────── */
.sample-prompts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: .5rem;
}
.sample-prompt-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .65rem .85rem;
  font-family: inherit; font-size: .8rem;
  color: var(--text-sec); cursor: pointer; text-align: left; line-height: 1.45;
  transition: background var(--ease), border-color var(--ease), color var(--ease);
}
.sample-prompt-btn:hover {
  background: var(--bg-overlay);
  border-color: rgba(255,255,255,0.12);
  color: var(--text);
}

/* ── Dashboard header ──────────────────────────────────────── */
.dash-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-base);
}
.dash-header h1 { font-size: 1rem; letter-spacing: -0.01em; }
.dash-meta { color: var(--text-muted); font-size: .78rem; margin-top: .15rem; }

/* ── Edit prompt bar ───────────────────────────────────────── */
.edit-bar {
  display: flex; gap: .75rem; align-items: flex-start;
  padding: .85rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-base);
}
.edit-bar textarea { min-height: 44px; font-size: .825rem; }

/* ── Charts grid ───────────────────────────────────────────── */
.charts-grid {
  display: grid; gap: 1rem;
  padding: 1.25rem;
  background: var(--bg-base);
  min-height: calc(100vh - 160px);
}
.charts-grid.cols-1 { grid-template-columns: 1fr; }
.charts-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.charts-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
/* ── Responsive: tablet — collapse chart columns ───────────── */
@media (max-width: 900px) {
  .charts-grid.cols-2,
  .charts-grid.cols-3 { grid-template-columns: 1fr; }
}

/* ── Responsive: mobile (≤ 768 px) ────────────────────────── */
@media (max-width: 768px) {
  /* Main layout: sidebar removed from grid flow (it's a drawer now) */
  .app-layout { grid-template-columns: 1fr; }

  /* Show the hamburger toggle */
  .sidebar-toggle { display: flex; }

  /* Sidebar becomes a slide-in drawer */
  .sidebar {
    position: fixed;
    top: 48px; left: 0;
    width: 280px;
    height: calc(100vh - 48px);
    z-index: 90;
    overflow-y: auto;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 8px 0 32px rgba(0,0,0,0.5);
    border-right: 1px solid var(--border);
  }
  .sidebar.open { transform: translateX(0); }

  /* Prevent background scroll while drawer is open */
  body.sidebar-open { overflow: hidden; }

  /* Hide the email address to keep the topbar uncluttered */
  .topbar-email { display: none; }

  /* iOS Safari zooms in when tapping an input with font-size < 16 px */
  input[type=text],
  input[type=email],
  input[type=password],
  select,
  textarea { font-size: 16px; }

  /* Shorter chart tiles on phone screens */
  .chart-canvas  { height: 240px; }
  .chart-overlay { height: 240px; }

  /* Stack the edit-prompt bar vertically */
  .edit-bar { flex-direction: column; }
  .edit-bar .btn { align-self: flex-end; }

  /* Modal shouldn't overflow the screen */
  .modal { max-width: calc(100vw - 2rem); padding: 1.5rem; }

  /* Give the prompt area a bit more breathing room */
  .main-content { padding: 1rem; }
}

/* ── Chart card ────────────────────────────────────────────── */
.chart-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.chart-card-header {
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border-light);
  font-size: .8rem; font-weight: 500; color: var(--text-sec);
}
.chart-canvas { width: 100%; height: 360px; }

.chart-overlay {
  display: flex; align-items: center; justify-content: center;
  height: 360px;
  font-size: .8rem; color: var(--text-muted);
  padding: 1rem; text-align: center;
}
.chart-overlay-error { color: var(--danger); }

/* ── Spinner ───────────────────────────────────────────────── */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 1.5px solid currentColor; border-top-color: transparent;
  border-radius: 50%; animation: spin .55s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Misc helpers ──────────────────────────────────────────── */
.text-muted  { color: var(--text-muted); font-size: .8rem; }
.text-sec    { color: var(--text-sec);   font-size: .825rem; }
.divider     { height: 1px; background: var(--border); margin: .75rem 0; }
.mt-1  { margin-top: .5rem; }
.mt-2  { margin-top: 1rem; }
.mt-3  { margin-top: 1.5rem; }
.mb-1  { margin-bottom: .5rem; }

.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 3rem 1.5rem; color: var(--text-muted);
  text-align: center; gap: .5rem;
}

.kbd {
  display: inline-block;
  padding: .1rem .35rem;
  background: var(--bg-overlay); border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: .7rem; color: var(--text-muted);
  font-family: inherit;
}

[x-cloak] { display: none !important; }
