/* Base element styles, forms, buttons, tables, flash messages, and inline
 * field errors. Colors come exclusively from tokens.css. */

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

  body {
    margin: 0;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    font-size: var(--text-sm);
    line-height: 1.5;
  }

  h1 { font-size: var(--text-lg); margin: 0 0 var(--space-5); }
  h2 { font-size: var(--text-md); margin: var(--space-6) 0 var(--space-4); }

  p { margin: 0 0 var(--space-4); }

  a {
    color: var(--color-accent);
  }
  a:hover { color: var(--color-accent-hover); }

  hr {
    border: 0;
    border-top: 1px solid var(--color-border);
    margin: var(--space-6) 0;
  }

  small, .muted { color: var(--color-text-muted); font-size: var(--text-xs); }

  code {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: var(--text-xs);
    background: var(--color-code-bg);
    border-radius: var(--radius-sm);
    padding: var(--space-1) var(--space-2);
  }

  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

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

  /* Forms: a .field groups label, control, hint and error; submit buttons
   * sit outside it in .form-actions. */
  label { font-weight: 550; }

  .field {
    display: grid;
    gap: var(--space-1);
    max-width: 26rem;
    margin-bottom: var(--space-4);
  }
  .field--inline {
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: var(--space-2);
  }
  .hint { color: var(--color-text-muted); font-size: var(--text-xs); }
  .form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
  }

  input:where([type="text"], [type="password"], [type="email"], [type="file"]) {
    display: block;
    width: 100%;
    max-width: 26rem;
    padding: var(--space-2) var(--space-3);
    font: inherit;
    color: var(--color-text);
    background: var(--color-surface);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
  }
  input:where([type="text"], [type="password"], [type="email"], [type="file"]):hover { border-color: var(--color-text-muted); }
  input:where([type="text"], [type="password"], [type="email"]):focus-visible {
    outline: var(--focus-ring);
    outline-offset: 0;
    border-color: var(--color-accent);
  }
  input:disabled { background: var(--color-bg); color: var(--color-text-muted); cursor: not-allowed; }

  /* Rails wraps invalid fields in div.field_with_errors — neutralize the
   * layout impact and mark the field itself. */
  .field_with_errors { display: contents; }
  .field_with_errors input { border-color: var(--color-danger); }
  .field-error {
    display: block;
    color: var(--color-danger);
    font-size: var(--text-xs);
  }

  /* Buttons are the .btn component in components.css (via ui_button);
   * this only resets the native box so stray buttons inherit type. */
  button, input[type="submit"] { font: inherit; }

  /* Tables: above 40em they keep their columns and scroll inside
   * .table-wrap (the page never scrolls horizontally); below it each row
   * becomes a card and every cell shows its column header from data-label. */
  .table-wrap {
    overflow-x: auto;
    margin-bottom: var(--space-5);
  }
  table {
    width: 100%;
    min-width: 40rem;
    border-collapse: collapse;
  }
  th, td {
    text-align: left;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--color-border);
    vertical-align: middle;
  }
  th { font-size: var(--text-xs); color: var(--color-text-muted); font-weight: 550; }
  .nowrap { white-space: nowrap; }
  .truncate {
    max-width: 18rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  td.actions { text-align: right; white-space: nowrap; }
  td.actions > * + * { margin-left: var(--space-2); }

  @media (max-width: 39.99em) {
    table { min-width: 0; display: block; }
    thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
    tbody, tr { display: block; }
    tr {
      background: var(--color-surface);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-md);
      padding: var(--space-2) var(--space-4);
      margin-bottom: var(--space-3);
    }
    td {
      display: grid;
      grid-template-columns: 6rem minmax(0, 1fr);
      gap: var(--space-3);
      align-items: baseline;
      padding: var(--space-2) 0;
      border: 0;
    }
    td::before {
      content: attr(data-label);
      font-size: var(--text-xs);
      font-weight: 550;
      color: var(--color-text-muted);
    }
    td.actions {
      display: flex;
      flex-wrap: wrap;
      gap: var(--space-2);
      justify-content: flex-start;
      white-space: normal;
      margin-top: var(--space-1);
      padding-top: var(--space-3);
      border-top: 1px solid var(--color-border);
    }
    td > .badge { justify-self: start; }
    td.actions::before { content: none; }
    td.actions > * + * { margin-left: 0; }
    .nowrap { white-space: normal; }
    .truncate { max-width: none; white-space: normal; overflow-wrap: anywhere; }
  }

  /* Flash messages */
  .flash {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border);
    margin-bottom: var(--space-5);
  }
  .flash__dismiss { color: inherit; font-size: var(--text-md); line-height: 1; }
  .flash--notice { background: var(--color-success-bg); color: var(--color-success); border-color: currentColor; }
  .flash--alert { background: var(--color-danger-bg); color: var(--color-danger); border-color: currentColor; }
}
