:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #111827;
  --muted: #6b7280;
  --border: #e3e6ec;
  --accent: #d97706;
  --accent-strong: #b45309;
  --accent-soft: #fff4e0;
  --user-bubble: #0f172a;
  --user-text: #ffffff;
  --danger: #b91c1c;
  --quote-bg: #fff7ea;
  --quote-border: #f59e0b;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.06);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111a2e;
    --surface-2: #17223a;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #22304d;
    --accent: #f59e0b;
    --accent-strong: #fbbf24;
    --accent-soft: #2a2110;
    --user-bubble: #f59e0b;
    --user-text: #111827;
    --danger: #f87171;
    --quote-bg: #241c0c;
    --quote-border: #f59e0b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.kecil { font-size: 12px; display: block; }
.error { color: var(--danger); font-size: 14px; margin: 0; }

/* ---------------- login ---------------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-kartu {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 14px; text-align: center;
}
.login-logo { margin: 0 auto; border-radius: 14px; }
.login-kartu h1 { margin: 0; font-size: 22px; }
.login-kartu p { margin: 0; }
.login-kartu label { display: grid; gap: 6px; text-align: left; font-size: 13px; color: var(--muted); }
.login-kartu input {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px; outline: none;
}
.login-kartu input:focus { border-color: var(--accent); }
.btn-utama {
  background: var(--accent); color: #111827; border: 0; border-radius: 10px; padding: 12px; font-weight: 700;
}
.btn-utama:disabled { opacity: 0.6; }

/* ---------------- app ---------------- */
.app { height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; max-width: 900px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 8px; flex: none; }
.brand strong { display: block; line-height: 1.2; }
.brand .kecil { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 48vw; }
.aksi { display: flex; gap: 4px; }
.btn-teks {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 6px 12px; font-size: 13px;
}
.btn-teks:hover { background: var(--surface-2); }

.pesan { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.sambutan { margin: auto 0; text-align: center; padding: 24px 8px; }
.sambutan h2 { margin: 0 0 6px; font-size: 20px; }
.saran { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.saran button {
  background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 999px;
  padding: 8px 14px; font-size: 13px; text-align: left;
}
.saran button:hover { border-color: var(--accent); }

.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg.user { align-items: flex-end; }
.bubble {
  max-width: min(88%, 680px); padding: 10px 14px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.ai .bubble {
  background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 6px; box-shadow: var(--shadow);
  white-space: pre-wrap;
}
.msg.ai .bubble b { font-weight: 700; }
.msg.ai .bubble a { color: var(--accent-strong); }
.msg.ai .bubble code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 0.92em; }
.msg.ai .bubble pre { background: var(--surface-2); padding: 10px; border-radius: 10px; overflow-x: auto; white-space: pre; }
.msg.ai .bubble blockquote {
  margin: 6px 0; padding: 8px 12px; background: var(--quote-bg); border-left: 3px solid var(--quote-border); border-radius: 8px;
}
.msg .waktu { font-size: 11px; color: var(--muted); margin: 3px 6px 0; }
.msg .lamp-user { font-size: 12px; color: var(--muted); margin: 4px 6px 0; }
.status { color: var(--muted); font-style: italic; display: flex; align-items: center; gap: 8px; }
.status::before {
  content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--accent);
  animation: denyut 1s ease-in-out infinite; flex: none;
}
@keyframes denyut { 0%, 100% { opacity: 0.3; transform: scale(0.8); } 50% { opacity: 1; transform: scale(1); } }
.gagal { color: var(--danger); }

.foto-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-top: 8px; max-width: min(88%, 680px); width: 100%; }
.foto-grid figure { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.foto-grid img { width: 100%; height: 140px; object-fit: cover; display: block; cursor: zoom-in; background: var(--surface-2); }
.foto-grid figcaption { font-size: 11px; color: var(--muted); padding: 6px 8px; line-height: 1.3; }
.file-daftar { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; max-width: min(88%, 680px); }
.file-chip {
  display: inline-flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px; color: var(--text); text-decoration: none; font-size: 13px;
}
.file-chip:hover { border-color: var(--accent); }

/* ---------------- composer ---------------- */
.composer {
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}
.composer-baris { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; min-width: 0; resize: none; font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 20px; padding: 10px 14px; max-height: 160px; outline: none; line-height: 1.4;
}
.composer textarea:focus { border-color: var(--accent); }
.btn-ikon, .btn-kirim {
  width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; flex: none; border: 0;
}
.btn-ikon { background: var(--surface-2); color: var(--muted); cursor: pointer; }
.btn-kirim { background: var(--accent); color: #111827; }
.btn-kirim:disabled { opacity: 0.4; cursor: default; }
.lampiran-daftar { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.lampiran-daftar span {
  background: var(--accent-soft); border: 1px solid var(--border); border-radius: 999px; padding: 4px 6px 4px 10px;
  font-size: 12px; display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
}
.lampiran-daftar button { border: 0; background: transparent; color: var(--muted); padding: 0 4px; }

/* ---------------- penampil foto ---------------- */
.penampil {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.9); z-index: 50; display: grid; place-items: center; padding: 16px;
}
.penampil img { max-width: 100%; max-height: 85vh; border-radius: 8px; }
.penampil p { color: #e5e7eb; font-size: 13px; text-align: center; margin: 8px 0 0; }
.penampil-tutup {
  position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; width: 40px; height: 40px;
  border-radius: 50%; border: 0; background: rgba(255, 255, 255, 0.15); color: #fff; font-size: 18px;
}

/* Mode widget di dalam APMS: tanpa tombol keluar/pasang */
body.embed .topbar { padding-top: 10px; }
body.embed #btn-keluar, body.embed #btn-pasang { display: none; }
body.embed .app { max-width: none; }

/* jawaban yang sedang ditulis (streaming) */
.msg.ai .bubble.alir::after { content: "▌"; margin-left: 2px; opacity: 0.6; animation: kedip 1s steps(2) infinite; }
@keyframes kedip { 50% { opacity: 0; } }

/* tabel di jawaban AI */
.msg.ai .bubble .tabel { margin: 6px 0 8px; overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: 10px; white-space: normal; }
.msg.ai .bubble table { border-collapse: collapse; width: 100%; font-size: 0.94em; line-height: 1.35; }
.msg.ai .bubble th, .msg.ai .bubble td { overflow-wrap: normal; word-break: normal; padding: 7px 10px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.msg.ai .bubble th { background: var(--surface-2); font-weight: 700; font-size: 0.9em; white-space: nowrap; }
.msg.ai .bubble tbody tr:last-child td { border-bottom: none; }
.msg.ai .bubble tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--surface-2) 45%, transparent); }
.msg.ai .bubble th.right, .msg.ai .bubble td.right { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.msg.ai .bubble th.center, .msg.ai .bubble td.center { text-align: center; }
.msg.ai .bubble:has(.tabel) { width: 100%; }
.msg.ai .bubble td code { white-space: nowrap; }
