:root {
  color-scheme: light dark;
  --bg: #f3f1ec;
  --panel: #ffffff;
  --line: #e4e0d8;
  --ink: #1e2320;
  --muted: #6c746f;
  --accent: #1f7a5c;
  --on-accent: #ffffff;
  --mine: #d8efe3;
  --theirs: #ffffff;
  --chat-bg: #ece8df;
  --tile: #d9d5cb;
  --mark: #ffe39b;
  --shadow: 0 1px 1px rgb(0 0 0 / 10%);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101412;
    --panel: #181d1b;
    --line: #2a312e;
    --ink: #e7ece9;
    --muted: #93a09a;
    --accent: #55c29b;
    --on-accent: #06251b;
    --mine: #1f4a3a;
    --theirs: #232a27;
    --chat-bg: #111614;
    --tile: #2b3330;
    --mark: #7a5c10;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, p { margin: 0; }
a { color: var(--accent); }

.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(340px, 100%); padding: 28px; display: grid; gap: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
}
.login-card h1 { font-size: 22px; margin-bottom: 8px; }
.login-card label { font-size: 13px; color: var(--muted); }
.login-card input { padding: 11px 12px; border: 1px solid var(--line); border-radius: 9px; background: transparent; font-size: 16px; }
.login-card button { padding: 11px; border: 0; border-radius: 9px; background: var(--accent); color: var(--on-accent); font-weight: 600; }
.login-error { min-height: 20px; font-size: 14px; color: #c0392b; }

.app { height: 100dvh; display: grid; grid-template-columns: minmax(300px, 380px) 1fr; }
.sidebar { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; padding-top: max(14px, env(safe-area-inset-top)); }
.side-head h1 { font-size: 22px; }
.quiet { border: 0; background: none; color: var(--muted); padding: 6px 8px; border-radius: 7px; font-size: 14px; }
.quiet:hover { background: var(--bg); }
.wide { width: 100%; padding: 12px; }

.search { padding: 2px 16px 8px; }
.search input[type=search] { width: 100%; padding: 9px 12px; border: 0; border-radius: 9px; background: var(--bg); font-size: 16px; outline-offset: 0; }
.scope { display: flex; gap: 6px; align-items: center; margin-top: 7px; font-size: 13px; color: var(--muted); }
.filters { display: flex; gap: 6px; padding: 0 16px 10px; }
.filters button { border: 0; background: var(--bg); color: var(--muted); padding: 5px 11px; border-radius: 99px; font-size: 13px; }
.filters button.on { background: var(--accent); color: var(--on-accent); }

.side-scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: env(safe-area-inset-bottom); }
.side-note { padding: 18px 16px; color: var(--muted); font-size: 14px; }
#results h2 { font-size: 13px; color: var(--muted); padding: 12px 16px 6px; font-weight: 600; }

.row {
  display: flex; gap: 11px; align-items: center; width: 100%; padding: 9px 16px; border: 0; background: none; text-align: left;
  content-visibility: auto; contain-intrinsic-size: auto 62px;
}
.row:hover { background: var(--bg); }
.row.active { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.row-text { min-width: 0; flex: 1; }
.row-top { display: flex; justify-content: space-between; gap: 8px; }
.row-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { font-size: 12px; color: var(--muted); flex: none; }
.row-sub { font-size: 13.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hit-sub { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
mark { background: var(--mark); color: inherit; border-radius: 2px; }

.avatar {
  flex: none; width: 44px; height: 44px; border-radius: 50%; overflow: hidden; display: grid; place-items: center;
  font-weight: 600; font-size: 16px; color: #fff;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.chat-head .avatar { width: 38px; height: 38px; font-size: 14px; }

.pane { min-width: 0; min-height: 0; background: var(--chat-bg); position: relative; }
.empty { height: 100%; display: grid; place-items: center; color: var(--muted); }
.chat { height: 100%; display: flex; flex-direction: column; position: relative; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 9px 14px; padding-top: max(9px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.back { display: none; font-size: 26px; line-height: 1; padding: 2px 8px; }
.head-text { min-width: 0; flex: 1; }
.head-text h2 { font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-text p { font-size: 12.5px; color: var(--muted); }
.jump { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); flex: none; }
.jump input { border: 1px solid var(--line); border-radius: 7px; background: transparent; padding: 4px 6px; font-size: 13px; }

.scroller { flex: 1; min-height: 0; overflow-y: auto; overflow-anchor: none; -webkit-overflow-scrolling: touch; }
.messages { max-width: 860px; margin: 0 auto; padding: 14px 16px calc(16px + env(safe-area-inset-bottom)); }
.day { text-align: center; margin: 12px 0; }
.day span, .sys span {
  display: inline-block; padding: 3px 11px; border-radius: 8px; background: var(--panel); color: var(--muted);
  font-size: 12.5px; box-shadow: var(--shadow);
}
.sys { text-align: center; margin: 8px 0; }

.msg { display: flex; margin: 2px 0; }
.msg.me { justify-content: flex-end; }
.bubble {
  max-width: min(78%, 520px); min-width: 90px; padding: 6px 9px 5px; border-radius: 10px;
  background: var(--theirs); box-shadow: var(--shadow); overflow-wrap: anywhere;
}
.me .bubble { background: var(--mine); }
.flash .bubble { outline: 2px solid var(--accent); }
.sender { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.text { white-space: pre-wrap; }
.deleted { color: var(--muted); font-style: italic; }
.foot { font-size: 11px; color: var(--muted); text-align: right; margin-top: 1px; }
.quote {
  display: block; width: 100%; text-align: left; border: 0; border-left: 3px solid var(--accent); border-radius: 5px; padding: 4px 8px;
  margin-bottom: 5px; font-size: 13px; color: var(--muted); background: color-mix(in srgb, var(--ink) 6%, transparent);
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
}

.tile {
  position: relative; display: block; width: min(320px, 62vw); padding: 0; border: 0; border-radius: 7px; overflow: hidden;
  background: var(--tile); margin-bottom: 4px;
}
.tile img { display: block; width: 100%; height: 100%; object-fit: cover; }
.tile.blur img { filter: blur(6px); transform: scale(1.08); }
.tile .play {
  position: absolute; inset: 50% auto auto 50%; transform: translate(-50%, -50%); width: 54px; height: 54px; border-radius: 50%;
  background: rgb(0 0 0 / 55%); color: #fff; display: grid; place-items: center; font-size: 20px;
}
.tile .length { position: absolute; right: 6px; bottom: 6px; color: #fff; background: rgb(0 0 0 / 55%); border-radius: 5px; font-size: 11.5px; padding: 1px 5px; }
.player { display: block; width: min(320px, 62vw); max-height: 70vh; border-radius: 7px; background: #000; margin-bottom: 4px; }
audio { display: block; width: min(280px, 62vw); height: 42px; margin-bottom: 3px; }
.sticker { display: block; width: 130px; height: 130px; object-fit: contain; }
.card {
  display: flex; flex-direction: column; gap: 1px; padding: 8px 10px; margin-bottom: 4px; border-radius: 7px; text-decoration: none;
  color: inherit; background: color-mix(in srgb, var(--ink) 6%, transparent); min-width: 180px;
}
.card b { font-weight: 600; overflow-wrap: anywhere; }
.card small { color: var(--muted); }
.gone { padding: 9px 10px; margin-bottom: 4px; border-radius: 7px; font-size: 13px; color: var(--muted); background: color-mix(in srgb, var(--ink) 6%, transparent); }
.loading { text-align: center; color: var(--muted); font-size: 13px; padding: 8px; }

.to-latest {
  position: absolute; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); border: 1px solid var(--line); border-radius: 99px;
  background: var(--panel); color: var(--ink); padding: 7px 13px; font-size: 13px; box-shadow: 0 2px 8px rgb(0 0 0 / 18%);
}

.head-open { min-width: 0; flex: 1; display: flex; align-items: center; gap: 10px; padding: 2px 4px; border: 0; border-radius: 8px; background: none; text-align: left; }
.head-open:hover { background: var(--bg); }
.head-text { display: flex; flex-direction: column; }
.head-name { font-size: 16px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-count { font-size: 12.5px; color: var(--muted); }
.app.in-gallery .back { display: block; }
.app.in-gallery #jump-wrap, .app.in-gallery .to-latest, .app.in-gallery .scroller { display: none; }
.gallery { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 3px; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 3px; }
.grid-tile { position: relative; aspect-ratio: 1; padding: 0; border: 0; overflow: hidden; background: var(--tile); content-visibility: auto; contain-intrinsic-size: auto 120px; }
.grid-tile img { display: block; width: 100%; height: 100%; object-fit: cover; }
.grid-tile .length { position: absolute; left: 5px; bottom: 5px; color: #fff; background: rgb(0 0 0 / 60%); border-radius: 5px; font-size: 11.5px; padding: 1px 5px; }

.import { width: min(460px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 22px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--ink); }
.import::backdrop { background: rgb(0 0 0 / 50%); }
.import h2 { font-size: 19px; margin-bottom: 12px; }
.import ol { margin: 0 0 12px; padding-left: 20px; line-height: 1.55; }
.import-note { font-size: 13.5px; color: var(--muted); margin: 6px 0 12px; }
.import label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.import input[list] { width: 100%; padding: 10px 11px; border: 1px solid var(--line); border-radius: 9px; background: transparent; font-size: 16px; }
.import input[type=file] { width: 100%; font-size: 15px; }
.import progress { width: 100%; margin-top: 8px; }
.import-status { min-height: 20px; margin-top: 10px; font-size: 14px; color: var(--muted); }
.import fieldset { border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px; margin: 0; }
.import legend { font-size: 13px; color: var(--muted); padding: 0 5px; }
.import fieldset label { display: flex; gap: 8px; align-items: center; margin: 7px 0; color: var(--ink); font-size: 15px; overflow-wrap: anywhere; }
.import-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.primary { border: 0; border-radius: 8px; padding: 9px 15px; background: var(--accent); color: var(--on-accent); font-weight: 600; }
.primary:disabled { opacity: 0.55; }

.viewer { width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0; background: rgb(0 0 0 / 94%); }
.viewer[open] { display: grid; place-items: center; }
.viewer img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.viewer-stage { width: 100vw; height: 100dvh; display: grid; place-items: center; }
.viewer-stage video { max-width: 100vw; max-height: 100dvh; }
.viewer button, .viewer-save {
  border: 0; background: rgb(255 255 255 / 15%); color: #fff; border-radius: 8px; padding: 8px 12px; text-decoration: none; font-size: 14px;
}
#viewer-close { position: fixed; top: max(14px, env(safe-area-inset-top)); right: 14px; z-index: 2; }
.viewer .viewer-nav { position: fixed; top: 50%; transform: translateY(-50%); z-index: 2; width: 44px; height: 64px; padding: 0; font-size: 30px; }
.viewer .viewer-nav.prev { left: 10px; }
.viewer .viewer-nav.next { right: 10px; }
.viewer-actions { position: fixed; bottom: max(14px, env(safe-area-inset-bottom)); right: 14px; z-index: 2; display: flex; gap: 8px; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .pane { display: none; }
  .app.in-chat .sidebar { display: none; }
  .app.in-chat .pane { display: block; }
  .back { display: block; }
  .sidebar { border-right: 0; }
  .jump span { display: none; }
  .jump input { max-width: 124px; }
  .bubble { max-width: 86%; }
  .messages { padding-left: 10px; padding-right: 10px; }
}
