:root {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #e9edf3;
  --text: #16202c;
  --muted: #64748b;
  --accent: #2f6df6;
  --accent-text: #ffffff;
  --bubble-in: #ffffff;
  --danger: #d9342b;
  --warn: #b45309;
  --ok: #15803d;
  --border: #d6dde7;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 6px 20px rgba(16, 24, 40, .07);
  --topbar-h: 60px;
  --composer-h: 62px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Arabic",
          "Segoe UI Emoji", Tahoma, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1319;
    --surface: #171c24;
    --surface-2: #212934;
    --text: #e8edf4;
    --muted: #93a1b5;
    --accent: #4c86ff;
    --bubble-in: #1d2530;
    --border: #2a3441;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

/* Author styles beat the UA stylesheet, so any rule that sets `display` on an
   element we toggle with the `hidden` attribute would leave it on screen. The
   overlays below are all `display: grid`, so this must win. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* ---------- shared controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .7rem 1.1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: filter .15s ease, background .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { display: flex; width: 100%; margin-top: .6rem; }

.icon-btn {
  background: transparent;
  border: 0;
  color: var(--text);
  font-size: 1.15rem;
  line-height: 1;
  padding: .5rem .6rem;
  border-radius: 9px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.danger:hover { background: var(--danger); color: #fff; }

.link-btn {
  background: none; border: 0; padding: 0;
  color: var(--accent); font: inherit; cursor: pointer; text-decoration: underline;
}

.muted { color: var(--muted); }
.inline { display: inline; }
.stack { display: block; }

.alert {
  padding: .75rem .9rem;
  border-radius: 10px;
  margin: .8rem 0;
  font-size: .95rem;
  border: 1px solid transparent;
}
.alert.error { background: color-mix(in srgb, var(--danger) 12%, transparent); border-color: var(--danger); }
.alert.warn { background: color-mix(in srgb, var(--warn) 14%, transparent); border-color: var(--warn); }

/* ---------- login ---------- */

.auth-page {
  display: grid;
  place-items: center;
  padding: 1.2rem;
  min-height: 100dvh;
}

.auth-card {
  width: 100%;
  max-width: 430px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1.6rem;
}

/* The sign-in card deliberately says nothing: no branding, no clock, no hint
   about how the passcode is formed. Anyone who finds the URL learns nothing. */
.auth-card.minimal { max-width: 340px; padding: 1.4rem; }
.auth-card.minimal .pin-form { display: flex; flex-direction: column; gap: .6rem; }
.auth-card.minimal .btn.block { margin-top: 0; }
.confirm-q { margin: 0 0 1rem; font-size: 1.05rem; text-align: center; word-break: break-word; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.brand { margin: 0 0 .2rem; font-size: 1.7rem; letter-spacing: -.02em; }
.tagline { margin: 0 0 1.1rem; color: var(--muted); font-size: .96rem; }

.clock-box {
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: .9rem;
  text-align: center;
  margin-bottom: 1rem;
}
.clock-label { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.clock {
  font-size: 2.9rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
  margin: .1rem 0;
}
.clock-tz { font-size: .78rem; color: var(--muted); }

.field-label { display: block; font-size: .88rem; color: var(--muted); margin-bottom: .35rem; }

.pin-row { display: flex; gap: .5rem; }

.pin-input {
  flex: 1;
  width: 100%;
  padding: .8rem .9rem;
  font-size: 1.15rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}
.pin-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.parse-preview { min-height: 1.5rem; font-size: .9rem; margin: .55rem 0 .2rem; color: var(--muted); }
.parse-preview .ok { color: var(--ok); font-weight: 600; }
.parse-preview .bad { color: var(--danger); font-weight: 600; }
.parse-preview code {
  background: var(--surface-2); padding: .1rem .35rem; border-radius: 5px; font-size: .95em;
}

.help { margin-top: 1.2rem; font-size: .9rem; color: var(--muted); }
.help summary { cursor: pointer; color: var(--accent); }
.help p { margin: .6rem 0; }
.help code { background: var(--surface-2); padding: .12rem .35rem; border-radius: 5px; }
.chat-name { word-break: break-word; }

/* ---------- chat shell ---------- */

.chat-page {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  padding: 0 .8rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: .6rem; min-width: 0; }
.avatar {
  width: 38px; height: 38px; flex: 0 0 auto;
  border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
  font-weight: 700;
}
.room-name { font-weight: 650; line-height: 1.15; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-sub { font-size: .77rem; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: .15rem; }

.search {
  width: 150px;
  padding: .45rem .65rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: .9rem;
}
@media (max-width: 620px) { .search { width: 90px; } }

.banner, .trash-banner {
  flex: 0 0 auto;
  padding: .6rem .9rem;
  font-size: .9rem;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-bottom: 1px solid var(--border);
}
.trash-banner { background: color-mix(in srgb, var(--warn) 14%, transparent); }

.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem .8rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: .1rem;
}

/* The bubbles live inside #message-list, so that wrapper — not .messages — is
   the flex container their align-self resolves against. */
#message-list {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.load-more {
  align-self: center;
  margin-bottom: .8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: .4rem .9rem;
  border-radius: 999px;
  font: inherit; font-size: .85rem;
  cursor: pointer;
}

.empty-state { margin: auto; text-align: center; color: var(--muted); }

.date-sep {
  align-self: center;
  flex: 0 0 auto;
  margin: .8rem 0 .5rem;
  padding: .2rem .75rem;
  font-size: .76rem;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 999px;
}

/* ---------- message bubbles ---------- */

.msg {
  align-self: flex-end;
  max-width: min(680px, 86%);
  background: var(--bubble-in);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: .55rem .7rem .35rem;
  margin-bottom: .45rem;
  position: relative;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.pending { opacity: .62; }
.msg.failed { border-color: var(--danger); }
.msg.deleted { opacity: .75; border-style: dashed; }

.msg-text { white-space: pre-wrap; }

.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: .45rem;
  margin-top: .25rem;
  font-size: .72rem;
  color: var(--muted);
}

.msg-actions { display: flex; gap: .1rem; }
.msg-actions button, .msg-actions a {
  background: none; border: 0; padding: .15rem .3rem;
  color: var(--muted); cursor: pointer; border-radius: 6px;
  font-size: .82rem; text-decoration: none; line-height: 1;
}
.msg-actions button:hover, .msg-actions a:hover { background: var(--surface-2); color: var(--text); }

.msg-media { display: block; border-radius: 10px; overflow: hidden; margin-bottom: .35rem; }
.msg-media img, .msg-media video {
  display: block; max-width: 100%; max-height: 62vh; border-radius: 10px; background: #000;
}
.msg-media img { cursor: zoom-in; }
.msg-media audio { width: 100%; }

.file-row {
  display: flex; align-items: center; gap: .65rem;
  padding: .35rem .1rem .4rem;
}
.file-icon {
  width: 40px; height: 40px; flex: 0 0 auto;
  border-radius: 9px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 1.1rem;
}
.file-info { min-width: 0; }
.file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { font-size: .78rem; color: var(--muted); }

.edit-box { width: 100%; }
.edit-box textarea {
  width: 100%; padding: .5rem; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--bg); color: var(--text);
  font: inherit; resize: vertical;
}
.edit-actions { display: flex; gap: .4rem; justify-content: flex-end; margin-top: .4rem; }

/* ---------- upload cards ---------- */

.uploads {
  flex: 0 0 auto;
  max-height: 42vh;
  overflow-y: auto;
  padding: 0 .8rem;
}
.upload-card {
  display: flex; align-items: center; gap: .7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .55rem .7rem;
  margin-bottom: .45rem;
  box-shadow: var(--shadow);
}
.upload-thumb {
  width: 44px; height: 44px; flex: 0 0 auto;
  border-radius: 8px; object-fit: cover; background: var(--surface-2);
  display: grid; place-items: center; font-size: 1.1rem;
}
.upload-body { flex: 1 1 auto; min-width: 0; }
.upload-name { font-size: .9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-stats { font-size: .76rem; color: var(--muted); display: flex; gap: .6rem; }
.upload-stats .err { color: var(--danger); }
.progress {
  height: 6px; border-radius: 999px; background: var(--surface-2);
  overflow: hidden; margin: .3rem 0 .25rem;
}
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .18s linear; }
.upload-card.failed .progress-bar { background: var(--danger); }
.upload-actions { display: flex; gap: .2rem; flex: 0 0 auto; }

/* ---------- composer ---------- */

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: .4rem;
  padding: .5rem .7rem calc(.5rem + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer-input {
  flex: 1 1 auto;
  max-height: 40vh;
  padding: .6rem .8rem;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  resize: none;
  overflow-y: auto;
}
.composer-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.icon-btn.send { color: var(--accent); font-size: 1.3rem; }
.icon-btn.attach { font-size: 1.4rem; }

/* ---------- overlays ---------- */

.drop-overlay {
  position: fixed; inset: 0; z-index: 40;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  backdrop-filter: blur(2px);
  display: grid; place-items: center;
  font-size: 1.3rem; font-weight: 700; color: var(--text);
  border: 3px dashed var(--accent);
}

.lightbox {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .93);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: .5rem;
  padding: 1rem;
}
.lightbox-body { display: grid; place-items: center; max-height: 88dvh; }
.lightbox-body img, .lightbox-body video { max-width: 100%; max-height: 84dvh; border-radius: 8px; }
.lightbox-close {
  position: absolute; top: .8rem; inset-inline-end: .8rem;
  background: rgba(255, 255, 255, .12); color: #fff;
  border: 0; border-radius: 50%; width: 40px; height: 40px;
  font-size: 1.1rem; cursor: pointer;
}
.lightbox-nav {
  background: rgba(255, 255, 255, .12); color: #fff; border: 0;
  border-radius: 50%; width: 44px; height: 44px; font-size: 1.6rem; cursor: pointer;
}
.lightbox-download { position: absolute; bottom: 1.2rem; left: 50%; transform: translateX(-50%); }

.modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0, 0, 0, .55);
  display: grid; place-items: center; padding: 1rem;
}
.modal-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 1.3rem;
  width: 100%; max-width: 420px;
  box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 .5rem; font-size: 1.15rem; }
.modal-card p { margin: 0 0 .8rem; color: var(--muted); font-size: .93rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .9rem; }

.toast {
  position: fixed; bottom: calc(var(--composer-h) + 1rem); left: 50%;
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: .6rem 1rem; border-radius: 999px;
  font-size: .9rem; z-index: 70; box-shadow: var(--shadow);
}
