/* ==========================================================================
   Blocks Modal Component
   ==========================================================================

   Scrolling strategy (cross-browser, including Safari):

   Avoids position:sticky (Safari bleed-through bug). Instead, the table
   uses display overrides to structurally separate the header from scroll:

     <table>  → display: flex  (column)
     <thead>  → display: block (flex-shrink: 0, never scrolls)
     <tbody>  → display: block (overflow-y: auto, sole scroll container)
     <tr>     → display: table (table-layout: fixed, keeps columns aligned)

   ========================================================================== */

@layer components {
  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    animation: fadeIn 0.2s ease;
  }

  .modal-content {
    background: white;
    border-radius: 8px;
    width: 90%;
    max-width: 900px;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1),
                0 10px 20px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    animation: slideUp 0.3s ease;
  }

  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid #e0e0e0;
    flex-shrink: 0;
  }

  .modal-title {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
    color: #333;
  }

  .modal-close {
    background: none;
    border: none;
    font-size: 2rem;
    cursor: pointer;
    color: #666;
    padding: 0;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
  }

  .modal-close:hover {
    background: #f5f5f5;
    color: #333;
  }

  .modal-body {
    padding: 1.5rem;
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .search-section {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    flex-shrink: 0;
  }

  .search-section input,
  .search-section select {
    padding: 0.75rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 1rem;
    font-family: inherit;
    transition: all 0.2s ease;
  }

  .search-section input {
    flex: 1;
    min-width: 200px;
  }

  .search-section select {
    min-width: 150px;
  }

  .search-section input:focus,
  .search-section select:focus {
    outline: none;
    border-color: #4CAF50;
    box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
  }

  .table-wrapper {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .blocks-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.95rem;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
  }

  .blocks-table thead {
    display: block;
    flex-shrink: 0;
  }

  .blocks-table thead tr {
    display: table;
    width: 100%;
    table-layout: fixed;
  }

  .blocks-table th {
    background: #f5f5f5;
    padding: 0.75rem;
    text-align: left;
    border-bottom: 2px solid #ddd;
    font-weight: 600;
    color: #333;
  }

  .blocks-table tbody {
    display: block;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
  }

  .blocks-table tbody tr {
    display: table;
    width: 100%;
    table-layout: fixed;
    transition: background-color 0.2s ease;
  }

  .blocks-table td {
    padding: 0.75rem;
    border-bottom: 1px solid #eee;
    vertical-align: middle;
  }

  .blocks-table tbody tr:hover {
    background: #fafafa;
  }

  .blocks-table tbody tr.block-added-flash {
    animation: blockAddedFlash 1.4s ease;
  }

  .block-name {
    font-weight: 500;
    color: #333;
  }

  .block-type {
    color: #666;
    font-size: 0.9rem;
  }

  .block-gain,
  .block-nf,
  .block-p1db {
    text-align: right;
    color: #666;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 0.9rem;
  }

  .block-action {
    text-align: center;
    width: 128px;
  }

  .block-action form {
    display: flex;
    justify-content: center;
  }

  .btn--small {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    border-radius: 4px;
    cursor: pointer;
    border: none;
    transition: all 0.2s ease;
    white-space: nowrap;
    min-width: 6.5rem;
  }

  .btn--primary {
    background: #4CAF50;
    color: white;
  }

  .btn--primary:hover:not(:disabled) {
    background: #45a049;
  }

  .btn--primary:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.6;
  }

  .btn--small.btn--added {
    background: linear-gradient(135deg, #4CAF50, #79c957);
    box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.35);
    animation: buttonCelebrate 0.6s cubic-bezier(0.2, 0.9, 0.25, 1.25),
               buttonGlow 1.6s ease;
  }

  .btn--secondary {
    background: #f0f0f0;
    color: #333;
    border: 1px solid #ddd;
  }

  .btn--secondary:hover {
    background: #e8e8e8;
  }

  .no-results td {
    text-align: center;
    padding: 2rem 1rem !important;
    color: #999;
    font-style: italic;
  }

  .modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid #e0e0e0;
    text-align: right;
    flex-shrink: 0;
    background: #fafafa;
  }

  .toast {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    padding: 1rem 1.5rem;
    background: #4CAF50;
    color: white;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2),
                0 4px 8px rgba(0, 0, 0, 0.15);
    z-index: 2000;
    animation: slideInDown 0.3s ease;
    opacity: 0;
    transform: translateY(20px);
    pointer-events: none;
  }

  .toast-show {
    opacity: 1;
    transform: translateY(0);
  }

  .toast-success {
    background: #4CAF50;
  }

  .toast-error {
    background: #f44336;
  }

  .toast-info {
    background: #2196F3;
  }

  .toast-warning {
    background: #ff9800;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  @keyframes slideUp {
    from {
      transform: translateY(20px);
      opacity: 0;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }

  @keyframes slideInDown {
    from {
      transform: translateY(-100%);
      opacity: 0;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }

  @keyframes buttonCelebrate {
    0% {
      transform: scale(1);
    }
    35% {
      transform: scale(1.1) rotate(-2deg);
    }
    65% {
      transform: scale(0.96) rotate(1deg);
    }
    100% {
      transform: scale(1);
    }
  }

  @keyframes buttonGlow {
    0% {
      box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.35);
    }
    45% {
      box-shadow: 0 0 0 10px rgba(76, 175, 80, 0);
    }
    100% {
      box-shadow: 0 0 0 0 rgba(76, 175, 80, 0);
    }
  }

  @keyframes blockAddedFlash {
    0% {
      background: rgba(121, 201, 87, 0);
    }
    20% {
      background: rgba(121, 201, 87, 0.18);
    }
    100% {
      background: rgba(121, 201, 87, 0);
    }
  }

  @media (max-width: 768px) {
    .modal-content {
      width: 95%;
      max-height: 90vh;
    }

    .search-section {
      flex-direction: column;
    }

    .search-section input,
    .search-section select {
      width: 100%;
    }

    .modal-header {
      padding: 1rem;
    }

    .modal-body {
      padding: 1rem;
    }

    .modal-footer {
      padding: 1rem;
    }

    .blocks-table {
      font-size: 0.85rem;
    }

    .blocks-table th,
    .blocks-table td {
      padding: 0.5rem;
    }

    .block-action {
      width: auto;
    }
  }

  .modal-close:focus,
  .btn:focus {
    outline: 2px solid #4CAF50;
    outline-offset: 2px;
  }

  @media (prefers-color-scheme: dark) {
    .modal-overlay {
      background: rgba(0, 0, 0, 0.7);
    }

    .modal-content {
      background: #1e1e1e;
      color: #e0e0e0;
    }

    .modal-header {
      border-bottom-color: #333;
    }

    .modal-title {
      color: #fff;
    }

    .modal-close {
      color: #999;
    }

    .modal-close:hover {
      background: #333;
      color: #fff;
    }

    .search-section input,
    .search-section select {
      background: #2a2a2a;
      color: #e0e0e0;
      border-color: #444;
    }

    .blocks-table th {
      background: #2a2a2a;
      color: #e0e0e0;
      border-bottom-color: #444;
    }

    .blocks-table td {
      border-bottom-color: #333;
    }

    .block-name {
      color: #fff;
    }

    .block-type,
    .block-gain,
    .block-nf,
    .block-p1db {
      color: #bbb;
    }

    .blocks-table tbody tr:hover {
      background: #2a2a2a;
    }

    .modal-footer {
      background: #2a2a2a;
      border-top-color: #333;
    }
  }
}
