:root {
  --bg: #0f1419;
  --panel: #1a2129;
  --panel-2: #222b35;
  --text: #e7edf3;
  --muted: #8a97a5;
  --accent: #2f80ed;
  --mine: #2f80ed;
  --theirs: #2a3441;
  --danger: #e5484d;
  --ok: #30a46c;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Thai", "Sarabun", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

/* ---------- Login ---------- */
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border-radius: var(--radius); padding: 28px 24px; box-shadow: 0 10px 40px rgba(0,0,0,.4);
}
.login-card h1 { margin: 0 0 18px; font-size: 22px; text-align: center; }
.login-card label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.login-card input {
  display: block; width: 100%; margin-top: 6px; padding: 11px 12px; font-size: 15px;
  border-radius: 10px; border: 1px solid #33404d; background: var(--panel-2); color: var(--text);
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card button {
  width: 100%; padding: 12px; margin-top: 6px; font-size: 15px; font-weight: 600;
  border: 0; border-radius: 10px; background: var(--accent); color: #fff; cursor: pointer;
}
.login-card .back { display: block; text-align: center; margin-top: 14px; color: var(--muted); text-decoration: none; }
.muted { color: var(--muted); font-size: 13px; text-align: center; margin-top: -8px; }
.alert { background: rgba(229,72,77,.15); color: #ff8b8f; padding: 10px 12px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
.alert.ok { background: rgba(48,164,108,.15); color: #6fd39d; }

/* ---------- Chat ---------- */
.chat { height: 100%; display: flex; flex-direction: column; max-width: 820px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid #2a3441;
}
.peer { display: flex; align-items: center; gap: 10px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px rgba(48,164,108,.25); }
.peer-name { font-weight: 600; }
.peer-status { font-size: 12px; color: var(--muted); }
.menu { display: flex; align-items: center; gap: 12px; }
.menu .me { font-size: 13px; color: var(--muted); }
.menu a { text-decoration: none; font-size: 18px; color: var(--text); opacity: .8; }
.menu a:hover { opacity: 1; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 4px 12px; border-radius: 999px; margin: 10px 0 6px; }
.msg { max-width: 78%; padding: 9px 13px; border-radius: 16px; line-height: 1.45; word-wrap: break-word; white-space: pre-wrap; position: relative; }
.msg.theirs { align-self: flex-start; background: var(--theirs); border-bottom-left-radius: 6px; }
.msg.mine { align-self: flex-end; background: var(--mine); color: #fff; border-bottom-right-radius: 6px; }
.msg .time { display: block; font-size: 11px; opacity: .6; margin-top: 3px; text-align: right; }
.msg a { color: inherit; text-decoration: underline; }
.msg.pending { opacity: .6; }
.empty { margin: auto; color: var(--muted); text-align: center; }

.composer {
  display: flex; gap: 8px; padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid #2a3441;
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 11px 14px; font: inherit; font-size: 15px;
  border-radius: 20px; border: 1px solid #33404d; background: var(--panel-2); color: var(--text);
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer #sendBtn {
  width: 44px; height: 44px; align-self: flex-end; flex: none; border: 0; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 18px; cursor: pointer;
}
.composer #sendBtn:disabled { opacity: .5; cursor: default; }

.offline-banner {
  position: fixed; top: 0; left: 0; right: 0; background: var(--danger); color: #fff;
  text-align: center; font-size: 13px; padding: 6px; z-index: 10; display: none;
}
.offline-banner.show { display: block; }

[hidden] { display: none !important; }

/* ---------- v2: logo ---------- */
.logo { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 20px; }
.logo h1 { margin: 0; font-size: 24px; letter-spacing: .5px; }
.logo-mark {
  width: 56px; height: 56px; border-radius: 18px; display: grid; place-items: center;
  font-size: 26px; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, var(--accent), #7c5cff); box-shadow: 0 6px 20px rgba(47,128,237,.35);
}

/* ---------- v2: message parts ---------- */
.msg { padding-bottom: 6px; }
.msg .text { white-space: pre-wrap; }
.msg .meta { display: flex; justify-content: flex-end; gap: 6px; font-size: 11px; opacity: .65; margin-top: 3px; }
.msg .read { color: #b9dcff; }
.msg.mine .read::before { content: '✓✓ '; }
.msg .time { display: inline; }
.msg.flash { outline: 2px solid #ffd166; outline-offset: 2px; transition: outline-color 1.2s; }

.quote {
  border-left: 3px solid rgba(255,255,255,.55); background: rgba(0,0,0,.18);
  padding: 5px 9px; border-radius: 8px; margin-bottom: 6px; cursor: pointer; font-size: 13px;
}
.msg.theirs .quote { border-left-color: var(--accent); background: rgba(255,255,255,.05); }
.quote-name { font-weight: 600; font-size: 12px; opacity: .9; }
.quote-text { opacity: .8; white-space: pre-wrap; word-break: break-word; }

.photo { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; cursor: zoom-in; margin-bottom: 4px; background: #000; }
.expired { font-size: 13px; opacity: .7; font-style: italic; }

.reply-btn {
  position: absolute; top: 4px; border: 0; background: var(--panel-2); color: var(--text);
  width: 26px; height: 26px; border-radius: 50%; font-size: 13px; cursor: pointer; opacity: 0; transition: opacity .15s;
}
.msg.theirs .reply-btn { right: -32px; }
.msg.mine .reply-btn { left: -32px; }
.msg:hover .reply-btn { opacity: .9; }
@media (hover: none) { .reply-btn { opacity: .55; } }

/* ---------- v2: composer ---------- */
.composer { flex-direction: column; gap: 6px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.icon-btn { border: 0; background: transparent; color: var(--text); font-size: 18px; cursor: pointer; opacity: .8; padding: 6px; border-radius: 8px; }
.icon-btn:hover { opacity: 1; background: var(--panel-2); }
.attach-btn { height: 44px; width: 44px; flex: none; font-size: 20px; }
.reply-bar .icon-btn, .attach-bar .icon-btn { font-size: 14px; opacity: .6; }
.reply-bar, .attach-bar {
  display: flex; align-items: center; gap: 10px; background: var(--panel-2);
  border-left: 3px solid var(--accent); border-radius: 8px; padding: 6px 10px; font-size: 13px;
}
.reply-bar-body { flex: 1; min-width: 0; }
.reply-bar-name { font-weight: 600; color: #b9dcff; }
.reply-bar-text { opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-bar img { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; background: #000; }
.attach-info { flex: 1; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quota { font-size: 11px; color: var(--muted); text-align: center; }
.quota.full { color: #ff8b8f; }

.drop-hint {
  position: fixed; inset: 0; display: none; place-items: center; z-index: 20; pointer-events: none;
  background: rgba(47,128,237,.25); border: 3px dashed var(--accent); font-size: 20px; font-weight: 600;
}
.drop-hint.show { display: grid; }

.lightbox {
  position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 96vw; max-height: 86vh; border-radius: 6px; }
.lightbox a { color: #fff; font-size: 14px; opacity: .8; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(10px);
  background: #2a3441; color: #fff; padding: 9px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; transition: all .25s; z-index: 40; box-shadow: 0 6px 20px rgba(0,0,0,.4); max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
