/* ============================================================
   Design tokens
   ============================================================ */
:root {
  --color-bg: #f5f5f7;
  --color-surface: #fff;
  --color-surface-muted: #f0f0f2;
  --color-border: #e0e0e0;
  --color-border-strong: #ccc;
  --color-text: #1a1a1a;
  --color-text-muted: #666;
  --color-text-faint: #999;

  --color-primary: #1a1a1a;
  --color-primary-hover: #333;

  /* Accent — Phase 14 (scope.md §28 point 1): the one deliberate departure from the otherwise
     monochrome palette, carrying primary actions, active nav, links, and focus rings. */
  --color-accent: #4f46e5;
  --color-accent-hover: #4338ca;
  --color-accent-bg: #eef1fd;

  --color-danger: #a13b2f;
  --color-danger-bg: #fdecea;
  --color-success: #2e6b2a;
  --color-success-bg: #e3f2e1;
  --color-warning: #8a6d00;
  --color-warning-bg: #fff8db;
  --color-info: #33409e;
  --color-info-bg: #eef0fb;

  --radius: 6px;
  --radius-sm: 4px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.08);

  --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;

  /* Shared height for .btn/input/select so a button sitting next to a select in the same
     toolbar row lines up on both height and vertical-center (Phase 14). */
  --control-height: 2.25rem;
  --control-height-sm: 1.85rem;
}

/* ============================================================
   Base
   ============================================================ */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
}

a {
  color: var(--color-accent);
}

a.plain-link {
  color: inherit;
  text-decoration: none;
}

.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 1px;
}

.icon {
  flex-shrink: 0;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  width: auto;
  height: var(--control-height);
  padding: 0 0.9rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
  white-space: nowrap;
  box-sizing: border-box;
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
}

.btn-outline {
  background: var(--color-surface);
  border-color: var(--color-text-faint);
  color: var(--color-text);
}

.btn-outline:hover:not(:disabled) {
  background: var(--color-surface-muted);
}

.btn-danger {
  background: var(--color-danger);
  border-color: var(--color-danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: #8a2f25;
  border-color: #8a2f25;
}

.btn-sm {
  height: var(--control-height-sm);
  padding: 0 0.55rem;
  font-size: 0.75rem;
  gap: 0.3rem;
}

.btn-block {
  width: 100%;
}

/* Icon-only square button — row actions in dense tables/lists (edit/delete/view/copy). Always
   carries aria-label + title (Phase 14, scope.md §28 point 2); never ships as a silent icon. */
.btn-icon {
  width: var(--control-height);
  height: var(--control-height);
  padding: 0;
  flex-shrink: 0;
}

.btn-icon.btn-sm {
  width: var(--control-height-sm);
  height: var(--control-height-sm);
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6rem;
  height: 1.6rem;
  padding: 0;
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface);
  color: var(--color-text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.copy-btn:hover {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

.copyable-cell {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

/* ============================================================
   Layout: topbar / topnav / footer
   ============================================================ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-4);
  padding: 0.7rem var(--space-5);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.brand {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
}

.topnav {
  display: flex;
  gap: 0.25rem;
  font-size: 0.85rem;
  flex: 1;
  overflow-x: auto;
}

.topnav a {
  padding: 0.4rem 0.7rem;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  text-decoration: none;
  white-space: nowrap;
}

.topnav a:hover {
  color: var(--color-text);
  background: var(--color-surface-muted);
}

.topnav a.active {
  color: var(--color-accent);
  background: var(--color-accent-bg);
  font-weight: 600;
}

.logout-form {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

.signed-in-as {
  color: var(--color-text-muted);
  font-size: 0.85rem;
}

.admin-footer {
  padding: var(--space-4) var(--space-5);
  text-align: center;
  color: var(--color-text-faint);
  font-size: 0.75rem;
}

/* ============================================================
   Shared page structure
   ============================================================ */
.page {
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.page h1 {
  margin: 0;
  font-size: 1.35rem;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.run-status {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-left: auto;
}

/* ============================================================
   Cards / panels
   ============================================================ */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0.6rem var(--space-4);
  background: var(--color-surface-muted);
  border-bottom: 1px solid var(--color-border);
  font-size: 0.9rem;
}

.card-header h2 {
  margin: 0;
  font-size: 0.9rem;
}

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

/* ============================================================
   Forms / fields
   ============================================================ */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

.field input,
.field select,
input,
select {
  height: var(--control-height);
  padding: 0 0.6rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-text);
  box-sizing: border-box;
}

.field textarea,
textarea {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-text);
  box-sizing: border-box;
}

.field input[type='file'],
.field input[type='checkbox'] {
  height: auto;
}

.field.checkbox-field {
  flex-direction: row;
  align-items: center;
  gap: 0.4rem;
}

/* Inside a horizontal toolbar, a label stacked above its control (the .field default, correct
   for a standalone form) makes that whole block taller than a plain sibling button and throws
   off vertical alignment — Phase 14's "buttons and selects aren't aligned" fix. Label moves
   beside its control instead, so the control itself lines up with buttons. Deliberately NOT
   applied inside .inline-form (below): a label-beside-control field is wider than a stacked one,
   and .inline-form is also used inside narrow sidebar panels (e.g. Storage's 280px bucket
   panel) where that extra width pushed fields past the card edge. */
.toolbar > .field,
.page-header-actions > .field {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.inline-form {
  display: flex;
  align-items: flex-end;
  gap: var(--space-3);
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  flex-wrap: wrap;
}

.inline-form > .field {
  flex: 1 1 160px;
  min-width: 0;
}

.inline-form > .field input,
.inline-form > .field select {
  width: 100%;
}

/* ============================================================
   Page header
   ============================================================ */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.page-header h1 {
  margin: 0;
}

.page-header-text {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.page-description {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.85rem;
}

.page-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

/* ============================================================
   Tables
   ============================================================ */
.table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.85rem;
}

.table th,
.table td {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #eee;
  vertical-align: top;
}

.table th {
  background: var(--color-bg);
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.table-wrap {
  overflow-x: auto;
}

.actions-cell {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* ============================================================
   Badges
   ============================================================ */
.badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--color-surface-muted);
  color: var(--color-text-muted);
  font-weight: 500;
  white-space: nowrap;
}

.badge.status-active,
.badge.kind-publishable,
.badge.rls-on,
.badge.api-exposed {
  background: var(--color-success-bg);
  color: var(--color-success);
}

.badge.status-disabled,
.badge.kind-secret,
.badge.rls-off {
  background: var(--color-danger-bg);
  color: var(--color-danger);
}

.badge.status-invited {
  background: var(--color-info-bg);
  color: var(--color-info);
}

.badge.exposure-danger {
  background: var(--color-danger);
  color: #fff;
  font-weight: 600;
}

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

/* ============================================================
   Banners
   ============================================================ */
.banner {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.banner-warning {
  background: var(--color-warning-bg);
  border: 1px solid #e0c700;
  color: #6b5600;
}

.banner-danger {
  background: var(--color-danger-bg);
  color: var(--color-danger);
  white-space: pre-wrap;
}

.secret-banner {
  padding: 0.75rem 1rem;
  background: var(--color-warning-bg);
  border: 1px solid #e0c700;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.secret-banner-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  font-weight: 600;
}

.secret-banner-list {
  margin: 0;
  padding-left: 1.1rem;
  font-weight: normal;
  max-height: 200px;
  overflow-y: auto;
}

.secret-banner-close {
  background: transparent;
  border: 1px solid transparent;
  color: #6b5600;
}

.secret-banner-close:hover {
  background: rgba(0, 0, 0, 0.06);
}

.secret-banner-note {
  color: #6b5600;
  margin: 0.2rem 0 0.5rem;
}

.secret-banner-token-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.secret-banner-token-row code {
  font-family: var(--font-mono);
  white-space: pre-wrap;
  word-break: break-all;
  flex: 1;
}

/* ============================================================
   Pagination
   ============================================================ */
.pagination {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

/* ============================================================
   Auth / login page
   ============================================================ */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.auth-card {
  background: var(--color-surface);
  padding: 2.5rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 360px;
}

.auth-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
}

.subtitle {
  margin: 0 0 var(--space-5);
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.error {
  background: var(--color-danger-bg);
  color: var(--color-danger);
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin-bottom: var(--space-4);
}

.auth-card label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: var(--space-4);
}

.auth-card input {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
}

/* ============================================================
   Modal (Phase 15, scope.md §29) — generic confirm/detail dialog primitive. Every prior
   destructive admin action used a bare window.confirm(); this exists specifically for actions
   that need more than a yes/no (blocker lists, a typed-name-to-confirm field, read-only content).
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(0, 0, 0, 0.45);
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 520px;
  max-height: calc(100vh - var(--space-6));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal.modal-wide {
  max-width: 780px;
}

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

.modal-header h2 {
  margin: 0;
  font-size: 1rem;
}

.modal-close {
  background: transparent;
  border: 1px solid transparent;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.modal-close:hover {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

.modal-body {
  padding: var(--space-4);
  overflow-y: auto;
  font-size: 0.85rem;
}

.modal-body p {
  margin: 0 0 0.6rem;
}

.modal-body p:last-child {
  margin-bottom: 0;
}

/* ============================================================
   Toasts
   ============================================================ */
.toast-container {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 380px;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-text-faint);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: 0.85rem;
  color: var(--color-text);
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast-leaving {
  opacity: 0;
  transform: translateY(4px);
}

.toast-success {
  border-left-color: var(--color-success);
}

.toast-success .toast-icon {
  color: var(--color-success);
}

.toast-error {
  border-left-color: var(--color-danger);
}

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

.toast-info .toast-icon {
  color: var(--color-info);
}

.toast-icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  margin-top: 1px;
}

.toast-message {
  flex: 1;
  line-height: 1.4;
  word-break: break-word;
}

.toast-close {
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: var(--color-text-faint);
  cursor: pointer;
  padding: 2px;
  display: flex;
}

.toast-close:hover {
  color: var(--color-text);
}

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