:root {
  --bg: #f4f4f2;
  --panel: #ffffff;
  --bubble: #ffffff;
  --text: #1c1c1e;
  --muted: #6c6c72;
  --border: #e2e2de;
  --hover: #f0f0ed;
  --accent: #2f6feb;
  --on-accent: #ffffff;
  --mark: #ffe27a;
  --danger: #d1242f;
  --radius: 14px;
  --col: 760px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141517;
    --panel: #1b1c1f;
    --bubble: #222428;
    --text: #e9e9eb;
    --muted: #9a9aa2;
    --border: #303237;
    --hover: #2a2c31;
    --accent: #5b8cff;
    --mark: #6d5b10;
    --danger: #ff6b6b;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
a { color: var(--accent); }
.visually-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ---------- buttons ---------- */
.btn {
  min-height: 40px; padding: 8px 16px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel);
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--on-accent); }
.btn:disabled { opacity: .5; cursor: default; }
button.icon {
  flex: none; width: 44px; height: 44px; padding: 0;
  display: grid; place-items: center;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted);
}
button.icon:hover { background: var(--hover); color: var(--text); }
button.icon svg { width: 22px; height: 22px; }
button.send { background: var(--accent); color: var(--on-accent); width: 40px; height: 40px; margin: 2px; }
button.send:hover { background: var(--accent); color: var(--on-accent); filter: brightness(1.08); }
button.send:disabled { opacity: .35; cursor: default; filter: none; }

/* ---------- login ---------- */
#login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
#login-form { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
#login-form img { align-self: center; border-radius: 14px; }
#login-form h1 { margin: 0 0 8px; font-size: 24px; }
#login-form input[type=password] {
  padding: 12px 14px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--panel);
}
.error { color: var(--danger); margin: 0; font-size: 14px; }

/* ---------- app shell ---------- */
#app { height: var(--app-h, 100dvh); display: flex; flex-direction: column; }
.col { width: min(var(--col), 100%); margin: 0 auto; }

header {
  flex: none; background: var(--panel); border-bottom: 1px solid var(--border);
  padding: calc(6px + env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
}
header .col { display: flex; align-items: center; gap: 6px; }
header h1 { font-size: 17px; font-weight: 650; margin: 0 6px 0 4px; }
.search {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px;
  padding: 0 10px; height: 38px; border-radius: 10px; background: var(--bg); border: 1px solid var(--border);
}
.search svg { width: 17px; height: 17px; color: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; font-size: 16px; padding: 0; }

#scroller {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column;
  padding: 0 max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
}
#top-sentinel { height: 1px; flex: none; }
#list {
  width: min(var(--col), 100%); margin: auto auto 0; /* push to bottom when short */
  display: flex; flex-direction: column; gap: 8px; padding: 12px 0;
}
.hint { text-align: center; color: var(--muted); font-size: 14px; padding: 16px; }
#empty { margin-bottom: auto; }

.day {
  align-self: center; margin: 6px 0 2px; padding: 2px 10px;
  font-size: 12px; color: var(--muted);
  background: var(--panel); border: 1px solid var(--border); border-radius: 999px;
}

/* ---------- messages ---------- */
.msg {
  min-width: 0; padding: 9px 12px 4px;
  background: var(--bubble); border: 1px solid var(--border); border-radius: var(--radius);
}
.msg.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 3px var(--accent); } to { box-shadow: 0 0 0 3px transparent; } }
.body { white-space: pre-wrap; overflow-wrap: anywhere; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 4px; margin-top: 8px; }
.gallery a { display: block; border-radius: 8px; overflow: hidden; background: var(--hover); }
/* Grid of several images: square crops. */
.gallery:not(.single) img { display: block; width: 100%; height: 100%; aspect-ratio: 1; object-fit: cover; }
/* Single image: natural shape. Its width/height attributes give the aspect
   ratio, so space is reserved before it loads (no jump while scrolling). */
.gallery.single { display: block; }
.gallery.single a { display: inline-block; max-width: 100%; vertical-align: top; }
.gallery.single img { display: block; width: auto; max-width: 100%; height: auto; max-height: 420px; }

.media { display: block; width: 100%; max-height: 420px; margin-top: 8px; border-radius: 8px; background: #000; }
audio.media { background: transparent; height: 44px; }

.file {
  display: flex; align-items: center; gap: 10px; margin-top: 8px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 10px; color: inherit; text-decoration: none;
}
.file:hover { background: var(--hover); }
.file svg { width: 22px; height: 22px; color: var(--muted); flex: none; }
.file-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { color: var(--muted); font-size: 13px; flex: none; }

.preview {
  display: flex; gap: 10px; margin-top: 8px; padding: 8px 10px;
  border-left: 3px solid var(--accent); border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 7%, transparent);
  color: inherit; text-decoration: none;
}
.preview:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.preview > div { min-width: 0; flex: 1; }
.preview img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; flex: none; order: 2; }
.p-site { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-title { font-weight: 600; font-size: 15px; overflow-wrap: anywhere; }
.p-desc {
  font-size: 14px; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

.meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); margin-top: 2px; }
.meta .more { width: 36px; height: 28px; border-radius: 8px; }
.meta .more svg { width: 18px; height: 18px; fill: currentColor; stroke: none; }
@media (hover: hover) and (pointer: fine) {
  .msg .more { opacity: 0; }
  .msg:hover .more, .msg .more:focus-visible, .msg .more[aria-expanded=true] { opacity: 1; }
}

/* ---------- editing ---------- */
.msg.editing { border-color: var(--accent); padding-bottom: 9px; }
.msg.editing textarea {
  display: block; width: 100%; min-height: 90px; max-height: 50dvh; resize: vertical;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-size: 16px;
}
.edit-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.edit-files:empty { display: none; }
.edit-actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }

/* ---------- chips (pending uploads, edit attachments) ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 3px 3px 3px 8px; font-size: 13px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--panel);
}
.chip img { width: 32px; height: 32px; object-fit: cover; border-radius: 6px; margin-left: -4px; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip .size { color: var(--muted); flex: none; }
.chip.new { border-style: dashed; }
.chip button { width: 30px; height: 30px; border-radius: 8px; }
.chip button svg { width: 16px; height: 16px; }

/* ---------- composer ---------- */
footer {
  flex: none; background: var(--panel); border-top: 1px solid var(--border);
  padding: 6px max(8px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}
#pending { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 0 6px; }
#progress { height: 3px; margin: 0 0 6px; border-radius: 2px; background: var(--border); overflow: hidden; }
#progress > div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.row { display: flex; align-items: flex-end; gap: 4px; }
#text {
  flex: 1; min-width: 0; resize: none; max-height: 40dvh;
  padding: 9px 14px; font-size: 16px; line-height: 1.35;
  border: 1px solid var(--border); border-radius: 20px; background: var(--bg); outline: none;
}
#text:focus { border-color: var(--accent); }

/* ---------- overlays ---------- */
#drop {
  position: fixed; inset: 0; z-index: 20; pointer-events: none;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border: 3px dashed var(--accent);
}
#drop div { background: var(--panel); padding: 12px 20px; border-radius: 12px; font-weight: 600; }

.menu {
  position: fixed; z-index: 30; min-width: 170px; padding: 4px;
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
}
.menu button { min-height: 44px; padding: 0 14px; text-align: left; border: 0; border-radius: 8px; background: none; }
.menu button:hover { background: var(--hover); }
.danger { color: var(--danger) !important; }

#toast {
  position: fixed; z-index: 40; left: 50%; transform: translateX(-50%);
  bottom: calc(80px + env(safe-area-inset-bottom)); max-width: calc(100% - 32px);
  padding: 8px 16px; border-radius: 999px; font-size: 14px;
  background: var(--text); color: var(--bg);
}
