/*
 * Reusable UI components. Pages compose these; page-specific tweaks go to pages.css.
 */
@layer components {
  .ico {
    width: 18px;
    height: 18px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .ico-lg { width: 20px; height: 20px; }

  /* ---------- Buttons ---------- */
  .btn {
    --btn-h: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: var(--btn-h);
    padding: 0 16px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: var(--surface-2);
    color: var(--text);
    font-weight: 600;
    font-size: 14px;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color .15s, border-color .15s, filter .15s;

    &:hover { text-decoration: none; filter: brightness(1.08); }
    &:disabled, &[aria-disabled="true"] { opacity: .5; cursor: not-allowed; filter: none; }
  }
  .btn-primary { background: var(--btn-bg); color: var(--btn-fg); }
  a.btn-primary { color: var(--btn-fg); }
  .btn-outline { background: transparent; border-color: var(--border-strong); color: var(--text); }
  a.btn-outline { color: var(--text); }
  .btn-ghost { background: transparent; color: var(--muted); }
  .btn-danger { background: var(--danger-soft); color: var(--danger); }
  .btn-sm { --btn-h: 32px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
  .btn-lg { --btn-h: 52px; font-size: 16px; border-radius: 14px; }
  .btn-block { width: 100%; }
  .btn-icon { width: var(--btn-h); padding: 0; }
  @media (max-width: 991.98px) {
    .btn-icon.btn-ghost { --btn-h: 44px; border-radius: 12px; }
  }

  /* ---------- Cards ---------- */
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    min-width: 0;
    @media (max-width: 991.98px) { padding: 16px; }
  }
  .card-accent { background: var(--primary-soft); border-color: transparent; }
  .card-flush { padding: 0; overflow: hidden; }
  .card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    & h2 { font-size: 15px; font-weight: 600; }
    & a { font-size: 13px; font-weight: 600; }
  }
  .card-head-wrap { flex-wrap: wrap; & .seg { max-width: 100%; } }
  .card-flush > .card-head { padding: 20px 20px 0; @media (max-width: 991.98px) { padding: 16px 16px 0; } }
  a.card { display: block; color: var(--text); &:hover { text-decoration: none; border-color: var(--border-strong); } }

  .big-num { font-size: 36px; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; font-variant-numeric: tabular-nums;
    @media (max-width: 991.98px) { font-size: 32px; } }
  .mid-num { font-size: 22px; font-weight: 700; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }

  .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; }
  .stats > div { min-width: 0; }
  .stat-v { font-weight: 600; font-variant-numeric: tabular-nums; }

  /* Single-line summary strip (team summary etc.) */
  .strip { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 32px; padding: 12px 20px;
    & > div { display: flex; align-items: baseline; gap: 8px; }
    & b { font-size: 18px; font-variant-numeric: tabular-nums; }
    @media (max-width: 991.98px) { padding: 12px 16px; gap: 6px 20px; }
  }

  /* ---------- Chips & badges ---------- */
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 26px;
    padding: 0 10px;
    border-radius: 999px;
    background: var(--surface-2);
    font-size: 12px;
    font-weight: 500;
    color: var(--muted);
    white-space: nowrap;
    & b { color: var(--text); font-variant-numeric: tabular-nums; }
  }
  .chip-success { background: var(--success-soft); color: var(--success); }
  .chip-warn { background: var(--warn-soft); color: var(--warn); }
  .chip-danger { background: var(--danger-soft); color: var(--danger); }
  .chip-primary { background: var(--primary-soft); color: var(--primary); }
  .badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    display: inline-grid;
    place-items: center;
    font-size: 11px;
    font-weight: 700;
    background: var(--surface-2);
    color: var(--muted);
  }

  /* ---------- Avatars ---------- */
  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: var(--primary-soft);
    color: var(--primary);
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 14px;
    flex: none;
    border: 0;
    &:hover { text-decoration: none; }
    @media (max-width: 991.98px) { width: 36px; height: 36px; border-radius: 10px; font-size: 13px; }
  }
  .avatar-sm { width: 36px; height: 36px; border-radius: 10px; background: var(--surface-2); font-size: 13px; }

  /* ---------- List rows ---------- */
  .list { display: flex; flex-direction: column; }
  .list-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 0;
    border-top: 1px solid var(--border);
    min-width: 0;
    &:first-child { border-top: 0; }
    & .grow { flex: 1; min-width: 0; }
  }
  a.list-row { color: var(--text); &:hover { text-decoration: none; background: var(--surface-2); } }
  .card-flush .list-row { padding-inline: 20px; @media (max-width: 991.98px) { padding-inline: 16px; } }

  /* Tappable action tile */
  .action {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-radius: 12px;
    background: var(--surface-2);
    color: var(--text);
    font-weight: 500;
    &:hover { text-decoration: none; filter: brightness(1.08); }
    & > .ico { color: var(--primary); }
    & .chev { margin-left: auto; color: var(--subtle); }
  }

  /* ---------- Menu list (mobile «Ще», profile) ---------- */
  .menu { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
  .menu-item {
    display: flex;
    align-items: center;
    gap: 14px;
    min-height: 52px;
    padding: 0 16px;
    color: var(--text);
    border-top: 1px solid var(--border);
    background: none;
    border-left: 0; border-right: 0; border-bottom: 0;
    text-align: left;
    width: 100%;
    font-weight: 500;
    &:first-child { border-top: 0; }
    &:hover { text-decoration: none; background: var(--surface-2); }
    & > .ico { color: var(--muted); }
    & .val { margin-left: auto; color: var(--muted); font-weight: 400; }
    & .chev { margin-left: auto; color: var(--subtle); }
    & .val + .chev { margin-left: 0; }
  }
  .menu-item-danger, .menu-item-danger > .ico { color: var(--danger); }
  .menu-title { font-size: 12px; font-weight: 600; color: var(--muted); padding: 0 4px 8px; }

  /* ---------- Tables ---------- */
  .table-wrap { overflow-x: auto; }
  .table {
    width: 100%;
    font-size: 14px;
    & th { text-align: left; font-size: 12px; font-weight: 500; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
    & td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
    & tr:last-child td { border-bottom: 0; }
    & .num, & .ta-r { text-align: right; }
    & th:first-child, & td:first-child { padding-left: 20px; }
    & th:last-child, & td:last-child { padding-right: 20px; }
    & tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
  }
  /* On phones tables turn into stacked cards; each td needs data-label */
  @media (max-width: 767.98px) {
    .table-stack {
      & thead { display: none; }
      & tr { display: block; padding: 12px 16px; border-bottom: 1px solid var(--border); }
      & tr:last-child { border-bottom: 0; }
      & td { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0 !important; border: 0; text-align: right; }
      & td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; }
      & td[data-label=""]::before { content: none; }
      & tbody tr:hover td { background: none; }
    }
  }

  /* ---------- Segmented tabs ---------- */
  .seg { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-2); overflow-x: auto; scrollbar-width: none; }
  .seg > * {
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 12px;
    border-radius: 9px;
    color: var(--muted);
    font-weight: 600;
    font-size: 13px;
    border: 0;
    background: none;
    &:hover { text-decoration: none; color: var(--text); }
    &[aria-current="page"], &.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .2); }
    & small { font-weight: 500; color: var(--subtle); }
  }

  /* ---------- Breadcrumbs ---------- */
  .crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; color: var(--muted);
    & [aria-current] { color: var(--text); font-weight: 600; } }

  /* ---------- Forms ---------- */
  .form { display: flex; flex-direction: column; gap: 16px; }
  .form-row { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
  .field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
  .field > label, .field-label { font-size: 13px; font-weight: 500; }
  .field-help { font-size: 12px; color: var(--muted); }
  .input, .field input:not([type="checkbox"], [type="radio"], [type="hidden"]), .field select, .field textarea {
    width: 100%;
    min-height: 44px;
    padding: 10px 14px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font-size: 15px;
    &::placeholder { color: var(--subtle); }
    &:focus { outline: none; border-color: var(--primary); box-shadow: var(--focus); }
    &[readonly], &:disabled { background: var(--surface-2); color: var(--muted); }
  }
  .field textarea { min-height: 96px; resize: vertical; }
  .input-group {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--muted);
    &:focus-within { border-color: var(--primary); box-shadow: var(--focus); }
    & input { flex: 1; min-width: 0; border: 0 !important; background: transparent !important; box-shadow: none !important; padding: 0 !important; min-height: 42px; outline: none; }
  }
  .check { display: flex; align-items: flex-start; gap: 10px; font-size: 14px;
    & input { width: 18px; height: 18px; margin-top: 1px; accent-color: var(--primary); flex: none; } }
  .form-errors, .field ul { list-style: none; padding: 0; margin: 0; color: var(--danger); font-size: 13px; }
  .field:has(ul li) :is(input, select, textarea) { border-color: var(--danger); }

  /* ---------- Alerts / flashes ---------- */
  .alert {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 12px 14px;
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font-size: 14px;
    & > .ico { margin-top: 1px; }
  }
  .alert-success { background: var(--success-soft); color: var(--success); }
  .alert-warn { background: var(--warn-soft); color: var(--warn); }
  .alert-danger { background: var(--danger-soft); color: var(--danger); }
  .alert-info { background: var(--primary-soft); color: var(--text); & > .ico { color: var(--primary); } }
  .flashes { display: flex; flex-direction: column; gap: 8px; margin-bottom: var(--gap); }

  /* ---------- Empty state ---------- */
  .empty { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 32px 16px; text-align: center; color: var(--muted);
    & > .ico { width: 32px; height: 32px; color: var(--subtle); } }

  /* ---------- Dialog (native <dialog>) ---------- */
  .dialog {
    /* Pages set --dialog-w instead of width, so the mobile sheet below always wins */
    width: min(var(--dialog-w, 440px), calc(100vw - 32px));
    max-width: none;
    max-height: calc(100dvh - 48px);
    margin: auto;
    padding: 0;
    overflow: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
    &::backdrop { background: rgb(0 0 0 / .55); backdrop-filter: blur(2px); }
    &:focus { outline: none; }
    /* Phones: bottom sheet */
    @media (max-width: 575.98px) {
      inset: auto 0 0 0;
      width: 100%;
      max-height: 88dvh;
      margin: 0;
      border-width: 1px 0 0;
      border-radius: 20px 20px 0 0;
      padding-bottom: env(safe-area-inset-bottom);
      &::before { content: ""; display: block; width: 40px; height: 4px; margin: 8px auto 0; border-radius: 2px; background: var(--border-strong); }
    }
  }
  /* Side panel variant: right edge on desktop, full-height sheet on phones */
  .dialog.drawer {
    @media (min-width: 576px) {
      inset: 0 0 0 auto;
      width: min(var(--dialog-w, 560px), 100vw);
      height: 100dvh;
      max-height: none;
      margin: 0;
      border-width: 0 0 0 1px;
      border-radius: 0;
      &[open] { animation: drawer-in .2s ease-out; }
    }
    @media (max-width: 575.98px) {
      max-height: 94dvh;
      height: 94dvh;
      &[open] { animation: sheet-in .2s ease-out; }
    }
  }
  @keyframes drawer-in { from { transform: translateX(40px); opacity: 0; } }
  @keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }
  @media (prefers-reduced-motion: reduce) { .dialog.drawer[open] { animation: none; } }

  .dialog-head {
    position: sticky; top: 0; z-index: 1;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 14px 12px 10px 20px;
    background: var(--surface);
    & h2 { font-size: 17px; min-width: 0; }
  }
  .dialog-body { padding: 6px 20px 20px; }

  /* ---------- ID card visuals ---------- */
  .code-img { background: #fff; border-radius: 8px; padding: 8px; }
  .code-img img { width: 100%; height: auto; image-rendering: pixelated; }
  .code-barcode img { height: 72px; object-fit: fill; }
  .code-qr { width: 200px; margin-inline: auto; & img { aspect-ratio: 1; } }
  @media (max-height: 700px) { .code-qr { width: 160px; } }
  .card-number { font-size: 18px; font-weight: 600; letter-spacing: .06em; font-variant-numeric: tabular-nums; }

  /* ---------- Pagination ---------- */
  .pager { display: flex; flex-wrap: wrap; gap: 4px; justify-content: center; padding: 16px;
    & a, & span { min-width: 36px; height: 36px; padding: 0 10px; display: grid; place-items: center; border-radius: 10px; color: var(--muted); font-weight: 600; }
    & a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
    & [aria-current] { background: var(--primary-soft); color: var(--text); } }

  /* Copy feedback */
  /* Copy buttons: [data-copy] swaps .when-idle for .when-copied for 1.5s (see cabinet.js) */
  .when-idle, .when-copied { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
  .when-copied { display: none; }
  [data-copied] .when-idle { display: none; }
  [data-copied] .when-copied { display: inline-flex; animation: copied-in .25s ease-out; }
  [data-copy] { transition: background-color .2s, color .2s, border-color .2s; }
  [data-copy][data-copied] { background: var(--success-soft); border-color: transparent; color: var(--success); filter: none; }
  [data-copy][data-copied] .ico { color: var(--success); }
  @keyframes copied-in { from { opacity: 0; transform: scale(.85); } }
  @media (prefers-reduced-motion: reduce) { [data-copied] .when-copied { animation: none; } }
}
