:root {
  --color-primary: #1d4ed8;
  --color-primary-dark: #1e40af;
  --color-primary-light: #dbeafe;
  --color-accent: #f59e0b;
  --color-success: #059669;
  --color-danger: #dc2626;
  --color-warning: #d97706;
  --color-text: #111827;
  --color-muted: #6b7280;
  --color-border: #e5e7eb;
  --color-bg: #f9fafb;
  --color-white: #ffffff;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.05);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
  font-family: var(--font);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.6;
  min-height: 100vh;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

img { max-width: 100%; }

/* ── Layout ── */
.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 1rem;
}

.container--narrow { max-width: 640px; }

main.container, main { padding-top: 2rem; padding-bottom: 3rem; }

/* ── Header ── */
.site-header {
  background: var(--color-white);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

.site-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--color-primary);
  text-decoration: none;
  white-space: nowrap;
}

.site-brand svg { flex-shrink: 0; }

.site-nav {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex: 1;
}

.nav-link {
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius);
  color: var(--color-text);
  font-size: 0.9rem;
  white-space: nowrap;
  transition: background 0.15s;
}

.nav-link:hover { background: var(--color-primary-light); text-decoration: none; }
.nav-link.active { background: var(--color-primary-light); color: var(--color-primary); font-weight: 600; }

.user-menu {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.user-name {
  font-size: 0.875rem;
  color: var(--color-muted);
  white-space: nowrap;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  white-space: nowrap;
  background: transparent;
  font-family: var(--font);
  line-height: 1.4;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:hover:not(:disabled) { text-decoration: none; }

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

.btn-secondary {
  background: var(--color-white);
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn-secondary:hover:not(:disabled) { background: var(--color-bg); }

.btn-danger {
  background: var(--color-danger);
  color: var(--color-white);
  border-color: var(--color-danger);
}
.btn-danger:hover:not(:disabled) { background: #b91c1c; }

.btn-success {
  background: var(--color-success);
  color: var(--color-white);
  border-color: var(--color-success);
}

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

.btn-sm { padding: 0.25rem 0.625rem; font-size: 0.8rem; }
.btn-lg { padding: 0.75rem 1.5rem; font-size: 1rem; }
.btn-block { width: 100%; justify-content: center; }

/* ── Forms ── */
.form-group { margin-bottom: 1.25rem; }

.form-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text);
  margin-bottom: 0.375rem;
}

.form-label .required { color: var(--color-danger); margin-left: 0.125rem; }

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: var(--font);
  color: var(--color-text);
  background: var(--color-white);
  transition: border-color 0.15s, box-shadow 0.15s;
  line-height: 1.5;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(29,78,216,0.12);
}

.form-textarea { resize: vertical; min-height: 6rem; }

.form-hint {
  font-size: 0.8rem;
  color: var(--color-muted);
  margin-top: 0.25rem;
}

.form-error {
  font-size: 0.8rem;
  color: var(--color-danger);
  margin-top: 0.25rem;
}

.form-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.form-check input[type="checkbox"],
.form-check input[type="radio"] {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
  cursor: pointer;
  accent-color: var(--color-primary);
}

.form-check-label { font-size: 0.9rem; cursor: pointer; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-width: 480px) { .form-row { grid-template-columns: 1fr; } }

/* ── Cards ── */
.card {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

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

.card-header h2, .card-header h3 { font-size: 1rem; font-weight: 600; margin: 0; }

.card-body { padding: 1.25rem; }

.card-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ── Tables ── */
.table-wrapper { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.table th {
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  padding: 0.625rem 0.75rem;
  border-bottom: 2px solid var(--color-border);
  background: var(--color-bg);
  white-space: nowrap;
}

.table td {
  padding: 0.75rem;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.table tbody tr:hover { background: var(--color-bg); }
.table tbody tr:last-child td { border-bottom: none; }

/* ── Badges ── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
}

.badge-primary { background: var(--color-primary-light); color: var(--color-primary-dark); }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-neutral { background: var(--color-border); color: var(--color-muted); }

/* ── Alerts ── */
.alert {
  padding: 0.875rem 1rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.alert-success { background: #d1fae5; border-color: #6ee7b7; color: #065f46; }
.alert-error { background: #fee2e2; border-color: #fca5a5; color: #991b1b; }
.alert-info { background: var(--color-primary-light); border-color: #93c5fd; color: var(--color-primary-dark); }
.alert-warning { background: #fef3c7; border-color: #fcd34d; color: #92400e; }

/* ── Page titles ── */
.page-header {
  margin-bottom: 1.5rem;
}

.page-header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.2;
}

.page-header p {
  margin-top: 0.375rem;
  color: var(--color-muted);
}

.page-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}

/* ── Field type badge colors ── */
.field-type-multiple_choice { background: #ede9fe; color: #5b21b6; }
.field-type-checkbox { background: #fce7f3; color: #9d174d; }
.field-type-short_text { background: #dcfce7; color: #166534; }
.field-type-paragraph { background: #ffedd5; color: #9a3412; }

/* ── Loading states ── */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  color: var(--color-muted);
  font-size: 0.9rem;
  gap: 0.5rem;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Empty states ── */
.empty {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--color-muted);
}

.empty h3 { font-size: 1rem; margin-bottom: 0.5rem; color: var(--color-text); }
.empty p { font-size: 0.875rem; margin-bottom: 1rem; }

/* ── QR Code display ── */
.qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem;
}

.qr-container canvas { border-radius: var(--radius); border: 4px solid var(--color-white); }
.qr-container p { font-size: 0.8rem; color: var(--color-muted); text-align: center; }

/* ── Field list (form builder) ── */
.field-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-white);
  transition: background 0.1s;
}

.field-item:last-child { border-bottom: none; }
.field-item:hover { background: var(--color-bg); }

.field-item-content { flex: 1; min-width: 0; }
.field-item-label { font-weight: 500; font-size: 0.9rem; }
.field-item-meta { font-size: 0.8rem; color: var(--color-muted); margin-top: 0.125rem; }
.field-item-actions { display: flex; gap: 0.25rem; flex-shrink: 0; }

/* ── Child/roster info ── */
.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.75rem;
}

.info-item { }
.info-item dt {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  margin-bottom: 0.125rem;
}
.info-item dd { font-size: 0.9rem; }

/* ── Divider ── */
.divider {
  height: 1px;
  background: var(--color-border);
  margin: 1.5rem 0;
}

/* ── Section heading ── */
.section-heading {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  margin-bottom: 0.75rem;
}

/* ── Form options list (for multiple_choice/checkbox builder) ── */
.options-list { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.5rem; }

.option-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.option-item .form-input { flex: 1; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .site-nav { display: none; }
  .user-name { display: none; }
  .page-header h1 { font-size: 1.4rem; }
}

/* ── Print styles ── */
@media print {
  .site-header,
  .btn,
  .no-print,
  app-header { display: none !important; }

  body { background: white; }

  .card { box-shadow: none; border: 1px solid #ccc; }

  .table th { background: #f0f0f0; }

  .table tbody tr:hover { background: none; }

  @page {
    margin: 1cm;
    size: landscape;
  }
}
