/* Posts theme: clean indigo look, responsive from phones to wide desktops,
   follows the device's light / dark setting. */
/* Colours live here. Dark values are applied when the device is in dark mode,
   unless the user picked a theme with the toggle (data-theme on <html>). */
:root {
  color-scheme: light dark;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --text: #141a2e;
  --muted: #5d6678;
  --border: #e2e6ef;
  --border-strong: #c3cadb;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef0ff;
  --accent-text: #4338ca;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --danger-text: #991b1b;
  --ring: rgba(79, 70, 229, .35);
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 10px 26px -14px rgba(16, 24, 40, .22);
  --btn-shadow: 0 1px 2px rgba(16, 24, 40, .12), 0 6px 14px -6px rgba(79, 70, 229, .55);
  --radius: 14px;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1120; --surface: #171b2e; --surface-2: #1d2238; --text: #e8eaf4; --muted: #9aa3ba;
  --border: #2b3150; --border-strong: #454d78; --primary: #6366f1; --primary-hover: #7c7ff5;
  --primary-soft: #232850; --accent-text: #c7d2fe; --danger-soft: #3a1618; --danger-border: #6b2326;
  --danger-text: #fecaca; --ring: rgba(129, 140, 248, .45);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 26px -14px rgba(0, 0, 0, .65);
  --btn-shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(99, 102, 241, .6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1120; --surface: #171b2e; --surface-2: #1d2238; --text: #e8eaf4; --muted: #9aa3ba;
    --border: #2b3150; --border-strong: #454d78; --primary: #6366f1; --primary-hover: #7c7ff5;
    --primary-soft: #232850; --accent-text: #c7d2fe; --danger-soft: #3a1618; --danger-border: #6b2326;
    --danger-text: #fecaca; --ring: rgba(129, 140, 248, .45);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 26px -14px rgba(0, 0, 0, .65);
    --btn-shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(99, 102, 241, .6);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
}

img { max-width: 100%; height: auto; }
a { color: var(--primary); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 var(--primary-soft);
  padding: .6rem max(1rem, env(safe-area-inset-right)) .6rem max(1rem, env(safe-area-inset-left));
}
.topbar-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(max-content, 1fr);  /* title stays centred; the buttons side never gets squeezed */
  align-items: center;
  gap: .5rem;
}
.topbar-left, .topbar-actions { display: flex; align-items: center; gap: .5rem; min-width: 0; flex-wrap: nowrap; }
.topbar-left { justify-self: start; overflow: hidden; padding-right: .5rem; }
.topbar-left .btn { flex: none; }
.topbar-actions { justify-self: end; justify-content: flex-end; }
.topbar-actions > * { flex: none; }
.topbar form { margin: 0; }
.brand {
  display: flex;
  align-items: center;
  gap: .6rem;
  color: var(--text);
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -.01em;
  text-decoration: none;
  white-space: nowrap;
}
.brand img { width: 36px; height: 36px; display: block; border-radius: 10px; }

/* current database: small and quiet, top left */
.db-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: .85rem;
  font-weight: 600;
}

/* ---------- layout ---------- */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem max(1rem, env(safe-area-inset-right)) 3rem max(1rem, env(safe-area-inset-left));
}
.container > .card:not(.empty) { max-width: 720px; margin-left: auto; margin-right: auto; }

h1 { margin: 0 0 .5rem; font-size: clamp(1.5rem, 1.2rem + 1.5vw, 2rem); line-height: 1.2; font-weight: 800; letter-spacing: -.02em; }
h2 { margin: 0; font-size: 1.2rem; line-height: 1.3; font-weight: 700; overflow-wrap: anywhere; }
.lead { margin: 0 0 1.25rem; color: var(--muted); }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.page-head h1 { margin: 0 0 .35rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(1rem, 3vw, 1.5rem);
  margin-bottom: 1.1rem;
  box-shadow: var(--shadow);
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  min-height: 46px;
  padding: .55rem 1.3rem;
  border: 1.5px solid transparent;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: var(--btn-shadow);
  transition: background .15s ease, color .15s ease, transform .05s ease, box-shadow .15s ease;
}
.btn:hover { background: var(--primary-hover); }
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:focus-visible, .field:focus-visible, .db-option:has(input:focus-visible), a:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 2px;
}
/* outlined accent button: clearly a button, but quieter than the solid one */
.btn-secondary { background: var(--primary-soft); color: var(--accent-text); border-color: var(--primary); box-shadow: none; }
.btn-secondary:hover { background: var(--primary); color: #fff; }
/* neutral, for Cancel */
.btn-neutral { background: var(--surface); color: var(--text); border-color: var(--border-strong); box-shadow: none; }
.btn-neutral:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); box-shadow: 0 1px 2px rgba(16, 24, 40, .12), 0 6px 14px -6px rgba(220, 38, 38, .55); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-xs { min-height: 34px; padding: .2rem .85rem; font-size: .85rem; font-weight: 600; border-radius: 8px; }
/* quiet buttons: outlined, neutral until hovered (post Edit / Delete) */
.btn-quiet { background: transparent; color: var(--muted); border-color: var(--border-strong); box-shadow: none; }
.btn-quiet:hover { background: var(--primary-soft); color: var(--accent-text); border-color: var(--primary); }
.btn-quiet-danger:hover { background: var(--danger-soft); color: var(--danger-text); border-color: var(--danger-border); }
.btn-sm { min-height: 42px; padding: .35rem 1rem; font-size: .98rem; }
.icon-btn { width: 42px; padding: 0; }
.icon-btn svg, .only-narrow { display: block; width: 18px; height: 18px; }
.only-narrow { display: none; }
.btn-disabled, .btn:disabled { opacity: .45; pointer-events: none; box-shadow: none; }

/* ---------- forms ---------- */
label { display: block; margin: 1rem 0 .35rem; font-weight: 600; }
label:first-of-type { margin-top: 0; }

.field {
  display: block;
  width: 100%;
  padding: .7rem .85rem;
  color: var(--text);
  font: inherit;
  font-size: 1rem;              /* 16px+ stops iOS zooming into fields */
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px var(--ring); }
textarea.field { min-height: 9rem; resize: vertical; }

input[type="file"] { display: block; width: 100%; font: inherit; color: var(--muted); }
input[type="file"]::file-selector-button {
  margin-right: .8rem;
  padding: .5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.form-group { margin-top: 1rem; }
.form-group label { margin-top: 0; }
.form-actions { display: flex; flex-wrap: wrap; gap: .75rem; margin-top: 1.4rem; }

.current-image { display: block; max-width: 100%; max-height: 200px; border-radius: 10px; border: 1px solid var(--border); }
label.check { display: flex; align-items: center; gap: .6rem; margin: .7rem 0 0; font-weight: 500; cursor: pointer; }
label.check input { width: 1.15rem; height: 1.15rem; margin: 0; accent-color: var(--danger); }
.form-group label.spaced { margin-top: 1.1rem; }

/* ---------- database choice ---------- */
.db-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr)); gap: .65rem; }
.db-option {
  display: flex;
  align-items: center;
  gap: .8rem;
  margin: 0;
  padding: .85rem 1rem;
  min-height: 52px;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: border-color .15s ease, background .15s ease;
}
.db-option:hover { border-color: var(--primary); }
.db-option.is-disabled { opacity: .55; cursor: not-allowed; filter: grayscale(1); background: var(--bg); }
.db-option.is-disabled:hover { border-color: var(--border); }
.db-tag { margin-left: auto; padding: .05rem .6rem; border: 1px solid var(--border-strong); border-radius: 999px; color: var(--muted); font-size: .75rem; font-weight: 600; white-space: nowrap; }
.note { margin: 1rem 0 0; color: var(--muted); font-size: .92rem; }
.db-option input { width: 1.25rem; height: 1.25rem; margin: 0; accent-color: var(--primary); flex: none; }
.db-option:has(input:checked) { background: var(--primary-soft); border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }

/* ---------- posts ---------- */
.count { margin: 0; color: var(--muted); }

.post-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 1.1rem; align-items: start; }
.post-grid .card { margin: 0; display: flex; flex-direction: column; gap: .8rem; }
.post-head { display: flex; align-items: center; gap: .75rem; }
.avatar {
  flex: none;
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 800;
}
.post-meta { margin: 0; color: var(--muted); font-size: .88rem; }
.post-body { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.post-image { width: 100%; border-radius: 10px; display: block; }
.post-actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; padding-top: .7rem; border-top: 1px solid var(--border); }
.post-actions form { margin: 0; }
.empty { text-align: center; padding: 2.5rem 1rem; max-width: 520px; margin: 0 auto; }
.empty p { margin: .4rem 0 1.2rem; color: var(--muted); }

/* ---------- posts: list view, one line per post (switch in the top bar; both views are in the page, so switching is instant) ---------- */
#view-toggle .ico-cards { display: none; }
:root[data-view="list"] #view-toggle .ico-list { display: none; }
:root[data-view="list"] #view-toggle .ico-cards { display: block; }
.post-list { display: none; }
:root[data-view="list"] .post-grid { display: none; }
:root[data-view="list"] .post-list {
  display: block;
  max-width: 820px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.post-row {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .5rem .85rem;
  line-height: 1.35;
  color: inherit;
  text-decoration: none;
  border-top: 1px solid var(--border);
}
.post-row:first-child { border-top: 0; }
.post-row:hover { background: var(--primary-soft); }
.post-row:focus-visible { outline: 3px solid var(--ring); outline-offset: -3px; }
.row-author, .row-snippet, .row-date { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-author { flex: 0 0 5.2rem; font-weight: 700; }
.row-snippet { flex: 1 1 auto; min-width: 0; color: var(--muted); font-size: .93rem; }
.row-date { flex: none; color: var(--muted); font-size: .8rem; }
@media (min-width: 700px) {
  .post-row { gap: 1rem; padding: .55rem 1rem; }
  .row-author { flex-basis: 9rem; }
}

/* card view: a pager inside the fetched page (only shown when CARDS_PER_PAGE is smaller than the page) */
.is-off { display: none !important; }
nav.card-pager { display: none; }
:root[data-view="card"] nav.card-pager.is-on { display: flex; }

/* ---------- pager ---------- */
.pager { display: flex; justify-content: space-between; align-items: center; gap: .6rem; margin: 1.4rem 0 .5rem; }
.pager-label { color: var(--muted); font-weight: 600; text-align: center; }

/* ---------- messages ---------- */
.alert {
  margin: 0 0 1rem;
  padding: .8rem 1rem;
  background: var(--danger-soft);
  color: var(--danger-text);
  border: 1px solid var(--danger-border);
  border-radius: 10px;
  overflow-wrap: anywhere;
}
.container > .card.narrow { max-width: 420px; }
.center { text-align: center; }

/* ---------- small screens ---------- */
@media (max-width: 520px) {
  .topbar { padding-top: .5rem; padding-bottom: .5rem; }
  .brand { font-size: 1.15rem; gap: .4rem; }
  .brand img { display: none; }
  .topbar .btn-sm { min-height: 38px; padding: .25rem .65rem; font-size: .88rem; }
  .topbar .icon-btn { width: 38px; padding: 0; }
  .topbar-left, .topbar-actions { gap: .35rem; }
  .db-name { font-size: .78rem; }
  .btn-text { display: none; }          /* Exit and Log out shrink to icons so the bar stays on one line */
  .only-narrow { display: block; }
  .form-actions .btn { flex: 1 1 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .btn, .field, .db-option { transition: none; }
}
