@layer components {

  /* Ícones e botões de ícone */
  .icon-button {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    color: var(--text);
    background: var(--surface);
  }

  /* .mobile-only vive aqui (camada components) para vencer o display:grid de .icon-button */
  .mobile-only {
    display: none;
  }

  @media (max-width: 959.98px) {
    .mobile-only {
      display: grid;
    }
  }

  .icon-button svg.icon {
    width: 20px;
    height: 20px;
  }

  /*
    Botão de fechar da gaveta da navegação: escondido acima de 960px (regra aqui,
    não em shell.css, porque precisa de prioridade igual/maior que .icon-button
    dentro da mesma camada @layer components para vencer o display:grid acima).
  */
  .sidebar-close {
    display: none;
    align-self: flex-end;
    margin-bottom: var(--sp-3);
    color: inherit;
    background: transparent;
    border-color: rgba(234, 255, 252, .25);
  }

  @media (max-width: 959.98px) {
    .sidebar-close {
      display: inline-flex;
    }
  }

  /* Botões */
  .primary-button,
  .secondary-button,
  .ghost-button,
  .danger-button,
  .danger-outline-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    min-height: 40px;
    padding: 10px var(--sp-4);
    border-radius: var(--r-sm);
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: var(--fs-13);
    white-space: nowrap;
  }

  .primary-button {
    border: 0;
    color: var(--ink);
    background: var(--brand);
  }

  .primary-button:hover {
    background: var(--brand-strong);
  }

  .secondary-button {
    border: 1px solid var(--line);
    color: var(--text);
    background: var(--surface);
  }

  .ghost-button {
    border: 0;
    color: var(--text-2);
    background: transparent;
  }

  .ghost-button:hover {
    background: var(--surface-soft);
  }

  .danger-button {
    border: 0;
    color: var(--white);
    background: var(--danger);
  }

  .danger-outline-button {
    border: 1px solid var(--danger);
    color: var(--danger);
    background: transparent;
  }

  .primary-button:disabled,
  .secondary-button:disabled,
  .ghost-button:disabled,
  .danger-button:disabled,
  .danger-outline-button:disabled {
    opacity: .5;
    cursor: not-allowed;
  }

  /* Badges semânticas */
  .badge {
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border-radius: 999px;
    color: var(--neutral);
    background: var(--neutral-soft);
    font-family: var(--font-heading);
    font-size: var(--fs-12);
    font-weight: 700;
    white-space: nowrap;
  }

  .badge-success {
    color: var(--success);
    background: var(--success-soft);
  }

  .badge-warning {
    color: var(--warning);
    background: var(--warning-soft);
  }

  .badge-danger {
    color: var(--danger);
    background: var(--danger-soft);
  }

  .badge-info {
    color: var(--info);
    background: var(--info-soft);
  }

  .badge-neutral {
    color: var(--neutral);
    background: var(--neutral-soft);
  }

  /* Painéis */
  .panel {
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
  }

  .panel-heading {
    display: flex;
    justify-content: space-between;
    padding: var(--sp-5) var(--sp-5);
    border-bottom: 1px solid var(--line);
  }

  .panel-heading > small,
  .panel-heading > div > small {
    display: block;
    margin-top: var(--sp-2);
    color: var(--muted);
    font-size: var(--fs-12);
  }

  .panel h3 {
    margin: var(--sp-1) 0 0;
    font-size: var(--fs-16);
  }

  /* Toolbar de busca */
  .content-toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: var(--sp-4);
    margin-bottom: var(--sp-4);
  }

  .search-wrap {
    width: min(420px, 100%);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 0 var(--sp-4);
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    background: var(--surface);
  }

  .search-wrap svg.icon,
  .search-wrap span {
    color: var(--muted);
  }

  .search-wrap input {
    width: 100%;
    height: 44px;
    border: 0;
    outline: 0;
    color: var(--text);
    background: transparent;
    font-size: var(--fs-14);
  }

  /* Filtros */
  .filter-panel {
    margin-bottom: var(--sp-4);
    padding: var(--sp-4);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
  }

  .filter-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--sp-3);
  }

  .filter-heading strong {
    font-size: var(--fs-13);
  }

  .filter-heading button {
    padding: var(--sp-1) 0;
    border: 0;
    color: var(--brand-ink);
    background: transparent;
    font-size: var(--fs-12);
    font-weight: 700;
  }

  .filter-heading button:hover {
    text-decoration: underline;
  }

  .filter-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr));
    gap: var(--sp-3);
  }

  .filter-grid label {
    display: grid;
    gap: var(--sp-1);
    min-width: 0;
    color: var(--muted);
    font-size: var(--fs-12);
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
  }

  .filter-grid select,
  .filter-grid input {
    width: 100%;
    min-height: 40px;
    padding: var(--sp-2) 10px;
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    outline: none;
    color: var(--text);
    background: var(--canvas);
    font-size: var(--fs-13);
    text-transform: none;
    letter-spacing: normal;
  }

  .filter-grid select:focus,
  .filter-grid input:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(25, 216, 197, .18);
  }

  /* Tabelas */
  .table-panel {
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
  }

  .table-wrap {
    position: relative;
    overflow-x: auto;
    scrollbar-width: thin;
    scrollbar-gutter: stable;
    scrollbar-color: var(--line-2) transparent;
  }

  .table-wrap::-webkit-scrollbar {
    height: 8px;
  }

  .table-wrap::-webkit-scrollbar-thumb {
    background: var(--line-2);
    border-radius: 99px;
  }

  /*
    Sombra de continuação para qualquer contêiner com overflow-x:auto.
    Aplicada via JS (observeTableOverflow / window.observeScrollOverflow)
    quando há conteúdo escondido à esquerda e/ou à direita.
  */
  .has-overflow-left {
    box-shadow: inset 16px 0 12px -12px rgba(0, 30, 28, .16);
  }

  .has-overflow-right {
    box-shadow: inset -16px 0 12px -12px rgba(0, 30, 28, .16);
  }

  .has-overflow-left.has-overflow-right {
    box-shadow: inset 16px 0 12px -12px rgba(0, 30, 28, .16), inset -16px 0 12px -12px rgba(0, 30, 28, .16);
  }

  table {
    width: 100%;
    min-width: 100%;
    border-collapse: collapse;
  }

  th, td {
    padding: var(--sp-3) var(--sp-4);
    text-align: left;
    overflow-wrap: anywhere;
  }

  th {
    color: var(--muted);
    background: var(--surface-soft);
    font-size: var(--fs-12);
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }

  td {
    border-top: 1px solid var(--line);
    font-size: var(--fs-14);
    font-variant-numeric: tabular-nums;
  }

  td:has(.badge),
  td.nowrap,
  th.nowrap {
    white-space: nowrap;
  }

  td:last-child,
  th:last-child {
    white-space: nowrap;
  }

  /* Primeira coluna e coluna de ações fixas para nunca sumirem */
  tr > *:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    background: var(--surface);
  }

  thead tr > *:first-child {
    z-index: 2;
    background: var(--surface-soft);
  }

  tr > *:last-child {
    position: sticky;
    right: 0;
    z-index: 1;
    background: var(--surface);
    box-shadow: -8px 0 8px -8px rgba(0, 30, 28, .16);
  }

  thead tr > *:last-child {
    z-index: 2;
    background: var(--surface-soft);
  }

  thead tr:first-child th:first-child {
    border-top-left-radius: var(--r-md);
  }

  thead tr:first-child th:last-child {
    border-top-right-radius: var(--r-md);
  }

  tbody tr:last-child td:first-child {
    border-bottom-left-radius: var(--r-md);
  }

  tbody tr:last-child td:last-child {
    border-bottom-right-radius: var(--r-md);
  }

  .table-name {
    font-weight: 700;
  }

  .table-sub {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: var(--fs-12);
  }

  .table-panel footer {
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: var(--fs-12);
  }

  .empty-state {
    padding: var(--sp-7);
    color: var(--muted);
    text-align: center;
  }

  .sort-button {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 0;
    border: 0;
    color: inherit;
    background: transparent;
    font: inherit;
    font-weight: 700;
    letter-spacing: inherit;
    text-transform: inherit;
  }

  .sort-button:hover,
  .sort-button.active {
    color: var(--brand-ink);
  }

  .sort-indicator {
    display: inline-flex;
    font-size: var(--fs-13);
    line-height: 1;
  }

  .sort-indicator svg.icon {
    width: 13px;
    height: 13px;
    transition: transform .15s, opacity .15s;
  }

  .sort-button[aria-sort="none"] .sort-indicator svg.icon,
  .sort-indicator[data-dir="none"] svg.icon {
    opacity: .45;
  }

  .sort-indicator[data-dir="asc"] svg.icon {
    transform: rotate(180deg);
  }

  /* Diálogos */
  dialog {
    width: min(650px, calc(100% - var(--sp-4) * 2));
    max-width: calc(100vw - 32px);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    color: var(--text);
    background: var(--surface);
    box-shadow: var(--shadow-2);
  }

  dialog::backdrop {
    background: rgba(0, 30, 28, .58);
    backdrop-filter: blur(3px);
  }

  .dialog-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-5) var(--sp-6);
    border-bottom: 1px solid var(--line);
  }

  .dialog-heading h2 {
    margin: var(--sp-1) 0 0;
    font-size: var(--fs-20);
  }

  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
    gap: var(--sp-4);
    padding: var(--sp-5) var(--sp-6);
  }

  .field {
    display: grid;
    min-width: 0;
    gap: var(--sp-2);
  }

  .field.full {
    grid-column: 1 / -1;
  }

  .field label {
    font-size: var(--fs-12);
    font-weight: 700;
  }

  .field input,
  .field select,
  .field textarea {
    width: 100%;
    min-height: 43px;
    padding: 10px var(--sp-3);
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    outline: none;
    color: var(--text);
    background: var(--canvas);
    font-size: var(--fs-14);
  }

  .field textarea {
    min-height: 90px;
    resize: vertical;
  }

  .field input:focus,
  .field select:focus,
  .field textarea:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(25, 216, 197, .18);
  }

  .checkbox-field {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding-top: var(--sp-2);
  }

  .checkbox-field input {
    width: 18px;
    height: 18px;
    accent-color: var(--brand-ink);
  }

  .dialog-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: var(--sp-2);
    padding: var(--sp-4) var(--sp-6);
    border-top: 1px solid var(--line);
  }

  .confirm-dialog {
    text-align: center;
    padding-top: var(--sp-6);
  }

  .confirm-dialog h2 {
    margin: var(--sp-3) 0 var(--sp-1);
  }

  .confirm-dialog p {
    margin: 0 var(--sp-6) var(--sp-5);
    color: var(--muted);
    font-size: var(--fs-13);
  }

  .confirm-icon {
    display: grid;
    place-items: center;
    width: 48px;
    height: 48px;
    margin: auto;
    border-radius: 50%;
    color: var(--danger);
    background: var(--danger-soft);
    font-weight: 800;
  }

  .toast {
    position: fixed;
    z-index: 200;
    right: var(--sp-5);
    top: var(--sp-5);
    max-width: 380px;
    padding: var(--sp-3) var(--sp-4);
    border-radius: var(--r-sm);
    color: var(--white);
    background: var(--brand-ink);
    box-shadow: var(--shadow-1);
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
    transition: .2s;
  }

  .toast.show {
    opacity: 1;
    transform: none;
  }

  .toast.error {
    background: var(--danger);
  }

  /* Editor de markdown (pauta e ata) */
  .markdown-editor {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--canvas);
  }

  .markdown-toolbar {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    flex-wrap: wrap;
    padding: var(--sp-2);
    border-bottom: 1px solid var(--line);
    background: var(--surface-soft);
  }

  .markdown-toolbar > span {
    flex: 1;
  }

  .markdown-toolbar button {
    min-height: 32px;
    padding: 5px 9px;
    border: 1px solid transparent;
    border-radius: var(--r-sm);
    color: var(--muted);
    background: transparent;
    font-size: var(--fs-12);
  }

  .markdown-toolbar button:hover,
  .markdown-toolbar button.active {
    border-color: var(--line);
    color: var(--text);
    background: var(--surface);
  }

  .markdown-toolbar button:disabled {
    opacity: .45;
    cursor: not-allowed;
  }

  .markdown-editor textarea {
    display: block;
    width: 100%;
    min-height: 280px;
    padding: var(--sp-5);
    border: 0;
    outline: 0;
    resize: vertical;
    color: var(--text);
    background: var(--canvas);
    font: var(--fs-13)/1.65 ui-monospace, SFMono-Regular, Consolas, monospace;
  }

  .markdown-editor[data-markdown-editor="minutes"] textarea {
    min-height: 390px;
  }

  .markdown-preview {
    min-height: 280px;
    padding: var(--sp-6);
    overflow: auto;
    color: var(--text);
    font-size: var(--fs-14);
    line-height: 1.7;
  }

  .markdown-preview h1, .markdown-preview h2, .markdown-preview h3, .markdown-preview h4 {
    margin: 1.2em 0 .55em;
    line-height: 1.25;
  }

  .markdown-preview h1:first-child, .markdown-preview h2:first-child,
  .markdown-preview h3:first-child, .markdown-preview p:first-child {
    margin-top: 0;
  }

  .markdown-preview p {
    margin: .7em 0;
  }

  .markdown-preview ul, .markdown-preview ol {
    padding-left: 25px;
  }

  .markdown-preview blockquote {
    margin: var(--sp-4) 0;
    padding: var(--sp-2) var(--sp-4);
    border-left: 4px solid var(--brand);
    color: var(--muted);
    background: var(--surface-soft);
  }

  .markdown-preview code {
    padding: 2px 5px;
    border-radius: 5px;
    background: var(--surface-soft);
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  }

  .markdown-preview pre {
    overflow: auto;
    padding: var(--sp-4);
    border-radius: var(--r-sm);
    background: var(--surface-soft);
  }

  .markdown-preview pre code {
    padding: 0;
  }

  .markdown-preview a {
    color: var(--brand-ink);
  }

  .markdown-empty {
    color: var(--muted);
    font-style: italic;
  }

  .markdown-editor.expanded {
    position: fixed;
    z-index: 100;
    inset: 20px;
    display: flex;
    flex-direction: column;
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-2);
  }

  .markdown-editor.expanded textarea,
  .markdown-editor.expanded .markdown-preview {
    flex: 1;
    min-height: 0;
    resize: none;
  }

  @media (max-width: 639.98px) {
    .content-toolbar,
    .settings-heading {
      align-items: stretch;
      flex-direction: column;
    }

    .primary-button {
      width: 100%;
    }

    .dialog-actions .primary-button {
      width: auto;
    }

    .form-grid {
      grid-template-columns: 1fr;
    }

    .field.full {
      grid-column: auto;
    }

    .markdown-toolbar > span {
      display: none;
    }

    .markdown-editor.expanded {
      inset: 8px;
    }
  }
}
