/* Carworx custom theme. Tailwind handles utilities; this layer adds brand polish.
   Dark-mode swap is purely a CSS-variable change — components don't need to be rewritten. */

:root {
  --attn-invoice: #7C3AED;
  --attn-invoice-soft: #A78BFA;
  --carworx-red: #E31B23;
  --carworx-red-dark: #B0141A;
  --carworx-red-soft: #FFE5E6;
  --carworx-ink: #0F172A;
  --carworx-ink-soft: #475569;
  --carworx-surface: #F8FAFC;
  --carworx-line: #E2E8F0;

  /* Surfaces / chrome layered on top of the page */
  --carworx-card: #FFFFFF;
  --carworx-card-soft: #FAFBFD;          /* row hover, button-secondary hover */
  --carworx-row-line: #EEF2F7;           /* table row dividers */
  --carworx-chip-bg: #F1F5F9;
  --carworx-shadow: rgba(15,23,42,0.04);

  --status-standing: #64748B;
  --status-dispatched: #2563EB;
  --status-pickedup: #D97706;
  --status-delivered: #059669;
  --status-cancelled: #94A3B8;
  --status-overdue: #DC2626;
}

/* OLED-true-black dark mode. Pure black surfaces save battery on OLED screens
   and read more cleanly than the typical slate-900 dark mode. */
html.dark {
  --carworx-red-soft: #3A0F12;
  --carworx-ink: #F5F5F7;                 /* primary text → near-white */
  --carworx-ink-soft: #9CA3AF;
  --carworx-surface: #000000;             /* page background — pure black */
  --carworx-card: #0A0A0A;                /* cards lift just enough to be visible */
  --carworx-card-soft: #141414;           /* row hover, secondary surfaces */
  --carworx-row-line: #1A1A1A;
  --carworx-line: #262626;
  --carworx-chip-bg: #1F1F1F;
  --carworx-shadow: rgba(0,0,0,0.65);
}
html.dark { color-scheme: dark; }
html.dark body { background: #000; }

html, body { background: var(--carworx-surface); color: var(--carworx-ink); }
body { font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif; }

/* Sidebar */
.sidebar {
  background: linear-gradient(180deg, #0F172A 0%, #111827 100%);
  color: #E2E8F0;
}
.sidebar a { color: #CBD5E1; }
.sidebar a:hover { color: #fff; background: rgba(255,255,255,0.06); }
.sidebar a.active { background: var(--carworx-red); color: #fff; }
.sidebar .brand-line { background: var(--carworx-red); }

/* Cards */
.card {
  background: var(--carworx-card);
  border: 1px solid var(--carworx-line);
  border-radius: 14px;
  box-shadow: 0 1px 2px var(--carworx-shadow), 0 4px 16px var(--carworx-shadow);
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem; border-bottom: 1px solid var(--carworx-line);
}

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.62rem 1rem; border-radius: 9px;
  font-weight: 600; font-size: 0.9rem; line-height: 1.2; transition: all 0.15s;
  border: 1px solid transparent; min-height: 40px;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  text-decoration: none; cursor: pointer; user-select: none;
}
.btn-primary { background: var(--carworx-red); color: #fff; }
.btn-primary:hover { background: var(--carworx-red-dark); }
.btn-secondary { background: var(--carworx-card); color: var(--carworx-ink); border-color: var(--carworx-line); }
.btn-secondary:hover { background: var(--carworx-card-soft); }
.btn-ghost { background: transparent; color: var(--carworx-ink-soft); }
.btn-ghost:hover { color: var(--carworx-ink); background: var(--carworx-card-soft); }
.btn-danger { background: var(--carworx-card); color: #DC2626; border-color: #FCA5A5; }
.btn-danger:hover { background: #FEF2F2; border-color: #F87171; color: #B91C1C; }
html.dark .btn-danger { color: #F87171; border-color: #7F1D1D; }
html.dark .btn-danger:hover { background: #1F1212; border-color: #B91C1C; color: #FCA5A5; }

/* Status pill */
.pill {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.18rem 0.6rem; border-radius: 999px;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em;
  text-transform: uppercase;
}
.pill-standing { background: #E2E8F0; color: #334155; }
.pill-dispatched { background: #DBEAFE; color: #1D4ED8; }
.pill-picked_up { background: #FEF3C7; color: #B45309; }
.pill-delivered { background: #D1FAE5; color: #047857; }
.pill-cancelled { background: #F1F5F9; color: #64748B; }
.pill-paid { background: #D1FAE5; color: #047857; }
.pill-sent { background: #D1FAE5; color: #047857; }
.pill-unpaid { background: #FEE2E2; color: #B91C1C; }
.pill-partial { background: #FEF3C7; color: #B45309; }

/* Inbox — sanitized email HTML. nh3 strips scripts/styles upstream; this just keeps
   whatever layout survives contained inside the card. */
.email-body { overflow-x: auto; word-break: break-word; line-height: 1.55; }
.email-body a { color: var(--carworx-red); text-decoration: underline; }
.email-body table { max-width: 100%; }
.email-body img { max-width: 100%; height: auto; }
.email-body p { margin: 0.5rem 0; }
.email-body blockquote {
  border-left: 3px solid var(--carworx-line); margin: 0.5rem 0;
  padding-left: 0.75rem; color: var(--carworx-ink-soft);
}

/* KPI tiles */
.tile {
  background: var(--carworx-card); border: 1px solid var(--carworx-line); border-radius: 14px;
  padding: 1rem 1.1rem; position: relative; overflow: hidden;
}
.tile .tile-label { font-size: 0.78rem; color: var(--carworx-ink-soft); font-weight: 600; }
.tile .tile-value { font-size: 1.85rem; font-weight: 800; margin-top: 0.25rem; color: var(--carworx-ink); }
.tile .tile-foot { font-size: 0.78rem; color: var(--carworx-ink-soft); margin-top: 0.35rem; }
.tile .tile-bar {
  position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--carworx-red);
}
.tile-blue .tile-bar { background: var(--status-dispatched); }
.tile-amber .tile-bar { background: var(--status-pickedup); }
.tile-green .tile-bar { background: var(--status-delivered); }
.tile-slate .tile-bar { background: var(--status-standing); }
.tile-red .tile-bar { background: var(--carworx-red); }

/* Denser KPI tiles on phones — drop the description line and tighten padding/sizes so
   more fit per row without the wasted vertical space. */
@media (max-width: 640px) {
  .tile { padding: 0.55rem 0.65rem; }
  .tile .tile-label { font-size: 0.66rem; line-height: 1.15; }
  .tile .tile-value { font-size: 1.4rem; margin-top: 0.1rem; }
  .tile .tile-foot { display: none; }
}

/* Dashboard — doughnut cards */
.seg-dot { display: inline-block; width: 0.6rem; height: 0.6rem; border-radius: 3px; flex-shrink: 0; }
.tabular-nums { font-variant-numeric: tabular-nums; }
.donut-wrap { position: relative; width: 150px; height: 150px; flex-shrink: 0; }
.donut-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; pointer-events: none;
}
.donut-legend-row {
  display: flex; align-items: center; gap: 0.5rem; padding: 0.4rem 0.5rem;
  border-radius: 8px; font-size: 0.8rem; min-height: 34px; text-decoration: none;
}
.donut-legend-row:hover { background: var(--carworx-card-soft); }
.donut-ring-empty { width: 150px; height: 150px; border-radius: 9999px; border: 12px solid var(--carworx-chip-bg); }
.donut-ring-clear { width: 150px; height: 150px; border-radius: 9999px; border: 12px solid #D1FAE5; }
html.dark .donut-ring-clear { border-color: #064E3B; }

/* Tables */
.tbl { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 0.9rem; }
.tbl thead th {
  text-align: left; font-weight: 700; font-size: 0.72rem; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--carworx-ink-soft);
  padding: 0.7rem 0.9rem; border-bottom: 1px solid var(--carworx-line);
}
.tbl tbody td { padding: 0.75rem 0.9rem; border-bottom: 1px solid var(--carworx-row-line); vertical-align: top; }
.tbl tbody tr:hover { background: var(--carworx-card-soft); }
.tbl tbody tr:last-child td { border-bottom: 0; }

/* Forms */
.field label { display:block; font-size: 0.78rem; font-weight: 600; color: var(--carworx-ink-soft); margin-bottom: 0.3rem; }
.field input, .field select, .field textarea {
  width: 100%; padding: 0.65rem 0.75rem; border-radius: 9px;
  border: 1px solid var(--carworx-line); background: var(--carworx-card);
  font: inherit; font-size: 16px; /* avoid iOS zoom */ color: var(--carworx-ink);
  min-height: 44px; /* iOS touch target */
}
html.dark .field input::placeholder, html.dark .field textarea::placeholder { color: #475569; }
@media (min-width: 768px) {
  .field input, .field select, .field textarea { font-size: 0.9rem; min-height: 0; padding: 0.55rem 0.7rem; }
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--carworx-red);
  box-shadow: 0 0 0 3px rgba(227,27,35,0.12);
}
.field textarea { min-height: 92px; resize: vertical; }
.field-grid { display: grid; gap: 1rem; }
@media (min-width: 768px) { .field-grid-2 { grid-template-columns: repeat(2, 1fr); } .field-grid-3 { grid-template-columns: repeat(3, 1fr); } }

/* Toast */
.toast {
  position: fixed; bottom: calc(1.25rem + env(safe-area-inset-bottom)); right: 1.25rem;
  left: 1.25rem; max-width: 28rem; margin-left: auto; z-index: 60;
  background: var(--carworx-ink); color: #fff; padding: 0.7rem 1rem;
  border-radius: 9px; font-size: 0.88rem; box-shadow: 0 10px 30px rgba(15,23,42,0.2);
  animation: toast-in 0.18s ease-out;
}
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* Header bar above main content */
.topbar {
  background: var(--carworx-card); border-bottom: 1px solid var(--carworx-line);
  padding: 0.85rem 1.5rem; display: flex; align-items: center; justify-content: space-between;
}

/* In dark mode, soften the brutally bright Tailwind utilities used in templates so we
   don't have to rewrite every page. These are deliberate scoped overrides. */
html.dark .bg-white { background-color: var(--carworx-card) !important; }
html.dark .bg-slate-50 { background-color: var(--carworx-card-soft) !important; }
html.dark .bg-slate-100 { background-color: var(--carworx-card-soft) !important; }
html.dark .border-slate-200, html.dark .border-slate-100, html.dark .border-slate-300 { border-color: var(--carworx-line) !important; }
html.dark .divide-slate-100 > * + *, html.dark .divide-slate-200 > * + * { border-color: var(--carworx-row-line) !important; }
html.dark .text-ink, html.dark .text-slate-900, html.dark .text-slate-800, html.dark .text-slate-700 { color: var(--carworx-ink) !important; }
html.dark .text-ink-soft, html.dark .text-slate-600, html.dark .text-slate-500, html.dark .text-slate-400 { color: var(--carworx-ink-soft) !important; }
html.dark .bg-gradient-to-br.from-slate-50.to-slate-100,
html.dark .bg-gradient-to-br.from-white.to-slate-50,
html.dark .bg-gradient-to-br.from-white.to-red-50 {
  background-image: linear-gradient(to bottom right, var(--carworx-card), var(--carworx-card-soft)) !important;
}
/* Pill backgrounds in dark mode are slightly darker and the text adjusts for contrast */
html.dark .pill-standing { background: #1F2937; color: #CBD5E1; }
html.dark .pill-dispatched { background: #1E3A8A; color: #BFDBFE; }
html.dark .pill-picked_up { background: #422006; color: #FCD34D; }
html.dark .pill-delivered { background: #064E3B; color: #6EE7B7; }
html.dark .pill-cancelled { background: #1F1F1F; color: #94A3B8; }
html.dark .pill-paid { background: #064E3B; color: #6EE7B7; }
html.dark .pill-sent { background: #064E3B; color: #6EE7B7; }
html.dark .pill-unpaid { background: #4A0E0E; color: #FCA5A5; }
html.dark .pill-partial { background: #422006; color: #FCD34D; }
/* The BOL print preview must stay light/black on screen so the on-screen draft
   matches what comes out of the printer. */
html.dark .bol-page { background: #fff !important; color: #000 !important; }

/* BOL print styling — used both on screen and when printing */
.bol-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 0.5rem; }
.bol-page {
  width: 8.5in; min-height: 11in; padding: 0.55in 0.55in 0.45in;
  background: #fff; color: #000; margin: 0 auto;
  font: 10.5pt/1.35 "Helvetica Neue", Arial, sans-serif;
}
@media (max-width: 900px) { .bol-page { box-shadow: 0 2px 12px rgba(15,23,42,0.08); } }
.bol-divider {
  display: flex; align-items: center; gap: 0.5rem;
  font-weight: 700; color: #1d4ed8; letter-spacing: 0.04em;
  font-size: 9pt; margin: 0.45rem 0;
}
.bol-divider::before, .bol-divider::after {
  content: ""; flex: 1; border-top: 1px solid #93c5fd;
}
.bol-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.bol-label { color: #6B7280; font-style: italic; font-size: 9pt; }
.bol-section-title { font-weight: 700; font-size: 10pt; letter-spacing: 0.03em; }
.bol-damage-codes { display: grid; grid-template-columns: repeat(6, 1fr); gap: 0.15rem 0.5rem; font-size: 8.5pt; }
.bol-damage-codes b { font-weight: 700; }
.signature-line {
  border-top: 1px solid #000; margin-top: 2.4rem; padding-top: 0.25rem;
  font-size: 9pt; color: #374151;
}
@media print {
  @page { size: Letter; margin: 0.4in; }
  body { background: #fff !important; }
  .no-print { display: none !important; }
  .bol-page { width: auto; min-height: auto; padding: 0; box-shadow: none; }
}

.donut-row-hot { background: var(--carworx-card-soft); }

/* Mobile refinements */
@media (max-width: 420px) {
  .donut-wrap, .donut-ring-empty, .donut-ring-clear { width: 118px; height: 118px; }
  .donut-ring-empty, .donut-ring-clear { border-width: 10px; }
}
@media (max-width: 767px) {
  .donut-legend-row { min-height: 44px; }
}
.pill-tap { padding: 0.45rem 0.75rem; }
@media (min-width: 768px) { .pill-tap { padding: 0.2rem 0.6rem; } }
