/*
 * HxCore element base (M5 F1), ported from ../hxcore/admin/src/design/base.css
 * (DS-1, DS3-6). Plain semantic HTML gets the "Papier/Graphit & Signal" look:
 * body and headings, links (underlined in the content area, WCAG 1.4.1), the
 * focus ring, scrollbars, button/input/select/textarea/label, fieldset/legend,
 * table/th/td, dl, p[role="alert"] and dialog. The Tailwind preflight parts it
 * relied on are inlined at the top. Layer order: see hxcore.css.
 */
@layer base {
  /* M5: the parts of Tailwind's preflight the element base relied on. */
  *,
  ::before,
  ::after {
    box-sizing: border-box;
    border: 0 solid;
    margin: 0;
    padding: 0;
  }

  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    line-height: 1.5;
    font-family: var(--font-sans);
  }

  code,
  kbd,
  samp,
  pre {
    font-family: var(--font-mono);
  }

  table {
    border-collapse: collapse;
    text-indent: 0;
    border-color: inherit;
  }

  button,
  input,
  select,
  optgroup,
  textarea {
    font: inherit;
    font-feature-settings: inherit;
    letter-spacing: inherit;
    color: inherit;
  }

  img,
  video,
  canvas {
    display: block;
    max-width: 100%;
  }

  [hidden]:where(:not([hidden='until-found'])) {
    display: none !important;
  }

  html,
  body {
    background: var(--bg);
    color: var(--text);
  }

  body {
    min-height: 100vh;
    font: var(--text-body);
    font-feature-settings: 'ss01' 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
  }

  /* ---------- Typography ---------- */

  h1,
  h2,
  h3,
  h4 {
    margin: 0 0 var(--space-3);
    font-family: var(--font-display);
    color: var(--text);
    letter-spacing: var(--tracking-heading);
    line-height: 1.2;
  }

  h1 {
    font-size: var(--size-h1);
    font-weight: 700; /* = --text-h1 (F16) */
  }

  h2 {
    font-size: var(--size-h2);
    font-weight: 700;
    margin-top: var(--space-6);
  }

  h3 {
    font-size: var(--size-h3);
    font-weight: 700;
    margin-top: var(--space-4);
  }

  h4 {
    font-size: var(--size-body);
    font-weight: 700;
  }

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

  a {
    color: var(--text);
    text-decoration: none;
    border-radius: 3px;
    transition: color var(--transition-fast);
  }

  a:hover {
    color: var(--text-strong);
  }

  /* Every link in page content is underlined (WCAG 1.4.1: not by color
     alone). Shell links (unlayered classes) and Hx component links
     (`no-underline` utility, DS3-2) opt out there. */
  :is(.hx-content, .hx-auth__card, .hx-auth-split__panel) a {
    color: var(--link); /* HexaMain: cyan = clickable */
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, var(--link) 45%, transparent);
    text-underline-offset: 3px;
  }

  :is(.hx-content, .hx-auth__card, .hx-auth-split__panel) a:hover {
    text-decoration-color: currentColor;
  }

  /* Font family from preflight (--default-mono-font-family = --font-mono);
     the body's font-feature-settings stay inherited (preflight resets them). */
  code,
  kbd,
  samp,
  pre {
    font-size: 0.92em;
    font-feature-settings: inherit;
  }

  kbd {
    display: inline-block;
    padding: 1px 6px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    font-size: var(--size-label);
    color: var(--text-subtle);
  }

  b,
  strong {
    font-weight: 600;
  }

  /* .sr-only is Tailwind's utility since DS3-1 (same visually-hidden rule). */

  /* ---------- Focus ---------- */

  :focus {
    outline: none;
  }

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

  input:focus-visible,
  select:focus-visible,
  textarea:focus-visible {
    outline: none;
    border-color: var(--focus-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-color) 25%, transparent);
  }

  /* ---------- Scrollbars (subtle) ---------- */

  * {
    scrollbar-width: thin;
    scrollbar-color: var(--border-stronger) transparent;
  }

  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  ::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 10px;
    border: 2px solid var(--bg);
  }

  ::-webkit-scrollbar-track {
    background: transparent;
  }

  ::selection {
    background: color-mix(in srgb, var(--accent) 55%, transparent);
    color: var(--text-strong);
  }

  /* ---------- Buttons ---------- */

  button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--control-height-sm);
    padding: 0 var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text);
    font-size: var(--size-control);
    font-weight: 500;
    line-height: 1.2;
    cursor: pointer;
    transition:
      background-color var(--transition-fast),
      border-color var(--transition-fast),
      color var(--transition-fast);
  }

  button:hover:not(:disabled) {
    background: var(--surface-hover);
    border-color: var(--border-stronger);
  }

  button:disabled {
    cursor: not-allowed;
    opacity: 0.5;
  }

  button[type='submit'] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
    font-weight: 600;
  }

  button[type='submit']:hover:not(:disabled) {
    background: color-mix(in srgb, var(--accent) 88%, var(--text-strong));
    border-color: transparent;
  }

  /* ---------- Form controls ---------- */

  input,
  select,
  textarea {
    font-size: var(--size-body);
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    transition:
      border-color var(--transition-fast),
      box-shadow var(--transition-fast);
  }

  input:not([type='checkbox'], [type='radio'], [type='range'], [type='file'], [type='color']),
  select {
    min-height: 34px;
    padding: 6px 10px;
  }

  select[multiple],
  select[size] {
    padding: 4px;
  }

  /* Preflight strips the file-input button to bare text; keep the UA button
     (DS3-1 review: Firmenprofil logo upload, Artikel-Medien). */
  ::file-selector-button {
    margin: revert;
    padding: revert;
    border: revert;
    border-radius: revert;
    background-color: revert;
    font: revert;
    font-feature-settings: revert;
    font-variation-settings: revert;
    letter-spacing: revert;
    color: revert;
  }

  /* Preflight zeroes every padding; options keep the UA inset (DS3-1). */
  option {
    padding: revert;
  }

  input[type='number'],
  input[inputmode='decimal'],
  input[inputmode='numeric'] {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
  }

  input[type='checkbox'],
  input[type='radio'] {
    width: 16px;
    height: 16px;
    margin: 0;
    accent-color: var(--accent-text);
  }

  input[type='file'] {
    padding: 6px;
    font-size: var(--size-small);
  }

  input::placeholder,
  textarea::placeholder {
    color: var(--text-subtle);
  }

  input:hover:not(:disabled, :focus),
  select:hover:not(:disabled, :focus),
  textarea:hover:not(:disabled, :focus) {
    border-color: var(--border-stronger);
  }

  input:disabled,
  select:disabled,
  textarea:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }

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

  textarea {
    padding: 10px;
    min-height: 72px;
  }

  select:not([multiple], [size]) {
    padding-right: 30px;
    appearance: none;
    background-image:
      linear-gradient(45deg, transparent 50%, var(--text-subtle) 50%),
      linear-gradient(135deg, var(--text-subtle) 50%, transparent 50%);
    background-position:
      calc(100% - 15px) 55%,
      calc(100% - 10px) 55%;
    background-size:
      5px 5px,
      5px 5px;
    background-repeat: no-repeat;
  }

  label {
    color: var(--text-muted);
    font-size: var(--size-control);
  }

  /* <label>Text <input/></label> pattern used by all existing forms. */
  form label:has(> input:not([type='checkbox'], [type='radio'])),
  form label:has(> select),
  form label:has(> textarea) {
    display: inline-flex;
    flex-direction: column;
    gap: 6px;
    margin: 0 var(--space-3) var(--space-3) 0;
    vertical-align: top;
  }

  label:has(> input[type='checkbox']),
  label:has(> input[type='radio']) {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    margin-right: var(--space-3);
    color: var(--text);
  }

  fieldset {
    margin: 0 0 var(--space-4);
    padding: var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
  }

  legend {
    padding: 0 var(--space-2);
    font-size: var(--size-label);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--text-subtle);
  }

  /* ---------- Tables ---------- */

  /* Table element defaults: hairline rows and uppercase heads. The card, the
     scroll container and the cell padding come from HxDataTable / HxTable,
     the only tables (designLint rule 3). */
  table {
    width: 100%;
    margin: 0 0 var(--space-6);
    font-size: var(--size-body);
    font-variant-numeric: tabular-nums;
  }

  /* DS2-8 (Ruling D-4): no `display: block` on tables — it dropped the table
     semantics in Safari/VoiceOver. */

  th {
    padding: 10px 12px;
    text-align: left;
    font-size: var(--size-label);
    font-weight: 500;
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--text-subtle);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
  }

  td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-subtle);
    vertical-align: middle;
    color: var(--text);
  }

  tbody tr:last-child > td,
  tfoot tr:last-child > td {
    border-bottom: 0;
  }

  tbody tr {
    transition: background-color var(--transition-fast);
  }

  tbody tr:hover > :is(td, th) {
    background: var(--surface-hover);
  }

  tfoot td {
    background: var(--surface-2);
    border-top: 1px solid var(--border);
    font-family: var(--font-mono);
  }

  td input:not([type='checkbox'], [type='radio']),
  td select {
    min-height: 30px;
    padding: 4px 8px;
  }

  /* ---------- Description lists ---------- */

  dl {
    display: grid;
    grid-template-columns: minmax(140px, max-content) 1fr;
    gap: var(--space-2) var(--space-5);
    margin: 0 0 var(--space-5);
  }

  dt {
    color: var(--text-subtle);
    font-size: var(--size-control);
  }

  dd {
    margin: 0;
    color: var(--text);
  }

  /* Preflight strips list bullets and the block margin; plain lists keep
     the UA look (DS3-1). Components set their own (HxStack as="ul"). */
  ul,
  ol {
    padding-left: 1.25rem;
    margin-block: revert;
    list-style: revert;
  }

  /* ---------- Feedback ---------- */

  /* The views use <p role="alert"> for request/validation errors. Inline
     <span role="alert"> is also used for confirmation questions, so only
     the paragraph form is styled as an error. */
  p[role='alert'] {
    padding: var(--space-2) var(--space-3);
    background: var(--danger-bg);
    color: var(--danger);
    font-size: var(--size-control);
    border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
    border-radius: var(--radius-md);
  }

  /* DS2-9 (I-1): no base rule for [role='alertdialog'] — the only consumer is
     VersionConflictDialog, where the role sits on HxDialog's <dialog>; a
     margin here beat the UA `dialog:modal { margin: auto }` and pinned the
     409 dialog top-left. Guarded by baseCss.test.ts. */

  /* ---------- Dialog ---------- */

  dialog {
    /* Preflight's `* { margin: 0 }` would beat the UA `margin: auto` that
       centers modal dialogs (DS3-1). */
    margin: auto;
    padding: 0;
    max-width: min(640px, calc(100vw - 32px));
    max-height: calc(100vh - 64px);
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
  }

  dialog::backdrop {
    background: var(--scrim);
    backdrop-filter: blur(2px);
  }

  img,
  svg {
    vertical-align: middle;
  }

  .hx-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
  }

  /* Read-only users (no *.write): forms stay visible but cannot be used. */
  [inert] {
    opacity: 0.7;
  }

  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      transition-duration: 0.01ms !important;
      animation-duration: 0.01ms !important;
    }
  }
}
