/* ============================================================================
   Theme + design tokens (single source of truth)
   ----------------------------------------------------------------------------
   Light is the default. Dark is opted into via <html data-theme="dark">, set
   before first paint by the inline script in app.html and kept in sync by
   $lib/theme.ts. Every colour the app uses resolves from a token here, so a
   surface that reads from tokens themes automatically in both modes.

   Scene that forced the dark palette: an operations lead reconciling pasta
   production costs late in a dim back-office, wanting the glare off a white
   screen without losing the soft-green brand identity.
   ============================================================================ */

:root {
  color-scheme: light;

  /* ── Brand: green-forward (Weavy). Emerald carries primary actions,
        active content states, charts, and positive deltas. ──────── */
  --color-brand:        oklch(0.56 0.125 162);  /* primary green button   */
  --color-brand-hover:  oklch(0.49 0.115 162);
  --color-brand-tint:   oklch(0.95 0.04 162);
  --color-on-brand:     oklch(0.99 0.012 162);
  --color-secondary:    oklch(0.45 0.01 240);   /* neutral secondary      */

  /* ── Accent (brighter emerald for data / positive emphasis) ── */
  --color-accent:       oklch(0.65 0.15 162);
  --color-accent-hover: oklch(0.56 0.13 162);
  --color-accent-tint:  oklch(0.95 0.05 162);

  /* ── Content surfaces: neutral light-gray canvas, white cards ── */
  --color-bg-app:       oklch(0.966 0.002 240);
  --color-bg-surface:   oklch(0.998 0.001 240);
  --color-bg-elevated:  oklch(0.99 0.0015 240);
  --color-surface-hover:    oklch(0.955 0.004 240);
  --color-surface-selected: color-mix(in srgb, var(--color-brand) 10%, var(--color-bg-surface));

  /* ── Form inputs: a touch recessed from the card surface ── */
  --color-input-bg: var(--panel-soft);

  /* ── Borders ────────────────────────────────────────────── */
  --color-border:       oklch(0.92 0.005 240);
  --color-divider:      oklch(0.94 0.004 240);

  /* ── Text (neutral) ─────────────────────────────────────── */
  --color-text-primary:   oklch(0.25 0.006 240);
  --color-text-secondary: oklch(0.45 0.008 240);
  --color-text-muted:     oklch(0.6 0.01 240);

  /* ── Sidebar: deep-green rail matching the customer ordering portal
        (CustomerPortalShell). The current item shows as a white pill with
        green text. Held constant across light/dark themes so internal staff
        and customers see the same navigation styling. ── */
  --sidebar-bg:          #1f3a2c;
  --sidebar-hover:       rgba(255, 255, 255, 0.08);
  --sidebar-active-bg:   #ffffff;
  --sidebar-active-text: #1f3a2c;
  --sidebar-border:      rgba(255, 255, 255, 0.12);
  --sidebar-text:        rgba(231, 239, 233, 0.85);
  --sidebar-text-strong: #ffffff;
  --sidebar-text-muted:  rgba(231, 239, 233, 0.6);
  --sidebar-icon:        rgba(231, 239, 233, 0.8);
  --sidebar-logo-bg:     rgba(255, 255, 255, 0.1);

  /* ── Semantic ───────────────────────────────────────────── */
  --color-success:      oklch(0.66 0.16 162);  /* emerald, cohesive with accent */
  --color-warning:      oklch(0.66 0.12 78);
  --color-error:        oklch(0.58 0.2 25);
  --color-info:         oklch(0.55 0.13 245);

  --color-success-text: oklch(0.52 0.14 162);
  --color-success-tint: oklch(0.95 0.05 164);
  --color-warning-text: oklch(0.45 0.11 69);
  --color-warning-tint: oklch(0.96 0.045 78);
  --color-info-tint:    oklch(0.965 0.025 245);

  /* ── Brand-literal deep green: kept for legacy success / positive
        text and tints. Solid brand chips point at --color-brand. ── */
  --green-deep:  oklch(0.42 0.13 150);

  /* ── Radii / spacing (theme-independent) ────────────────── */
  --radius-panel:   0.9rem;
  --radius-control: 0.68rem;
  --radius-row:     0.78rem;
  --space-page:     1.25rem;
  --space-card:     1.15rem;
  --shadow:         none; /* flat by design: separate with borders, not shadows */

  /* ── Legacy aliases (resolve through the tokens above, so they
        re-theme automatically when the tokens are overridden).
        --green maps to the accent so existing green usages stay green. ── */
  --bg:          var(--color-bg-app);
  --panel:       var(--color-bg-surface);
  --panel-soft:  var(--color-bg-app);
  --line:        var(--color-border);
  --line-strong: var(--color-border);
  --text:        var(--color-text-primary);
  --muted:       var(--color-text-muted);
  --green:       var(--color-accent);
  --green-soft:  var(--color-success-tint);
  --blue-soft:   var(--color-info-tint);
}

:root[data-theme='dark'] {
  color-scheme: dark;

  /* ── Brand: green-forward, brightened for dark ─────────────── */
  --color-brand:        oklch(0.62 0.13 162);
  --color-brand-hover:  oklch(0.7 0.13 162);
  --color-brand-tint:   oklch(0.32 0.06 162);
  --color-on-brand:     oklch(0.99 0.012 162);
  --color-secondary:    oklch(0.72 0.01 240);

  /* ── Accent (emerald) ───────────────────────────────────── */
  --color-accent:       oklch(0.72 0.15 162);
  --color-accent-hover: oklch(0.8 0.14 162);
  --color-accent-tint:  oklch(0.33 0.06 162);

  /* ── Surfaces (neutral dark) ────────────────────────────── */
  --color-bg-app:       oklch(0.17 0.004 240);
  --color-bg-surface:   oklch(0.215 0.005 240);
  --color-bg-elevated:  oklch(0.26 0.006 240);
  --color-surface-hover:    oklch(0.27 0.006 240);
  --color-surface-selected: color-mix(in srgb, var(--color-brand) 14%, var(--color-bg-surface));

  /* ── Form inputs: sit just above the card so fields don't read as
        black holes punched into the surface. ── */
  --color-input-bg:     oklch(0.25 0.005 240);

  /* ── Borders ────────────────────────────────────────────── */
  --color-border:       oklch(0.32 0.006 240);
  --color-divider:      oklch(0.28 0.005 240);

  /* ── Sidebar: in dark mode the rail drops the deep-green and joins the
        neutral dark surfaces, sitting a touch below the app canvas so it
        reads as a distinct rail. The active item becomes a green-tinted pill
        with bright brand text rather than the light-mode white chip. ── */
  --sidebar-bg:          oklch(0.2 0.005 240);
  --sidebar-hover:       oklch(0.27 0.006 240);
  --sidebar-active-bg:   color-mix(in srgb, var(--color-brand) 22%, var(--color-bg-surface));
  --sidebar-active-text: oklch(0.9 0.07 162);
  --sidebar-border:      oklch(0.3 0.006 240);
  --sidebar-text:        oklch(0.78 0.006 240);
  --sidebar-text-strong: oklch(0.96 0.003 240);
  --sidebar-text-muted:  oklch(0.6 0.008 240);
  --sidebar-icon:        oklch(0.72 0.006 240);
  --sidebar-logo-bg:     rgba(255, 255, 255, 0.08);

  /* ── Text (neutral) ─────────────────────────────────────── */
  --color-text-primary:   oklch(0.96 0.003 240);
  --color-text-secondary: oklch(0.78 0.006 240);
  --color-text-muted:     oklch(0.62 0.008 240);

  /* ── Semantic ───────────────────────────────────────────── */
  --color-success:      oklch(0.75 0.16 162);
  --color-warning:      oklch(0.8 0.12 80);
  --color-error:        oklch(0.7 0.18 25);
  --color-info:         oklch(0.72 0.12 240);

  --color-success-text: oklch(0.84 0.14 162);
  --color-success-tint: oklch(0.32 0.06 162);
  --color-warning-text: oklch(0.85 0.1 80);
  --color-warning-tint: oklch(0.32 0.05 78);
  --color-info-tint:    oklch(0.3 0.05 240);

  --green-deep:  oklch(0.7 0.15 150);
}

/* ============================================================================
   Base
   ============================================================================ */

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--color-bg-app);
  color: var(--color-text-primary);
  font-family: 'Inter', 'Segoe UI', sans-serif;
  font-size: 14px;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: 'Inter', 'Segoe UI', sans-serif;
  letter-spacing: -0.03em;
}

button,
input,
select,
textarea {
  font: inherit;
}

a {
  color: inherit;
  text-decoration: none;
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--color-brand) 42%, transparent);
  outline-offset: 2px;
}

/* ============================================================================
   Shared product surfaces (used across every route, so they live here rather
   than in any one shell component)
   ============================================================================ */

.ui-stack {
  display: grid;
  gap: var(--space-page);
}

.ui-panel,
.ui-metric-card {
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-panel);
  box-shadow: var(--shadow);
}

.ui-panel {
  padding: var(--space-card);
}

.ui-panel-soft {
  background: var(--panel-soft);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-row);
}

.ui-section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.85rem;
  margin-bottom: 1rem;
}

.ui-section-heading h3,
.ui-section-heading h4 {
  margin: 0.18rem 0 0;
  font-size: 1.06rem;
  font-weight: 700;
  letter-spacing: 0;
}

.ui-eyebrow {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.ui-muted {
  color: var(--color-text-muted);
}

.ui-metric-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
}

.ui-metric-card {
  padding: 1.05rem 1.1rem;
}

.ui-metric-card span {
  display: block;
  color: var(--color-text-muted);
  font-size: 0.84rem;
}

.ui-metric-card strong {
  display: block;
  margin: 0.5rem 0 0.28rem;
  font-size: 1.75rem;
  font-weight: 700;
}

.ui-metric-card p {
  margin: 0;
  color: var(--color-text-muted);
}

.ui-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.6rem;
  padding: 0.72rem 0.9rem;
  border-radius: var(--radius-control);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    border-color 160ms cubic-bezier(0.22, 1, 0.36, 1), color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.ui-button.primary {
  border: 1px solid var(--color-brand);
  color: var(--color-on-brand);
  background: var(--color-brand);
}

.ui-button.primary:hover:not(:disabled) {
  background: var(--color-brand-hover);
  border-color: var(--color-brand-hover);
}

.ui-button.secondary {
  border: 1px solid var(--color-border);
  color: var(--color-text-primary);
  background: var(--color-bg-surface);
}

.ui-button.secondary:hover:not(:disabled) {
  background: var(--color-surface-hover);
}

.ui-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.ui-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: fit-content;
  border-radius: 999px;
  padding: 0.4rem 0.74rem;
  font-size: 0.82rem;
  font-weight: 600;
  text-transform: capitalize;
  white-space: nowrap;
}

.ui-pill.positive {
  color: var(--color-success-text);
  background: var(--color-success-tint);
}

.ui-pill.warning {
  color: var(--color-warning-text);
  background: var(--color-warning-tint);
}

.ui-pill.neutral {
  color: var(--color-text-secondary);
  background: color-mix(in srgb, var(--panel-soft) 74%, var(--color-bg-surface));
}

.ui-table-wrap {
  overflow-x: auto;
}

.ui-table {
  width: 100%;
  min-width: 48rem;
  border-collapse: separate;
  border-spacing: 0 0.65rem;
}

.ui-table th,
.ui-table td {
  padding: 0.9rem 0.95rem;
  text-align: left;
  white-space: nowrap;
}

.ui-table th {
  color: var(--color-text-muted);
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.ui-table tbody td {
  background: var(--panel-soft);
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
}

.ui-table tbody td:first-child {
  border-left: 1px solid var(--color-border);
  border-radius: var(--radius-row) 0 0 var(--radius-row);
}

.ui-table tbody td:last-child {
  border-right: 1px solid var(--color-border);
  border-radius: 0 var(--radius-row) var(--radius-row) 0;
}

.ui-table-identity {
  display: flex;
  align-items: center;
  gap: 0.74rem;
  min-width: 0;
}

.ui-row-mark {
  width: 2.25rem;
  height: 2.25rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 0.76rem;
  color: var(--color-on-brand);
  background: var(--color-brand);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.ui-table-identity strong,
.ui-number-block strong {
  display: block;
  font-size: 0.94rem;
}

.ui-table-identity span,
.ui-number-block span {
  display: block;
  margin-top: 0.16rem;
  color: var(--color-text-muted);
  font-size: 0.8rem;
}

.ui-number-block {
  display: grid;
  gap: 0.08rem;
}

.ui-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
}

.ui-form-grid.compact {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.ui-field {
  display: grid;
  gap: 0.36rem;
  color: var(--color-text-secondary);
  font-size: 0.88rem;
  font-weight: 600;
}

.ui-field input,
.ui-field textarea,
.ui-field select {
  width: 100%;
  padding: 0.82rem 0.9rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-control);
  background: var(--color-input-bg);
  color: var(--color-text-primary);
  transition: background-color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    border-color 160ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.ui-field input:focus,
.ui-field textarea:focus,
.ui-field select:focus {
  outline: none;
  border-color: var(--color-brand);
  background: var(--color-bg-surface);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-brand) 20%, transparent);
}

@media (max-width: 980px) {
  .ui-metric-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 760px) {
  .ui-section-heading {
    flex-direction: column;
    align-items: flex-start;
  }

  .ui-table {
    min-width: 0;
    border-spacing: 0;
  }

  .ui-table,
  .ui-table thead,
  .ui-table tbody,
  .ui-table tr,
  .ui-table td {
    display: block;
    width: 100%;
  }

  .ui-table thead {
    display: none;
  }

  .ui-table tbody {
    display: grid;
    gap: 0.85rem;
  }

  .ui-table tbody tr {
    padding: 0.3rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-row);
    background: var(--panel-soft);
  }

  .ui-table tbody td {
    padding: 0.76rem 0.8rem;
    white-space: normal;
    border: none;
    border-radius: 0;
    background: transparent;
  }

  .ui-table tbody td:first-child,
  .ui-table tbody td:last-child {
    border: none;
    border-radius: 0;
  }

  .ui-table tbody td + td {
    border-top: 1px solid var(--color-border);
  }

  .ui-table tbody td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 0.35rem;
    color: var(--color-text-muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .ui-form-grid,
  .ui-form-grid.compact {
    grid-template-columns: 1fr;
  }
}

/* ============================================================================
   Tooltip (rendered to <body> by the use:tooltip action)
   ----------------------------------------------------------------------------
   Auto-inverts via the text/surface tokens: a near-black bubble in light mode,
   a near-white bubble in dark mode — readable contrast in both.
   ============================================================================ */

.app-tooltip {
  position: fixed;
  z-index: 1000;
  padding: 0.36rem 0.55rem;
  border-radius: 0.5rem;
  background: var(--color-text-primary);
  color: var(--color-bg-surface);
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: 0.005em;
  white-space: normal;
  overflow-wrap: break-word;
  text-align: center;
  width: max-content;
  max-width: min(16rem, calc(100vw - 1rem));
  pointer-events: none;
  box-shadow: 0 6px 20px -6px rgba(0, 0, 0, 0.35), 0 2px 6px -2px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 130ms ease, transform 130ms ease;
}

.app-tooltip[data-placement='top'] {
  transform: translateY(2px);
}

.app-tooltip.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .app-tooltip {
    transition: opacity 130ms ease;
    transform: none;
  }
}
