/*
 * Margit base stylesheet — hand-written design system (replaces Pico CSS).
 * Clean & modern SaaS look: neutral grays, one accent, subtle shadows,
 * generous whitespace. Everything is token-driven so dark mode only remaps
 * the variables in :root. App-specific components live in application.css.
 */

/* ---------- Design tokens ---------- */
:root {
  color-scheme: light dark;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", "Liberation Sans", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Menlo", "Consolas",
          "Liberation Mono", monospace;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --text: #1b2230;
  --muted: #687385;
  --border: #e6e8ec;
  --border-strong: #d3d7df;

  /* Monochrome slate primary for buttons; a muted blue-gray accent for links,
   * focus rings and outline buttons. */
  --primary: #1b2230;
  --primary-hover: #2d3850;
  --primary-text: #ffffff;
  --accent: #3a5a8f;
  --accent-hover: #2c4674;
  --accent-soft: #eef1f7;
  --danger: #dc2626;
  --danger-soft: #fdeded;
  --ring: rgba(58, 90, 143, 0.32);

  --radius: 10px;
  --radius-sm: 7px;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.10);
  --shadow-md: 0 6px 20px rgba(16, 24, 40, 0.10);

  --container: 72rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --surface: #171a21;
    --surface-2: #1f242d;
    --text: #e7e9ee;
    --muted: #9aa3b2;
    --border: #2a2f3a;
    --border-strong: #3a4150;

    --primary: #e7e9ee;
    --primary-hover: #ffffff;
    --primary-text: #171a21;
    --accent: #8fa9d4;
    --accent-hover: #abc0e3;
    --accent-soft: #1c2435;
    --danger: #f87171;
    --danger-soft: #2a1b1b;
    --ring: rgba(143, 169, 212, 0.36);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.30), 0 1px 3px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.45);
  }
}

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

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

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

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

/* ---------- Layout container ---------- */
body > main,
body > footer {
  width: 100%;
  max-width: var(--container);
  margin-inline: auto;
  padding-inline: 1.5rem;
}

body > main { padding-block: 0.5rem 3rem; }

body > footer {
  margin-top: 2rem;
  padding-block: 1.5rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
}

/* ---------- Top bar (full-width with centered inner content) ---------- */
body > header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.75rem;
}

body > header > nav,
body > header > p {
  max-width: var(--container);
  margin-inline: auto;
  padding-inline: 1.5rem;
}

body > header > nav { padding-block: 0.7rem; justify-content: space-between; }
body > header > p { padding-block: 0 0.7rem; margin: 0; }

body > header strong a {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}

/* ---------- Navigation ---------- */
nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 1.25rem;
}

nav ul {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 1.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

nav a {
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
}
nav a:hover { color: var(--text); }

/* ---------- Typography ---------- */
h1, h2, h3, h4, h5, h6 { line-height: 1.25; font-weight: 600; }
h1 { font-size: 1.9rem; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 0.5rem; }
h2 { font-size: 1.4rem; margin: 2rem 0 0.75rem; }
h3 { font-size: 1.12rem; margin: 1.25rem 0 0.5rem; }
h4 { font-size: 1rem; margin: 1rem 0 0.5rem; }

p { margin-block: 0.75rem; }

/* :where() keeps anchors at zero specificity so a[role="button"] link-buttons
 * pick up the button text colour (below) instead of the link colour. */
:where(a) { color: var(--accent); text-decoration: none; }
:where(a):hover { text-decoration: underline; }

strong { font-weight: 600; }
small { font-size: 0.8rem; color: var(--muted); }

mark {
  background: var(--accent-soft);
  color: var(--accent-hover);
  padding: 0.45rem 0.8rem;
  border-radius: var(--radius-sm);
  display: inline-block;
  font-weight: 500;
}

ins {
  text-decoration: none;
  background: var(--danger-soft);
  color: var(--danger);
  padding: 0.45rem 0.8rem;
  border-radius: var(--radius-sm);
  display: inline-block;
  font-weight: 500;
}

hgroup { margin: 1rem 0 1.5rem; }
hgroup > * { margin: 0; }
hgroup > p { color: var(--muted); margin-top: 0.4rem; }

ul, ol { margin-block: 0.75rem; padding-left: 1.4rem; }
li { margin-block: 0.2rem; }

dl { margin-block: 0.75rem; }
dt { font-weight: 600; }
dd { margin: 0 0 0.5rem; color: var(--muted); }

hr { border: 0; border-top: 1px solid var(--border); margin-block: 1.75rem; }

blockquote {
  margin: 1rem 0;
  padding: 0.25rem 0 0.25rem 1rem;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}

figure { margin: 1rem 0; overflow-x: auto; }

/* ---------- Code ---------- */
code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--surface-2);
  padding: 0.1rem 0.35rem;
  border-radius: 5px;
}

pre {
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.5;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  overflow: auto;
}
pre code { background: none; padding: 0; font-size: inherit; }

/* ---------- Sections & cards ---------- */
section { margin-block: 1.5rem; }

article {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  margin-block: 1rem;
  box-shadow: var(--shadow-sm);
}

article > header {
  margin: 0 0 0.75rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
article > *:last-child { margin-bottom: 0; }

/* ---------- Tables ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-block: 1rem;
  font-size: 0.925rem;
}

th, td {
  text-align: left;
  padding: 0.6rem 0.85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

thead th {
  background: var(--surface-2);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
}

tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }

/* ---------- Forms ---------- */
label {
  display: block;
  margin-block: 0.85rem;
  font-weight: 500;
  color: var(--text);
}

label > input:not([type="checkbox"]):not([type="radio"]),
label > select,
label > textarea {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  font-weight: 400;
}

input, select, textarea, button { font: inherit; }
/* color only for fields — buttons get their colour from the button rules below
 * (otherwise this would override the primary button's text colour). */
input:not([type="submit"]):not([type="reset"]):not([type="button"]),
select,
textarea { color: var(--text); }

input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]),
select,
textarea {
  width: 100%;
  max-width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.7rem;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

textarea { min-height: 4rem; resize: vertical; line-height: 1.5; }

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

input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.8; }

input[type="file"] {
  padding: 0.45rem 0.5rem;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

input[type="checkbox"], input[type="radio"] {
  width: auto;
  margin-right: 0.45rem;
  accent-color: var(--accent);
  vertical-align: middle;
}

input:disabled, select:disabled, textarea:disabled {
  background: var(--surface-2);
  color: var(--muted);
  cursor: not-allowed;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 1.1rem 1.1rem;
  margin-block: 1.25rem;
}

legend {
  padding: 0 0.4rem;
  font-weight: 600;
  color: var(--text);
}

/* Inline input group (Pico role="group") */
[role="group"] {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  border: 0;
  padding: 0;
}
[role="group"] > * { margin: 0; width: auto; flex: 0 1 auto; }
[role="group"] > input { flex: 1 1 8rem; }

/* Responsive two-column layout (Pico .grid) */
.grid {
  display: grid;
  gap: 1rem 1.25rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr));
}
.grid > * { margin-block: 0; }

/* ---------- Buttons & link-buttons ---------- */
/* Base styles are wrapped in :where() so they carry ZERO specificity — the
 * .secondary / .outline / .contrast variant classes below always win, even
 * over the a[role="button"] selector. */
:where(button,
       [type="submit"], [type="button"], [type="reset"],
       a[role="button"]) {
  display: inline-block;
  cursor: pointer;
  font-weight: 500;
  line-height: 1.25;
  padding: 0.55rem 1rem;
  border: 1px solid var(--primary);
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--primary-text);
  text-decoration: none;
  text-align: center;
  transition: background 0.12s ease, border-color 0.12s ease,
              box-shadow 0.12s ease, color 0.12s ease;
}

:where(button,
       [type="submit"], [type="button"], [type="reset"],
       a[role="button"]):hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
  text-decoration: none;
}

:where(button,
       [type="submit"], [type="button"], [type="reset"],
       a[role="button"]):focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--ring);
}

:where(button,
       [type="submit"], [type="button"], [type="reset"],
       a[role="button"]):disabled,
:where([disabled][role="button"], a[role="button"][disabled]) {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Muted / neutral */
.secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}
.secondary:hover { background: var(--surface-2); border-color: var(--border-strong); }

/* Ghost / outline */
.outline {
  background: transparent;
  border-color: var(--accent);
  color: var(--accent);
}
.outline:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-hover); }

/* Muted ghost (Pico's "outline secondary") */
.outline.secondary, .secondary.outline {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--muted);
}
.outline.secondary:hover, .secondary.outline:hover {
  background: var(--surface-2);
  color: var(--text);
}

/* Destructive */
.contrast {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.contrast:hover { background: var(--danger); border-color: var(--danger); filter: brightness(0.93); }

/* Rails button_to wraps the button in a <form> — keep them inline */
form.button_to { display: inline-block; margin: 0 0.4rem 0.4rem 0; }

a[role="button"], [type="submit"] { margin: 0.25rem 0.4rem 0.25rem 0; }

/* ---------- Disclosure ---------- */
details {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.95rem;
  margin-block: 1rem;
}
summary { cursor: pointer; font-weight: 500; }
summary:hover { color: var(--accent); }
details[open] > summary { margin-bottom: 0.75rem; }

/* ---------- Loading state (Pico aria-busy) ---------- */
@keyframes margit-spin { to { transform: rotate(360deg); } }

[aria-busy="true"]::before {
  content: "";
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  margin-right: 0.5em;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  vertical-align: -0.1em;
  animation: margit-spin 0.7s linear infinite;
}

/* ---------- Misc ---------- */
::selection { background: var(--accent-soft); }
