/* Shared parts for every screen (docs/design.md chapter 5). The layer order is declared in tokens.css. */
@layer components {
  :focus-visible {
    outline: 2px solid var(--color-focus-inner);
    outline-offset: 0;
    box-shadow: 0 0 0 6px var(--color-focus-outer);
  }

  .screen {
    box-sizing: content-box;
    max-width: var(--content-width);
    margin-inline: auto;
    padding: var(--sp-5) var(--page-gutter) var(--sp-7);

    &[data-width="wide"] {
      max-width: var(--content-wide);
    }
  }

  .screen-back {
    margin-block-end: var(--sp-1);
  }

  .screen-header {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    justify-content: space-between;
    gap: var(--sp-3) var(--sp-4);
    margin-block-end: var(--sp-5);
  }

  .screen-header-main {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    min-width: 0;
  }

  .screen-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--sp-2);
    margin: 0;
    color: var(--color-text-muted);
    font-size: var(--fs-sm);
  }

  .screen-header-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--sp-3);

    &:empty {
      display: none;
    }
  }

  .screen-title {
    margin: 0;
    font-size: var(--fs-xl);
    font-weight: 700;
    line-height: var(--lh-dense);
  }

  .section {
    margin-block-end: var(--sp-5);
  }

  /* Single-level surface panel shared across screens (design 5.3: never nest panels inside panels). */
  .panel {
    padding: var(--sp-5);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
  }

  .panel-emphasis {
    padding: var(--sp-5);
    border: 1px solid var(--color-border);
    border-inline-start: 4px solid var(--color-primary);
    border-radius: var(--radius-md);
    background: var(--color-surface);
  }

  .section-heading {
    margin: 0 0 var(--sp-3);
    font-size: var(--fs-lg);
    font-weight: 700;
    line-height: var(--lh-dense);
  }

  .section-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-2) var(--sp-4);
    margin-block-end: var(--sp-3);

    & > .section-heading {
      margin: 0;
    }
  }

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

  /* Buttons: one primary per screen, see design 5.4. */

  .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    min-height: var(--control-height);
    padding: 0 var(--sp-5);
    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(--fs-md);
    font-weight: 700;
    line-height: var(--lh-dense);
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition:
      background-color 120ms ease-out,
      border-color 120ms ease-out,
      color 120ms ease-out;

    &:hover:not(:disabled) {
      background: var(--color-bg);
    }

    &:disabled {
      cursor: progress;
      opacity: 0.6;
    }

    &[data-variant="primary"] {
      border-color: var(--color-primary);
      background: var(--color-primary);
      color: var(--color-surface);

      &:hover:not(:disabled) {
        border-color: var(--color-primary-hover);
        background: var(--color-primary-hover);
      }
    }

    &[data-variant="danger"] {
      border-color: var(--color-danger);
      color: var(--color-danger);

      &:hover:not(:disabled) {
        background: var(--color-danger-tint);
      }
    }

    &[data-variant="text"] {
      min-height: var(--target-min);
      padding-inline: var(--sp-1);
      border-color: transparent;
      background: transparent;
      color: var(--color-primary);
      text-decoration: underline;
      text-underline-offset: 0.18em;

      &:hover:not(:disabled) {
        background: transparent;
        color: var(--color-primary-hover);
        text-decoration-thickness: 2px;
      }
    }

    &[data-size="small"] {
      min-height: var(--control-height-sm);
      margin-block: calc((var(--target-min) - var(--control-height-sm)) / 2);
      padding-inline: var(--sp-3);
      font-size: var(--fs-sm);
    }

    &[data-size="assist"] {
      min-height: var(--control-height);
      padding-inline: var(--sp-3);
      border-color: var(--color-border-strong);
      background: var(--color-surface-subtle);
      color: var(--color-text);
      font-size: var(--fs-sm);
      font-weight: 700;

      &:hover:not(:disabled) {
        border-color: var(--color-primary);
        background: var(--color-primary-tint);
        color: var(--color-primary-hover);
      }
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .button {
      transition:
        background-color 120ms ease-out,
        border-color 120ms ease-out,
        color 120ms ease-out,
        scale 100ms ease-out;

      &:active:not(:disabled) {
        scale: 0.985;
      }
    }
  }

  /* Left: back and cancel. Right: secondary then primary. Danger stays apart from both. */
  .actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--sp-3);

    & > .actions-end {
      display: flex;
      flex-wrap: wrap;
      gap: var(--sp-3);
      margin-inline-start: auto;
    }
  }

  .status-label {
    display: inline-block;
    padding: 0 var(--sp-2);
    border: 1px solid var(--status-ended-border);
    border-radius: var(--radius-sm);
    background: var(--status-ended-bg);
    color: var(--status-ended-text);
    font-size: var(--fs-sm);
    font-weight: 700;
    line-height: 1.6;
    white-space: nowrap;

    &[data-status="draft"] {
      border-color: var(--status-draft-border);
      background: var(--status-draft-bg);
      color: var(--status-draft-text);
    }

    &[data-status="active"] {
      border-color: var(--status-active-border);
      background: var(--status-active-bg);
      color: var(--status-active-text);
    }

    &[data-status="overdue"] {
      border-color: var(--status-overdue-border);
      background: var(--status-overdue-bg);
      color: var(--status-overdue-text);
    }

    &[data-status="pending"] {
      border-color: var(--status-pending-border);
      background: var(--status-pending-bg);
      color: var(--status-pending-text);
    }
  }

  /* Lists are tables with rule lines only (design 5.3); a row's name links to the child detail. */

  .data-table {
    width: 100%;
    border: 1px solid var(--color-border);
    border-collapse: separate;
    border-spacing: 0;
    border-radius: var(--radius-md);
    background: var(--color-surface);
    line-height: var(--lh-dense);
    overflow: hidden;

    & th,
    & td {
      padding: var(--sp-2) var(--sp-3);
      border-block-end: 1px solid var(--color-border);
      text-align: start;
      vertical-align: middle;
    }

    & thead th {
      background: var(--color-surface-subtle);
      color: var(--color-text-muted);
      font-size: var(--fs-sm);
      font-weight: 700;
      white-space: nowrap;
    }

    & tbody tr {
      transition: background-color 120ms ease-out;

      &:hover {
        background: var(--color-bg);
      }
    }

    & tbody th {
      font-weight: 700;
    }

    & tbody tr:last-child th,
    & tbody tr:last-child td {
      border-block-end: 0;
    }

    & a {
      display: inline-block;
      min-height: var(--target-min);
      align-content: center;
      color: var(--color-primary);
      text-underline-offset: 0.18em;

      &:hover {
        color: var(--color-primary-hover);
        text-decoration-thickness: 2px;
      }
    }
  }

  /* Form fields: label above, hint between label and control, error below (design 5.5). */

  .field-inline {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--sp-2);

    & > input {
      flex: 1 1 11rem;
    }
  }

  /* Unified checkbox/radio row with a full 44px click target (design 5.4). */
  .choice {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    min-height: var(--target-min);

    & input {
      width: 1.25rem;
      height: 1.25rem;
      margin: 0;
      flex: 0 0 auto;
      cursor: pointer;
    }

    & label {
      display: flex;
      align-items: center;
      min-height: var(--target-min);
      padding-inline-end: var(--sp-2);
      cursor: pointer;
    }
  }

  .field {
    display: flex;
    flex-direction: column;
    gap: var(--sp-1);
    margin-block-end: var(--sp-4);

    & > label {
      font-weight: 700;
    }

    & input:not([type="checkbox"], [type="radio"]),
    & select,
    & textarea {
      box-sizing: border-box;
      width: 100%;
      min-height: var(--control-height);
      padding: var(--sp-2) var(--sp-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(--fs-md);
    }

    & textarea {
      min-block-size: calc(3lh + 2 * var(--sp-2));
      resize: vertical;
      /* Grows with its text where supported; elsewhere it keeps three lines and scrolls. */
      field-sizing: content;
    }

    &[data-size="date"] input {
      max-width: 14rem;
    }

    &[data-size="number"] input {
      max-width: 12rem;
    }

    &[data-size="short"] input {
      max-width: 24rem;
    }

    & [aria-invalid="true"] {
      border-color: var(--color-danger);
    }
  }

  .field-required {
    margin-inline-start: var(--sp-2);
    color: var(--color-danger);
    font-size: var(--fs-sm);
  }

  .field-hint {
    margin: 0;
    color: var(--color-text-muted);
    font-size: var(--fs-sm);
  }

  .field-message {
    margin: 0;
    color: var(--color-danger);
    font-weight: 700;
  }

  /* A warning does not block the operation (design 2.4), so it is told apart from an error by colour and shape. */
  .field-warning {
    margin: 0;
    padding: var(--sp-2) var(--sp-3);
    border-inline-start: 4px solid var(--status-draft-border);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    background: var(--status-draft-bg);
    color: var(--status-draft-text);

    & + .field-message {
      margin-block-start: var(--sp-2);
    }
  }

  .notice {
    margin: 0 0 var(--sp-3);
    padding: var(--sp-2) var(--sp-3);
    border-inline-start: 4px solid var(--status-pending-border);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    background: var(--status-pending-bg);
    color: var(--status-pending-text);
    font-size: var(--fs-sm);

    &[data-tone="danger"] {
      border-inline-start-color: var(--color-danger);
      background: var(--color-danger-tint);
      color: var(--status-overdue-text);
    }
  }

  /* Unified native <details> disclosure styling across screens; inside a panel or disclosure it drops its outer box (design 5.3). */
  .disclosure {
    margin-block-end: var(--sp-4);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);

    & > summary {
      min-height: var(--control-height);
      padding: var(--sp-2) var(--sp-4);
      align-content: center;
      color: var(--color-primary);
      font-weight: 700;
      cursor: pointer;
      transition: background-color 120ms ease-out;

      &:hover {
        background: var(--color-bg);
      }
    }

    &[open] > summary {
      border-block-end: 1px solid var(--color-border);
    }

    & > .disclosure-body {
      padding: var(--sp-4);
    }
  }

  :is(.panel, .disclosure) .disclosure {
    border: 0;
    background: var(--color-surface-subtle);
  }

  /* Dialogs: native <dialog> opened with showModal(); the browser handles focus, Esc and inertness. */

  dialog.modal {
    box-sizing: border-box;
    width: min(100% - 2 * var(--page-gutter), 520px);
    max-width: none;
    max-height: calc(100dvh - 2 * var(--sp-5));
    padding: 0;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: none;

    &[open] {
      display: flex;
      flex-direction: column;
    }

    &:modal {
      overscroll-behavior: contain;
    }

    &::backdrop {
      overflow: hidden;
      overscroll-behavior: contain;
      background: rgb(31 42 39 / 0.45);
    }

    &.modal-sm {
      width: min(100% - 2 * var(--page-gutter), 420px);
    }

    &.modal-wide {
      width: min(100% - 2 * var(--page-gutter), 800px);
    }

    &.modal-plan-detail {
      width: min(100% - 2 * var(--page-gutter), 920px);
    }

    & .modal-body {
      flex: 1;
      min-height: 0;
      padding: var(--sp-5);
      overflow-y: auto;
    }
  }

  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: var(--sp-4) var(--sp-5);
    border-block-end: 1px solid var(--color-border);
  }

  .modal-title {
    margin: 0;
    font-size: var(--fs-lg);
    font-weight: 700;
    line-height: var(--lh-dense);
  }

  .modal-close {
    min-width: var(--target-min);
    min-height: var(--target-min);
    padding: 0;
    border: 0;
    border-radius: var(--radius-md);
    background: none;
    color: var(--color-text-muted);
    font: inherit;
    font-size: var(--fs-lg);
    line-height: 1;
    cursor: pointer;

    &:hover {
      color: var(--color-text);
    }
  }

  /* The footer holds one .actions row across its width: cancel or close at the left end, the primary at the right (design 5.4). */
  .modal-footer {
    padding: var(--sp-4) var(--sp-5);
    border-block-start: 1px solid var(--color-border);
  }

  .modal-form {
    display: contents;
  }

  /* What a delete confirmation removes: a two-column list of label and value. */
  .delete-summary {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: var(--sp-2) var(--sp-4);
    margin: 0 0 var(--sp-4);

    & > div {
      display: contents;
    }

    & dt {
      color: var(--color-text-muted);
    }

    & dd {
      margin: 0;
      white-space: pre-wrap;
    }
  }
}
