:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --border: #d9dfeb;
  --text: #1b2333;
  --muted: #5d6881;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --accent-soft: #e4edff;
  --danger: #c62b3a;
  --danger-soft: #fdecee;
  --warn-soft: #fff4dc;
  --warn: #8a5b00;
  --ok: #1c7c4d;
  --focus: #1a54c8;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 4px 16px rgba(20, 30, 60, .06);
  --radius: 10px;
  --gutter: 16px;
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #171e2e;
    --surface-2: #1f2740;
    --border: #2c3650;
    --text: #e6eaf4;
    --muted: #9aa6c0;
    --accent: #6c9bff;
    --accent-text: #0b1226;
    --accent-soft: #1f2d52;
    --danger: #ff6b7a;
    --danger-soft: #3a1f27;
    --warn-soft: #3a2f14;
    --warn: #f0c36a;
    --ok: #5fd39a;
    --focus: #8db3ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); min-height: 100vh; overflow-x: hidden; }
h1, h2 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.4rem; overflow-wrap: anywhere; }
h2 { font-size: 1rem; margin-bottom: 10px; }
p { margin: 0 0 8px; }
a { color: var(--accent); }
ul.plain, ol { list-style: none; margin: 0; padding: 0; }
ol { list-style: decimal; padding-left: 20px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pad { padding: var(--gutter); }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; font-family: inherit; }
pre.pre { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .85rem; background: var(--surface-2); padding: 8px; border-radius: 6px; max-height: 260px; overflow: auto; }
.error-text { color: var(--danger); }
.noscript { padding: var(--gutter); }
#page-title:focus { outline: none; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }

.skip { position: absolute; left: -999px; top: 0; background: var(--accent); color: var(--accent-text); padding: 8px 12px; z-index: 100; }
.skip:focus { left: 8px; top: 8px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger-ghost { border-color: transparent; background: transparent; color: var(--danger); }
.btn.danger-ghost:hover { background: var(--danger-soft); }
.btn.icon-only { min-width: 40px; padding: 0; }
.btn.wide { width: 100%; }
.btn.primary span, .connect .btn span { overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

input[type=text], input[type=password], input[type=search], input[type=number], select, textarea {
  width: 100%; min-height: 42px; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; min-width: 0;
}
textarea { resize: vertical; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .9rem; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; }

.row { display: flex; align-items: center; }
.row.gap { gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.center { justify-content: center; margin: 12px 0; }
.grow { flex: 1; }

/* ---------- top bar & layout ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px var(--gutter);
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 800; letter-spacing: .04em; text-decoration: none; color: var(--accent); padding: 4px 8px; }
.topnav { display: flex; gap: 2px; }
.topnav a { white-space: nowrap; padding: 8px 8px; border-radius: 8px; text-decoration: none; color: var(--text); font-weight: 500; }
.topnav a:hover { background: var(--surface-2); }
.topnav a[aria-current] { background: var(--accent-soft); color: var(--accent); }
.topbar .btn span { display: none; }
.layout { display: block; }
.sidebar { padding: var(--gutter); }
.content { padding: var(--gutter); min-width: 0; outline: none; }
body[data-view=detail] .sidebar { display: none; }
body[data-view=list] .content { display: none; }
body[data-view=list] .page-head .back { visibility: hidden; }

@media (min-width: 900px) {
  .topbar .btn span { display: inline; }
  .layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); align-items: start; min-height: calc(100vh - 57px); }
  .sidebar { position: sticky; top: 57px; height: calc(100vh - 57px); overflow-y: auto; border-right: 1px solid var(--border); background: var(--surface); }
  body[data-view=detail] .sidebar, body[data-view=list] .content { display: block; }
  .page-head .back { display: none; }
  .content { padding: 24px 32px; max-width: 900px; }
}

.side-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.search { position: relative; flex: 1; min-width: 0; }
.search .icon { position: absolute; left: 10px; top: 11px; color: var(--muted); }
.search input { padding-left: 36px; }
.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; }
.menu summary::-webkit-details-marker { display: none; }
.menu-pop { position: absolute; right: 0; top: 46px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); min-width: 150px; padding: 4px; z-index: 30; }
.menu-pop a { display: block; padding: 10px 12px; border-radius: 6px; text-decoration: none; color: var(--text); }
.menu-pop a:hover { background: var(--surface-2); }

/* ---------- tree ---------- */
.tree-list.nested { margin-left: 14px; padding-left: 6px; border-left: 1px solid var(--border); }
.tree-row { display: flex; align-items: center; gap: 2px; }
.tw { min-height: 32px; min-width: 32px; }
.tw .icon { width: 16px; transition: transform .12s; }
.tw.open .icon { transform: rotate(90deg); }
.tw-spacer { width: 32px; flex: none; }
.tree-link { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; padding: 8px; border-radius: 8px; text-decoration: none; color: var(--text); min-height: 40px; }
.tree-link:hover { background: var(--surface-2); }
.tree-link[aria-current=page] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tree-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-sub { margin-left: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40%; }
.count { margin: 0 0 6px; }

.badge {
  display: inline-flex; align-items: center; justify-content: center; flex: none; width: 34px; height: 26px; border-radius: 6px;
  font-size: .62rem; font-weight: 800; letter-spacing: .03em; background: var(--surface-2); color: var(--muted);
}
.badge .icon { width: 16px; height: 16px; }
.t-Rdp { background: #dcebff; color: #1850b8; }
.t-Ssh, .t-Telnet, .t-PowerShell { background: #dff5e6; color: #1c6b3e; }
.t-Web { background: #ffe9d6; color: #a1510b; }
.t-Credential { background: #fff2c9; color: #7a5a00; }
.t-Vpn, .t-Vnc { background: #ece2ff; color: #5a35b0; }
.t-Folder { background: #e5e9f2; color: #4a5775; }
.t-Note { background: #fde2ee; color: #a3205f; }
@media (prefers-color-scheme: dark) {
  .t-Rdp { background: #1c2f57; color: #9cc0ff; }
  .t-Ssh, .t-Telnet, .t-PowerShell { background: #173826; color: #7fdba6; }
  .t-Web { background: #43290f; color: #ffb877; }
  .t-Credential { background: #403410; color: #f4d36e; }
  .t-Vpn, .t-Vnc { background: #2d2250; color: #c2a8ff; }
  .t-Folder { background: #283149; color: #aab6d4; }
  .t-Note { background: #45213a; color: #ff9fcf; }
}

/* ---------- content ---------- */
.page-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.page-head h1 { flex: 1 1 200px; min-width: 0; outline: none; }
.page-head .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.meta { color: var(--muted); font-size: .9rem; margin-bottom: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card.error { color: var(--danger); }
.empty { text-align: center; padding: 60px 16px; }
.alert { border-radius: 8px; padding: 10px 12px; margin: 10px 0; background: var(--surface-2); }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

dl { margin: 0; }
.field { display: grid; grid-template-columns: 1fr; gap: 2px; padding: 8px 0; border-top: 1px solid var(--border); }
.field:first-child { border-top: 0; }
.field dt { color: var(--muted); font-size: .85rem; }
.field dd { margin: 0; display: flex; align-items: center; gap: 8px; min-width: 0; }
.field .val { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.field .acts { display: flex; gap: 2px; flex: none; }
.settings .field { padding: 6px 0; }
@media (min-width: 600px) {
  .field { grid-template-columns: 160px 1fr; align-items: center; gap: 12px; }
}
code.secret { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; overflow-wrap: anywhere; }
code.secret.multi { white-space: pre-wrap; }
code.totp { font-size: 1.4rem; letter-spacing: .12em; font-weight: 600; margin-right: 10px; }
.totp-wrap { display: inline-flex; align-items: center; flex-wrap: wrap; gap: 6px; }
progress { width: 70px; height: 8px; accent-color: var(--accent); }
.tags { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; padding: 0; margin: 0; }
.tags li, .pill { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 2px 10px; font-size: .85rem; }
.pill.danger { background: var(--danger-soft); color: var(--danger); }
.link-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; text-decoration: none; color: var(--text); }
.connect { margin-top: 12px; }

/* ---------- forms ---------- */
.form-row { margin-bottom: 14px; min-width: 0; }
.form-row label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
.form-row.check { display: flex; align-items: center; gap: 8px; }
.form-row.check label { margin: 0; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
@media (min-width: 600px) { .form-grid { grid-template-columns: 1fr 1fr; } }
.hint { color: var(--muted); font-size: .85rem; margin: 4px 0 0; }
.fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin: 0 0 14px; min-width: 0; }
.fieldset legend { font-weight: 700; padding: 0 6px; }
.input-group { display: flex; gap: 6px; flex-wrap: wrap; }
.input-group input { flex: 1 1 180px; }
.adv summary { cursor: pointer; font-weight: 600; margin: 4px 0 10px; }
.sticky-actions { position: sticky; bottom: 0; background: var(--surface); padding-top: 10px; }

/* ---------- history & log ---------- */
.history { display: grid; gap: 0; list-style: none; margin: 0; padding: 0; }
.hist-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 4px; }
.hist-detail { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 8px; }
.log { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.log-row { display: grid; grid-template-columns: 1fr; gap: 2px; padding: 10px 14px; border-top: 1px solid var(--border); overflow-wrap: anywhere; }
.log-row:first-child { border-top: 0; }
.log-action { font-weight: 600; }
@media (min-width: 700px) { .log-row { grid-template-columns: 170px 150px 1fr auto; gap: 12px; align-items: center; } }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--gutter); }
.login { width: 100%; max-width: 420px; padding: 24px; }
.login h1 { margin-bottom: 6px; }

/* ---------- dialog & toast ---------- */
.dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); max-width: min(440px, calc(100vw - 32px)); padding: 20px; box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(10, 15, 30, .55); }
.dialog h2 { margin-bottom: 8px; font-size: 1.15rem; }
.toasts { position: fixed; left: var(--gutter); right: var(--gutter); bottom: var(--gutter); display: flex; justify-content: center; pointer-events: none; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: 520px; }
.toast.error { background: var(--danger); color: #fff; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
