* { box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; background: #f8f9fb; color: #222; }
main { max-width: 1100px; margin: 0 auto; padding: 2rem 1rem; }

.topnav { display: flex; gap: 1rem; padding: 0.75rem 1.5rem; background: #222; color: #fff; align-items: center; }
.topnav a { color: #fff; text-decoration: none; }
.topnav a.brand { font-weight: 600; margin-right: 1rem; }
.topnav .spacer { flex: 1; }
.user-menu { position: relative; }
.user-menu summary { cursor: pointer; list-style: none; }
.user-menu[open] .user-menu-body { position: absolute; right: 0; top: 100%; background: #fff; color: #222; padding: 0.75rem; border: 1px solid #ccc; border-radius: 4px; min-width: 180px; box-shadow: 0 4px 12px rgba(0,0,0,0.12); }
.user-menu .user-menu-sub { font-size: 0.8rem; color: #888; margin-bottom: 0.5rem; font-family: monospace; }
.muted { color: #888; font-weight: normal; font-size: 0.9em; }

.login-box { max-width: 400px; margin: 4rem auto; background: #fff; padding: 2rem; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
.login-box h1 { margin-top: 0; }
.login-box label { display: block; margin-bottom: 1rem; }
.login-box input { width: 100%; padding: 0.5rem; font-size: 1rem; border: 1px solid #ccc; border-radius: 4px; }
.login-box button { width: 100%; padding: 0.75rem; font-size: 1rem; background: #0066cc; color: #fff; border: 0; border-radius: 4px; cursor: pointer; }
.form-error { color: #c00; background: #fee; padding: 0.5rem; border-radius: 4px; margin-bottom: 1rem; }

.flash { padding: 0.75rem 1rem; border-radius: 4px; margin-bottom: 1rem; }
.flash-info { background: #e3f2fd; color: #0d47a1; }
.flash-error { background: #ffebee; color: #b71c1c; }

.library-header { display: flex; gap: 1rem; align-items: center; margin-bottom: 1.5rem; }
.library-header h1 { margin: 0; flex: 1; }
.library-header .search { display: flex; gap: 0.25rem; }
.library-header .search input { padding: 0.5rem; border: 1px solid #ccc; border-radius: 4px; }
.library-header .search button { padding: 0.5rem 0.75rem; border: 1px solid #ccc; background: #fff; border-radius: 4px; cursor: pointer; }
.btn { padding: 0.5rem 1rem; border-radius: 4px; text-decoration: none; display: inline-block; cursor: pointer; border: 0; font-size: 0.95rem; font-weight: 500; line-height: 1.25; transition: opacity 0.15s; font-family: inherit; }
.btn-primary { background: #0066cc; color: #fff; }
.btn-secondary { background: #fff; color: #0066cc; border: 1px solid #0066cc; }
.btn-danger { background: #c00; color: #fff; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn:hover:not([disabled]) { opacity: 0.88; }

.query-list { display: grid; gap: 1rem; }
.query-card { background: #fff; padding: 1rem 1.25rem; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.query-card h3 { margin: 0 0 0.25rem; }
.query-card h3 a { color: #0066cc; text-decoration: none; }
.query-card .meta { font-size: 0.85rem; color: #666; margin-bottom: 0.5rem; }
.query-card .desc { margin: 0.5rem 0; color: #444; }
.query-card .tags { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.tag { background: #e3f2fd; color: #0d47a1; padding: 0.2rem 0.5rem; border-radius: 10px; font-size: 0.8rem; }

.pagination { margin-top: 2rem; text-align: center; }
.pagination a { color: #0066cc; text-decoration: none; margin: 0 0.5rem; }
.pagination span { margin: 0 1rem; color: #666; }
.filters { color: #666; margin-bottom: 1rem; }
.empty { color: #999; text-align: center; padding: 2rem; }

.detail-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.detail-header h1 { margin: 0; }
.detail .actions { display: flex; gap: 0.5rem; }
.meta { color: #666; font-size: 0.9rem; margin-bottom: 0.5rem; }
.description { margin: 1rem 0; line-height: 1.6; }

.sql-actions { display: flex; gap: 0.5rem; align-items: center; margin: 1.5rem 0 0.75rem; }

.composer > * + * { margin-top: 0.75rem; }
.composer-actions { display: flex; gap: 0.5rem; align-items: center; margin-top: 1rem; }
.post-run-actions { display: flex; gap: 0.5rem; margin-top: 1rem; }

#sql-editor { width: 100%; min-height: 200px; font-family: monospace; }
.cm-editor { border: 1px solid #ccc; border-radius: 4px; min-height: 200px; }

#run-result { margin-top: 1.5rem; }
.htmx-indicator { display: none; color: #666; }
.htmx-request .htmx-indicator { display: inline; }

/* Run button loading state — button stays clickable-looking, shows spinner + "Running…" */
.btn-run { position: relative; min-width: 6.5rem; display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem; }
.btn-run .btn-label-idle { display: inline-flex; align-items: center; gap: 0.35rem; }
.btn-run .btn-label-busy { display: none; align-items: center; gap: 0.4rem; }
.btn-run.htmx-request .btn-label-idle { display: none; }
.btn-run.htmx-request .btn-label-busy { display: inline-flex; }
.btn-run.htmx-request[disabled] { opacity: 0.9; cursor: wait; }
.btn-run .spinner {
  display: inline-block;
  width: 0.9em; height: 0.9em;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: btn-spin 0.7s linear infinite;
  vertical-align: -2px;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }

/* Fade the previous result while a new run is in-flight so user clearly sees activity. */
.htmx-request.run-result,
#run-result.htmx-request { opacity: 0.4; transition: opacity 0.15s ease; }

.run-meta { display: flex; gap: 0.75rem; align-items: center; margin-bottom: 0.75rem; color: #555; flex-wrap: wrap; font-size: 0.9rem; }
.run-meta .run-db { font-family: monospace; font-size: 0.85rem; color: #0d47a1; background: #e3f2fd; padding: 0.2rem 0.55rem; border-radius: 4px; }
.run-meta .run-sep { color: #bbb; }
.run-error { background: #ffebee; color: #b71c1c; padding: 1rem; border-radius: 4px; font-family: monospace; white-space: pre-wrap; margin-top: 0.5rem; }
.run-table-wrap { overflow-x: auto; max-height: 500px; overflow-y: auto; border: 1px solid #e0e0e0; border-radius: 4px; background: #fff; }
.run-table { width: 100%; border-collapse: collapse; font-family: monospace; font-size: 0.9rem; }
.run-table th, .run-table td { border: 1px solid #e0e0e0; padding: 0.4rem 0.6rem; text-align: left; white-space: nowrap; }
.run-table th { background: #f0f0f0; position: sticky; top: 0; z-index: 2; box-shadow: 0 1px 0 #d0d0d0; }
.run-result-set { margin-bottom: 1.25rem; }
.run-result-set:last-child { margin-bottom: 0; }
.run-set-header { font-size: 0.85rem; color: #555; font-weight: 600; margin-bottom: 0.35rem; padding: 0.3rem 0.55rem; background: #f5f5f5; border-radius: 4px; display: inline-block; }

.form-page { max-width: 800px; }
.form-page label { display: block; margin-bottom: 1rem; font-weight: 600; }
.form-page input, .form-page textarea { width: 100%; padding: 0.5rem; font-size: 1rem; border: 1px solid #ccc; border-radius: 4px; margin-top: 0.25rem; font-weight: normal; font-family: inherit; }
.form-page textarea { font-family: monospace; }
.form-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1.5rem; }

/* Phase F: page title in brand */
.page-title-nav { opacity: 0.6; font-weight: normal; margin-left: 6px; }
.topnav .brand strong { margin-right: 2px; }

/* Save-to-Library modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal {
  background: #fff; border-radius: 8px; padding: 1.5rem 1.75rem;
  width: 100%; max-width: 520px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  max-height: calc(100vh - 2rem); overflow-y: auto;
}
.modal h3 { margin: 0 0 1rem; }
.modal label { display: block; margin-bottom: 1rem; font-weight: 600; font-size: 0.95rem; }
.modal input[type="text"],
.modal input:not([type]),
.modal textarea {
  display: block; width: 100%; margin-top: 0.3rem;
  padding: 0.5rem 0.6rem; font-size: 0.95rem; font-weight: normal;
  border: 1px solid #ccc; border-radius: 4px;
  font-family: inherit; box-sizing: border-box;
}
.modal textarea { font-family: monospace; resize: vertical; }
.modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1.25rem; }

/* New Query layout — editor left, schema right (w3schools-style) */
.new-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 1.25rem;
  align-items: start;
}
.new-layout .composer { min-width: 0; }
.new-layout .schema-sidebar {
  position: sticky;
  top: 1rem;
  align-self: start;
  font-size: 0.9rem;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
}
.schema-own {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 1rem;
}
.schema-own .schema-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}
.schema-own .schema-header code {
  background: #e3f2fd;
  color: #0d47a1;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-size: 0.85rem;
}
.schema-own-tables { width: 100%; border-collapse: collapse; }
.schema-own-tables th {
  text-align: left;
  font-weight: 600;
  border-bottom: 1px solid #e0e0e0;
  padding: 0.3rem 0.25rem;
  font-size: 0.85rem;
  color: #555;
}
.schema-own-tables th.num { text-align: right; }
.schema-own-tables td {
  padding: 0.3rem 0.25rem;
  border-bottom: 1px solid #f0f0f0;
}
.schema-own-tables td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: #666;
  font-family: monospace;
  font-size: 0.85rem;
}
.schema-own-tables tr:last-child td { border-bottom: 0; }
.table-link {
  background: none;
  border: 0;
  color: #0066cc;
  cursor: pointer;
  padding: 0;
  font: inherit;
  text-align: left;
}
.table-link:hover { text-decoration: underline; }
.schema-others {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 0.75rem;
}
.schema-others strong { display: block; margin-bottom: 0.5rem; font-size: 0.9rem; }
.btn-tiny {
  background: none;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 0.1rem 0.35rem;
  font-size: 0.8rem;
  cursor: pointer;
}
.schema-own .schema-header .btn-tiny { margin-left: auto; }
.btn-tiny:hover { background: #f0f0f0; }

@media (max-width: 900px) {
  .new-layout { grid-template-columns: 1fr; }
  .new-layout .schema-sidebar { position: static; max-height: none; }
}

/* Tutorial page */
.tutorial-layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: 1.5rem;
  align-items: start;
}
.tut-nav {
  position: sticky;
  top: 1rem;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  font-size: 0.9rem;
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 1rem;
}
.tut-nav h2 { margin: 0 0 0.75rem; font-size: 1rem; }
.tut-nav .tut-cat { margin-bottom: 0.75rem; }
.tut-nav .tut-cat h3 {
  margin: 0.5rem 0 0.25rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: #888;
  letter-spacing: 0.05em;
}
.tut-nav ul { list-style: none; margin: 0; padding: 0; }
.tut-nav li { margin: 0; }
.tut-nav a {
  display: block;
  color: #0066cc;
  text-decoration: none;
  padding: 0.25rem 0.5rem;
  border-radius: 3px;
  font-size: 0.9rem;
}
.tut-nav a:hover { background: #f0f7ff; }
.tut-nav a.active {
  background: #0066cc;
  color: #fff;
  font-weight: 600;
}
.tut-content {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 1.5rem 2rem;
  line-height: 1.65;
  max-width: 820px;
}
.tut-content h1 {
  margin-top: 0;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #e0e0e0;
}
.tut-content h2 { margin-top: 1.5rem; }
.tut-content p { margin: 0.75rem 0; }
.tut-content code {
  background: #f4f4f4;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-size: 0.9em;
}
.tut-code {
  position: relative;
  margin: 1rem 0;
}
.tut-code .tut-code-pre {
  background: #f6f8fa;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 0.75rem 1rem;
  overflow-x: auto;
  font-size: 0.9rem;
  margin: 0;
}
.tut-code pre { margin: 0; }
.try-in-new {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: #0066cc;
  color: #fff;
  border: 0;
  border-radius: 3px;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  cursor: pointer;
  opacity: 0.85;
}
.try-in-new:hover { opacity: 1; }

@media (max-width: 900px) {
  .tutorial-layout { grid-template-columns: 1fr; }
  .tut-nav { position: static; max-height: none; }
}

/* ─── Schema diagram ─────────────────────────────────────────────────── */
.diagram-page { max-width: none; padding: 0 1rem; }
.diagram-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.diagram-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 1rem;
  height: calc(100vh - 180px);
  min-height: 500px;
}
@media (max-width: 900px) {
  .diagram-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(320px, 55vh) minmax(220px, auto);
    height: auto;
    min-height: 0;
  }
  .diagram-sidebar { max-height: 60vh; }
}
.diagram-canvas {
  position: relative;
  background: #fafafa;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  overflow: hidden;
}
.diagram-canvas svg { width: 100%; height: 100%; display: block; }
.diagram-loading, .diagram-empty {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: #777; font-family: monospace;
}
.diagram-empty[hidden] { display: none; }
.diagram-sidebar {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 0.75rem;
  overflow-y: auto;
}
.diagram-sidebar .empty { color: #999; font-style: italic; }
.diagram-warnings { margin-top: 0.5rem; }
.diagram-warnings .warn {
  background: #fff3e0; color: #e65100;
  padding: 0.5rem 0.75rem; border-radius: 4px; font-size: 0.9rem;
  margin-bottom: 0.25rem;
}

/* Node */
.node .node-bg { fill: #fff; stroke: #b0bec5; stroke-width: 1; }
.node .node-header { fill: #1a237e; }
.node-view .node-header { fill: #00695c; }
.node .node-title { fill: #fff; font: 600 12px sans-serif; }
.node .col-bg { fill: transparent; }
.node .col:hover .col-bg { fill: #e3f2fd; }
.node .col-icon { font: 11px sans-serif; }
.node .col-name { font: 11px 'SF Mono', Consolas, monospace; fill: #263238; }
.node .col-name.pk { font-weight: 700; }
.node .col-name.fk { fill: #6a1b9a; }
.node .col-type { font: 10px 'SF Mono', Consolas, monospace; fill: #607d8b; }
.node .col-more { font: italic 10px sans-serif; fill: #90a4ae; }

/* Edge */
.edge {
  fill: none;
  stroke: #90a4ae;
  stroke-width: 1.5;
  transition: stroke 0.15s, stroke-width 0.15s, opacity 0.15s;
}

.diagram-layout .edge.active,
.diagram-layout .edge:hover {
  stroke: #1976d2;
  stroke-width: 2.5;
  stroke-dasharray: 8 4;
  animation: marching-ants 0.6s linear infinite;
  cursor: pointer;
}
@keyframes marching-ants {
  to { stroke-dashoffset: -12; }
}
.diagram-layout .node.dimmed { opacity: 0.3; transition: opacity 0.15s; }
.diagram-layout .edge.dimmed { opacity: 0.12; animation: none; }
.diagram-layout .node.selected .node-bg { stroke: #1976d2; stroke-width: 2; }
.diagram-layout .node { cursor: grab; }
.diagram-layout .node.dragging { cursor: grabbing; }
.diagram-layout .node.dragging .node-bg { stroke: #1976d2; stroke-width: 2; filter: drop-shadow(0 2px 6px rgba(25,118,210,0.25)); }
.diagram-layout .node:focus { outline: none; }
.diagram-layout .node:focus .node-bg { stroke: #1976d2; stroke-width: 2; }

@media (prefers-reduced-motion: reduce) {
  .diagram-layout .edge.active,
  .diagram-layout .edge:hover {
    animation: none;
    stroke-dasharray: none;
  }
}

.diagram-sidebar .schema-preview { font-size: 0.85rem; }
.diagram-sidebar table { width: 100%; border-collapse: collapse; }
.diagram-sidebar th, .diagram-sidebar td {
  padding: 0.25rem 0.4rem;
  border: 1px solid #eee;
  font: 10px 'SF Mono', monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.btn-diagram {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px; height: 24px;
  margin-left: 0.35rem;
  border-radius: 4px;
  text-decoration: none;
  font-size: 0.95rem;
  line-height: 1;
}
.btn-diagram:hover { background: #e3f2fd; }

.diagram-warnings .info {
  background: #e3f2fd; color: #0d47a1;
  padding: 0.5rem 0.75rem; border-radius: 4px; font-size: 0.9rem;
  margin-bottom: 0.25rem;
}
.diagram-toolbar {
  position: absolute; top: 0.5rem; right: 0.5rem; z-index: 2;
  display: flex; gap: 0.5rem; align-items: center;
  background: rgba(255,255,255,0.9);
  padding: 0.3rem 0.6rem; border-radius: 4px;
  font-size: 0.85rem; color: #455a64;
  border: 1px solid #e0e0e0;
}
.diagram-toolbar button {
  width: 28px; height: 28px; border: 1px solid #cfd8dc;
  background: #fff; border-radius: 4px; cursor: pointer;
  font-size: 14px;
}
.diagram-toolbar button:hover { background: #eceff1; }

/* === Status page === */
body.status-page {
  margin: 0;
  background: #f5f7fa;
  color: #263238;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.status-header {
  background: #fff;
  padding: 24px 32px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.status-header h1 { margin: 0; font-size: 1.6rem; }
.status-header .status-sub {
  display: flex; align-items: center; gap: 12px;
  color: #546e7a; font-size: 0.95rem;
}
.status-header .status-meta { color: #78909c; font-size: 0.85rem; }

.status-grid {
  display: grid;
  gap: 16px;
  padding: 24px 32px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.status-grid .card-wide { grid-column: span 3; }
@media (max-width: 960px) {
  .status-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .status-grid .card-wide { grid-column: span 2; }
}
@media (max-width: 640px) {
  .status-grid { grid-template-columns: 1fr; padding: 16px; }
  .status-grid .card-wide { grid-column: span 1; }
  .status-header { padding: 16px; }
}

.status-page .card {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 16px 20px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}
.status-page .card h2 {
  margin: 0 0 12px;
  font-size: 1rem;
  color: #37474f;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metrics { margin: 0; display: grid; gap: 8px; }
.metrics dt {
  font-size: 0.8rem; color: #78909c;
  text-transform: uppercase; letter-spacing: 0.03em;
  margin-top: 6px;
}
.metrics dd { margin: 0; font-size: 0.95rem; }
.metrics dd small { display: block; color: #90a4ae; font-size: 0.8rem; margin-top: 2px; }

.bar {
  margin-top: 6px;
  height: 6px;
  background: #eceff1;
  border-radius: 3px;
  overflow: hidden;
}
.bar-fill { height: 100%; transition: width 0.4s ease; }
.bar-fill.healthy  { background: #43a047; }
.bar-fill.warning  { background: #fb8c00; }
.bar-fill.critical { background: #e53935; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge-healthy  { background: #e8f5e9; color: #2e7d32; }
.badge-warning  { background: #fff3e0; color: #e65100; }
.badge-critical { background: #ffebee; color: #c62828; }

.days.healthy  { color: #2e7d32; font-weight: 600; }
.days.warning  { color: #e65100; font-weight: 600; }
.days.critical { color: #c62828; font-weight: 700; }

.tbl {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.tbl th, .tbl td {
  padding: 6px 8px;
  border-bottom: 1px solid #eceff1;
  text-align: left;
}
.tbl th {
  color: #78909c; font-weight: 600; font-size: 0.8rem;
  text-transform: uppercase; letter-spacing: 0.03em;
}
.tbl td.num, .tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }

.status-footer {
  padding: 16px 32px 32px;
  color: #78909c;
  font-size: 0.9rem;
}
.status-footer a { color: #1976d2; text-decoration: none; }
.status-footer a:hover { text-decoration: underline; }
