:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1b1f24;
  --muted: #5d6672;
  --border: #dde1e6;
  --accent: #c2410c;
  --accent-hover: #9a3412;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --ok: #15803d;
  --focus: #c2410c55;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 8px 24px rgb(0 0 0 / 8%);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #20262e;
    --text: #e6e9ee;
    --muted: #9aa4b1;
    --border: #2b323b;
    --accent: #f97316;
    --accent-hover: #fb923c;
    --accent-text: #1c0d02;
    --danger: #f87171;
    --ok: #4ade80;
    --focus: #f9731666;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.2rem; margin-bottom: 1rem; }
.muted { color: var(--muted); margin: 0; }

input, textarea, button { font: inherit; color: inherit; }
input, textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .55rem .7rem;
  min-width: 0;
}
textarea { resize: vertical; }
input:focus, textarea:focus, button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 0;
  border-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: .5rem .9rem;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.wide { width: 100%; }
.btn.small { padding: .3rem .65rem; font-size: .9rem; }
.btn.icon { padding: .45rem; }
.ico { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

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

.error { color: var(--danger); margin: 0; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 2rem; display: grid; gap: 1rem; text-align: center; justify-items: center; }
.login-card .field { text-align: left; width: 100%; }

.field { display: grid; gap: .3rem; }
.field > span, .field > label { font-size: .85rem; color: var(--muted); font-weight: 500; }

/* App */
.app { max-width: 1280px; margin: 0 auto; padding: 0 16px 24px; }
.topbar { display: flex; align-items: center; gap: 1rem; padding: 16px 0; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.15rem; }
.search { flex: 1 1 260px; max-width: 480px; }
.top-actions { display: flex; gap: .5rem; margin-left: auto; flex-wrap: wrap; }

.tag-filter { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: .35rem;
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 999px; padding: .2rem .7rem; font-size: .85rem; cursor: pointer;
}
.chip[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.chip .dot, .dot { width: .6rem; height: .6rem; border-radius: 50%; display: inline-block; flex: none; }
.chip .count { color: var(--muted); }

.tag { display: inline-block; border-radius: 999px; padding: .05rem .55rem; font-size: .8rem; font-weight: 500; margin: .1rem .25rem .1rem 0; }

.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
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; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--surface-2); user-select: none; }
th[data-sort] { cursor: pointer; }
th[aria-sort="ascending"]::after { content: " ▲"; }
th[aria-sort="descending"]::after { content: " ▼"; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.peer-name { font-weight: 600; }
.peer-sub { color: var(--muted); font-size: .85rem; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }
.col-actions { width: 1%; }
.row-actions { display: flex; gap: .3rem; justify-content: flex-end; }
.status { display: inline-flex; align-items: center; gap: .4rem; color: var(--muted); font-size: .9rem; white-space: nowrap; }
.status.online { color: var(--ok); }
.status .dot { background: currentColor; }
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--muted); margin: 0; }

.footer { display: flex; justify-content: space-between; color: var(--muted); font-size: .85rem; padding: 12px 2px; }

/* Dialogs */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  width: min(680px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog form, .tags-manager { padding: 1.4rem; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: .85rem 1rem; }
.span2 { grid-column: span 2; }
.input-group { display: flex; gap: .35rem; }
dialog form.tag-new { padding: 0; }
/* Buttons stay visible while a long form scrolls inside the dialog */
.dialog-actions {
  display: flex; gap: .5rem; align-items: center;
  position: sticky; bottom: 0;
  margin: 1.2rem -1.4rem -1.4rem; padding: .9rem 1.4rem;
  background: var(--surface); border-top: 1px solid var(--border);
}
.spacer { flex: 1; }
#peer-error { margin-top: 1rem; }

.tag-editor {
  display: flex; flex-wrap: wrap; gap: .3rem; align-items: center;
  border: 1px solid var(--border); border-radius: 8px; padding: .3rem .4rem; background: var(--surface);
}
.tag-editor:focus-within { outline: 3px solid var(--focus); border-color: var(--accent); }
.tag-editor input { border: 0; outline: 0; flex: 1 1 120px; padding: .25rem; width: auto; }
.tag-editor .tag { margin: 0; display: inline-flex; align-items: center; gap: .2rem; }
.tag-editor .tag button { border: 0; background: none; padding: 0 .1rem; cursor: pointer; color: inherit; font-size: 1rem; line-height: 1; }

.device { display: grid; grid-template-columns: max-content 1fr; gap: .25rem 1rem; margin: 1.2rem 0 0; padding: .8rem 1rem; background: var(--surface-2); border-radius: 8px; font-size: .9rem; }
.device dt { color: var(--muted); }
.device dd { margin: 0; overflow-wrap: anywhere; }

.tags-list { list-style: none; margin: 1rem 0; padding: 0; display: grid; gap: .45rem; }
.tags-list li, .tag-new { display: flex; gap: .5rem; align-items: center; }
input[type="color"] { width: 2.6rem; height: 2.3rem; padding: .15rem; flex: none; cursor: pointer; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 8px;
  box-shadow: var(--shadow); z-index: 10; max-width: calc(100vw - 32px);
}

/* Phone: table rows become cards */
@media (max-width: 760px) {
  .topbar { gap: .6rem; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .top-actions { margin-left: auto; }
  .list { background: none; border: 0; }
  table, tbody { display: block; }
  thead { display: none; }
  tbody tr {
    display: grid; grid-template-columns: 1fr auto; gap: .2rem .75rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: .75rem; margin-bottom: .6rem;
  }
  td { border: 0; padding: 0; }
  td.c-name { grid-column: 1; }
  td.c-status { grid-column: 2; grid-row: 1; text-align: right; }
  td.c-id, td.c-company, td.c-contact, td.c-tags { grid-column: 1 / -1; }
  td.c-company:empty, td.c-contact:empty, td.c-tags:empty { display: none; }
  td.c-actions { grid-column: 1 / -1; margin-top: .3rem; }
  .row-actions { justify-content: flex-start; }
  .grid { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
}
