/* =========================================================
   Ngobrol — UI mobile-first
   ========================================================= */

/* ==== Token warna (terang) ==== */
:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f3f5f8;
  --border: #e3e7ee;
  --text: #121826;
  --muted: #667085;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #e8efff;
  --on-primary: #ffffff;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --success: #10b981;
  --warn-bg: #fff7e0;
  --warn-border: #f5d27a;
  --warn-text: #7a4b00;
  --bubble-user: #2563eb;
  --bubble-user-text: #ffffff;
  --bubble-bot: #f1f3f7;
  --bubble-bot-text: #121826;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ==== Mode gelap ==== */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #090d13;
    --surface: #0f1720;
    --surface-2: #17212c;
    --border: #1f2b38;
    --text: #e8edf3;
    --muted: #8b98a9;
    --primary: #4c8dff;
    --primary-hover: #6a9fff;
    --primary-soft: #14233d;
    --on-primary: #08111f;
    --danger: #ff6b6b;
    --danger-soft: #2a1618;
    --warn-bg: #2b2310;
    --warn-border: #5b4716;
    --warn-text: #f3d68a;
    --bubble-user: #3b78e7;
    --bubble-user-text: #ffffff;
    --bubble-bot: #17212c;
    --bubble-bot-text: #e8edf3;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* ==== Reset & dasar ==== */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;           /* scroll hanya di dalam area chat */
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ==== Kerangka aplikasi ==== */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--app-h, 100dvh);   /* diisi JS dari visualViewport (keyboard iOS) */
  max-width: 760px;
  margin: 0 auto;
  background: var(--surface);
  position: relative;
}
@media (min-width: 800px) {
  body { padding: 20px 0; }
  .app {
    height: calc(100dvh - 40px);
    border: 1px solid var(--border);
    border-radius: 22px;
    box-shadow: var(--shadow);
    overflow: hidden;
  }
}

/* ==== Header ==== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 14px 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo {
  width: 40px; height: 40px;
  border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  flex: 0 0 auto;
}
.brand-text h1 { margin: 0; font-size: 1.05rem; font-weight: 700; letter-spacing: -.01em; line-height: 1.2; }
.status {
  display: flex; align-items: center; gap: 6px;
  font-size: .78rem; color: var(--muted);
  line-height: 1.2; margin-top: 2px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa5b4; flex: 0 0 auto; transition: background .2s; }
.dot.listening { background: var(--success); animation: pulse 1.2s infinite; }
.dot.thinking  { background: #f59e0b; animation: pulse 1.2s infinite; }
.dot.speaking  { background: var(--primary); animation: pulse 1.2s infinite; }
.dot.error     { background: var(--danger); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* ==== Tombol ikon ==== */
.icon-btn {
  width: 48px; height: 48px;
  border: 0; border-radius: 50%;
  display: inline-grid; place-items: center;
  cursor: pointer;
  flex: 0 0 auto;
  transition: background .15s, transform .1s, opacity .15s;
  touch-action: manipulation;
}
.icon-btn:active:not(:disabled) { transform: scale(.94); }
.icon-btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn-quiet { width: 44px; height: 44px; background: transparent; color: var(--muted); }
.icon-btn-quiet:hover { background: var(--surface-2); color: var(--text); }
.icon-btn-primary { background: var(--primary); color: var(--on-primary); }
.icon-btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.mic.is-listening { background: var(--danger); color: #fff; animation: ring 1.4s infinite; }
.mic.is-speaking  { background: var(--surface-2); color: var(--primary); box-shadow: inset 0 0 0 2px var(--primary); }
@keyframes ring {
  0%   { box-shadow: 0 0 0 0 rgba(220, 38, 38, .45); }
  70%  { box-shadow: 0 0 0 14px rgba(220, 38, 38, 0); }
  100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0); }
}

/* ==== Peringatan ==== */
.warn {
  margin: 10px 12px 0;
  padding: 10px 12px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  border-radius: 12px;
  font-size: .85rem;
  flex: 0 0 auto;
}
.warn code { font-size: .8rem; background: rgba(0,0,0,.07); padding: 1px 5px; border-radius: 5px; }

/* ==== Area chat ==== */
.chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px 14px 8px;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) { .chat-log { scroll-behavior: auto; } }

.msg { display: flex; flex-direction: column; max-width: 86%; animation: rise .22s ease-out; }
.msg-user { align-self: flex-end; align-items: flex-end; }
.msg-bot  { align-self: flex-start; align-items: flex-start; }
.msg-system { align-self: center; max-width: 92%; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 1rem;
  line-height: 1.4;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.msg-user .bubble { background: var(--bubble-user); color: var(--bubble-user-text); border-bottom-right-radius: 6px; }
.msg-bot  .bubble { background: var(--bubble-bot);  color: var(--bubble-bot-text);  border-bottom-left-radius: 6px; }
.msg-system .bubble {
  background: var(--danger-soft); color: var(--danger);
  font-size: .85rem; padding: 8px 12px; border-radius: 12px; text-align: center;
}
.msg-time { font-size: .7rem; color: var(--muted); margin: 3px 6px 8px; }
.msg-system .msg-time { display: none; }

/* Indikator mengetik */
.typing { display: inline-flex; gap: 5px; padding: 14px 16px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* Tampilan kosong */
.empty {
  margin: auto;
  text-align: center;
  padding: 24px 12px;
  max-width: 420px;
}
.empty-icon {
  width: 64px; height: 64px; margin: 0 auto 14px;
  border-radius: 20px; display: grid; place-items: center;
  background: var(--primary-soft); color: var(--primary);
}
.empty h2 { margin: 0 0 6px; font-size: 1.15rem; }
.empty p { margin: 0 0 18px; color: var(--muted); font-size: .92rem; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggestion {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 999px;
  font-size: .9rem;
  cursor: pointer;
  min-height: 44px;
  touch-action: manipulation;
  transition: background .15s, border-color .15s;
}
.suggestion:hover { background: var(--primary-soft); border-color: var(--primary); }

/* ==== Composer ==== */
.composer {
  flex: 0 0 auto;
  padding: 8px 12px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* Bar mendengarkan */
.listen-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; margin-bottom: 8px;
  background: var(--primary-soft);
  border-radius: 14px;
  color: var(--text);
  font-size: .92rem;
  min-height: 44px;
}
.listen-text {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.listen-text.is-interim { font-style: italic; }
.wave { display: inline-flex; align-items: center; gap: 3px; height: 20px; flex: 0 0 auto; }
.wave i { width: 3px; height: 8px; border-radius: 2px; background: var(--primary); animation: wave 1s infinite ease-in-out; }
.wave i:nth-child(2) { animation-delay: .1s; }
.wave i:nth-child(3) { animation-delay: .2s; }
.wave i:nth-child(4) { animation-delay: .3s; }
.wave i:nth-child(5) { animation-delay: .4s; }
@keyframes wave { 0%, 100% { height: 6px; } 50% { height: 20px; } }

/* Switch mode percakapan */
.chip-switch {
  display: inline-flex; align-items: center; gap: 10px;
  min-height: 40px; padding: 2px 4px 2px 0;
  cursor: pointer; user-select: none;
  font-size: .85rem; color: var(--text);
  max-width: 100%;
}
.chip-switch input, .switch { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip-track {
  width: 40px; height: 24px; border-radius: 999px; flex: 0 0 auto;
  background: #b8c1cf; position: relative; transition: background .2s;
}
.chip-thumb {
  position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .2s;
}
.chip-switch input:checked + .chip-track { background: var(--primary); }
.chip-switch input:checked + .chip-track .chip-thumb { transform: translateX(16px); }
.chip-switch input:focus-visible + .chip-track { outline: 2px solid var(--primary); outline-offset: 2px; }
.chip-label { font-weight: 600; white-space: nowrap; }
.chip-hint { color: var(--muted); font-size: .78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 520px) { .chip-hint { display: none; } }

/* Baris input */
.composer-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.composer-row input[type="text"] {
  flex: 1; min-width: 0;
  height: 48px;
  padding: 0 18px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  font: inherit;
  font-size: 16px;               /* ≥16px agar iOS tidak zoom saat fokus */
}
.composer-row input::placeholder { color: var(--muted); }
.composer-row input:focus { outline: none; border-color: var(--primary); background: var(--surface); box-shadow: 0 0 0 3px var(--primary-soft); }

/* ==== Bottom sheet pengaturan ==== */
.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(8, 12, 20, .5); animation: fade .2s; }
.sheet-panel {
  position: relative;
  width: 100%; max-width: 560px;
  max-height: 85dvh; overflow-y: auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  box-shadow: var(--shadow);
  animation: slide .25s ease-out;
}
@media (min-width: 800px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 22px; }
}
@keyframes fade  { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-grip { width: 38px; height: 4px; border-radius: 2px; background: var(--border); margin: 4px auto 6px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.sheet-head h2 { margin: 0; font-size: 1.05rem; }
.sheet-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; min-height: 52px; padding: 12px 14px; margin-top: 8px;
  background: var(--surface-2); border: 0; border-radius: 14px;
  color: var(--text); font-size: .95rem; text-align: left; cursor: pointer;
  touch-action: manipulation;
}
.sheet-item.danger { color: var(--danger); justify-content: flex-start; }
.sheet-item.danger:hover { background: var(--danger-soft); }
.sheet-item .switch {
  opacity: 1; width: 42px; height: 26px; appearance: none; -webkit-appearance: none;
  border-radius: 999px; background: #b8c1cf; position: relative; cursor: pointer; transition: background .2s; flex: 0 0 auto;
}
.sheet-item .switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s;
}
.sheet-item .switch:checked { background: var(--primary); }
.sheet-item .switch:checked::after { transform: translateX(16px); }

/* ==== Log debug ==== */
.log {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .72rem; color: var(--muted);
  max-height: 180px; overflow-y: auto;
  background: var(--surface-2); border-radius: 12px;
  padding: 10px 12px; margin-top: 8px;
  white-space: pre-wrap;
}

/* ==== Aksi header ==== */
.top-actions { display: flex; align-items: center; gap: 2px; flex: 0 0 auto; }

/* ==== Daftar riwayat sesi ==== */
.sheet-item.primary { justify-content: flex-start; background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sheet-item.primary:hover { filter: brightness(.97); }
.hist-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.hist-empty { text-align: center; color: var(--muted); font-size: .9rem; padding: 28px 12px; }
.hist-item {
  display: flex; align-items: stretch;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 14px;
  overflow: hidden;
}
.hist-item.active { border-color: var(--primary); background: var(--primary-soft); }
.hist-main {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 2px;
  padding: 11px 6px 11px 14px;
  background: transparent; border: 0; text-align: left; cursor: pointer;
  color: var(--text);
  min-height: 60px;
  touch-action: manipulation;
}
.hist-title { font-weight: 600; font-size: .95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-preview { font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-meta { font-size: .72rem; color: var(--muted); }
.hist-del {
  flex: 0 0 auto; width: 48px;
  background: transparent; border: 0; color: var(--muted);
  display: grid; place-items: center; cursor: pointer;
  touch-action: manipulation;
}
.hist-del:hover { color: var(--danger); background: var(--danger-soft); }

/* ==== Toast ==== */
.toast {
  position: fixed; left: 50%; bottom: calc(110px + var(--safe-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--text); color: var(--surface);
  padding: 10px 16px; border-radius: 999px;
  font-size: .85rem; z-index: 60;
  box-shadow: var(--shadow);
  animation: rise .2s ease-out;
}

/* ==== Modal konfirmasi ==== */
.modal { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(8, 12, 20, .55); animation: fade .18s; }
.modal-card {
  position: relative;
  width: 100%; max-width: 360px;
  background: var(--surface);
  border-radius: 22px;
  padding: 24px 20px 18px;
  text-align: center;
  box-shadow: var(--shadow);
  animation: pop .2s ease-out;
}
@keyframes pop { from { opacity: 0; transform: scale(.94) translateY(8px); } to { opacity: 1; transform: none; } }
.modal-icon {
  width: 52px; height: 52px; margin: 0 auto 12px;
  border-radius: 50%; display: grid; place-items: center;
  background: var(--primary-soft); color: var(--primary);
}
.modal-card.is-danger .modal-icon { background: var(--danger-soft); color: var(--danger); }
.modal-card h2 { margin: 0 0 6px; font-size: 1.1rem; }
.modal-card p { margin: 0 0 20px; color: var(--muted); font-size: .92rem; }
.modal-actions { display: flex; gap: 10px; }
.modal-btn {
  flex: 1; min-height: 48px; padding: 0 14px;
  border: 0; border-radius: 14px;
  font-size: .95rem; font-weight: 600; cursor: pointer;
  touch-action: manipulation;
  transition: background .15s, transform .1s;
}
.modal-btn:active { transform: scale(.97); }
.modal-btn-ghost { background: var(--surface-2); color: var(--text); }
.modal-btn-ghost:hover { background: var(--border); }
.modal-btn-primary { background: var(--primary); color: var(--on-primary); }
.modal-btn-primary:hover { background: var(--primary-hover); }
.modal-btn-danger { background: var(--danger); color: #fff; }
.modal-btn-danger:hover { filter: brightness(.92); }
@media (prefers-reduced-motion: reduce) { .modal-card, .modal-backdrop { animation: none; } }
