:root {
  --bg: #f5f4fa;
  --card: #ffffff;
  --ink: #1f1d2e;
  --muted: #706d85;
  --accent: #5b4bd6;
  --accent-dark: #4638b3;
  --accent-soft: #ebe8fb;
  --border: #e2e0ef;
  --danger: #c0392b;
  --radius: 12px;
  color-scheme: light;
}

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

/* Press feedback on every button */
button { transition: transform 0.08s ease; font: inherit; }
button:active { transform: scale(0.95); }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 1.25rem; }
.topbar-actions { display: flex; gap: 8px; }

.container { max-width: 860px; margin: 0 auto; padding: 16px; padding-bottom: calc(40px + env(safe-area-inset-bottom)); }

.btn {
  border: 1px solid transparent; border-radius: 10px; padding: 9px 14px;
  font-weight: 600; font-size: 0.92rem; cursor: pointer; background: none; color: var(--ink);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { border-color: var(--border); background: var(--card); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-small { padding: 6px 11px; font-size: 0.84rem; }
.btn-danger-ghost { border-color: var(--border); background: var(--card); color: var(--danger); }

.search {
  width: 100%; padding: 11px 14px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 1rem; background: var(--card); color: var(--ink);
}
.search:focus, .field input:focus, .field select:focus, .field textarea:focus, .inline-row input:focus {
  outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.chip {
  border: 1px solid var(--border); background: var(--card); color: var(--ink);
  padding: 7px 13px; border-radius: 999px; font-size: 0.88rem; font-weight: 600; cursor: pointer;
}
.chip .cnt { color: var(--muted); font-weight: 500; margin-left: 4px; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip.active .cnt { color: #d9d4ff; }

.list { display: grid; gap: 12px; }
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: grid; gap: 8px;
}
.card-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tag {
  background: var(--accent-soft); color: var(--accent-dark);
  font-size: 0.76rem; font-weight: 700; padding: 3px 9px; border-radius: 999px;
}
.card-title { font-weight: 700; font-size: 1rem; line-height: 1.3; word-break: break-word; }
.card-text {
  margin: 0; white-space: pre-wrap; word-break: break-word; line-height: 1.45;
  font-size: 0.92rem; color: #3b3951; cursor: pointer;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.card.expanded .card-text { display: block; overflow: visible; }
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; padding-top: 4px; }

.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
.muted { color: var(--muted); margin: 0; word-break: break-word; }

/* Dialogs */
.dlg {
  border: none; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px); background: var(--card); color: var(--ink);
  box-shadow: 0 20px 60px rgba(20, 15, 60, 0.35);
}
.dlg-small { width: min(400px, calc(100vw - 24px)); }
.dlg::backdrop { background: rgba(20, 15, 40, 0.5); }
.dlg-body { padding: 20px; display: grid; gap: 14px; }
.dlg-body h2 { margin: 0; font-size: 1.15rem; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }

.field { display: grid; gap: 6px; }
.field > span { font-size: 0.85rem; font-weight: 600; }
.field i { color: var(--muted); font-weight: 400; }
.req { color: var(--danger); }
.field input, .field select, .field textarea, .inline-row input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  font: inherit; font-size: 1rem; background: #fff; color: var(--ink);
}
.field textarea { resize: vertical; min-height: 160px; line-height: 1.45; }
.field.invalid select, .field.invalid textarea { border-color: var(--danger); }

.inline-row { display: flex; gap: 8px; }
.inline-row input { flex: 1; min-width: 0; }

.cats-list { display: grid; gap: 8px; max-height: 50dvh; overflow-y: auto; }
.cat-row { display: flex; gap: 8px; align-items: center; }
.cat-row input { flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.cat-row .cnt { color: var(--muted); font-size: 0.82rem; min-width: 22px; text-align: right; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(16px); max-width: calc(100vw - 48px);
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 10px;
  font-size: 0.88rem; font-weight: 600; line-height: 1.35; text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3); opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease; z-index: 1000;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: #a8321f; }
.toast.toast-success { background: #2f7a45; }

@media (max-width: 480px) {
  .topbar-actions .btn { padding: 8px 11px; }
  .cat-row { flex-wrap: wrap; }
}
