/*
 * Powershop tools: the one stylesheet for all screens.
 *
 * Design brief: docs/design.md. Calm and functional: neutral greys, generous
 * spacing, one accent colour (brand yellow from the logo).
 *
 * 1. Design tokens (variables): change colours, sizes and spacing HERE only.
 * 2. Base elements
 * 3. Layout (shell, page)
 * 4. Components (one block per template in templates/components/)
 * 5. Print
 *
 * Pages never add their own styles (CLAUDE.md rule 11). If a page needs
 * something new, it becomes a component here and on the style guide page.
 */

/* ---------------------------------------------------------------------------
 * 1. Design tokens
 * ------------------------------------------------------------------------ */

:root {
  /* Brand (from docs/brand/logo_psnl.ai) */
  --brand-yellow: #fecb2c;
  --brand-black: #000000;

  /* Type */
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --text-2xl: 1.75rem;

  /* Spacing scale */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;

  /* Shapes and sizes */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --touch-target: 2.75rem; /* 44 px: minimum height of anything you tap */
  --sidebar-width: 15.5rem;
  --env-banner-height: 0px; /* 1.75rem on staging, see .has-env-banner */
  --page-narrow: 48rem;
  --page-wide: 100rem;
}

/* Light theme: the default, and for data-theme="light" */
:root,
[data-theme="light"] {
  color-scheme: light;
  --color-background: #f6f6f5;
  --color-surface: #ffffff;
  --color-surface-sunken: #efefee;
  --color-border: #e1e1df;
  --color-border-strong: #c4c4c0;
  --color-text: #1a1a19;
  --color-text-muted: #5e5e5a;
  --color-accent: var(--brand-yellow);
  --color-accent-hover: #f2bb12;
  --color-on-accent: #1a1a19;
  --color-focus: #1a1a19;
  --color-shadow: rgb(0 0 0 / 0.06);

  --color-info-bg: #eef4fc;
  --color-info-border: #b9d1f0;
  --color-info-text: #1d4f91;
  --color-success-bg: #edf7f0;
  --color-success-border: #b5dcc1;
  --color-success-text: #1d6b37;
  --color-warning-bg: #fdf4e6;
  --color-warning-border: #f0cf9b;
  --color-warning-text: #8a4b06;
  --color-danger-bg: #fcefee;
  --color-danger-border: #f0bcb8;
  --color-danger-text: #a52a21;
  --color-danger-solid: #b8322a;
}

/* Dark theme. Defined twice on purpose: once for an explicit choice,
 * once for "Auto" when the device is set to dark. Keep both blocks equal. */
[data-theme="dark"] {
  color-scheme: dark;
  --color-background: #121212;
  --color-surface: #1c1c1b;
  --color-surface-sunken: #171716;
  --color-border: #2f2f2d;
  --color-border-strong: #474744;
  --color-text: #ededeb;
  --color-text-muted: #a3a39e;
  --color-accent: var(--brand-yellow);
  --color-accent-hover: #ffd755;
  --color-on-accent: #121212;
  --color-focus: var(--brand-yellow);
  --color-shadow: rgb(0 0 0 / 0.4);

  --color-info-bg: #15233a;
  --color-info-border: #2b4a78;
  --color-info-text: #a9c8f5;
  --color-success-bg: #142a1c;
  --color-success-border: #285a38;
  --color-success-text: #9fd8b0;
  --color-warning-bg: #2e2210;
  --color-warning-border: #6b4c17;
  --color-warning-text: #f3c67f;
  --color-danger-bg: #331816;
  --color-danger-border: #6e2a25;
  --color-danger-text: #f3aaa4;
  --color-danger-solid: #c9443a;
}

@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    color-scheme: dark;
    --color-background: #121212;
    --color-surface: #1c1c1b;
    --color-surface-sunken: #171716;
    --color-border: #2f2f2d;
    --color-border-strong: #474744;
    --color-text: #ededeb;
    --color-text-muted: #a3a39e;
    --color-accent: var(--brand-yellow);
    --color-accent-hover: #ffd755;
    --color-on-accent: #121212;
    --color-focus: var(--brand-yellow);
    --color-shadow: rgb(0 0 0 / 0.4);

    --color-info-bg: #15233a;
    --color-info-border: #2b4a78;
    --color-info-text: #a9c8f5;
    --color-success-bg: #142a1c;
    --color-success-border: #285a38;
    --color-success-text: #9fd8b0;
    --color-warning-bg: #2e2210;
    --color-warning-border: #6b4c17;
    --color-warning-text: #f3c67f;
    --color-danger-bg: #331816;
    --color-danger-border: #6e2a25;
    --color-danger-text: #f3aaa4;
    --color-danger-solid: #c9443a;
  }
}

/* ---------------------------------------------------------------------------
 * 2. Base elements
 * ------------------------------------------------------------------------ */

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

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

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-background);
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  line-height: 1.25;
  letter-spacing: -0.01em;
}

a {
  color: inherit;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
}

a:hover {
  text-decoration-thickness: 2px;
}

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

img,
svg {
  max-width: 100%;
}

:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

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

/* A value that is not known yet (CLAUDE.md rule 5): never shown as 0. */
.value-unknown {
  color: var(--color-text-muted);
  font-style: italic;
}

.skip-link {
  position: absolute;
  left: -999rem;
  top: var(--space-2);
  z-index: 100;
  padding: var(--space-2) var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
}

.skip-link:focus {
  left: var(--space-2);
}

/* ---------------------------------------------------------------------------
 * 3. Layout
 * ------------------------------------------------------------------------ */

/* Phone and tablet: a bar with logo and Menu button; the menu opens below it. */
.shell__bar {
  position: sticky;
  top: var(--env-banner-height);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 3.5rem;
  padding: var(--space-2) var(--space-4);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.shell__sidebar {
  display: none;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.shell__sidebar.is-open {
  display: flex;
}

.shell__sidebar .shell__sidebar-brand {
  display: none; /* the bar above already shows the logo */
}

/* Desktop: fixed sidebar on the left. */
@media (min-width: 64rem) {
  .shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    min-height: calc(100vh - var(--env-banner-height));
  }

  .shell__bar {
    display: none;
  }

  .shell__sidebar,
  .shell__sidebar.is-open {
    display: flex;
    position: sticky;
    top: var(--env-banner-height);
    height: calc(100vh - var(--env-banner-height));
    overflow-y: auto;
    border-bottom: 0;
    border-right: 1px solid var(--color-border);
  }

  .shell__sidebar .shell__sidebar-brand {
    display: inline-flex;
    padding: var(--space-2) var(--space-3) var(--space-3);
  }
}

.page {
  width: 100%;
  padding: var(--space-5) var(--space-4) var(--space-7);
}

.page--narrow {
  max-width: calc(var(--page-narrow) + 2 * var(--space-6));
}

.page--wide {
  max-width: calc(var(--page-wide) + 2 * var(--space-6));
}

@media (min-width: 64rem) {
  .page {
    padding: var(--space-6) var(--space-6) var(--space-7);
  }
}

/* Login and other pages without the menu. */
.page--centered {
  max-width: 26rem;
  margin-inline: auto;
  padding-top: max(var(--space-6), 10vh);
}

.section {
  margin-block: var(--space-6);
}

.section__title {
  margin: 0 0 var(--space-3);
  font-size: var(--text-lg);
  font-weight: 600;
}

/* ---------------------------------------------------------------------------
 * 4. Components
 * ------------------------------------------------------------------------ */

/* Brand: logo + product name (components/brand.html) */
.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text);
  text-decoration: none;
}

.brand__logo {
  display: block;
  height: 2.25rem;
  width: auto;
}

.brand__product {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.brand--standalone {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin-bottom: var(--space-5);
}

.brand--standalone .brand__logo {
  height: 4rem;
}

/* Navigation (components/nav.html, components/user_menu.html) */
.nav__list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.nav__heading {
  margin: var(--space-4) var(--space-3) var(--space-1);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.nav__link {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: var(--touch-target);
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-text);
  font: inherit;
  font-size: 0.9375rem;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.nav__link:hover {
  background: var(--color-surface-sunken);
}

.nav__link[aria-current="page"] {
  background: var(--color-surface-sunken);
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--color-accent);
}

.user-menu {
  margin-top: auto;
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.user-menu__name {
  margin: 0 var(--space-3) var(--space-3);
  font-size: var(--text-sm);
  font-weight: 600;
}

/* Segmented control, e.g. the theme switch (components/theme_switch.html) */
.segmented {
  margin: 0 var(--space-3) var(--space-3);
}

.segmented__label {
  display: block;
  margin-bottom: var(--space-1);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.segmented__options {
  display: flex;
  padding: 2px;
  background: var(--color-surface-sunken);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.segmented__option {
  flex: 1;
  min-height: 2.5rem;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-text-muted);
  font: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
}

.segmented__option[aria-pressed="true"] {
  background: var(--color-surface);
  color: var(--color-text);
  font-weight: 600;
  box-shadow: 0 1px 2px var(--color-shadow);
}

/* Page header (components/page_header.html) */
.page-header {
  margin-bottom: var(--space-5);
}

.page-header__title {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: 650;
}

.page-header__intro {
  max-width: 65ch;
  margin: var(--space-2) 0 0;
  color: var(--color-text-muted);
}

@media (min-width: 64rem) {
  .page-header__title {
    font-size: var(--text-2xl);
  }
}

/* Buttons */
.button-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
  padding: 0 var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.btn:hover {
  background: var(--color-surface-sunken);
}

.btn--primary {
  border-color: var(--color-accent);
  background: var(--color-accent);
  color: var(--color-on-accent);
}

.btn--primary:hover {
  border-color: var(--color-accent-hover);
  background: var(--color-accent-hover);
}

.btn--ghost {
  border-color: transparent;
  background: none;
}

.btn--danger {
  border-color: var(--color-danger-solid);
  background: var(--color-danger-solid);
  color: #ffffff;
}

.btn--danger:hover {
  border-color: var(--color-danger-solid);
  background: var(--color-danger-solid);
  filter: brightness(0.92);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn--block {
  width: 100%;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-surface-sunken);
  color: var(--color-text);
  font-size: var(--text-xs);
  font-weight: 600;
  white-space: nowrap;
}

.badge--info {
  border-color: var(--color-info-border);
  background: var(--color-info-bg);
  color: var(--color-info-text);
}

.badge--success {
  border-color: var(--color-success-border);
  background: var(--color-success-bg);
  color: var(--color-success-text);
}

.badge--warning {
  border-color: var(--color-warning-border);
  background: var(--color-warning-bg);
  color: var(--color-warning-text);
}

.badge--danger {
  border-color: var(--color-danger-border);
  background: var(--color-danger-bg);
  color: var(--color-danger-text);
}

.badge--accent {
  border-color: var(--color-accent);
  background: var(--color-accent);
  color: var(--color-on-accent);
}

/* Messages (components/messages.html) */
.messages {
  display: grid;
  gap: var(--space-2);
  margin: 0 0 var(--space-5);
  padding: 0;
  list-style: none;
}

.message {
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-info-border);
  border-left-width: 4px;
  border-radius: var(--radius-md);
  background: var(--color-info-bg);
  color: var(--color-info-text);
}

.message--success {
  border-color: var(--color-success-border);
  background: var(--color-success-bg);
  color: var(--color-success-text);
}

.message--warning {
  border-color: var(--color-warning-border);
  background: var(--color-warning-bg);
  color: var(--color-warning-text);
}

.message--error {
  border-color: var(--color-danger-border);
  background: var(--color-danger-bg);
  color: var(--color-danger-text);
}

/* Cards (components/tool_card.html) */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}

.card--padded {
  padding: var(--space-5);
}

.card__heading {
  margin: 0 0 var(--space-4);
  font-size: var(--text-xl);
}

.card--link {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-height: 5.5rem;
  padding: var(--space-4) var(--space-5);
  color: var(--color-text);
  text-decoration: none;
}

.card--link:hover {
  border-color: var(--color-border-strong);
  box-shadow: 0 2px 8px var(--color-shadow);
}

.card__title {
  font-weight: 600;
}

.card__text {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: var(--space-3);
}

/* Empty state (components/empty_state.html) */
.empty-state {
  padding: var(--space-7) var(--space-5);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  text-align: center;
}

.empty-state__title {
  margin: 0;
  font-weight: 600;
}

.empty-state__text {
  max-width: 45ch;
  margin: var(--space-2) auto 0;
  color: var(--color-text-muted);
}

/* Forms: styles Django's standard form output ({{ form.as_div }}). */
.form > div + div {
  margin-top: var(--space-4);
}

.form label {
  display: block;
  margin-bottom: var(--space-1);
  font-size: 0.9375rem;
  font-weight: 600;
}

.form input:not([type="checkbox"], [type="radio"], [type="hidden"]),
.form select,
.form textarea {
  display: block;
  width: 100%;
  min-height: var(--touch-target);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-size: var(--text-base); /* 16 px or more: phones do not zoom in */
}

.form select {
  appearance: none;
  padding-right: var(--space-6);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%238a8a85' stroke-width='2'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
}

.form textarea {
  min-height: 6rem;
  resize: vertical;
}

.form input[aria-invalid="true"],
.form select[aria-invalid="true"],
.form textarea[aria-invalid="true"] {
  border-color: var(--color-danger-solid);
}

/* Checkbox: box first, label next to it, whole row tappable. */
.form > div:has(> input[type="checkbox"]) {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
}

.form > div:has(> input[type="checkbox"]) > input[type="checkbox"] {
  order: -1;
  width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  accent-color: var(--color-accent);
}

.form > div:has(> input[type="checkbox"]) > label {
  margin: 0;
  font-weight: 400;
}

.form .helptext {
  margin-bottom: var(--space-1);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.form .errorlist {
  margin: 0 0 var(--space-1);
  padding: 0;
  list-style: none;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-danger-text);
}

.form__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-5);
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9375rem;
}

.table th {
  padding: var(--space-2) var(--space-4);
  background: var(--color-surface-sunken);
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}

.table td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
}

.table tbody tr:last-child td {
  border-bottom: 0;
}

.table tbody tr:hover td {
  background: var(--color-surface-sunken);
}

/* Numbers: right-aligned, digits of equal width so columns line up. */
.table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Text blocks with headings and paragraphs. */
.prose > :first-child {
  margin-top: 0;
}

.prose > :last-child {
  margin-bottom: 0;
}

.prose h1 {
  font-size: var(--text-2xl);
}

.prose h2 {
  font-size: var(--text-xl);
}

.prose h3 {
  font-size: var(--text-lg);
}

/* Colour swatches (style guide only) */
.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
  gap: var(--space-3);
}

.swatch {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding-bottom: var(--space-3);
  overflow: hidden;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
}

.swatch::before {
  content: "";
  height: 3.5rem;
  border-bottom: 1px solid var(--color-border);
}

.swatch > * {
  padding-inline: var(--space-3);
}

.swatch > span {
  margin-top: var(--space-2);
  font-weight: 600;
}

.swatch > code {
  color: var(--color-text-muted);
}

.swatch--accent::before {
  background: var(--color-accent);
}

.swatch--text::before {
  background: var(--color-text);
}

.swatch--muted::before {
  background: var(--color-text-muted);
}

.swatch--surface::before {
  background: var(--color-surface);
}

.swatch--background::before {
  background: var(--color-background);
}

.swatch--border::before {
  background: var(--color-border-strong);
}

/* Staging banner (components/environment_banner.html) */
/* The bar stays at the top; the layout below is exactly this much shorter,
 * so the page does not scroll because of it. */
.has-env-banner {
  --env-banner-height: 1.75rem;
}

.env-banner {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--env-banner-height);
  padding: 0 var(--space-4);
  overflow: hidden;
  line-height: var(--env-banner-height);
  white-space: nowrap;
  text-overflow: ellipsis;
  background: repeating-linear-gradient(
    -45deg,
    var(--brand-yellow) 0 0.75rem,
    #f3bd14 0.75rem 1.5rem
  );
  color: #1a1a19;
  font-size: var(--text-sm);
  text-align: center;
}

/* ---------------------------------------------------------------------------
 * 5. Print (browser "Print"; PDFs use static/css/print.css)
 * ------------------------------------------------------------------------ */

@media print {
  :root,
  [data-theme] {
    color-scheme: light;
    --color-background: #ffffff;
    --color-surface: #ffffff;
    --color-surface-sunken: #f2f2f2;
    --color-text: #000000;
    --color-text-muted: #444444;
    --color-border: #bbbbbb;
  }

  .shell {
    display: block;
  }

  .shell__bar,
  .shell__sidebar,
  .skip-link,
  .btn,
  .messages {
    display: none !important;
  }

  .page {
    max-width: none;
    padding: 0;
  }

  .card,
  .table-wrap {
    box-shadow: none;
    break-inside: avoid;
  }
}
