:root {
  --bg: #0c0f14;
  --bg-elevated: #121820;
  --surface: #171d28;
  --surface2: #1f2736;
  --surface3: #283244;
  --border: #2e3a4d;
  --border-light: #3d4d66;
  --text: #f0f4fa;
  --muted: #8fa3bf;
  --primary: #4f8cff;
  --primary-hover: #3a7aef;
  --primary-dim: rgba(79, 140, 255, 0.15);
  --danger: #f87171;
  --danger-dim: rgba(248, 113, 113, 0.12);
  --success: #4ade80;
  --success-dim: rgba(74, 222, 128, 0.12);
  --warning: #fbbf24;
  --warning-dim: rgba(251, 191, 36, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --font: 'DM Sans', system-ui, -apple-system, sans-serif;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(79, 140, 255, 0.12), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(79, 140, 255, 0.06), transparent);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
.pad { padding: 2rem 1rem !important; }

.screen { min-height: 100vh; }

/* ─── Agency picker ───────────────────────────────────────────────────────── */

.agency-layout {
  max-width: 960px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 3rem;
}

.agency-hero {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  margin-bottom: 2rem;
}

.brand-mark {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), #7c5cff);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
  flex-shrink: 0;
  box-shadow: 0 8px 24px rgba(79, 140, 255, 0.35);
}

.agency-hero h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.agency-panel {
  padding: 1.5rem;
}

.search-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.25rem;
}

.search-row input {
  flex: 1;
}

.count-pill {
  font-size: 0.8rem;
  white-space: nowrap;
  padding: 0.35rem 0.65rem;
  background: var(--surface2);
  border-radius: 999px;
  border: 1px solid var(--border);
}

.agency-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.75rem;
  max-height: min(58vh, 520px);
  overflow-y: auto;
  padding-right: 0.25rem;
}

.agency-grid::-webkit-scrollbar { width: 6px; }
.agency-grid::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.agency-card {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.85rem 1rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: inherit;
  font: inherit;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.agency-card:hover {
  border-color: var(--primary);
  background: var(--surface3);
  transform: translateY(-1px);
}

.agency-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.agency-card img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  border-radius: 8px;
  background: #fff;
  flex-shrink: 0;
}

.agency-card .logo-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--primary-dim);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.agency-card-body { min-width: 0; flex: 1; }

.agency-card-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.agency-card-meta {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.agency-card.skeleton {
  min-height: 64px;
  cursor: default;
  animation: pulse 1.4s ease-in-out infinite;
  background: linear-gradient(90deg, var(--surface2) 25%, var(--surface3) 50%, var(--surface2) 75%);
  background-size: 200% 100%;
}

@keyframes pulse {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.empty-state {
  text-align: center;
  padding: 2rem;
  color: var(--muted);
}

.error-banner {
  background: var(--danger-dim);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  margin-top: 1rem;
  font-size: 0.9rem;
}

.manual-entry {
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.manual-entry summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  user-select: none;
}

.manual-row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.manual-row input { flex: 1; }

.all-agencies-btn {
  width: 100%;
  margin-top: 1rem;
}

/* ─── Dashboard ───────────────────────────────────────────────────────────── */

.dash-header {
  border-bottom: 1px solid var(--border);
  background: rgba(18, 24, 32, 0.85);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.dash-header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.agency-identity {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: 0;
}

.agency-logo,
.agency-logo-fallback {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  flex-shrink: 0;
}

.agency-logo {
  object-fit: contain;
  background: #fff;
}

.agency-logo-fallback {
  background: var(--primary-dim);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
}

.eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.15rem;
}

.dash-header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.agency-meta {
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.type-chip {
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border-radius: 4px;
  background: var(--surface3);
  border: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.dash-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.dash-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

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

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.stat-label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.35rem;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

/* ─── Shared components ───────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

label,
.toolbar-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 500;
}

.toolbar-field.grow { flex: 1; min-width: 160px; }

input, select, textarea, button {
  font-family: inherit;
}

input, select, textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.6rem 0.75rem;
  font-size: 0.92rem;
  max-width: 100%;
  transition: border-color 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-dim);
}

input[type="search"] {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%238fa3bf' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85zm-5.242 1.1a5 5 0 1 1 0-10 5 5 0 0 1 0 10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 0.75rem center;
  padding-left: 2.25rem;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.55rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn:hover { background: var(--surface3); border-color: var(--border-light); }
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.sm { padding: 0.3rem 0.55rem; font-size: 0.8rem; }
.btn-icon { font-size: 1.1rem; line-height: 1; }

.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  justify-content: center;
  font-size: 1.35rem;
  line-height: 1;
  border-radius: var(--radius-sm);
}

.toolbar {
  display: flex;
  align-items: end;
  gap: 1rem;
  flex-wrap: wrap;
}

.table-card { padding: 0; overflow: hidden; }

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

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

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

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  background: var(--surface2);
}

tbody tr {
  transition: background 0.1s;
}

tbody tr:hover { background: rgba(255, 255, 255, 0.02); }

tbody tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge.once { background: var(--primary-dim); color: #93c5fd; }
.badge.delayed { background: var(--warning-dim); color: var(--warning); }
.badge.scheduled { background: var(--success-dim); color: var(--success); }
.badge.unknown { background: var(--surface3); color: var(--muted); }
.badge.waiting, .badge.delayed-state { background: var(--warning-dim); color: var(--warning); }
.badge.active { background: var(--primary-dim); color: #93c5fd; }
.badge.completed { background: var(--success-dim); color: var(--success); }
.badge.failed { background: var(--danger-dim); color: var(--danger); }
.badge.pending { background: var(--surface3); color: var(--muted); }
.badge.complete { background: var(--success-dim); color: var(--success); }
.badge.manual { background: var(--surface3); color: var(--muted); }

.actions {
  display: flex;
  gap: 0.35rem;
  justify-content: flex-end;
}

/* ─── View tabs + calendar ────────────────────────────────────────────────── */

.view-tabs {
  display: flex;
  gap: 0.35rem;
}

.view-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: var(--radius-sm);
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

.view-tab:hover { color: var(--text); border-color: var(--border-light); }

.view-tab.active {
  background: var(--primary-dim);
  color: #93c5fd;
  border-color: var(--primary);
}

.calendar-toolbar {
  align-items: center;
  justify-content: flex-start;
}

.calendar-range {
  font-size: 1.05rem;
  font-weight: 600;
  flex: 1;
  text-align: center;
}

.calendar-card { padding: 1rem; }

.week-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.6rem;
}

@media (max-width: 800px) {
  .week-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}

.week-day {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.week-day.is-today { border-color: var(--primary); }

.week-day-head {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.week-day-head .day-num { color: var(--text); font-weight: 700; }

.month-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}

.month-weekdays span {
  text-align: center;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: minmax(90px, auto);
  gap: 0.6rem;
}

@media (max-width: 800px) {
  .month-grid { grid-template-columns: repeat(7, minmax(70px, 1fr)); grid-auto-rows: minmax(70px, auto); }
}

.month-day {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

.month-day.is-today { border-color: var(--primary); }
.month-day.is-outside { opacity: 0.4; }

.month-day-num {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text);
}

.occ-chip {
  display: flex;
  align-items: center;
  width: 100%;
  border-radius: 6px;
  padding: 0.15rem 0.15rem 0.15rem 0.4rem;
  font-size: 0.72rem;
  font-weight: 600;
  gap: 0.15rem;
}

.occ-chip.delayed { background: var(--warning-dim); color: var(--warning); }
.occ-chip.scheduled { background: var(--success-dim); color: var(--success); }

.occ-chip-label {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0.1rem 0;
}

.occ-icon-btn {
  flex-shrink: 0;
  border: none;
  background: none;
  color: inherit;
  opacity: 0.65;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 0.1rem 0.25rem;
  border-radius: 4px;
}

.occ-icon-btn:hover { opacity: 1; background: rgba(0, 0, 0, 0.15); }
.occ-icon-btn.danger:hover { color: var(--danger); }

.occ-more {
  font-size: 0.72rem;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  padding: 0.1rem 0.4rem;
}

.occ-more:hover { color: var(--text); }

.day-empty {
  font-size: 0.78rem;
  color: var(--muted);
}

.error { color: var(--danger); font-size: 0.88rem; }

/* ─── Modals ──────────────────────────────────────────────────────────────── */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 2rem;
  max-width: calc(100vw - 3rem);
  width: 640px;
  margin: auto;
  overflow: hidden;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

#detail-modal { max-width: 760px; min-width: 500px; }

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0 1rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 {
  font-size: 1.15rem;
  font-weight: 600;
}

#report-form,
#detail-modal > :not(.modal-head) {
  padding-left: 0;
  padding-right: 0;
}

#report-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-bottom: 0;
  padding-top: 1rem;
}

#detail-modal .detail-json { margin: 1rem 0; }
#detail-modal .section-title { padding: 0; font-size: 0.95rem; }
#detail-modal .history-toolbar { padding: 0; }
#detail-modal .history-list { padding: 0; }

.grid-2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 0.75rem;
}

.grid-2 > label { min-width: 0; }

@media (max-width: 600px) {
  .grid-2 { grid-template-columns: 1fr; }
  .dash-header-inner { flex-direction: column; align-items: flex-start; }
}

.type-fields {
  padding: 1rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.field-hint {
  font-size: 0.82rem;
  color: var(--muted);
  margin: -0.25rem 0 0;
}

.cron-hint {
  font-size: 0.78rem;
  color: var(--primary);
  font-style: italic;
  min-height: 1.1em;
  margin-top: 0.1rem;
}

.cron-hint.error {
  color: var(--danger);
  font-style: normal;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding-top: 0.5rem;
}

.detail-json {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem;
  overflow: auto;
  max-height: 260px;
  font-size: 0.78rem;
  line-height: 1.45;
  font-family: ui-monospace, 'Cascadia Code', monospace;
}

.history-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0.75rem 0;
}

.history-toolbar input { width: 4rem; text-align: center; }

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 300px;
  overflow-y: auto;
}

.history-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
  font-size: 0.85rem;
}

/* ─── Toast ───────────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem 1.15rem;
  max-width: 380px;
  box-shadow: var(--shadow);
  z-index: 1000;
  font-size: 0.9rem;
  animation: slideIn 0.25s ease;
}

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

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