:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #dde1e7;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --error: #b42318;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1d2129;
    --text: #e6e8eb;
    --muted: #9aa3af;
    --border: #2e343e;
    --accent: #5b8ef0;
    --error: #f97066;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

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); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 600; color: var(--text); text-decoration: none; }

.topbar-user { display: flex; align-items: center; gap: 12px; min-width: 0; }
.topbar-user form { margin: 0; }

.page { max-width: 960px; margin: 0 auto; padding: 16px; }

h1 { font-size: 1.5rem; margin: 0 0 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 16px;
}

.narrow { max-width: 400px; margin: 32px auto; }

.form { display: flex; flex-direction: column; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }

input {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}

button {
  font: inherit;
  padding: 10px 16px;
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

.link-button {
  padding: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
}

.muted { color: var(--muted); }
.error { color: var(--error); margin: 0 0 12px; }
.empty { color: var(--muted); }

h2 { font-size: 1.15rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 20px 0 10px; }

.card + .card, .rows + .card, .empty + .card { margin-top: 16px; }

.topbar-nav { display: flex; align-items: center; gap: 16px; }

.rows { list-style: none; margin: 0 0 16px; padding: 0; }
.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.row form { margin: 0; }

.small { font-size: 0.875rem; }

.badge {
  font-size: 0.8rem;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.form .checkbox { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.checkbox input { width: auto; }

textarea, select {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
textarea { resize: vertical; }

.heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  margin-bottom: 16px;
}
.heading h1 { margin: 0; }

.button {
  display: inline-block;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
}

.filter { margin-bottom: 16px; max-width: 320px; }
.filter label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }

.group + .group { margin-top: 24px; }
.group h2 a { color: var(--text); text-decoration: none; }

.facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 6px 16px;
  margin: 0 0 16px;
}
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }

.description { white-space: pre-wrap; overflow-wrap: anywhere; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.actions form { margin: 0; }
.danger { background: var(--error); }

.drag-handle {
  cursor: grab;
  touch-action: none;
  padding: 4px 6px;
  color: var(--muted);
  user-select: none;
  letter-spacing: -2px;
}
.row:has(.drag-handle) { justify-content: flex-start; }
.row:has(.drag-handle) > .badge { margin-left: auto; }
.sortable-ghost { opacity: 0.4; }

.inline-form { flex-direction: row; align-items: flex-end; gap: 12px; }
.inline-form label { flex: 1; max-width: 200px; }

.feed { margin-top: 16px; }
.events { list-style: none; margin: 0 0 16px; padding: 0; }
.event { padding: 10px 0; border-bottom: 1px solid var(--border); }
.event:last-child { border-bottom: 0; }
.comment { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 4px; }
.event-status, .event-priority { color: var(--muted); }

.attachments { list-style: none; margin: 8px 0 0; padding: 0; }
.attachments li { padding: 2px 0; overflow-wrap: anywhere; }
.attachments li::before { content: "📎 "; }
input[type="file"] { padding: 8px; }

.invoice-lines { width: 100%; border-collapse: collapse; margin: 16px 0; }
.invoice-lines th, .invoice-lines td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; }
.invoice-lines .num { text-align: right; white-space: nowrap; }
.invoice-lines tfoot th { border-bottom: 0; font-size: 1.1rem; }

@media print {
  .topbar, .no-print { display: none !important; }
  body { background: #fff; color: #000; }
  .page { max-width: none; padding: 0; }
}

.row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; }
