:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1f2328;
  --muted: #667085;
  --border: #dde1e6;
  --row-hover: #f2f5fa;
  --row-active: #e5eefc;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #c62828;
  --ok: #1e7b34;
  --badge-bg: #eef1f5;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --card: #1a1f25;
    --text: #e6e8eb;
    --muted: #9aa4b2;
    --border: #2c333b;
    --row-hover: #222933;
    --row-active: #1d3050;
    --primary: #4f8cff;
    --danger: #ef5350;
    --ok: #4caf50;
    --badge-bg: #262d36;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0; }
h3 { font-size: 16px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 8px 0; }
.spacer { flex: 1; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 9px;
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
label { display: block; margin: 10px 0; font-weight: 500; }
label input, label select, label textarea { display: block; width: 100%; margin-top: 4px; font-weight: normal; }
label.check { display: flex; gap: 8px; align-items: center; font-weight: normal; }
label.check input { width: auto; margin: 0; }
.help { display: block; font-weight: normal; font-size: 12px; margin-top: 3px; }

.btn {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--row-hover); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.link { border: 0; background: none; padding: 2px 6px; color: var(--primary); }
.btn.link.danger { color: var(--danger); }
.btn.wide { width: 100%; margin-top: 8px; padding: 9px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 28px; }

/* App shell */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 16px; }
.layout {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 16px;
  padding: 16px 20px;
  align-items: start;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }
.zones-pane, .detail-pane { padding: 14px; min-width: 0; }
.pane-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.pane-head p { margin: 2px 0 0; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
#zone-filter { width: 100%; margin-bottom: 8px; }
.status { margin: 8px 2px 0; font-size: 13px; }
.empty { padding: 60px 20px; text-align: center; color: var(--muted); }

/* Tables */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tbody tr:hover { background: var(--row-hover); }
.zones-table { table-layout: fixed; }
.zones-table th:nth-child(1) { width: 46%; }
.zones-table th:nth-child(3) { width: 72px; }
.zones-table td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zones-table tbody tr { cursor: pointer; }
.zones-table tbody tr.active { background: var(--row-active); }
.records-table td.value { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; }
.records-table td.name { font-weight: 500; word-break: break-all; }
.records-table td.row-actions { text-align: right; white-space: nowrap; }
.records-table .check { width: 28px; }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 12px;
  background: var(--badge-bg);
  color: var(--muted);
}
.badge.admin { color: var(--danger); }
.badge.owner { color: var(--ok); }
.badge.type { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); }

.soa {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 6px 16px;
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.soa dt { font-size: 12px; color: var(--muted); }
.soa dd { margin: 0; word-break: break-all; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.toolbar input[type=search] { min-width: 200px; }

/* Dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 22px;
  width: min(460px, calc(100vw - 32px));
  background: var(--card);
  color: var(--text);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(15, 20, 30, .45); }
#dialog-text { white-space: pre-line; }
#dialog-text:empty { display: none; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* Toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 10; }
.toast {
  max-width: 420px;
  padding: 10px 14px;
  border-radius: 6px;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--ok);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .15);
  word-break: break-word;
}
.toast.error { border-left-color: var(--danger); }
.toast .warn { display: block; margin-top: 4px; color: var(--muted); font-size: 13px; }

.badge.cert-issued { color: var(--ok); }
.badge.cert-failed { color: var(--danger); }
.badge.cert-pending { color: var(--primary); }
.records-table td.tls { white-space: nowrap; }
.records-table td.tls .btn.link { padding: 0 4px; font-size: 13px; }
.records-table td.tls small { display: block; }
input[readonly] { opacity: .7; }
.records-table td.name a { color: var(--primary); text-decoration: none; }
.records-table td.name a:hover { text-decoration: underline; }
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; resize: vertical; }
label.file-pick { font-weight: normal; font-size: 13px; margin-top: -4px; }
label.file-pick input[type=file] { border: 0; padding: 4px 0; background: none; }
.records-table td.value .delegation { display: block; font-family: system-ui, sans-serif; }
a.zone-link { color: var(--primary); text-decoration: none; }
a.zone-link:hover { text-decoration: underline; }
.key-row { margin: 10px 0; }
.key-line { display: flex; gap: 6px; margin-top: 4px; }
.key-line input.api-key { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
p.key-example { margin: 12px 0 4px; font-size: 13px; }
pre.key-example { margin: 0; padding: 8px; background: var(--badge-bg); border-radius: 6px; font-size: 12px; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }
.key-generate { margin: 12px 0 4px; }
.key-generate .help { margin-top: 4px; }

/* Sections */
.tabs { display: flex; gap: 2px; margin-left: 18px; }
.tabs a {
  padding: 6px 12px; border-radius: 6px; color: var(--muted); text-decoration: none; font-weight: 500;
}
.tabs a:hover { background: var(--row-hover); color: var(--text); }
.tabs a.active { background: var(--row-active); color: var(--text); }
.page { padding: 16px 20px; }
.page .pane { padding: 14px; }
.page .pane-head p { max-width: 70ch; }
@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; margin-left: 0; }
}

/* Engines and stacks tables */
.table tr.details:hover, .table tr.group:hover { background: none; }
.table tr.details td { padding: 4px 8px 12px; }
.table tr.details .soa { margin: 0; }
.table tr.group td { padding-top: 16px; border-bottom: 2px solid var(--border); }
.table tr.group .btn.link { float: right; }
.stacks-table td.image .file { display: block; word-break: break-all; }
.stacks-table td.image small { display: block; }
.stacks-table td.image .btn.link { padding-left: 0; }
.engines-table .engine-toggle { padding-left: 0; font-weight: 500; color: var(--text); }
progress { width: 100%; margin-top: 10px; }

/* User menu */
#whoami-badges { display: inline-flex; align-items: center; gap: 8px; }
.user-menu { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 160px;
  padding: 4px; background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18); display: flex; flex-direction: column;
}
.menu > * {
  font: inherit; color: var(--text); text-align: left; text-decoration: none; cursor: pointer;
  padding: 7px 10px; border: 0; border-radius: 5px; background: none; white-space: nowrap;
}
.menu > *:hover, .menu > *:focus-visible { background: var(--row-hover); outline: none; }

/* API keys dialog */
.key-label { margin-bottom: 3px; }
.key-generate { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 14px; }
#new-key-name { flex: 1; min-width: 0; }

/* Docs */
.docs-layout { grid-template-columns: minmax(200px, 260px) minmax(0, 1fr); }
@media (max-width: 860px) { .docs-layout { grid-template-columns: 1fr; } }
.docs-nav { padding: 14px; position: sticky; top: 12px; }
@media (max-width: 860px) { .docs-nav { position: static; } }
.docs-nav h2 { margin: 0 0 8px; }
.docs-nav h3 { margin: 16px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.docs-list { list-style: none; margin: 0; padding: 0; }
.docs-list a { display: block; padding: 5px 8px; border-radius: 5px; color: var(--text); text-decoration: none; }
.docs-list a:hover { background: var(--row-hover); }
.docs-list a.active { background: var(--row-active); font-weight: 600; }
#docs-toc a { font-size: 13px; color: var(--muted); }
#docs-toc li.sub a { padding-left: 20px; }
.doc { padding: 20px 28px 32px; min-width: 0; line-height: 1.6; }
.doc > * { max-width: 78ch; }
.doc > pre, .doc > .table-wrap { max-width: 100%; }
.doc h1 { margin: 0 0 12px; font-size: 26px; line-height: 1.25; }
.doc h2 { margin: 30px 0 8px; padding-top: 14px; border-top: 1px solid var(--border); font-size: 19px; }
.doc h3 { margin: 22px 0 6px; font-size: 16px; }
.doc h4 { margin: 18px 0 4px; font-size: 14px; }
.doc p { margin: 0 0 12px; }
.doc ul, .doc ol { margin: 0 0 12px; padding-left: 24px; }
.doc li { margin: 3px 0; }
.doc a { color: var(--primary); }
.doc code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; background: var(--badge-bg); padding: 1px 5px; border-radius: 4px; }
.doc pre { margin: 0 0 14px; padding: 12px 14px; background: var(--badge-bg); border-radius: 6px; overflow-x: auto; line-height: 1.45; }
.doc pre code { background: none; padding: 0; font-size: 12.5px; }
.doc blockquote { margin: 0 0 14px; padding: 10px 14px; border-left: 3px solid var(--primary); background: var(--row-hover); border-radius: 0 6px 6px 0; }
.doc blockquote > :last-child { margin-bottom: 0; }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }
.doc .table-wrap { margin: 0 0 14px; overflow-x: auto; }
.doc table { border-collapse: collapse; }
.doc th, .doc td { padding: 6px 12px; border: 1px solid var(--border); text-align: left; vertical-align: top; }
.doc th { background: var(--badge-bg); font-weight: 600; }

.tabs a[hidden] { display: none; }
