@layer components {
  .modal-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--z-overlay);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: var(--space-8) var(--space-4);
    overflow-y: auto;
    background: color-mix(in srgb, var(--ink) 42%, transparent);
    backdrop-filter: blur(3px);
    animation: overlay-in var(--dur-2) var(--ease);
  }

  .modal {
    width: min(38rem, 100%);
    box-shadow: var(--ring);
    border-radius: var(--r-3);
    background: var(--surface-raised);
    box-shadow: var(--shadow-3);
    animation: modal-in var(--dur-2) var(--ease);
  }

  .modal--wide { width: min(52rem, 100%); }

  .modal-header {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-4) var(--space-3);
    border-bottom: 1px solid var(--border);
  }

  .modal-title {
    flex: 1;
    min-width: 0;
    font-size: var(--text-md);
    font-weight: 620;
    letter-spacing: -0.014em;
  }

  .modal-subtitle {
    margin-top: var(--space-1);
    color: var(--muted);
    font-size: var(--text-xs);
  }

  .modal-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    border: 0;
    border-radius: var(--r-2);
    background: transparent;
    color: var(--muted);
    font-size: var(--text-lg);
    line-height: 1;
  }

  .modal-close-btn:hover {
    background: var(--inset);
    color: var(--ink);
  }

  .modal-body {
    padding: var(--space-4);
  }

  .modal--ask {
    max-width: 32rem;
  }

  .modal-ask {
    color: var(--muted);
    line-height: 1.55;
  }

  .modal-section {
    position: relative;
  }

  .modal-section:focus {
    outline: none;
  }

  .modal-section:focus::before {
    content: "";
    position: absolute;
    inset-block: 0;
    left: calc(var(--space-3) * -1);
    width: 2px;
    border-radius: 2px;
    background: var(--accent);
  }

  .modal-section + .modal-section {
    margin-top: var(--space-5);
    padding-top: var(--space-5);
    border-top: 1px solid var(--border);
  }

  .modal-section .steps-summary,
  .modal-section .thread-summary,
  .modal-section .share-summary {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
  }

  .modal-section + .modal-section .steps-summary,
  .modal-section + .modal-section .thread-summary,
  .modal-section + .modal-section .share-summary {
    top: var(--space-5);
  }

  .modal-section-title {
    margin-bottom: var(--space-2);
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: 560;
  }

  .modal-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-top: 1px solid var(--border);
    background: var(--surface);
  }

  .modal-keys-hint {
    flex: 1;
    color: var(--text-subtle);
    font-size: var(--text-xs);
  }

  .modal-keys-hint kbd {
    font-family: inherit;
    font-size: inherit;
  }

  .plan-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }

  .plan-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-2) var(--space-2) 0;
  }

  .plan-row + .plan-row {
    border-top: 1px solid var(--border);
  }

  .plan-row-title {
    flex: 1;
    min-width: 0;
    cursor: pointer;
  }

  .plan-row-hours {
    color: var(--muted);
    font-size: var(--text-xs);
    white-space: nowrap;
  }

  .modal--search { width: min(34rem, 100%); }

  .search-head {
    padding: var(--space-3);
    border-bottom: 1px solid var(--border);
  }

  .search-input {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text);
    font-family: inherit;
    font-size: var(--text-md);
  }

  .search-input:focus { outline: none; }

  .search-hits {
    max-height: 22rem;
    overflow-y: auto;
    padding: var(--space-2);
  }

  .search-hit {
    display: flex;
    align-items: baseline;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--r-2);
    color: inherit;
    text-decoration: none;
  }

  .search-hit:hover,
  .search-hit:focus-visible {
    outline: none;
    background: var(--accent-soft);
  }

  .search-hit-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .search-hit-context {
    color: var(--muted);
    font-size: var(--text-xs);
    white-space: nowrap;
  }

  .search-empty {
    padding: var(--space-5) var(--space-4);
    color: var(--muted);
    font-size: var(--text-sm);
    text-align: center;
  }

  .modal-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
    gap: var(--space-3);
  }

  @keyframes overlay-in {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  @keyframes modal-in {
    from { opacity: 0; transform: translateY(8px) scale(0.985); }
    to { opacity: 1; transform: none; }
  }

  @media (max-width: 640px) {
    .modal-overlay {
      align-items: flex-end;
      padding: 0;
    }

    .modal {
      width: 100%;
      border-radius: var(--r-3) var(--r-3) 0 0;
      max-height: 92vh;
      overflow-y: auto;
    }
  }
}
