/* ---------- Design tokens ---------- */
:root {
  --accent: #9a2846;
  --accent-hover: #7f1f3a;
  --accent-tint: #fdf1f4;
  --accent-tint-strong: #f6d9e0;

  --gray-50: #fafaf9;
  --gray-100: #f5f4f2;
  --gray-150: #efece9;
  --gray-200: #e7e3e0;
  --gray-300: #d6d1cc;
  --gray-400: #a89f97;
  --gray-500: #79706a;
  --gray-600: #574f4a;
  --gray-700: #3f3934;
  --gray-800: #292521;
  --gray-900: #1b1815;

  --success: #157f3c;
  --success-bg: #eafbf1;
  --warning: #a15c07;
  --warning-bg: #fef6e7;
  --orange: #b8460e;
  --orange-bg: #fdf0e8;
  --danger: #c22a3e;
  --danger-bg: #fdedef;
  --info: #574f4a;
  --info-bg: #f2f0ee;

  --bg: var(--gray-50);
  --surface: #ffffff;
  --border: var(--gray-200);
  --text: var(--gray-900);
  --text-muted: var(--gray-500);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(27, 24, 21, 0.06), 0 1px 1px rgba(27, 24, 21, 0.04);
  --shadow-md: 0 8px 24px rgba(27, 24, 21, 0.08), 0 2px 6px rgba(27, 24, 21, 0.05);

  --sidebar-width: 248px;
}

/* ---------- Reset & base ---------- */
* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  margin: 0;
  font-feature-settings: "tnum" 1, "cv11" 1, "ss01" 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3 { font-weight: 600; letter-spacing: -0.01em; color: var(--gray-900); }
h1 { font-size: 1.5rem; margin: 0 0 0.35rem 0; display: flex; align-items: center; gap: 0.6rem; }
h2 { font-size: 1.05rem; margin: 1.75rem 0 0.85rem 0; }

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

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 1.25rem 1.25rem 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand a { display: flex; align-items: center; gap: 0.6rem; color: var(--gray-900); }
.sidebar-brand a:hover { text-decoration: none; }

.brand-glyph {
  width: 34px; height: 34px;
  object-fit: contain;
  flex-shrink: 0;
}

.brand-text { line-height: 1.25; }
.brand-text .name { font-weight: 600; font-size: 0.9rem; display: block; }
.brand-text .tag { font-size: 0.72rem; color: var(--text-muted); display: block; }

.sidebar-search { padding: 0.75rem 1.25rem 0 1.25rem; }
.sidebar-search input {
  width: 100%;
  font-size: 0.82rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--gray-50);
}

.sidebar-nav { flex: 1; padding: 0.9rem 0.75rem; }

.nav-group { margin-bottom: 1.4rem; }
.nav-group-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--gray-400);
  padding: 0 0.6rem;
  margin-bottom: 0.35rem;
}

.nav-link {
  display: block;
  padding: 0.45rem 0.6rem;
  border-radius: var(--radius-sm);
  color: var(--gray-700);
  font-size: 0.87rem;
  font-weight: 500;
  margin-bottom: 0.1rem;
  transition: background 0.15s ease, color 0.15s ease, padding-left 0.15s ease;
}
.nav-link:hover { background: var(--gray-100); text-decoration: none; color: var(--gray-900); padding-left: 0.75rem; }
.nav-link.active { background: var(--accent-tint); color: var(--accent-hover); font-weight: 600; }

.sidebar-footer { padding: 0.85rem 1.1rem; border-top: 1px solid var(--border); }
.sidebar-footer form { margin: 0; }
.logout-link {
  background: none; border: none; padding: 0.3rem 0; cursor: pointer;
  color: var(--text-muted); font-size: 0.82rem; font-family: inherit;
}
.logout-link:hover { color: var(--danger); text-decoration: underline; }

.app-main { flex: 1; min-width: 0; }

.content-topbar {
  display: flex; align-items: center; justify-content: flex-end;
  padding: 0.85rem 2.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

main.content { max-width: 1180px; padding: 2rem 2.25rem 3.5rem 2.25rem; }

/* ---------- Cards / tables ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  transition: box-shadow 0.18s ease, transform 0.18s ease, border-color 0.18s ease;
}
/* Only a card that's actually a link gets the "this is clickable" lift --
   applying it to every .card (most are plain static containers: forms,
   detail panels) would be a false affordance. */
a.card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-3px);
  border-color: var(--accent-tint-strong);
  text-decoration: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0 1.5rem 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

/* A table wide enough to overflow (many columns, or columns that need real
   room -- e.g. ai_actions.html's Inputs/Output) should scroll inside its
   own box, not push the whole page wider than the viewport. Real bug found
   2026-08-30 via an automated overflow sweep across every page. */
.table-scroll {
  overflow-x: auto;
  margin: 1rem 0 1.5rem 0;
}
.table-scroll table { margin: 0; }

th, td {
  padding: 0.65rem 0.9rem;
  text-align: left;
  font-size: 0.86rem;
  border-bottom: 1px solid var(--border);
}

th {
  background: var(--gray-50);
  color: var(--gray-500);
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr { transition: background 0.12s ease; }
tbody tr:hover { background: var(--gray-50); }

td input, td select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.35rem 0.5rem;
  font-size: 0.84rem;
  background: var(--surface);
}

td select { padding-right: 1.4rem; }

/* ---------- Forms ---------- */
input, select, textarea {
  font-family: inherit;
  font-size: 0.88rem;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-tint);
}

label {
  display: block;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--gray-600);
  margin-bottom: 0.3rem;
}

.field { margin-bottom: 1rem; }
.field-row { display: flex; gap: 1.1rem; flex-wrap: wrap; }
.field-row .field { flex: 1; min-width: 200px; }
.field input, .field select, .field textarea { width: 100%; }

/* ---------- Buttons ---------- */
button, .btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.86rem;
  font-weight: 600;
  font-family: inherit;
  text-decoration: none;
  display: inline-block;
  transition: background 0.12s ease, box-shadow 0.15s ease, transform 0.1s ease;
  box-shadow: var(--shadow-sm);
}
button:hover, .btn:hover { background: var(--accent-hover); text-decoration: none; transform: translateY(-1px); box-shadow: var(--shadow-md); }
button:active, .btn:active { transform: translateY(1px) scale(0.97); box-shadow: var(--shadow-sm); }

button.secondary, .btn.secondary {
  background: var(--surface);
  color: var(--gray-700);
  border: 1px solid var(--border);
  box-shadow: none;
}
button.secondary:hover, .btn.secondary:hover { background: var(--gray-50); border-color: var(--gray-300); box-shadow: var(--shadow-sm); }

button.remove-row {
  background: transparent;
  color: var(--gray-400);
  box-shadow: none;
  padding: 0.2rem 0.5rem;
  font-size: 1rem;
}
button.remove-row:hover { background: var(--danger-bg); color: var(--danger); }

/* ---------- Status badges ---------- */
.status-badge {
  display: inline-block;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  vertical-align: middle;
}

.status-draft, .status-open, .status-quoted, .status-sent, .status-pending { background: var(--info-bg); color: var(--info); }
.status-finalized, .status-closed, .status-converted, .status-accepted, .status-matched, .status-delivered { background: var(--success-bg); color: var(--success); }
.status-cancelled, .status-lost, .status-rejected, .status-expired, .status-unmatched { background: var(--danger-bg); color: var(--danger); }
.status-dispatched { background: var(--warning-bg); color: var(--warning); }
.status-flagged { background: var(--warning-bg); color: var(--warning); }

.status-bucket-not-yet-due { background: var(--info-bg); color: var(--info); }
.status-bucket-0-30 { background: var(--success-bg); color: var(--success); }
.status-bucket-31-60 { background: var(--warning-bg); color: var(--warning); }
.status-bucket-61-90 { background: var(--orange-bg); color: var(--orange); }
.status-bucket-90plus { background: var(--danger-bg); color: var(--danger); }

/* ---------- Misc components ---------- */
.totals { margin-left: auto; width: 300px; }
.totals div { display: flex; justify-content: space-between; padding: 0.3rem 0; font-size: 0.87rem; }
.totals .grand-total {
  font-weight: 700; font-size: 1.05rem;
  border-top: 1px solid var(--border);
  margin-top: 0.5rem; padding-top: 0.65rem;
}

.error-banner, .success-banner {
  padding: 0.7rem 1rem;
  border-radius: var(--radius-md);
  margin-bottom: 1.25rem;
  font-size: 0.86rem;
}
.error-banner { background: var(--danger-bg); color: var(--danger); border: 1px solid var(--danger-bg); }
.success-banner { background: var(--success-bg); color: var(--success); border: 1px solid var(--success-bg); }

.muted { color: var(--text-muted); font-size: 0.85rem; }

/* ---------- Action bars (detail-page footer actions) ---------- */
.action-bar {
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.action-bar-group { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.inline-form { display: flex; gap: 0.5rem; align-items: center; }
.action-bar-danger { padding-left: 1rem; border-left: 1px solid var(--border); }

/* ---------- Stat tiles ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.9rem;
  margin: 1rem 0 1.75rem 0;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1rem 1.1rem;
}
.stat-tile .status-badge { margin-bottom: 0.55rem; }
.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--gray-900);
  font-variant-numeric: tabular-nums;
}

/* A table nested inside a card shouldn't double up on border/shadow */
.card table { box-shadow: none; border-color: var(--gray-150); margin-top: 0.25rem; }
.card > h2:first-child, .card > .muted:first-child + h2 { margin-top: 0; }

.line-amount-preview { font-variant-numeric: tabular-nums; }

/* ---------- Assistant chat ---------- */
.htmx-indicator { opacity: 0; transition: opacity 150ms ease; }
.htmx-request.htmx-indicator, .htmx-request .htmx-indicator { opacity: 1; }

.assistant-msg { margin-bottom: 1rem; max-width: 80%; }
.assistant-msg-role { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; color: var(--gray-500); margin-bottom: 0.2rem; }
.assistant-msg-content { white-space: pre-wrap; padding: 0.6rem 0.85rem; border-radius: 10px; font-size: 0.9rem; line-height: 1.45; }
.assistant-msg-user { margin-left: auto; }
.assistant-msg-user .assistant-msg-role { text-align: right; }
.assistant-msg-user .assistant-msg-content { background: var(--accent); color: #fff; }
.assistant-msg-assistant .assistant-msg-content { background: var(--gray-100); color: var(--gray-900); }

/* ---------- Page transitions (app/static/motion.js) ---------- */
@keyframes page-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
main.content { animation: page-fade-in 0.32s ease both; }
@media (prefers-reduced-motion: reduce) {
  main.content { animation: none; }
}

#page-progress {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 0;
  background: var(--accent);
  z-index: 3000;
  transition: width 0.3s ease, opacity 0.2s ease 0.15s;
  box-shadow: 0 0 8px var(--accent);
}

/* A number that counts up on load (app/static/motion.js) -- tabular-nums
   keeps the digit widths steady so the tile doesn't jiggle as it counts. */
.count-up { font-variant-numeric: tabular-nums; }

/* ---------- Guided tour (app/static/tour.js) ---------- */
.tour-highlight {
  position: relative;
  z-index: 2001;
  border-radius: var(--radius-sm);
  /* The spotlight effect: a huge shadow the color of the dim overlay,
     covering the whole viewport except this one element -- no separate
     overlay element or SVG mask needed. */
  box-shadow: 0 0 0 4px var(--accent), 0 0 0 9999px rgba(20, 16, 14, 0.55);
  transition: box-shadow 0.15s ease;
}

.tour-tooltip {
  position: fixed;
  z-index: 2002;
  max-width: 340px;
  background: var(--surface);
  color: var(--gray-900);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 1.1rem 1.25rem;
  font-size: 0.88rem;
  line-height: 1.5;
}
.tour-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2001;
  background: rgba(20, 16, 14, 0.55);
}
.tour-tooltip.tour-tooltip-center {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 420px;
}
.tour-tooltip h3 { margin: 0 0 0.5rem 0; font-size: 1.02rem; }
.tour-tooltip p { margin: 0 0 1rem 0; white-space: pre-line; }
.tour-tooltip-footer { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
.tour-tooltip-step { font-size: 0.72rem; color: var(--text-muted); white-space: nowrap; }
.tour-tooltip-actions { display: flex; gap: 0.5rem; }
.tour-tooltip-actions button { font-size: 0.82rem; padding: 0.4rem 0.8rem; }
.tour-skip {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 2002;
  background: rgba(20, 16, 14, 0.55);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.8rem;
  font-size: 0.8rem;
  cursor: pointer;
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: relative; border-right: none; border-bottom: 1px solid var(--border); }
  main.content { padding: 1.25rem; }
  .content-topbar { padding: 0.75rem 1.25rem; }
}
