:root {
  /* Tema gelap ala Telegram "night blue" */
  --bg: #0e1621;
  --panel: #17212b;
  --head: #17212b;
  --head-text: #f5f5f5;
  --head-muted: #708499;
  --head-online: #6ab3f3;
  --text: #f5f5f5;
  --muted: #708499;
  --accent: #3a8bd6;
  --accent-text: #6ab3f3;
  --accent-ink: #ffffff;
  --chat-bg: #0e1621;
  --mine: #2b5278;
  --mine-text: #ffffff;
  --mine-meta: #7da8d3;
  --theirs: #182533;
  --theirs-meta: #6d7f8f;
  --bubble-shadow: none;
  --service-bg: #1d2733;
  --service-text: #c5d0db;
  --input-bg: #0e1621;
  --border: #243140;
  --danger: #f87171;
  --warn-bg: #2f2a14;
  --warn-text: #f5d77a;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    /* Tema terang ala Telegram: header biru, wallpaper hijau lembut, gelembung keluar hijau muda */
    --bg: #f1f2f6;
    --panel: #ffffff;
    --head: #517da2;
    --head-text: #ffffff;
    --head-muted: #d2e1ee;
    --head-online: #ffffff;
    --text: #0f1419;
    --muted: #707579;
    --accent: #3390ec;
    --accent-text: #3390ec;
    --accent-ink: #ffffff;
    --chat-bg: linear-gradient(160deg, #dbe6b2 0%, #b9d5a3 45%, #93c1a4 100%);
    --mine: #eeffde;
    --mine-text: #0f1419;
    --mine-meta: #4fae4e;
    --theirs: #ffffff;
    --theirs-meta: #a2acb4;
    --bubble-shadow: 0 1px 1px rgba(0, 0, 0, 0.12);
    --service-bg: rgba(66, 105, 52, 0.45);
    --service-text: #ffffff;
    --input-bg: #f1f2f6;
    --border: #dfe1e5;
    --danger: #e53935;
    --warn-bg: #fff6d6;
    --warn-text: #6b4e00;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}
#app {
  position: fixed; left: 0; top: 0; width: 100%;
  height: var(--app-h, 100dvh);
  transform: translateY(var(--app-top, 0px));
  display: flex; flex-direction: column;
}
.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.screen.center {
  align-items: center; justify-content: center; text-align: center;
  padding: calc(24px + env(safe-area-inset-top)) 24px calc(24px + env(safe-area-inset-bottom));
}
h1, h2, h3 { margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center-text { text-align: center; }
.logo img { width: 72px; height: 72px; border-radius: 18px; display: block; }

/* tombol & form */
.btn {
  appearance: none; border: 0; border-radius: 12px; padding: 14px 18px; width: 100%;
  background: var(--accent); color: var(--accent-ink); font: inherit; font-weight: 600; cursor: pointer;
}
.btn.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn:disabled { opacity: .5; }
.link-btn { background: none; border: 0; color: var(--muted); text-decoration: underline; font: inherit; margin-top: 16px; }
label { display: block; font-size: 14px; color: var(--muted); margin: 12px 0; }
input, textarea { font: inherit; font-size: 16px; color: var(--text); }
label input {
  display: block; width: 100%; margin-top: 6px; padding: 12px 14px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--panel);
}
input:focus, textarea:focus { outline: 2px solid var(--accent-text); outline-offset: -1px; }
.form-msg { color: var(--danger); min-height: 1.2em; font-size: 14px; margin: 4px 0 10px; }

.setup-wrap {
  width: 100%; max-width: 420px; margin: 0 auto; overflow-y: auto;
  padding: calc(24px + env(safe-area-inset-top)) 22px calc(24px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: stretch;
}
.setup-wrap .logo { align-self: center; }
.setup-wrap h1 { text-align: center; }
#btn-install { margin: 8px 0; }
.steps { text-align: left; max-width: 340px; line-height: 1.8; }
.kbd { border: 1px solid var(--border); border-radius: 6px; padding: 0 6px; }

/* lock */
.lock-icon { color: var(--accent-text); }
.pin-dots { display: flex; gap: 16px; margin: 18px 0 8px; }
.pin-dots span { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); }
.pin-dots span.on { background: var(--accent); border-color: var(--accent); }
.pin-dots.shake { animation: shake .35s; }
@keyframes shake { 20%,60% { transform: translateX(-8px); } 40%,80% { transform: translateX(8px); } }
.lock-msg { min-height: 2.6em; color: var(--danger); font-size: 14px; max-width: 300px; margin: 4px 0 8px; }
.keypad { display: grid; grid-template-columns: repeat(3, 76px); gap: 16px; }
.keypad button {
  width: 76px; height: 76px; border-radius: 50%; border: 0;
  background: var(--panel); color: var(--text); font-size: 28px; cursor: pointer;
  box-shadow: inset 0 0 0 1px var(--border);
}
.keypad button:active { background: var(--border); }

/* chat */
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px 14px;
  background: var(--head); color: var(--head-text); border-bottom: 1px solid var(--border);
}
.chat-head .icon-btn { color: var(--head-text); }
.chat-head .icon-btn:active { background: rgba(255, 255, 255, 0.12); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700;
  background: linear-gradient(#72d5fd, #2a9ef1); color: #fff;
}
.peer-info { flex: 1; min-width: 0; }
.peer-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.verified { color: var(--head-online); margin-left: 6px; font-size: 14px; }
.peer-status { font-size: 13px; color: var(--head-muted); }
.peer-status.online { color: var(--head-online); }
.icon-btn {
  width: 42px; height: 42px; border: 0; border-radius: 50%; background: transparent;
  color: var(--text); display: grid; place-items: center; cursor: pointer;
}
.icon-btn:active { background: var(--border); }

.banner {
  padding: 12px 14px; background: var(--warn-bg); color: var(--warn-text);
  font-size: 14px; border-bottom: 1px solid var(--border);
}
.banner .actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.banner button {
  border: 1px solid currentColor; background: transparent; color: inherit;
  border-radius: 8px; padding: 6px 10px; font: inherit; font-size: 14px;
}

.messages {
  background: var(--chat-bg);
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px 10px; display: flex; flex-direction: column; gap: 4px;
}
.intro {
  align-self: center; text-align: center; max-width: 320px; margin: 6px 0 12px;
  font-size: 12.5px; color: var(--service-text); background: var(--service-bg);
  border: 0; border-radius: 12px; padding: 8px 12px;
}
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: 80%; padding: 7px 10px 5px; border-radius: 14px;
  background: var(--theirs); border: 0; box-shadow: var(--bubble-shadow);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 4px; }
.msg.theirs .bubble { border-bottom-left-radius: 4px; }
.msg.pending .bubble { opacity: .6; }
.msg.failed .bubble { outline: 1px solid var(--danger); }
.msg.bad .text { font-style: italic; color: var(--danger); }
.meta { display: flex; justify-content: flex-end; gap: 8px; font-size: 11px; margin-top: 2px; color: var(--theirs-meta); }
.msg.mine .meta { color: var(--mine-meta); }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; min-height: 42px;
  padding: 10px 14px; border-radius: 21px; border: 0; background: var(--input-bg);
}
.send-btn {
  width: 44px; height: 44px; flex: none; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; cursor: pointer;
}
.send-btn:disabled { opacity: .4; }

/* dialog */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 18px;
  background: var(--panel); color: var(--text); width: min(92vw, 380px);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg-actions { display: grid; gap: 8px; margin-top: 12px; }
.safety-code {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0;
  font: 600 20px/1 ui-monospace, "SF Mono", Menlo, monospace; text-align: center; letter-spacing: 1px;
}
.safety-code span { background: var(--bg); border-radius: 8px; padding: 10px 0; }
.menu-list { display: grid; gap: 4px; }
.menu-list button {
  text-align: left; border: 0; background: transparent; color: var(--text);
  font: inherit; padding: 14px 12px; border-radius: 10px; cursor: pointer;
}
.menu-list button:active { background: var(--border); }
.menu-list .danger { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #000c; color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 14px; z-index: 10;
  max-width: 90vw; text-align: center;
}

/* foto */
.attach-btn {
  width: 44px; height: 44px; flex: none; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted); display: grid; place-items: center; cursor: pointer;
}
.attach-btn:active { background: var(--border); }
.attach-btn:disabled { opacity: .4; }
.bubble.has-photo { padding: 4px 4px 5px; }
.bubble.has-photo .meta { padding: 0 6px; }
.photo {
  display: block; width: 240px; max-width: 100%; height: auto; border-radius: 10px;
  background: var(--border); cursor: zoom-in; object-fit: cover;
  -webkit-touch-callout: none; user-select: none; -webkit-user-select: none;
}
.photo-status { font-size: 12px; padding: 4px 6px 0; opacity: .8; }
dialog.photo-dlg {
  width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; padding: 0;
  border: 0; border-radius: 0; background: #000; color: #fff;
  flex-direction: column; justify-content: center; align-items: center;
}
dialog.photo-dlg[open] { display: flex; }
dialog.photo-dlg::backdrop { background: #000; }
#photo-full {
  flex: 1; min-height: 0; width: 100%; object-fit: contain;
  padding-top: env(safe-area-inset-top);
  -webkit-touch-callout: none; user-select: none; -webkit-user-select: none;
}
.photo-actions {
  display: flex; gap: 8px; width: 100%; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
}
.photo-actions .btn { color: #fff; border-color: #fff5; }
.photo-actions .danger-text { color: #fca5a5; }

/* penutup privasi */
#privacy-cover {
  display: none; position: fixed; inset: 0; z-index: 1000;
  background: var(--bg); align-items: center; justify-content: center;
}
#privacy-cover img { width: 72px; height: 72px; border-radius: 18px; }
html.covered #privacy-cover { display: flex; }
html.covered #app, html.covered dialog, html.covered .toast { visibility: hidden; }

/* status server */
:root { --ok: #22c55e; --wait: #f59e0b; --bad: #ef4444; }
.server-pill {
  display: inline-flex; align-items: center; gap: 7px; align-self: center;
  font-size: 13px; font-weight: 600; padding: 5px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--panel); color: var(--muted);
  margin-bottom: 20px;
}
.server-pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--dot, var(--wait)); }
.server-pill.online { --dot: var(--ok); }
.server-pill.connecting { --dot: var(--wait); }
.server-pill.offline, .server-pill.no-internet { --dot: var(--bad); color: var(--danger); border-color: var(--danger); }
.server-pill.connecting::before { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.chat-head .server-btn { position: relative; color: var(--head-muted); }
.srv-dot {
  position: absolute; right: 7px; bottom: 8px; width: 11px; height: 11px; border-radius: 50%;
  background: var(--dot, var(--wait)); border: 2px solid var(--head);
}
.server-btn.online { --dot: var(--ok); }
.server-btn.connecting { --dot: var(--wait); }
.server-btn.connecting .srv-dot { animation: pulse 1.2s ease-in-out infinite; }
.chat-head .server-btn.offline, .chat-head .server-btn.no-internet { --dot: var(--bad); color: #ff8a80; }

.server-strip {
  display: flex; align-items: center; gap: 10px; padding: 9px 14px; font-size: 13.5px;
  background: #7f1d1d; color: #fee2e2; border-bottom: 1px solid #991b1b;
}
.server-strip span { flex: 1; }
.server-strip button {
  border: 1px solid currentColor; background: transparent; color: inherit; flex: none;
  border-radius: 8px; padding: 5px 10px; font: inherit; font-size: 13px; font-weight: 600;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .server-strip { background: #fee2e2; color: #7f1d1d; border-bottom-color: #fecaca; }
}
