/* ==========================================================================
   Components: Tables, Forms, Dialogs, Badges (White Mode & Touch-Friendly)
   ========================================================================== */

/* Alerts */
output[role="alert"] {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 0.95rem;

  &[data-alert="success"] {
    background-color: var(--success-bg);
    color: var(--success);
    border: 1px solid var(--success-border);
  }

  &[data-alert="error"] {
    background-color: var(--danger-bg);
    color: var(--danger);
    border: 1px solid var(--danger-border);
  }
}

/* Responsive Table with horizontal touch scrolling */
[data-table-scroll] {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: right;
  white-space: nowrap;

  & th, & td {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border);
  }

  & th {
    background-color: var(--surface-alt);
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.85rem;
  }

  & tbody tr:hover td {
    background-color: #f1f5f9;
  }

  & a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;

    &:hover {
      text-decoration: underline;
    }
  }
}

/* Forms & Inputs */
form {
  display: flex;
  flex-direction: column;
  gap: 1rem;

  & fieldset {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    background-color: var(--surface);

    & legend {
      font-weight: 600;
      padding: 0 0.5rem;
      color: var(--primary);
      font-size: 0.95rem;
    }
  }

  & label {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    font-weight: 500;
    font-size: 0.9rem;

    & small {
      color: var(--text-muted);
      font-weight: normal;
    }
  }

  & input, & select, & textarea {
    font-family: inherit;
    font-size: 1rem; /* Prevents auto-zoom on iOS Safari */
    padding: 0.65rem 0.85rem;
    min-height: 44px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background-color: var(--surface);
    color: var(--text);
    width: 100%;
    transition: border-color 0.15s, box-shadow 0.15s;

    &:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
    }
  }

  & select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: left 0.85rem center;
    background-size: 16px 16px;
    padding-left: 2.25rem;
    padding-right: 0.85rem;
    cursor: pointer;

    &:focus {
      background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%232563eb' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    }

    & option {
      background-color: var(--surface);
      color: var(--text);
      padding: 0.5rem 0.75rem;
    }
  }

  & textarea {
    min-height: 120px;
    resize: vertical;
  }
}

/* Buttons */
button {
  font-family: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.65rem 1.25rem;
  min-height: 44px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  background-color: var(--primary);
  color: #ffffff;
  border: 1px solid transparent;
  transition: background-color 0.15s, border-color 0.15s;

  &:hover {
    background-color: var(--primary-hover);
  }

  &[data-variant="secondary"] {
    background-color: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);

    &:hover {
      background-color: var(--surface-alt);
      border-color: #cbd5e1;
    }
  }
}

/* Status Badges (Crisp Pastel White-Mode Pills) */
[data-status] {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 9999px;
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid transparent;
}

[data-status="open"] {
  background-color: #fef3c7;
  color: #92400e;
  border-color: #fde68a;
}

[data-status="in_progress"] {
  background-color: #eff6ff;
  color: #1d4ed8;
  border-color: #bfdbfe;
}

[data-status="resolved"] {
  background-color: #ecfdf5;
  color: #065f46;
  border-color: #a7f3d0;
}

[data-status="closed"] {
  background-color: #f1f5f9;
  color: #475569;
  border-color: #e2e8f0;
}

/* Priority Badges */
[data-priority] {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 600;
}

[data-priority="urgent"] {
  background-color: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

[data-priority="high"] {
  background-color: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
}

[data-priority="medium"] {
  background-color: #eff6ff;
  color: #1e40af;
  border: 1px solid #bfdbfe;
}

[data-priority="low"] {
  background-color: #f8fafc;
  color: #64748b;
  border: 1px solid var(--border);
}

/* Roles */
[data-role] {
  display: inline-flex;
  font-size: 0.8rem;
  padding: 0.2rem 0.5rem;
  border-radius: var(--radius-sm);
}

[data-role="admin"] {
  background-color: #f5f3ff;
  color: #6d28d9;
  border: 1px solid #ddd6fe;
  font-weight: 600;
}

[data-role="user"] {
  background-color: var(--surface-alt);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* Discussion Timeline */
section[data-timeline] {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;

  & article {
    background-color: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.15rem;

    &[data-author-role="admin"] {
      border-inline-start: 4px solid var(--primary);
      background-color: var(--primary-subtle);
    }

    & header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.75rem;
      border-bottom: 1px solid var(--border);
      padding-bottom: 0.5rem;

      & address {
        font-style: normal;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 0.5rem;
      }

      & time {
        font-size: 0.85rem;
        color: var(--text-muted);
      }
    }

    & p {
      margin: 0;
      white-space: pre-wrap;
      word-break: break-word;
    }
  }
}

/* Dialog (Modals - Mobile-friendly Bottom Sheet / Responsive) */
dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background-color: var(--surface);
  color: var(--text);
  padding: 1.5rem;
  max-width: 520px;
  width: 90%;
  box-shadow: var(--shadow-lg);

  &::backdrop {
    background-color: rgba(15, 23, 42, 0.45);
    backdrop-filter: blur(4px);
  }

  & header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.5rem;

    & h3 {
      margin: 0;
      font-size: 1.2rem;
    }

    & button {
      background: transparent;
      border: none;
      color: var(--text-muted);
      cursor: pointer;
      padding: 0.25rem;
      min-height: auto;

      &:hover {
        color: var(--text);
        background: transparent;
      }
    }
  }
}

@media (max-width: 640px) {
  dialog {
    width: calc(100% - 1.5rem);
    margin: 1rem auto;
    padding: 1.25rem;
  }
}

p[data-empty] {
  text-align: center;
  color: var(--text-muted);
  padding: 2.5rem 1rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  margin: 0;
}
