/* ===== Editorial Design Tokens ===== */
:root {
  --paper:   #F6F1E8;
  --paper2:  #EFE7D8;
  --ink:     #1C1714;
  --ink2:    #4A3F36;
  --muted:   #8A7A68;
  --rule:    #D9CDB6;
  --accent:  #B33A1A;
  --gold:    #C8932B;
  --online:  #3F8F4E;

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans:  ui-sans-serif, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

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

html { height: 100%; }
body {
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
}

/* ===== Layout ===== */
.layout {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

/* -- Sidebar -- */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  height: 100vh;
  height: 100dvh;
  position: sticky;
  top: 0;
  background: var(--paper2);
  border-right: 1px solid var(--rule);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 22px 22px 16px;
}

.sidebar-issue {
  font-family: var(--sans);
  font-size: 10px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 6px;
}

.sidebar-title {
  font-family: var(--serif);
  font-size: 22px;
  line-height: 1.05;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.sidebar-version {
  font-family: var(--sans);
  font-size: 11px;
  color: var(--muted);
  margin-top: 6px;
  letter-spacing: 0.04em;
}

/* -- Hairline -- */
.hairline {
  height: 1px;
  background: var(--rule);
  flex-shrink: 0;
}

/* -- New Inquiry btn -- */
.new-inquiry-wrap { padding: 14px 18px; }

.btn-new-inquiry {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 14px;
  background: var(--ink);
  color: var(--paper);
  border: none;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: opacity 0.15s;
}
.btn-new-inquiry:hover { opacity: 0.85; }

/* -- Model selector -- */
.model-wrap { padding: 4px 22px 14px; }

.model-label {
  font-family: var(--sans);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  display: block;
  margin-bottom: 6px;
}

.model-select {
  width: 100%;
  padding: 9px 12px;
  background: var(--paper);
  border: 1px solid var(--rule);
  font-family: var(--serif);
  font-size: 13px;
  color: var(--ink);
  font-style: italic;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%238A7A68' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
}

/* -- Health badge -- */
.health-wrap { padding: 14px 22px; }

.health-status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.health-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  flex-shrink: 0;
}
.health-dot.online  { background: var(--online); box-shadow: 0 0 0 3px rgba(63,143,78,0.13); }
.health-dot.offline { background: var(--accent); box-shadow: 0 0 0 3px rgba(179,58,26,0.13); }

.health-text {
  font-family: var(--sans);
  font-size: 11.5px;
  color: var(--ink2);
  letter-spacing: 0.04em;
}
.health-text strong { color: var(--ink); }

.health-detail {
  margin-top: 8px;
  font-family: var(--serif);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  font-style: italic;
}

/* -- History -- */
.history-label {
  padding: 14px 22px 8px;
  font-family: var(--sans);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 14px 18px;
}

.history-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  font-family: var(--serif);
  font-size: 13.5px;
  line-height: 1.35;
  color: var(--ink2);
  border: none;
  border-left: 2px solid transparent;
  background: transparent;
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.1s;
}
.history-item:hover { background: var(--paper); }
.history-item.active {
  color: var(--ink);
  border-left-color: var(--accent);
  background: var(--paper);
}

.history-actions {
  padding: 4px 22px 8px;
}

.btn-clear-history {
  background: none;
  border: none;
  font-family: var(--sans);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  cursor: pointer;
}
.btn-clear-history:hover { color: var(--accent); }

.sidebar-footer {
  padding: 12px 22px;
  font-family: var(--sans);
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ===== Main Area ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-width: 0;
  overflow-x: hidden;
}

.main-scroll {
  flex: 1;
  overflow-y: auto;
}

/* -- Chat header -- */
.chat-header {
  padding: 32px 48px 20px;
  border-bottom: 1px solid var(--rule);
}

.chat-header-kicker {
  font-family: var(--sans);
  font-size: 10.5px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 10px;
}

.chat-header h1 {
  font-family: var(--serif);
  font-size: 44px;
  line-height: 1.02;
  color: var(--ink);
  letter-spacing: -0.02em;
  margin: 0;
  font-weight: normal;
}
.chat-header h1 em { color: var(--accent); }

.chat-header-sub {
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink2);
  margin: 10px 0 16px;
  max-width: 560px;
  font-style: italic;
}

/* -- Chips -- */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  font-family: var(--sans);
  font-size: 12.5px;
  color: var(--ink2);
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 7px 13px;
  line-height: 1.2;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.chip:hover {
  background: var(--paper2);
  border-color: var(--ink2);
}

/* ===== Empty State ===== */
.empty-state {
  padding: 70px 48px;
  text-align: center;
  max-width: 560px;
  margin: 0 auto;
}

.empty-quote-mark {
  font-family: var(--serif);
  font-size: 120px;
  line-height: 1;
  color: var(--rule);
  font-style: italic;
  user-select: none;
}

.empty-quote {
  font-family: var(--serif);
  font-size: 20px;
  line-height: 1.4;
  color: var(--ink2);
  margin-top: -30px;
  font-style: italic;
}

.empty-hint {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 18px;
}

/* ===== Conversation Area ===== */
.chat-area {
  padding: 36px 48px 24px;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
}

/* -- User message -- */
.user-msg {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 28px;
}

.user-msg-bubble {
  max-width: 460px;
  background: var(--ink);
  color: var(--paper);
  padding: 12px 18px;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  border-radius: 2px;
  position: relative;
}

.user-msg-bubble::after {
  content: "";
  position: absolute;
  top: 6px; left: 6px; right: 6px; bottom: 6px;
  border: 1px solid var(--gold);
  opacity: 0.35;
  pointer-events: none;
}

/* -- Assistant message -- */
.assistant-msg {
  max-width: 680px;
  margin-right: auto;
  margin-bottom: 36px;
}

.msg-kicker {
  font-family: var(--sans);
  font-size: 10.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 6px;
}

.msg-headline {
  font-family: var(--serif);
  font-size: 26px;
  line-height: 1.18;
  color: var(--ink);
  margin: 0 0 14px;
  letter-spacing: -0.01em;
  font-weight: normal;
}

.msg-body {
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.65;
  color: var(--ink2);
  overflow-wrap: break-word;
}
.msg-body p { margin: 0 0 12px; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body strong { color: var(--ink); }
.msg-body em { font-style: italic; }

/* -- Error message -- */
.msg-error {
  max-width: 680px;
  margin-right: auto;
  margin-bottom: 36px;
  padding: 16px 20px;
  border: 1px solid var(--rule);
  border-left: 3px solid var(--accent);
  background: #FBF7EE;
  font-family: var(--sans);
  font-size: 13.5px;
  color: var(--ink2);
  line-height: 1.5;
}

/* ===== Charts ===== */
.chart-container {
  margin: 18px 0 8px;
}

.chart-container figure {
  margin: 0;
  border-top: 1px solid var(--ink);
  padding-top: 14px;
}

.chart-caption {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 14px;
  display: flex;
  justify-content: space-between;
}

.chart-source {
  border-top: 1px solid var(--rule);
  margin-top: 14px;
  padding-top: 8px;
  font-family: var(--sans);
  font-size: 10.5px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* Plotly charts wrapper */
.plotly-chart-wrap {
  margin: 18px 0 8px;
  border: 1px solid var(--rule);
  border-radius: 2px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Chart toggle selects */
.chart-toggles {
  display: flex;
  gap: 12px;
  margin-bottom: 10px;
}

.chart-toggle-select {
  font-family: var(--sans);
  font-size: 11px;
  padding: 5px 8px;
  border: 1px solid var(--rule);
  background: var(--paper);
  color: var(--ink2);
  cursor: pointer;
}

/* ===== Data Table ===== */
.data-table-wrap {
  margin-top: 16px;
  border: 1px solid var(--rule);
  background: #FBF7EE;
}

.data-table-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: transparent;
  border: none;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 11.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink2);
}

.data-table-toggle svg {
  width: 14px;
  height: 14px;
}

.data-table-content {
  border-top: 1px solid var(--rule);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  position: relative;
}
.data-table-content.has-scroll::after {
  content: "";
  position: sticky;
  top: 0;
  right: 0;
  bottom: 0;
  width: 24px;
  float: right;
  background: linear-gradient(to right, transparent, var(--paper2));
  pointer-events: none;
  margin-bottom: -100%;
  height: 100%;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 12.5px;
}

.data-table th {
  text-align: left;
  padding: 9px 14px;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-size: 10.5px;
  border-bottom: 1px solid var(--rule);
}

.data-table td {
  padding: 9px 14px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  border-bottom: 1px solid var(--rule);
}

.data-table tr:last-child td {
  border-bottom: none;
}

.data-table td:first-child {
  font-style: italic;
  font-family: var(--serif);
  font-size: 13.5px;
}

/* ===== Action Bar ===== */
.action-bar {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed var(--rule);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.action-bar-left {
  display: flex;
  gap: 18px;
}

.action-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--sans);
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-decoration: none;
  padding: 0;
}
.action-btn:hover { color: var(--ink2); }

.action-btn svg {
  width: 13px;
  height: 13px;
}

.action-meta {
  font-family: var(--sans);
  font-size: 10.5px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* SQL details */
.sql-details {
  margin-top: 8px;
}

.sql-details summary {
  cursor: pointer;
  font-family: var(--sans);
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: 0.04em;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  list-style: none;
}
.sql-details summary::-webkit-details-marker { display: none; }
.sql-details summary::before {
  content: "";
  display: inline-block;
  width: 13px;
  height: 13px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='13' height='13' viewBox='0 0 24 24' fill='none' stroke='%238A7A68' stroke-width='2'%3E%3Cpolyline points='16 18 22 12 16 6'/%3E%3Cpolyline points='8 6 2 12 8 18'/%3E%3C/svg%3E");
  background-size: contain;
}

.sql-code {
  margin-top: 8px;
  padding: 12px 14px;
  background: var(--ink);
  color: #e8dcc8;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 12px;
  line-height: 1.5;
  overflow-x: auto;
  border-radius: 2px;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ===== Loading State ===== */
.loading-indicator {
  max-width: 680px;
  margin-right: auto;
  margin-bottom: 24px;
}

.loading-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--serif);
  font-size: 14px;
  color: var(--muted);
  font-style: italic;
}

.loading-dash {
  display: inline-block;
  width: 22px;
  height: 1px;
  background: var(--accent);
  flex-shrink: 0;
}

@keyframes loading-pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}
.loading-inner { animation: loading-pulse 2s ease-in-out infinite; }

/* ===== Input Bar ===== */
.input-bar {
  border-top: 1px solid var(--rule);
  background: var(--paper);
  padding: 16px 48px 22px;
  flex-shrink: 0;
}

.input-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--ink);
  padding: 6px 6px 6px 16px;
  background: #FBF7EE;
}

.input-field {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--serif);
  font-size: 16px;
  color: var(--ink);
  font-style: italic;
  outline: none;
}
.input-field::placeholder {
  color: var(--muted);
  font-style: italic;
}

.btn-submit {
  background: var(--accent);
  color: var(--paper);
  border: none;
  padding: 8px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  white-space: nowrap;
  transition: opacity 0.15s;
}
.btn-submit:hover { opacity: 0.85; }
.btn-submit:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-submit svg {
  width: 12px;
  height: 12px;
}

.input-disclaimer {
  max-width: 760px;
  margin: 8px auto 0;
  font-family: var(--sans);
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
}

/* ===== Share Popover ===== */
.share-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  background: var(--paper2);
  border: 1px solid var(--rule);
  padding: 12px 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  z-index: 100;
  min-width: 200px;
  animation: share-pop-in 0.15s ease-out;
}

@keyframes share-pop-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.share-popover-title {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--online);
  margin-bottom: 10px;
}

.share-popover-links {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.share-link {
  display: block;
  padding: 7px 0;
  font-family: var(--serif);
  font-size: 14px;
  color: var(--accent);
  text-decoration: none;
  transition: color 0.1s;
}
.share-link:hover {
  color: var(--ink);
}

/* ===== HTMX loading class ===== */
.htmx-request .btn-submit { opacity: 0.5; pointer-events: none; }
.htmx-request .input-field { opacity: 0.5; }

/* ===== Touch feedback (global — works on desktop click + mobile tap) ===== */
.chip:active { background: var(--paper2); border-color: var(--ink2); transform: scale(0.97); }
.history-item:active { background: var(--paper); }
.btn-new-inquiry:active { opacity: 0.7; }
.action-btn:active { color: var(--ink); }
.btn-submit:active { opacity: 0.7; }
.mobile-menu-btn:active, .mobile-new-inquiry-btn:active { opacity: 0.6; }

/* ===== Mobile topbar & drawer (hidden on desktop) ===== */
.mobile-topbar { display: none; }
.drawer-backdrop { display: none; }

/* ===== Responsive ===== */
@media (max-width: 768px) {

  /* -- Layout: switch to column so topbar stacks above main -- */
  .layout { flex-direction: column; }

  /* -- Mobile top bar -- */
  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: var(--paper2);
    border-bottom: 1px solid var(--rule);
    flex-shrink: 0;
    z-index: 200;
  }
  .mobile-topbar-title {
    font-family: var(--serif);
    font-size: 17px;
    color: var(--ink);
    letter-spacing: -0.01em;
  }
  .mobile-topbar-title em { color: var(--accent); }
  .mobile-menu-btn,
  .mobile-new-inquiry-btn {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--ink);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  /* -- Sidebar as slide-out drawer -- */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    height: auto;
    z-index: 300;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    display: flex;
  }
  .sidebar.drawer-open {
    transform: translateX(0);
  }

  /* -- Drawer backdrop -- */
  .drawer-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.35);
    z-index: 250;
  }
  .drawer-backdrop.visible {
    display: block;
  }

  /* -- Main area fills remaining space below topbar -- */
  .main {
    height: auto;
    flex: 1;
    min-height: 0;
  }

  /* -- Header: compact for mobile -- */
  .chat-header { padding: 14px 16px 12px; }
  .chat-header-kicker { font-size: 9px; margin-bottom: 4px; }
  .chat-header h1 { font-size: 22px; line-height: 1.1; }
  .chat-header-sub { font-size: 13px; margin: 4px 0 10px; }

  /* -- Chips: larger tap targets -- */
  .chips { gap: 6px; }
  .chip {
    font-size: 12px;
    padding: 10px 14px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  /* -- Chat area -- */
  .chat-area { padding: 20px 16px 16px; }

  /* -- User bubble: fit within screen -- */
  .user-msg-bubble { max-width: calc(100vw - 56px); }

  /* -- Message headline -- */
  .msg-headline { font-size: 20px; }

  /* -- Action bar: stack vertically -- */
  .action-bar {
    flex-direction: column;
    align-items: flex-start;
  }
  .action-bar-left {
    gap: 4px;
    flex-wrap: wrap;
  }
  .action-btn {
    padding: 10px 8px;
    min-height: 44px;
  }
  .action-meta { margin-top: 4px; }

  /* -- Chart toggles: wrap + larger targets -- */
  .chart-toggles { flex-wrap: wrap; }
  .chart-toggle-select {
    padding: 10px 12px;
    min-height: 44px;
    font-size: 13px;
  }

  /* -- Data table toggle: larger target -- */
  .data-table-toggle {
    min-height: 48px;
    padding: 12px 14px;
  }

  /* -- Empty state: compact -- */
  .empty-state { padding: 20px 16px; }
  .empty-quote-mark { font-size: 48px; line-height: 0.8; }
  .empty-quote { font-size: 15px; margin-top: -10px; }
  .empty-hint { font-size: 10px; margin-top: 12px; }

  /* -- Input bar: safe area for iPhone notch -- */
  .input-bar {
    padding: 12px 16px 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }
}
