/*
 * Camelizer web UI stylesheet (feature 031).
 * Design tokens transcribed from shadcn/ui's Zinc-neutral palette (research R1/R2).
 * Light token set on :root; dark token set under prefers-color-scheme: dark.
 * This file defines tokens and base element rules only; component styles live in later tasks.
 */

:root {
  --page: #FAFAFA;
  --surface: #FFFFFF;
  --muted: #F4F4F5;
  --foreground: #09090B;
  --muted-foreground: #71717A;
  --border: #E4E4E7;
  --primary: #18181B;
  --primary-foreground: #FAFAFA;
  --radius: 6px;
  --danger: #B3261E;
  --danger-foreground: #FFFFFF;
  --danger-bg: #FDECEA;
  --success: #1E7D4B;
  --success-foreground: #FFFFFF;
  --success-bg: #E7F4ED;
  --warning: #8A5C00;
  --warning-foreground: #FFFFFF;
  --warning-bg: #FBF1DC;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #09090B;
    --surface: #18181B;
    --muted: #27272A;
    --foreground: #FAFAFA;
    --muted-foreground: #A1A1AA;
    --border: #27272A;
    --primary: #FAFAFA;
    --primary-foreground: #18181B;
    /* --radius unchanged across schemes */
    --danger: #F2554C;
    --danger-foreground: #18181B;
    --danger-bg: #3B1512;
    --success: #4ADE80;
    --success-foreground: #18181B;
    --success-bg: #0F2C1E;
    --warning: #E0B341;
    --warning-foreground: #18181B;
    --warning-bg: #2E2410;
  }
}

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

body {
  margin: 0;
  background-color: var(--page);
  color: var(--foreground);
  line-height: 1.5;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
}

a {
  color: var(--primary);
}

/*
 * Component layer (feature 031, T004).
 * Plain CSS classes and bare-element rules rebuilding the shadcn/ui look from the tokens above.
 * No new colours: every value derives from the tokens in :root and the dark @media block.
 */

/* --- Shell: header bar and centred content column --- */
.app-header {
  display: flex;
  align-items: center;
  padding: 0.75rem 1.5rem;
  background-color: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header strong {
  font-size: 1.125rem;
  font-weight: 600;
}

/* Header navigation for signed-in users (layout.html, NavigationAdvice). */
.app-nav {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 1rem;
  margin-left: 1.5rem;
}

.app-nav ul {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.app-nav a {
  color: var(--muted-foreground);
}

.app-nav a[aria-current="page"] {
  color: var(--foreground);
  font-weight: 600;
}

.app-signout {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0 0 0 auto;
}

.app-user {
  color: var(--muted-foreground);
  font-size: 0.875rem;
}

.app-signout button {
  padding: 0.25rem 0.75rem;
  background-color: transparent;
  color: var(--foreground);
  border: 1px solid var(--border);
}

.container {
  max-width: 48rem;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

/* --- Typography --- */
h1 {
  font-size: 1.75rem;
  font-weight: 600;
  line-height: 1.2;
  margin: 0 0 1rem;
}

h2 {
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.3;
  margin: 2rem 0 1rem;
}

a {
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* --- Buttons: bare <button> is the primary action; .button.secondary for later use --- */
button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  font: inherit;
  font-weight: 500;
  line-height: 1.25;
  color: var(--primary-foreground);
  background-color: var(--primary);
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
}

button:hover,
.button:hover {
  opacity: 0.9;
  text-decoration: none;
}

button:focus-visible,
.button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.button.secondary {
  color: var(--foreground);
  background-color: var(--surface);
  border-color: var(--border);
}

.button.secondary:hover {
  background-color: var(--muted);
  opacity: 1;
}

/* --- Form controls --- */
label {
  display: inline-block;
  margin-bottom: 0.375rem;
  font-weight: 500;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="file"],
select,
textarea {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  font: inherit;
  color: var(--foreground);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

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

input[type="text"]:focus-visible,
input[type="password"]:focus-visible,
input[type="email"]:focus-visible,
input[type="file"]:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
  border-color: var(--primary);
}

/* Checkbox rows keep the control next to its label rather than block-stacked. */
input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  margin: 0 0.5rem 0 0;
  vertical-align: middle;
  accent-color: var(--primary);
}

input[type="checkbox"] + label {
  display: inline;
  margin-bottom: 0;
  font-weight: 400;
}

/* The upload/login forms wrap each field in a <p>; use it as the field row. */
form p {
  margin: 0 0 1.25rem;
}

/* --- Tables (job list) --- */
table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

thead {
  background-color: var(--muted);
}

th,
td {
  padding: 0.625rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  font-weight: 600;
  color: var(--muted-foreground);
}

tbody tr:last-child td {
  border-bottom: none;
}

/* --- Definition lists (settings / job details) --- */
dl {
  display: grid;
  grid-template-columns: minmax(10rem, max-content) 1fr;
  gap: 0.5rem 1.5rem;
  margin: 0;
}

dt {
  font-weight: 500;
  color: var(--muted-foreground);
}

dd {
  margin: 0;
}

/* --- Card: surface panel (login form, detail panels) --- */
.card {
  padding: 1.5rem;
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* --- Badge: inline status pill, with job-state variants --- */
.badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.4;
  color: var(--muted-foreground);
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 9999px;
}

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

.badge.running {
  color: var(--primary-foreground);
  background-color: var(--primary);
  border-color: var(--primary);
}

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

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

/* --- Alert / error text (role="alert" or .error) --- */
[role="alert"],
.error {
  color: var(--danger);
  font-weight: 500;
}

/* An alert carrying text renders as a bordered danger panel; empty ones stay invisible. */
[role="alert"]:not(:empty) {
  display: block;
  padding: 0.625rem 0.75rem;
  margin: 0.375rem 0;
  background-color: var(--danger-bg);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
}

/* A usage/allowance rejection (FR-023) uses the warning accent, distinct from a content error. */
.limit-error {
  color: var(--warning);
  background-color: var(--warning-bg);
  border-color: var(--warning);
}

/* Access requests without outbound mail (feature 048): a page-wide notice and the one-time link. */
.mail-notice {
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--warning);
  border-radius: var(--radius);
}

/* One click selects the whole link for copying; long tokens wrap instead of widening the table. */
.activation-link {
  display: block;
  padding: 0.5rem 0.75rem;
  overflow-wrap: anywhere;
  user-select: all;
  -webkit-user-select: all;
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/*
 * Wizard constitution step (feature 046, contracts/wizard-constitution.md §6). Tokens-only.
 * The stale notice takes .limit-error's warning accent as a whole panel, since it carries no
 * role="alert" to borrow the panel from.
 */
.constitution-governs {
  margin: -0.5rem 0 1rem;
  color: var(--muted-foreground);
}

.constitution-note {
  padding: 0.625rem 0.75rem;
  color: var(--muted-foreground);
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* What stays fixed beside an aspect's wording: read as part of the section, not as a choice. */
.constitution-fixed-part {
  padding-left: 0.75rem;
  font-size: 0.875rem;
  color: var(--muted-foreground);
  border-left: 3px solid var(--border);
}

.constitution-stale {
  padding: 0.625rem 0.75rem;
  color: var(--warning);
  font-weight: 500;
  background-color: var(--warning-bg);
  border: 1px solid var(--warning);
  border-radius: var(--radius);
}

/* The delivered-default mark sits inside the option's label as a small pill. */
.constitution-default {
  display: inline-block;
  margin-left: 0.5rem;
  padding: 0 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--success);
  background-color: var(--success-bg);
  border: 1px solid var(--success);
  border-radius: 9999px;
}

/* The consequence of an option, indented under its radio and label. */
.constitution-consequence {
  display: block;
  margin-left: 1.5rem;
  font-size: 0.875rem;
  color: var(--muted-foreground);
}

/* The project's own wording, indented under the custom option like its scope sentence. */
.constitution-custom-text {
  width: calc(100% - 1.5rem);
  margin: 0.375rem 0 0 1.5rem;
}

/*
 * The read-only list of fixed sections: one column, each reason under its heading, on the muted
 * panel of .constitution-note so it reads as information rather than as a choice.
 */
.constitution-fixed {
  grid-template-columns: 1fr;
  gap: 0.25rem;
  padding: 0.625rem 0.75rem;
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.constitution-fixed dt {
  color: var(--foreground);
}

.constitution-fixed dd {
  margin: 0 0 0.5rem 1.5rem;
  font-size: 0.875rem;
  color: var(--muted-foreground);
}

.constitution-fixed dd:last-child {
  margin-bottom: 0;
}

/*
 * Wizard review of the constitution profile (contracts/wizard-constitution.md §7). The deviation
 * mark is a warning pill whose words carry the meaning; colour only repeats them.
 */
.constitution-deviation {
  display: inline-block;
  margin-left: 0.5rem;
  padding: 0 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--warning);
  background-color: var(--warning-bg);
  border: 1px solid var(--warning);
  border-radius: 9999px;
}

/* Custom wording spans both columns beneath its row, in full, wrapped and with its line breaks. */
.constitution-custom-wording {
  grid-column: 1 / -1;
}

.constitution-custom-wording pre {
  margin: 0;
  padding: 0.625rem 0.75rem;
  font-family: inherit;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/*
 * Per-option help affordance and modal (feature 031, T016).
 * Three parts: (1) the inline info control .opt-help > summary, (2) the no-JS
 * disclosure panel .opt-help-body, (3) the JS-enhanced native <dialog>.
 * Tokens-only; the single hard-coded colour is the translucent modal backdrop.
 */

/* --- (1) Info control: a small inline, muted, clickable info glyph --- */
.opt-help {
  display: inline;
}

.opt-help > summary {
  display: inline-flex;
  align-items: center;
  margin-left: 0.375rem;
  color: var(--muted-foreground);
  cursor: pointer;
  border-radius: var(--radius);
  list-style: none;
}

.opt-help > summary::-webkit-details-marker {
  display: none;
}

.opt-help > summary:hover,
.opt-help > summary:focus-visible {
  color: var(--foreground);
}

.opt-help > summary:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.opt-help > summary svg {
  width: 1.1em;
  height: 1.1em;
  vertical-align: middle;
}

/* --- (2) No-JS disclosure panel: a small muted/surface explanation card --- */
.opt-help-body {
  margin: 0.5rem 0 0;
  padding: 0.75rem 1rem;
  color: var(--muted-foreground);
  background-color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.opt-help-body p {
  margin: 0 0 0.5rem;
}

.opt-help-body p:last-child {
  margin-bottom: 0;
}

.opt-help-body strong {
  color: var(--foreground);
  font-weight: 600;
}

/* --- (3) Enhanced modal: the native <dialog> built by option-help.js --- */
.opt-help-dialog {
  width: min(32rem, 92vw);
  max-width: min(32rem, 92vw);
  padding: 1.5rem;
  color: var(--foreground);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.opt-help-dialog::backdrop {
  background-color: rgba(0, 0, 0, 0.5);
}

.opt-help-dialog-title {
  margin: 0 0 0.75rem;
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.3;
}

.opt-help-dialog-body {
  color: var(--muted-foreground);
}

.opt-help-dialog-body p {
  margin: 0 0 0.5rem;
}

.opt-help-dialog-body p:last-child {
  margin-bottom: 0;
}

.opt-help-dialog-body strong {
  color: var(--foreground);
  font-weight: 600;
}

/* The close button floats to the top-right; styled as a quiet secondary action. */
.opt-help-dialog-close {
  float: right;
  margin: -0.5rem -0.5rem 0 1rem;
  padding: 0.25rem 0.625rem;
  color: var(--foreground);
  background-color: var(--surface);
  border-color: var(--border);
}

.opt-help-dialog-close:hover {
  background-color: var(--muted);
  opacity: 1;
}

/*
 * Landing page (feature 047, contracts/landing-page.md §3; research R3, R4).
 * The intro video is a full-viewport scene behind the page; its final frame stays behind it as a
 * still image. One sign-in panel sits in front. landing.html renders the panel with .is-waiting, which
 * hides it only where landing.js will run (scripting enabled, motion allowed); landing.js removes it
 * and the panel's transition fades it in (.is-instant: at once). Tokens-only, except the scene colour.
 */

/* [hidden] must always win over the landing display rules that follow. */
[hidden] {
  display: none !important;
}

/* The scene's night sky, sampled from the final frame: a scene colour, not a UI token. */
.landing {
  --landing-scene: #113B46;
}

/* On the landing page only: the header stays above the scene, the column is uncapped. */
body:has(.landing) .app-header {
  position: relative;
  z-index: 2;
}

.container:has(.landing) {
  max-width: none;
}

.intro {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: url("/the-camelizer-final.webp") center / cover no-repeat;
}

/* Until the first frame arrives the visitor sees the empty sky, never the ending. */
#intro-video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-color: var(--landing-scene);
}

/* The video never plays without JavaScript or in an outcome state; the final image shows. */
@media (scripting: none) {
  #intro-video {
    display: none;
  }
}

.landing:has([data-reveal-now]) #intro-video {
  display: none;
}

#skip-intro {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  z-index: 2;
}

.landing-panel {
  position: relative;
  z-index: 1;
  max-width: 24rem;
  margin: 0 auto;
  padding: 1.5rem;
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: opacity 1s ease-out;
}

/* In @supports rather than as a second declaration: with var() inside, an unsupported color-mix()
   fails at computed-value time and would leave the panel transparent instead of falling back. */
@supports (background-color: color-mix(in srgb, red 50%, transparent)) {
  .landing-panel {
    background-color: color-mix(in srgb, var(--surface) 90%, transparent);
    backdrop-filter: blur(8px);
  }
}

.landing-panel.is-instant {
  transition: none;
}

.landing-panel h2 {
  margin-top: 0;
}

.landing-panel button[type="submit"] {
  width: 100%;
}

/* "No account yet?" drawn as a rule with the text in its middle. Full foreground colour: the muted
   one falls below 4.5:1 on the translucent panel. */
.landing-divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.5rem 0;
  font-size: 0.875rem;
  color: var(--foreground);
}

.landing-divider::before,
.landing-divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--border);
}

/* The panel is centred horizontally at every width (its auto margins). Wide screens: it is also
   vertically centred in the space below the header (header ~3.25rem plus the container's 4rem padding). */
@media (min-width: 80rem) {
  .landing {
    display: flex;
    align-items: center;
    min-height: calc(100vh - 8rem);
  }

  .landing-panel {
    width: 24rem;
  }
}

/*
 * The waiting state, only where landing.js will run. A transition takes the after-change style, so
 * entering it is instant and only the reveal fades; visibility keeps the forms out of the tab order
 * and the accessibility tree. Failsafe: if the script never runs, 30 s after this stylesheet first
 * applies a still-waiting panel shows and the video and Skip hide (the final image shows). The
 * script's own deadline (25 s after the page start) always comes first.
 */
@media (scripting: enabled) and (prefers-reduced-motion: no-preference) {
  .landing-panel.is-waiting {
    opacity: 0;
    visibility: hidden;
    transition: none;
    animation: landing-failsafe-show 0s 30s forwards;
  }

  .landing:has(.landing-panel.is-waiting) #intro-video,
  .landing:has(.landing-panel.is-waiting) #skip-intro {
    animation: landing-failsafe-hide 0s 30s forwards;
  }
}

@keyframes landing-failsafe-show {
  to {
    opacity: 1;
    visibility: visible;
  }
}

@keyframes landing-failsafe-hide {
  to {
    visibility: hidden;
  }
}

@media (prefers-reduced-motion: reduce) {
  #intro-video {
    display: none;
  }

  .landing-panel {
    transition: none;
    animation: none;
  }
}

/*
 * Responsive rules (feature 031, T018).
 * Keep every page free of horizontal scroll down to ~360 px: fluid container,
 * wrapping header, a self-scrolling job table and coverage block, and a dialog
 * that stays within the viewport. Layout only; no colour tokens change here.
 */

/* The coverage report can hold long unbroken lines; keep it inside its own box
   on every width so it never widens the page. */
#coverage-report {
  max-width: 100%;
  overflow-x: auto;
}

/* The modal must never exceed a narrow viewport and must keep its close control
   reachable; cap its height and let long bodies scroll inside the dialog. */
.opt-help-dialog {
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
}

@media (max-width: 40rem) {
  /* Comfortable ~16px side gutter; the centred column already caps width. */
  .container {
    padding: 1.5rem 1rem;
  }

  /* Let the brand wrap instead of overflowing on very narrow screens. */
  .app-header {
    flex-wrap: wrap;
    padding: 0.75rem 1rem;
  }

  .app-nav {
    flex: 0 0 100%;
    flex-wrap: wrap;
    margin: 0.5rem 0 0;
  }

  /* The 5-column job table scrolls within its own box rather than widening the
     page. display:block makes overflow-x apply to the table element itself. */
  table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
  }

  /* Keep the dialog clear of the viewport edges on small screens. */
  .opt-help-dialog {
    max-height: calc(100vh - 1rem);
  }

  #skip-intro {
    right: 1rem;
    bottom: 1rem;
  }
}
