:root {
  --bg: #0f1419;
  --surface: #1a222c;
  --surface-2: #243040;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --accent: #3d8bfd;
  --accent-2: #2f6fd1;
  --user: #1e3a5f;
  --bot: #1e2836;
  --danger: #ff6b6b;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

[x-cloak] { display: none !important; }

.app {
  position: relative;
  height: 100dvh;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* Satu layar aktif — tidak ditumpuk */
.view-login .chat-screen,
.view-chat .login-screen {
  visibility: hidden;
  pointer-events: none;
  opacity: 0;
  z-index: 0;
}

.view-login .login-screen,
.view-chat .chat-screen {
  visibility: visible;
  pointer-events: auto;
  opacity: 1;
  z-index: 1;
}

/* OTP step: sembunyi via inline style sampai Alpine tampilkan */

/* Login — lebih terang & jelas */
.login-screen {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(160deg, #1e4a7a 0%, #0f1419 55%);
}

.login-card {
  width: min(100%, 400px);
  background: #f0f4fa;
  color: #1a2332;
  border: 1px solid #c5d3e8;
  border-radius: calc(var(--radius) + 4px);
  padding: 1.75rem;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}

.login-card .subtitle,
.login-card .hint,
.login-card label { color: #4a5d78; }
.login-card .error {
  color: #c0392b;
  background: #fdecea;
  padding: .6rem .75rem;
  border-radius: 8px;
  display: none;
}
.login-card .error.is-visible { display: block; }
.login-card input {
  background: #fff;
  border: 1px solid #b8c8de;
  color: #1a2332;
}

.btn.btn-lg {
  width: 100%;
  margin-top: .5rem;
  padding: .9rem 1rem;
  font-size: 1.05rem;
}

.otp-panel { margin-top: .25rem; }

.login-step { width: 100%; }

.noscript-msg {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  color: #111;
  padding: 2rem;
  text-align: center;
  z-index: 9999;
}

.brand { text-align: center; margin-bottom: 1.5rem; }
.brand-icon {
  display: inline-flex;
  width: 52px; height: 52px;
  align-items: center; justify-content: center;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), #6c5ce7);
  font-size: 1.5rem; font-weight: 700;
  margin-bottom: .75rem;
}
.brand h1 { margin: 0; font-size: 1.75rem; }
.subtitle { margin: .35rem 0 0; color: var(--muted); font-size: .95rem; }

.form { display: flex; flex-direction: column; gap: .65rem; }
label { font-size: .85rem; color: var(--muted); }
input, textarea, select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: .75rem .9rem;
  font: inherit;
}
input:focus, textarea:focus, select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent);
  border-color: var(--accent);
}
.hint { font-size: .8rem; color: var(--muted); margin: 0; }

.btn {
  border: none;
  border-radius: 10px;
  padding: .75rem 1rem;
  font: inherit;
  cursor: pointer;
  transition: transform .1s, opacity .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); }
.btn.ghost { background: transparent; color: #4a5d78; }
.btn.block { width: 100%; margin-top: .5rem; background: var(--surface-2); color: var(--text); }

.error {
  color: var(--danger);
  font-size: .9rem;
  margin-top: .75rem;
  font-weight: 500;
}

/* Chat layout */
.chat-screen {
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0.85rem;
  min-height: 52px;
  border-bottom: 1px solid var(--surface-2);
  background: var(--surface);
}

.brand-mini {
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
  flex-shrink: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
  margin-left: auto;
}

.mode-field {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
}

.mode-label {
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}

.mode-select {
  width: auto;
  min-width: 7.5rem;
  height: 40px;
  margin: 0;
  padding: 0 1.75rem 0 0.65rem;
  font-size: 0.85rem;
  line-height: 1.2;
  border-radius: 10px;
  background-color: var(--bg);
  border: 1px solid var(--surface-2);
  color: var(--text);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238b9cb3' d='M2 4l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.55rem center;
  cursor: pointer;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--surface-2);
  border: none;
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.icon-btn:active {
  transform: scale(0.97);
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem .85rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
  -webkit-overflow-scrolling: touch;
}

.bubble {
  max-width: min(88%, 640px);
  padding: .7rem .9rem;
  border-radius: var(--radius);
  line-height: 1.45;
  font-size: .95rem;
  word-break: break-word;
}
.bubble.user {
  align-self: flex-end;
  background: var(--user);
  border-bottom-right-radius: 4px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--bot);
  border-bottom-left-radius: 4px;
}
.bubble.pending { opacity: .9; }
.bubble-meta { font-size: .72rem; color: var(--muted); margin-bottom: .25rem; }
.bubble-text {
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-text a {
  color: #6eb5ff;
  text-decoration: underline;
}
.bubble-text b,
.bubble-text strong { font-weight: 700; }
.bubble-text i,
.bubble-text em { font-style: italic; }
.bubble-text u,
.bubble-text ins { text-decoration: underline; }
.bubble-text s,
.bubble-text strike,
.bubble-text del { text-decoration: line-through; opacity: 0.85; }
.bubble-text :is(p, ul, ol) { margin: .35rem 0; }
.bubble-text pre {
  background: var(--bg);
  padding: .5rem .65rem;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .82rem;
  white-space: pre-wrap;
  margin: .4rem 0;
}
.bubble-text code {
  background: var(--bg);
  padding: .1rem .35rem;
  border-radius: 4px;
  font-size: .85em;
  font-family: ui-monospace, "Cascadia Code", "Fira Code", monospace;
}
.bubble-text pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

.dots { display: flex; gap: 4px; margin-top: .4rem; }
.dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: .3; transform: scale(.8); }
  40% { opacity: 1; transform: scale(1); }
}

.composer {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  padding: .65rem .85rem calc(.65rem + var(--safe-bottom));
  border-top: 1px solid var(--surface-2);
  background: var(--surface);
}
.composer textarea:disabled,
.send-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.composer textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 120px;
  line-height: 1.35;
}
.mic-btn, .send-btn {
  flex-shrink: 0;
  width: 44px; height: 44px;
  border-radius: 12px;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
}
.mic-btn.listening {
  background: var(--danger);
  animation: pulse-mic 1s infinite;
}
.mic-btn.unsupported { opacity: .35; }
.send-btn { background: var(--accent); color: #fff; }
@keyframes pulse-mic {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,107,107,.5); }
  50% { box-shadow: 0 0 0 8px rgba(255,107,107,0); }
}

/* Drawer */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 20;
}
.drawer {
  position: fixed;
  top: 0; right: 0;
  width: min(88vw, 320px);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--surface-2);
  padding: 1.25rem;
  z-index: 30;
  transform: translateX(100%);
  transition: transform .25s ease;
}
.drawer.open { transform: translateX(0); }
.drawer h2 { margin: 0 0 1rem; font-size: 1.1rem; }
.drawer h3 { margin: 0; font-size: .9rem; font-weight: 600; }
.drawer-meta { font-size: .85rem; color: var(--muted); margin: 0 0 .35rem; }

.drawer-section {
  margin: 1rem 0;
  padding-top: .75rem;
  border-top: 1px solid var(--surface-2);
}

.drawer-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .5rem;
}

.btn.compact {
  padding: .35rem .65rem;
  font-size: .8rem;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
}

.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: min(40vh, 280px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: .35rem;
}

.thread-item {
  display: flex;
  align-items: stretch;
  gap: .25rem;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid transparent;
}

.thread-item.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
}

.thread-item-btn {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: .55rem .65rem;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
}

.thread-item-title {
  display: block;
  font-size: .85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-item-meta {
  display: block;
  font-size: .7rem;
  color: var(--muted);
  margin-top: .15rem;
}

.thread-item-del {
  flex-shrink: 0;
  width: 32px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
  font-size: .95rem;
}

.thread-item-del:hover {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.thread-empty {
  font-size: .82rem;
  color: var(--muted);
  padding: .5rem .25rem;
  margin: 0;
}
.toggle {
  display: flex;
  align-items: center;
  gap: .5rem;
  margin: 1rem 0;
  font-size: .9rem;
  color: var(--muted);
}
.status-box {
  margin-top: 1rem;
  padding: .75rem;
  background: var(--bg);
  border-radius: 8px;
  font-size: .75rem;
  white-space: pre-wrap;
  color: var(--muted);
  max-height: 200px;
  overflow: auto;
}

@media (max-width: 380px) {
  .mode-label { display: none; }
  .mode-select { min-width: 6.5rem; }
  .topbar { padding: 0.5rem 0.65rem; }
}

@media (min-width: 768px) {
  .messages { padding: 1.25rem 2rem; }
  .composer { padding-left: 2rem; padding-right: 2rem; }
  .topbar { padding: 0.5rem 1.25rem; }
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f7fb;
    --surface: #fff;
    --surface-2: #e3eaf3;
    --text: #1a2332;
    --muted: #5c6b80;
    --user: #d6e8ff;
    --bot: #eef2f7;
  }
}
