@layer reset {
  html {
    box-sizing: border-box;
    font-size: 100%;
    block-size: 100%;
  }

  *,
  *::before,
  *::after {
    box-sizing: inherit;
  }

  body {
    margin: 0;
    display: flex;
    flex-direction: column;
    block-size: 100vh;
    block-size: 100dvh;
    font-family: var(--font-sans);
    line-height: 1.5;
    color: var(--text-main);
    background-color: var(--bg-canvas);
    overflow: hidden;
  }
}

@layer base {
  :where(a:any-link, button, input, select, [tabindex="0"]):focus-visible {
    outline: 3px solid var(--era-accent);
    outline-offset: 3px;
  }

  button,
  input,
  select {
    font: inherit;
    color: inherit;
  }

  button {
    min-block-size: 2.25rem;
    min-inline-size: 2.25rem;
    padding: 0.35rem 0.75rem;
    border-radius: 0.45rem;
    border: 1px solid var(--border-subtle);
    background: var(--surface-elevated);
    cursor: pointer;
    transition: background-color 0.18s ease, border-color 0.18s ease;
  }

  button:hover:not(:disabled) {
    border-color: var(--era-accent);
  }

  button[aria-pressed="true"],
  .primary-btn {
    background: var(--era-accent);
    color: var(--btn-active-text);
    border-color: var(--era-accent);
    font-weight: 700;
  }
}

@layer components {
  .visually-hidden:where(:not(:focus-within, :active)) {
    position: absolute !important;
    clip-path: inset(50%) !important;
    overflow: hidden !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    border: 0 !important;
    white-space: nowrap !important;
  }

  .skip-link:focus-visible {
    position: fixed;
    inset-block-start: 0.75rem;
    inset-inline-start: 0.75rem;
    z-index: 100;
    padding: 0.5rem 0.9rem;
    background: var(--surface-elevated);
    border: 2px solid var(--era-accent);
    border-radius: 0.4rem;
  }

  .top-bar {
    position: relative;
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 0.75rem;
    padding: 0.45rem 0.9rem;
    background: var(--surface);
    border-block-end: 2px solid var(--era-accent);
    backdrop-filter: blur(10px);
  }

  .brand-block {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
  }

  .site-title {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(1.05rem, 0.9rem + 0.7vw, 1.45rem);
    letter-spacing: 0.06em;
    line-height: 1.05;
    color: var(--era-accent);
  }

  .era-indicator {
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.78rem;
    color: var(--text-muted);
  }

  .era-badge {
    font-family: var(--font-mono);
    font-weight: 700;
    padding: 0.1rem 0.45rem;
    border-radius: 0.3rem;
    border: 1px solid var(--era-accent);
    color: var(--text-main);
  }

  .search-form {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    flex: 1 1 20rem;
    max-inline-size: 46rem;
  }

  .search-field-group {
    flex: 1 1 11rem;
  }

  .search-field-group input,
  .filter-group select {
    inline-size: 100%;
    min-block-size: 2.1rem;
    padding: 0.25rem 0.55rem;
    border-radius: 0.45rem;
    border: 1px solid var(--border-subtle);
    background: var(--surface-elevated);
    font-size: 0.85rem;
  }

  .filter-group {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  .filter-group select {
    inline-size: auto;
    max-inline-size: 14rem;
  }

  .toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
  }

  .segmented-control {
    display: inline-flex;
    gap: 0.2rem;
    padding: 0.15rem;
    border: 1px solid var(--border-subtle);
    border-radius: 0.55rem;
    background: var(--surface);
  }

  .stage-shell {
    position: relative;
    flex: 1 1 0%;
    min-block-size: 0;
    display: flex;
    align-items: stretch;
    overflow: hidden;
  }

  .canvas-section {
    position: relative;
    flex: 1 1 0%;
    min-inline-size: 0;
    min-block-size: 0;
    overflow: hidden;
  }

  #timeline-canvas {
    position: absolute;
    inset: 0;
    display: block;
    inline-size: 100%;
    block-size: 100%;
    touch-action: none;
    cursor: grab;
  }

  #timeline-canvas:active {
    cursor: grabbing;
  }

  .label-layer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    overflow: hidden;
  }

  .node-label {
    position: absolute;
    transform: translate(-50%, -125%);
    padding: 0.12rem 0.4rem;
    border-radius: 0.3rem;
    font-size: 0.76rem;
    font-weight: 600;
    white-space: nowrap;
    border: 1px solid var(--border-subtle);
    background: var(--surface);
    color: var(--text-main);
    pointer-events: auto;
    cursor: pointer;
  }

  .node-label[data-selected="true"] {
    border-color: var(--era-accent);
    box-shadow: 0 0 0 2px var(--era-accent);
    font-weight: 700;
  }

  .boot-banner {
    position: absolute;
    inset-block-start: 1rem;
    inset-inline-start: 50%;
    transform: translateX(-50%);
    z-index: 15;
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.55rem 0.9rem;
    border-radius: 0.55rem;
    border: 1px solid var(--era-accent);
    background: var(--surface-elevated);
    font-size: 0.85rem;
  }

  .boot-banner[hidden] {
    display: none;
  }

  .camera-hint {
    position: absolute;
    inset-block-end: 0.75rem;
    inset-inline-start: 0.85rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 0.35rem 0.7rem;
    border-radius: 0.45rem;
    border: 1px solid var(--border-subtle);
    background: var(--surface);
    font-size: 0.75rem;
    color: var(--text-muted);
    pointer-events: none;
  }

  .detail-drawer {
    position: relative;
    z-index: 18;
    inline-size: min(26rem, 92dvw);
    block-size: 100%;
    overflow-y: auto;
    padding: 1rem 1.15rem;
    background: var(--surface-elevated);
    border-inline-start: 2px solid var(--era-accent);
    scrollbar-gutter: stable;
  }

  .detail-drawer[hidden] {
    display: none;
  }

  .drawer-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    padding-block-end: 0.75rem;
    border-block-end: 1px solid var(--border-subtle);
  }

  .drawer-kind-pill {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 0.72rem;
    text-transform: uppercase;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    border: 1px solid var(--era-accent);
    margin-block-end: 0.35rem;
  }

  .drawer-header h2 {
    margin: 0;
    font-size: 1.25rem;
    line-height: 1.25;
    text-wrap: balance;
  }

  .drawer-years {
    margin: 0.2rem 0 0;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    color: var(--text-muted);
  }

  .drawer-summary {
    max-inline-size: 62ch;
    text-wrap: pretty;
    font-size: 0.92rem;
  }

  .drawer-meta {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.35rem 0.85rem;
    font-size: 0.84rem;
    margin: 0.75rem 0 1rem;
  }

  .drawer-meta dt {
    font-weight: 700;
    color: var(--text-muted);
  }

  .drawer-meta dd {
    margin: 0;
  }

  .drawer-section {
    margin-block-start: 1.15rem;
  }

  .drawer-section h3 {
    margin: 0 0 0.5rem;
    font-size: 0.92rem;
    color: var(--era-accent);
  }

  .connection-list,
  .similar-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
  }

  .connection-btn {
    inline-size: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    text-align: start;
    font-size: 0.84rem;
  }

  .connection-meta {
    font-family: var(--font-mono);
    font-size: 0.74rem;
    color: var(--text-muted);
  }

  .drawer-footer {
    margin-block-start: 1.25rem;
    padding-block-start: 0.75rem;
    border-block-start: 1px solid var(--border-subtle);
    font-size: 0.85rem;
  }

  .table-section {
    flex: 1 1 auto;
    padding: 1rem 1.25rem;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  .table-section[hidden] {
    display: none;
  }

  .table-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.5rem;
  }

  .table-toolbar h2 {
    margin: 0;
    font-size: 1.15rem;
  }

  .table-count {
    margin: 0;
    font-family: var(--font-mono);
    font-size: 0.82rem;
    color: var(--text-muted);
  }

  .table-wrapper {
    overflow: auto;
    max-inline-size: 100%;
    flex: 1 1 auto;
    border: 1px solid var(--border-subtle);
    border-radius: 0.5rem;
    background: var(--surface-elevated);
    container-type: inline-size;
  }

  table {
    inline-size: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.88rem;
  }

  thead th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    text-align: start;
    padding: 0.65rem 0.75rem;
    background: var(--surface);
    border-block-end: 2px solid var(--era-accent);
  }

  tbody th,
  tbody td {
    text-align: start;
    padding: 0.55rem 0.75rem;
    border-block-end: 1px solid var(--border-subtle);
  }

  tbody th {
    position: sticky;
    inset-inline-start: 0;
    background: var(--surface-elevated);
  }

  @container (width < 600px) {
    table,
    thead,
    tbody,
    tr,
    th,
    td {
      display: block;
    }

    thead {
      display: none;
    }

    tr {
      padding: 0.5rem;
      border-block-end: 2px solid var(--border-subtle);
    }

    tbody th {
      position: static;
      font-size: 1rem;
    }

    td:nth-child(2)::before {
      content: "Tipo: ";
      font-weight: 700;
    }

    td:nth-child(3)::before {
      content: "Período: ";
      font-weight: 700;
    }

    td:nth-child(4)::before {
      content: "Origen / Rol: ";
      font-weight: 700;
    }
  }

  .era-scrubber-bar {
    position: relative;
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.65rem;
    padding: 0.55rem 1rem;
    background: var(--surface);
    border-block-start: 1px solid var(--border-subtle);
  }

  .era-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  .era-btn {
    font-size: 0.78rem;
    min-block-size: 1.9rem;
    padding: 0.25rem 0.55rem;
  }

  .legend-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.85rem;
    font-size: 0.78rem;
  }

  .legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
  }

  .swatch {
    display: inline-block;
    inline-size: 0.8rem;
    block-size: 0.8rem;
    border: 1px solid currentColor;
  }

  .swatch-band {
    background: var(--color-band);
    border-radius: 0.15rem;
  }

  .swatch-person {
    background: var(--color-person);
    border-radius: 50%;
  }

  .swatch-intl {
    background: var(--color-intl);
    transform: rotate(45deg);
  }

  .swatch-context {
    background: var(--color-context);
    border-radius: 0.2rem;
  }

  dialog {
    max-inline-size: 32rem;
    inline-size: 92dvw;
    padding: 1.15rem 1.25rem;
    border-radius: 0.75rem;
    border: 2px solid var(--era-accent);
    background: var(--surface-elevated);
    color: var(--text-main);
  }

  dialog::backdrop {
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
  }

  .dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-block-end: 0.85rem;
  }

  .dialog-header h2 {
    margin: 0;
    font-size: 1.15rem;
  }

  .theme-fieldset {
    border: 1px solid var(--border-subtle);
    border-radius: 0.55rem;
    padding: 0.75rem;
    margin: 0 0 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
  }

  .radio-card,
  .checkbox-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.9rem;
  }

  .dialog-actions {
    display: flex;
    justify-content: flex-end;
  }
}
