:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #dfe2e8;
  --text: #1c2130;
  --muted: #5d6677;
  --accent: #2456d6;
  --accent-text: #ffffff;
  --accent-hover: #1c46b3;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --warn: #7a4d00;
  --warn-bg: #fff4d6;
  --ok: #17663a;
  --ok-bg: #e3f5ea;
  --info: #1c46b3;
  --info-bg: #e6edfd;
  --code-bg: #eef0f4;
  --log-bg: #12151c;
  --log-text: #d7dce6;
  --radius: 8px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #181c25;
    --border: #2b3140;
    --text: #e6e9ef;
    --muted: #9aa3b5;
    --accent: #7ea2ff;
    --accent-text: #0c1020;
    --accent-hover: #9db8ff;
    --danger: #ff8a80;
    --danger-bg: #3a1a18;
    --warn: #ffd27a;
    --warn-bg: #3a2c0c;
    --ok: #7fdca4;
    --ok-bg: #12301f;
    --info: #9db8ff;
    --info-bg: #17223f;
    --code-bg: #232937;
    --log-bg: #0a0c11;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

h1 { font-size: 1.6rem; margin: 0 0 .25rem; line-height: 1.25; overflow-wrap: anywhere; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: .9rem; margin: 1.25rem 0 0; padding-top: 1rem; border-top: 1px solid var(--border); }
h2 .count { color: var(--muted); font-weight: 400; margin-left: .25rem; }
p { margin: 0 0 .75rem; }
h1 + .card, h1 + section { margin-top: 1rem; }

code, pre { font-family: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; font-size: .9em; }
code { background: var(--code-bg); padding: .1em .35em; border-radius: 4px; overflow-wrap: anywhere; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 0; }

/* Layout */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 1000px; margin: 0 auto; padding: .75rem 1rem;
  display: flex; flex-wrap: wrap; gap: .5rem 1.5rem; align-items: center; justify-content: space-between;
}
.brand { font-weight: 650; color: var(--text); }
.topbar nav { display: flex; gap: 1.25rem; align-items: center; }
.topbar nav form { margin: 0; }

.container { max-width: 1000px; margin: 0 auto; padding: 1.5rem 1rem 4rem; }
.page-head { display: flex; flex-wrap: wrap; gap: 1rem; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.page-head p { margin: 0; }
.back { display: inline-block; margin-bottom: .5rem; font-size: .9rem; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem 1.25rem; margin-bottom: 1rem;
}
.card-head { display: flex; gap: .75rem; align-items: center; justify-content: space-between; margin-bottom: .75rem; }
.card > :last-child { margin-bottom: 0; }

.stack { display: flex; flex-direction: column; gap: .9rem; }
.stack > p { margin: 0; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .9rem; }
.form-narrow { max-width: 560px; }
.btn-row { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }

.empty { color: var(--muted); margin: .25rem 0 1rem; }
.empty-state { text-align: center; padding: 2.5rem 1.25rem; }
.empty-state p { max-width: 520px; margin: .5rem auto 1.25rem; }

.login { max-width: 380px; margin: 12vh auto 0; }
.login h1 { text-align: center; margin-bottom: 1.25rem; }

/* Forms */
label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; font-weight: 550; }
label em { font-weight: 400; font-style: normal; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: .4rem; font-weight: 400; }

input:not([type=checkbox]), textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: .5rem .65rem; width: 100%; min-width: 0;
}
textarea { resize: vertical; font-family: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; font-size: .85rem; line-height: 1.5; }
input::placeholder { color: var(--muted); opacity: .8; }
input[type=checkbox] { width: 1rem; height: 1rem; accent-color: var(--accent); }

:is(input, textarea, button, a, summary):focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 550; line-height: 1.2; cursor: pointer; white-space: nowrap;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: .5rem .9rem;
}
.btn:hover { border-color: var(--muted); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { border-color: var(--danger); }
.btn-small { padding: .2rem .55rem; font-size: .82rem; font-weight: 500; }
.btn:disabled, .switch:disabled { opacity: .55; cursor: default; }

button.link { font: inherit; background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; }
button.link:hover { text-decoration: underline; }

.inline-form, .add-account { display: flex; flex-wrap: wrap; gap: .75rem 1rem; align-items: flex-end; }
.inline-form label:not(.check), .add-account label:not(.check) { flex: 1 1 200px; }
.add-account { margin-top: .75rem; }
.add-account + .hint { margin-top: .6rem; }
h3 + .alert { margin-top: .75rem; margin-bottom: 0; }
.add-account .check { padding-bottom: .55rem; }
.inline-form label.narrow { flex: 0 1 16rem; }
.inline-form + .hint { margin-top: .6rem; }

details { border-top: 1px solid var(--border); padding: .75rem 0; }
details:first-of-type { border-top: 0; padding-top: 0; }
.card > details:last-child { padding-bottom: 0; }
details > summary { cursor: pointer; font-weight: 550; }
details[open] > summary { margin-bottom: .9rem; }
form details { border: 0; padding: 0; }

/* Tables */
.table-wrap { overflow-x: auto; }
.card.table-wrap { padding: 0; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
.card:not(.table-wrap) th:first-child, .card:not(.table-wrap) td:first-child { padding-left: 0; }
.card:not(.table-wrap) th:last-child, .card:not(.table-wrap) td:last-child { padding-right: 0; }
td.actions { text-align: right; white-space: nowrap; }
td.actions > * { margin-left: .35rem; }
tr.off td:first-child a, tr.off td code { opacity: .55; }

/* Key/value lists */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: .75rem 1.5rem; margin: 0; }
.kv dt { font-size: .8rem; color: var(--muted); }
.kv dd { margin: 0; }

.copy-line { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-top: .75rem; }
.copy-label { font-size: .8rem; color: var(--muted); width: 3.75rem; }
.copy-line code { flex: 1 1 260px; padding: .4rem .6rem; }

/* Status */
.badge {
  display: inline-block; font-size: .78rem; font-weight: 600; white-space: nowrap;
  padding: .15rem .6rem; border-radius: 999px; background: var(--code-bg); color: var(--muted);
}
.badge-ready { background: var(--ok-bg); color: var(--ok); }
.badge-error { background: var(--danger-bg); color: var(--danger); }
.badge-out_of_sync { background: var(--warn-bg); color: var(--warn); }
.badge-provisioning { background: var(--info-bg); color: var(--info); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
@media (prefers-reduced-motion: reduce) { .badge-provisioning { animation: none; } }

.alert { border-radius: 6px; padding: .7rem .9rem; margin-bottom: 1rem; overflow-wrap: anywhere; }
.alert p { margin: 0 0 .5rem; }
.alert > :last-child { margin-bottom: 0; }
.alert .detail { font-family: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; font-size: .85rem; white-space: pre-wrap; }
.alert-error { background: var(--danger-bg); color: var(--danger); }
.alert-warn { background: var(--warn-bg); color: var(--warn); }
.alert-ok { background: var(--ok-bg); color: var(--ok); }
.alert .btn { margin-top: .25rem; }

pre { margin: 0; }
.log, .conf { border-radius: 6px; padding: .8rem 1rem; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; }
.log { background: var(--log-bg); color: var(--log-text); max-height: 340px; font-size: .82rem; }
.conf { background: var(--code-bg); font-size: .85rem; }

/* Switch */
.switch {
  position: relative; width: 38px; height: 22px; padding: 0; cursor: pointer;
  border: 1px solid var(--border); border-radius: 999px; background: var(--code-bg);
  transition: background-color .15s;
}
.switch span {
  position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--muted); transition: transform .15s, background-color .15s;
}
.switch.on { background: var(--accent); border-color: var(--accent); }
.switch.on span { transform: translateX(16px); background: var(--accent-text); }
.switch.htmx-request { opacity: .5; }

/* WireGuard block */
.wg { display: flex; flex-wrap: wrap; gap: 1.25rem; align-items: flex-start; }
.wg-qr { text-align: center; }
.wg-qr img { display: block; background: #fff; border-radius: 6px; padding: 6px; max-width: 100%; height: auto; }
.wg-qr p { margin: .4rem auto 0; max-width: 320px; }
.wg-conf { flex: 1 1 320px; min-width: 0; }

/* Off-screen helper used by the copy fallback in app.js */
.copy-fallback { position: fixed; top: -1000px; left: 0; opacity: 0; }

.accounts-table td code { white-space: nowrap; }

/* On narrow screens each account becomes a small card instead of a row. */
@media (max-width: 640px) {
  .accounts-table, .accounts-table tbody { display: block; }
  .accounts-table thead { display: none; }
  .accounts-table tr {
    display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem .75rem;
    padding: .9rem 0; border-bottom: 1px solid var(--border);
  }
  .accounts-table tbody tr:last-child { border-bottom: 0; }
  .accounts-table td { display: block; padding: 0; border: 0; min-width: 0; }
  .accounts-table td:nth-child(1), .accounts-table td:nth-child(2), .accounts-table td.actions { grid-column: 1 / -1; }
  .accounts-table td:nth-child(1) { font-weight: 600; }
  .accounts-table td code { white-space: normal; }
  .accounts-table td[data-label]::before {
    content: attr(data-label); display: block; font-size: .8rem; color: var(--muted); margin-bottom: .2rem;
  }
  .accounts-table td.actions { text-align: left; white-space: normal; }
  .accounts-table td.actions > * { margin: 0 .35rem 0 0; }

  .add-account label:not(.check), .inline-form label:not(.check) { flex-basis: 100%; }
  .add-account .check { padding-bottom: 0; }
}
