/* ── Base ────────────────────────────────────────────── */
* { box-sizing: border-box; }
:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
}
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 16px 0; }
h2 { font-size: 17px; margin: 24px 0 8px; }
.muted { color: var(--muted); font-weight: normal; }

/* ── Topbar ──────────────────────────────────────────── */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 24px; background: var(--card); border-bottom: 1px solid var(--border);
}
.topbar .logo { font-weight: 600; color: var(--text); }
.topbar nav { display: flex; align-items: center; gap: 16px; }
.topbar .nav-link { color: var(--text); padding: 4px 8px; border-radius: 4px; font-weight: 500; font-size: 13px; }
.topbar .nav-link:hover { background: #f3f4f6; text-decoration: none; }
.user .role { color: var(--muted); font-size: 12px; }
.inline { display: inline; }
.link { background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; font: inherit; }
.link:hover { text-decoration: underline; }

/* ── Layout ──────────────────────────────────────────── */
main { max-width: 1100px; margin: 24px auto; padding: 0 24px; }

/* ── Login ───────────────────────────────────────────── */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  width: 360px; background: var(--card); padding: 32px; border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
}
.login-card h1 { text-align: center; margin: 0 0 20px; }
.login-card label { display: block; margin-bottom: 12px; }
.login-card label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.login-card input[type=text],
.login-card input[type=password] {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px;
}
.login-card button {
  width: 100%; margin-top: 12px; padding: 10px; background: var(--accent); color: white;
  border: none; border-radius: 6px; font-size: 14px; cursor: pointer;
}
.login-card button:hover { background: #1d4ed8; }
.error { color: var(--danger); margin: 0 0 12px; }

/* ── Dashboard ───────────────────────────────────────── */
.city-list { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
.city-tile {
  display: block; background: var(--card); padding: 24px; border-radius: 10px;
  border: 1px solid var(--border); transition: box-shadow 0.15s, transform 0.15s;
}
.city-tile:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.08); transform: translateY(-1px); text-decoration: none; }
.city-tile strong { display: block; font-size: 16px; }
.city-tile span { color: var(--muted); font-size: 12px; }

/* ── Breadcrumb ──────────────────────────────────────── */
.breadcrumb { color: var(--muted); margin: 8px 0 16px; }

/* ── Instructions block ──────────────────────────────── */
.instructions { background: #fffbeb; border: 1px solid #fde68a; border-radius: 6px; padding: 12px 16px; margin: 12px 0 20px; }
.instructions summary { cursor: pointer; padding: 4px 0; color: #92400e; font-size: 14px; }
.instructions summary:hover { color: #78350f; }
.instructions ol { margin: 12px 0 4px; padding-left: 20px; }
.instructions ol > li { margin-bottom: 12px; line-height: 1.55; }
.instructions ul { margin: 6px 0 0; padding-left: 20px; }
.instructions code { background: #fef3c7; padding: 1px 6px; border-radius: 3px; font-size: 13px; }
.instructions a { color: #92400e; font-weight: 500; }

/* ── Create form ─────────────────────────────────────── */
.create-block { background: var(--card); padding: 16px 20px; border-radius: 8px; border: 1px solid var(--border); margin-bottom: 24px; }
.create-form label { display: block; margin: 8px 0; }
.create-form label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.create-form input { width: 100%; max-width: 320px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; }
.create-form button { margin-top: 8px; padding: 10px 20px; background: var(--accent); color: white; border: none; border-radius: 6px; cursor: pointer; }
.create-form button:hover { background: #1d4ed8; }

/* ── Tables ──────────────────────────────────────────── */
table.subs, table.steps {
  width: 100%; border-collapse: collapse; background: var(--card);
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
}
table.subs th, table.subs td,
table.steps th, table.steps td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table.subs th, table.steps th { background: #f9fafb; font-weight: 600; font-size: 12px; color: var(--muted); }
table.steps pre { margin: 0; font: 12px/1.4 ui-monospace, monospace; white-space: pre-wrap; word-break: break-word; max-width: 500px; }

/* ── Spinner ─────────────────────────────────────────── */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid #e5e7eb; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
  vertical-align: middle; margin-left: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Status badges ───────────────────────────────────── */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 500;
  background: #e5e7eb; color: var(--text);
}
.badge.draft   { background: #e0e7ff; color: #3730a3; }
.badge.creating{ background: #fef3c7; color: #92400e; }
.badge.active  { background: #dcfce7; color: #166534; }
.badge.error   { background: #fee2e2; color: #991b1b; }
.badge.deleted { background: #f3f4f6; color: #6b7280; }
.badge.pending { background: #f3f4f6; color: #6b7280; }
.badge.ok      { background: #dcfce7; color: #166534; }
.badge.skipped { background: #f3f4f6; color: #6b7280; }

/* ── KV grid ─────────────────────────────────────────── */
dl.kv { display: grid; grid-template-columns: 200px 1fr; gap: 8px 16px; background: var(--card); padding: 16px 20px; border: 1px solid var(--border); border-radius: 8px; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }
dl.kv dd.error { color: var(--danger); }

/* ── Action buttons ──────────────────────────────────── */
.actions { margin-top: 24px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.actions form { margin: 0; }
.actions button { padding: 8px 16px; border: 1px solid var(--border); border-radius: 6px; background: white; cursor: pointer; }
.actions button.danger { color: var(--danger); border-color: var(--danger); }
.actions button:hover { background: #f9fafb; }
.hint { color: var(--muted); font-size: 12px; margin: 0; }
.hint.busy { color: var(--warn); font-size: 13px; }
.warn { color: var(--warn); background: #fffbeb; border: 1px solid #fde68a; border-radius: 6px; padding: 10px 14px; }

/* ── Direct dashboard layout ─────────────────────────────── */
.direct-layout { display: grid; grid-template-columns: 220px 1fr; gap: 24px; margin: 0 -24px; }
.direct-sidebar {
  background: var(--card); border-right: 1px solid var(--border);
  padding: 20px 0; min-height: calc(100vh - 80px);
  position: sticky; top: 0; align-self: start;
}
.direct-sidebar .sidebar-header { padding: 0 20px 16px; border-bottom: 1px solid var(--border); }
.direct-sidebar h3 { margin: 0 0 4px; font-size: 14px; }
.sidebar-nav { padding: 12px 0; display: flex; flex-direction: column; }
.sidebar-nav a {
  padding: 8px 20px; color: var(--text); display: flex; justify-content: space-between;
  align-items: center; font-size: 13px;
}
.sidebar-nav a:hover { background: #f3f4f6; text-decoration: none; }
.sidebar-nav a.active { background: #eff6ff; color: var(--accent); border-left: 3px solid var(--accent); padding-left: 17px; font-weight: 500; }
.sidebar-nav .count { background: #e5e7eb; color: var(--text); border-radius: 10px; font-size: 11px; padding: 1px 8px; }
.sidebar-nav a.muted { color: var(--muted); }
.sidebar-footer { padding: 12px 20px; border-top: 1px solid var(--border); font-size: 11px; }
.sidebar-footer p { margin: 4px 0; }

.direct-main { padding: 0 24px 40px; }

.direct-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 0 24px; border-bottom: 1px solid var(--border); margin-bottom: 24px;
  flex-wrap: wrap; gap: 16px;
}
.direct-toolbar h1 { margin: 0 0 2px; font-size: 20px; }
.direct-toolbar .muted { font-size: 13px; }
.toolbar-actions { display: flex; gap: 8px; align-items: center; }
.toolbar-actions select, .toolbar-actions input { padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; font-size: 13px; }
.toolbar-actions input[type=search] { min-width: 240px; }
.btn-primary { padding: 8px 16px; background: var(--accent); color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 500; }
.btn-primary:hover:not([disabled]) { background: #1d4ed8; }
.btn-primary[disabled] { opacity: 0.5; cursor: not-allowed; }

.section { margin-bottom: 36px; }
.section h2 { font-size: 16px; margin-bottom: 12px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.section-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.section-header h2 { margin: 0; border: none; padding: 0; }
.section-header button { padding: 7px 12px; border: 1px solid var(--border); border-radius: 6px; background: white; font-size: 13px; cursor: pointer; }
.section-header button[disabled] { opacity: 0.5; cursor: not-allowed; }

/* KPI grid */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.kpi { background: var(--card); border: 1px solid var(--border); padding: 14px 16px; border-radius: 8px; display: flex; flex-direction: column; gap: 4px; }
.kpi-value { font-size: 22px; font-weight: 600; line-height: 1.1; }
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi-trend { font-size: 11px; color: var(--muted); }
.kpi-trend.up { color: var(--ok); }
.kpi-trend.down { color: var(--danger); }

/* Filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters select, .filters input, .filters button {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; font-size: 13px;
}

/* Bulk actions */
.bulk-bar {
  background: #f9fafb; border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 12px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
  margin: 12px 0;
}
.bulk-bar > .muted { margin-right: 12px; font-size: 13px; }
.bulk-bar button { padding: 5px 10px; border: 1px solid var(--border); border-radius: 5px; background: white; font-size: 12px; cursor: pointer; }
.bulk-bar button[disabled] { opacity: 0.5; cursor: not-allowed; }

/* Data tables */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--card); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.data-table th, .data-table td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.data-table th { background: #f9fafb; font-weight: 600; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.data-table tbody tr:hover { background: #fafbfc; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table.compact th, .data-table.compact td { padding: 6px 10px; font-size: 12px; }

/* Cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.card h3 { margin: 0 0 12px; font-size: 14px; }
.template-card { display: flex; flex-direction: column; gap: 8px; }
.template-card button { align-self: flex-start; padding: 6px 12px; border: 1px solid var(--border); border-radius: 5px; background: white; cursor: pointer; font-size: 13px; margin-top: 8px; }
.template-card button[disabled] { opacity: 0.5; cursor: not-allowed; }

/* Auto-rules list */
.rules { list-style: none; padding: 0; margin: 0 0 12px; }
.rules li { padding: 10px 0; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; }
.rules li:last-child { border-bottom: none; }
.rules li > strong { font-size: 13px; }
.rules li > .muted { grid-column: 1 / 2; font-size: 12px; }
.rules li > .link { grid-column: 2 / 3; grid-row: 1 / 3; align-self: center; }

/* Chart */
.chart { width: 100%; height: auto; background: #f9fafb; border-radius: 4px; }

/* API debug panel */
.direct-controls { background: #f9fafb; padding: 12px 16px; border: 1px solid var(--border); border-radius: 6px; display: flex; gap: 12px; align-items: end; flex-wrap: wrap; margin: 16px 0; }
.direct-controls label { display: flex; flex-direction: column; gap: 4px; min-width: 200px; flex: 1; }
.direct-controls label span { font-size: 11px; color: var(--muted); }
.direct-controls input, .direct-controls select { padding: 6px 10px; border: 1px solid var(--border); border-radius: 5px; font: inherit; font-size: 13px; }
.direct-controls button { padding: 7px 14px; background: var(--accent); color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 13px; }
.api-panel { background: var(--card); border: 1px solid var(--border); border-radius: 6px; padding: 12px 16px; margin-bottom: 10px; }
.api-panel h3 { display: flex; align-items: center; gap: 8px; margin: 0 0 8px; font-size: 13px; }
.api-raw { background: #f9fafb; padding: 8px; border-radius: 4px; font: 11px/1.4 ui-monospace, monospace; overflow-x: auto; max-height: 200px; margin: 4px 0; }
.api-raw.error { background: #fef2f2; color: #991b1b; }

button.link[disabled] { color: var(--muted); cursor: not-allowed; }

/* ── Error card ──────────────────────────────────────── */
.error-card { max-width: 600px; margin: 80px auto; background: var(--card); padding: 32px; border-radius: 12px; border: 1px solid var(--border); }
.error-card pre { background: #f9fafb; padding: 12px; border-radius: 6px; overflow-x: auto; }
