/* Personal CRM — mobile-first, light/dark via prefers-color-scheme. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2128;
  --muted: #6b7280;
  --border: #e2e5ea;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151a;
    --surface: #1a2027;
    --text: #e6e9ed;
    --muted: #8b94a1;
    --border: #2a323c;
    --accent: #3b82f6;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }

/* Keyboard skip link: off-screen until focused. */
.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  z-index: 100;
  background: var(--accent);
  color: var(--accent-text);
  padding: 0.5rem 0.9rem;
  border-radius: 0 0 var(--radius) 0;
}
.skip-link:focus { left: 0; }

/* The hidden attribute must always win: author display rules (e.g. the
   flex contact-list rows) otherwise override the UA's [hidden] style,
   which silently broke search filtering and archived-row hiding. */
[hidden] { display: none !important; }

/* htmx indicator styles, vendored here because the CSP blocks htmx's
   injected inline <style> (htmx-config sets includeIndicatorStyles:false). */
.htmx-indicator { opacity: 0; transition: opacity 200ms ease-in; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

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

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

main {
  max-width: 56rem;
  margin: 0 auto;
  padding: 1rem;
}

/* Top bar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar nav {
  max-width: 56rem;
  margin: 0 auto;
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.brand {
  font-weight: 700;
  color: var(--text);
  font-size: 1.05rem;
}
.brand:hover { text-decoration: none; }

.nav-actions { display: flex; align-items: center; gap: 0.9rem; }
.nav-actions form { margin: 0; }
.nav-link {
  color: var(--muted);
  font-size: 0.92rem;
}
button.as-link {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font: inherit;
}
.nav-link:hover { color: var(--text); }

/* Buttons */
.btn {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-text);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0.45rem 0.9rem;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover { text-decoration: none; filter: brightness(1.08); }
.btn.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn.danger { background: var(--danger); }
.btn.small { padding: 0.25rem 0.6rem; font-size: 0.85rem; }

button.icon {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0 0.4rem;
  border-radius: var(--radius);
}
button.icon:hover { color: var(--danger); }

/* List page */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.toolbar input[type="search"] {
  flex: 1 1 14rem;
}
.check { display: flex; align-items: center; gap: 0.4rem; color: var(--muted); font-size: 0.9rem; }
.count { color: var(--muted); font-size: 0.85rem; }

.contact-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.contact-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding-right: 0.6rem;
}
.contact-list li:hover { border-color: var(--accent); }
.contact-list a {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.65rem 0.9rem;
  color: var(--text);
}
.contact-list a:hover { text-decoration: none; }
.quick-log { flex: none; white-space: nowrap; }
.who { display: flex; flex-direction: column; min-width: 0; }
.who small { color: var(--muted); }
.who strong { font-weight: 600; }

.avatar {
  flex: none;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 600;
}
.avatar.lg { width: 3.4rem; height: 3.4rem; font-size: 1.25rem; }
img.avatar { object-fit: cover; border: 1px solid var(--border); }

.badge {
  margin-left: auto;
  background: var(--border);
  color: var(--muted);
  font-size: 0.75rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.overdue {
  background: color-mix(in srgb, var(--danger) 15%, var(--surface));
  color: var(--danger);
}
.badge.state-ok { background: color-mix(in srgb, #16a34a 16%, var(--surface)); color: #16a34a; }
.badge.state-warn { background: color-mix(in srgb, #d97706 18%, var(--surface)); color: #d97706; }
.badge.state-error { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); color: var(--danger); }
.badge.state-na { background: var(--border); color: var(--muted); }
.status-table td:nth-child(2) { white-space: nowrap; }

.timeline { margin-top: 1rem; }
.timeline small { display: block; }
.log-form { margin-bottom: 0.5rem; }

/* Tags / chips */
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.75rem; }
.chip {
  --chip: var(--accent);
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  background: color-mix(in srgb, var(--chip) 14%, var(--surface));
  border: 1px solid var(--chip);
  color: var(--text);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip.static { cursor: default; }
.chip.active { background: var(--chip); color: #fff; }
.chip small { opacity: 0.7; }
.chip.mini {
  padding: 0 0.45rem;
  font-size: 0.72rem;
  cursor: default;
  border-color: var(--border);
  background: var(--bg);
  color: var(--muted);
}
.row-tags { display: flex; gap: 0.3rem; flex-wrap: wrap; margin-top: 0.15rem; }

/* Graph */
.graph-page .toolbar { align-items: flex-start; flex-direction: column; }
.graph-page h1 { margin: 0; font-size: 1.4rem; }
.legend { display: flex; flex-wrap: wrap; gap: 0.4rem; }
#graph {
  height: min(70vh, 42rem);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Tag management */
.tag-row { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; }
.tag-row .inline-add { flex: 0 1 auto; }

.merge-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.6rem;
  margin-top: 0.75rem;
}
.merge-form label { flex: 1 1 12rem; }
.merge-form .btn { flex: none; }

.empty { color: var(--muted); text-align: center; padding: 2rem 0; }
.muted { color: var(--muted); }

/* Detail page */
.detail-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.detail-id { flex: 1 1 12rem; }
.detail-head h1 { margin: 0; font-size: 1.4rem; }
.detail-head .sub { margin: 0.15rem 0 0; color: var(--muted); }
.detail-head .sub span + span::before { content: " · "; }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.actions form { margin: 0; }

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
@media (min-width: 44rem) {
  .grid { grid-template-columns: 1fr 1fr; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem;
}
.card h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

dl {
  margin: 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.35rem 1rem;
}
dt { color: var(--muted); font-size: 0.9rem; }
dd { margin: 0; }

.notes { white-space: pre-wrap; margin: 0; }

.rows {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
}
.rows li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}
.rows li:last-child { border-bottom: none; }

/* Forms */
input, select, textarea {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.65rem;
  font: inherit;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, button.icon:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="checkbox"] { width: auto; }

label { display: block; font-size: 0.9rem; color: var(--muted); }
label > input, label > select { margin-top: 0.25rem; color: var(--text); }

fieldset {
  border: none;
  margin: 0 0 1rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
legend {
  font-weight: 600;
  color: var(--text);
  padding: 0;
  margin-bottom: 0.5rem;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
}
@media (min-width: 36rem) {
  .field-row { grid-template-columns: 1fr 1fr; }
}

.inline-add {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.inline-add input { flex: 1 1 8rem; }
.inline-add .btn { flex: none; }

.row-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
  align-items: center;
}

.form-page h1 { font-size: 1.4rem; }

/* Auth pages */
.auth-page {
  max-width: 26rem;
  margin: 8vh auto 0;
}
.auth-page h1 { text-align: center; }
.auth-card { display: flex; flex-direction: column; gap: 0.9rem; }
.btn.wide { width: 100%; text-align: center; }
.recovery summary { cursor: pointer; color: var(--muted); font-size: 0.9rem; }
.recovery form { margin-top: 0.75rem; }

.codes {
  list-style: none;
  margin: 1rem 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
  gap: 0.5rem;
}
.codes code {
  display: block;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.6rem;
  text-align: center;
  font-size: 1.02rem;
  letter-spacing: 0.05em;
}

.errors {
  background: color-mix(in srgb, var(--danger) 12%, var(--surface));
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  margin-bottom: 1rem;
}
.errors ul { margin: 0.25rem 0; padding-left: 1.2rem; }

.ok {
  background: color-mix(in srgb, #16a34a 12%, var(--surface));
  border: 1px solid #16a34a;
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  margin: 0.75rem 0 0;
}

table.log {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
table.log th, table.log td {
  text-align: left;
  padding: 0.3rem 0.5rem;
  border-bottom: 1px solid var(--border);
}
table.log th { color: var(--muted); font-weight: 500; }
