:root {
  --purple: #6A22FF;
  --purple-dark: #3D1A8F;
  --purple-light: #F0EDFF;
  --text: #1a1a2e;
  --text-muted: #6b6b80;
  --border: #E6E1F4;
  --bg: #ffffff;
  --grey-bubble: #f2f2f5;
  --danger: #E53935;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100dvh;
}

.hidden { display: none !important; }

/* ===================== LOGIN ===================== */

#screen-login {
  background: var(--bg);
  align-items: center;
  justify-content: flex-start;
  padding: calc(40px + var(--safe-top)) 24px var(--safe-bottom);
  overflow-y: auto;
}

.view {
  width: 100%;
  display: flex;
  justify-content: center;
}

.login-wrap {
  width: 100%;
  max-width: 420px;
  text-align: center;
  margin-top: 6vh;
  padding-bottom: 40px;
}

.login-links {
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 14px;
}

.login-links a {
  color: var(--purple);
  text-decoration: none;
}

.login-links a:hover {
  text-decoration: underline;
}

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 20px 0 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.btn-secondary {
  background: #fff;
  color: var(--text);
  border: 2px solid var(--border);
  padding: 14px 18px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 12px;
  cursor: pointer;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.google-btn-wrap {
  display: flex;
  justify-content: center;
  min-height: 44px;
}

.form-message {
  color: var(--purple-dark);
  font-size: 14px;
  margin: 0;
  min-height: 20px;
  line-height: 1.4;
}

/* Menu panel */
.header-menu {
  margin-left: auto;
  background: none;
  border: none;
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  padding: 4px 10px;
}

.menu-panel {
  position: absolute;
  top: calc(60px + var(--safe-top));
  right: 10px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.15);
  z-index: 10;
  overflow: hidden;
}

.menu-item {
  display: block;
  width: 100%;
  padding: 12px 18px;
  background: #fff;
  border: none;
  text-align: left;
  font-size: 15px;
  color: var(--text);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.menu-item:last-child { border-bottom: none; }

.menu-item:hover {
  background: var(--purple-light);
}

.menu-item-danger {
  color: var(--danger);
}

.menu-item-danger:hover {
  background: #fff4f4;
}

.login-logo {
  margin-bottom: 24px;
  display: inline-block;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 6px;
  color: var(--text);
}

.login-sub {
  font-size: 16px;
  color: var(--text-muted);
  margin: 0 0 40px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Generic inputs inside any login form */
.login-form input[type="text"],
.login-form input[type="email"],
.login-form input[type="password"] {
  font-size: 16px;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: 12px;
  outline: none;
  transition: border-color 0.15s ease;
  width: 100%;
}

.login-form input:focus { border-color: var(--purple); }

/* Code-style inputs: centred, monospace, uppercase tracking */
#activate-code,
#reset-token,
#link-code {
  text-align: center;
  letter-spacing: 2px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  text-transform: uppercase;
  font-size: 18px;
}

.shake {
  animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

.code-error {
  color: var(--danger);
  font-size: 14px;
  margin: 0;
  min-height: 20px;
}

.btn-primary {
  background: var(--purple);
  color: #fff;
  border: none;
  padding: 16px 18px;
  font-size: 17px;
  font-weight: 600;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.15s ease;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ===================== CHAT ===================== */

#screen-chat {
  background: var(--bg);
}

.chat-header {
  background: var(--purple);
  color: #fff;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.header-logo { flex-shrink: 0; }

.header-title {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.2;
}

.header-status {
  font-size: 12px;
  color: rgba(255,255,255,0.75);
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #4ade80;
}

/* Banners */

.banner {
  background: var(--purple-light);
  color: var(--purple-dark);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.banner-text {
  flex: 1;
  line-height: 1.3;
}

.banner-btn {
  background: var(--purple);
  color: #fff;
  border: none;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.banner-close {
  background: none;
  border: none;
  color: var(--purple-dark);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}

/* Messages area */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.load-more-sentinel {
  height: 1px;
  flex-shrink: 0;
}

#messages-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 20px;
  font-size: 16px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
}

.msg-out {
  align-self: flex-end;
  background: var(--purple);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.msg-in {
  align-self: flex-start;
  background: var(--purple-light);
  color: var(--purple-dark);
  border-bottom-left-radius: 6px;
}

/* Scheduler-fired reminders stand out from normal chat replies so they're
   still findable when scrolling history. Brighter background, left accent. */
.msg-reminder {
  background: #FFF4D6;
  color: #5B3E00;
  border-left: 3px solid #E8A500;
  font-weight: 500;
}

.msg-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  padding: 0 4px;
}

.msg-out + .msg-time { text-align: right; }
.msg-in + .msg-time { text-align: left; }

.msg-system {
  align-self: center;
  background: var(--grey-bubble);
  color: var(--text-muted);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 14px;
  text-align: center;
  max-width: 85%;
}

/* Typing indicator */

.typing {
  align-self: flex-start;
  background: var(--purple-light);
  padding: 14px 16px;
  border-radius: 20px;
  border-bottom-left-radius: 6px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--purple);
  border-radius: 50%;
  opacity: 0.6;
  animation: typing-bounce 1.3s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-6px); opacity: 1; }
}

.typing-thinking {
  font-size: 13px;
  color: var(--purple-dark);
  margin-left: 6px;
}

/* Recording indicator + waveform */

.recording-indicator {
  background: #faf8ff;
  color: var(--purple-dark);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.waveform {
  flex: 1;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 0;
}

.wf-bar {
  width: 4px;
  min-height: 6px;
  background: var(--purple);
  border-radius: 2px;
  transition: height 0.06s linear;
  flex-shrink: 0;
}

.recording-time {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
  font-weight: 600;
  color: var(--purple);
  min-width: 38px;
  text-align: right;
  flex-shrink: 0;
}

/* Transcribing indicator */

.transcribing-indicator {
  background: #faf8ff;
  color: var(--purple-dark);
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 500;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.transcribing-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--purple);
  animation: transcribing-pulse 1.1s infinite ease-in-out;
}

@keyframes transcribing-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.7); }
}

/* Full-screen PWA install overlay — shown once per device until dismissed */

.pwa-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px 24px calc(28px + var(--safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.pwa-overlay.hidden {
  display: none;
}

.pwa-overlay-content {
  max-width: 460px;
  width: 100%;
  color: #6A22FF;
  text-align: center;
}

.pwa-overlay-title {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin: 0 0 12px;
  color: #6A22FF;
  line-height: 1.15;
}

.pwa-overlay-sub {
  font-size: 17px;
  line-height: 1.45;
  margin: 0 0 28px;
  color: #6A22FF;
  opacity: 0.9;
}

.pwa-overlay-section {
  margin: 0 0 22px;
  text-align: left;
}

.pwa-overlay-heading {
  font-size: 18px;
  font-weight: 700;
  margin: 0 0 10px;
  color: #6A22FF;
}

.pwa-overlay-steps {
  font-size: 16px;
  line-height: 1.55;
  margin: 0;
  padding-left: 22px;
  color: #6A22FF;
}

.pwa-overlay-steps li {
  margin-bottom: 6px;
}

.pwa-overlay-steps strong {
  font-weight: 700;
}

.pwa-overlay-btn {
  display: block;
  width: 100%;
  margin-top: 24px;
  padding: 16px 20px;
  background: #6A22FF;
  color: #fff;
  border: none;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(106, 34, 255, 0.28);
  -webkit-tap-highlight-color: transparent;
}

.pwa-overlay-btn:hover,
.pwa-overlay-btn:focus-visible {
  background: #5a12ef;
}

.pwa-overlay-btn:active {
  transform: scale(0.98);
}

/* Input bar */

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: #fff;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  font-size: 16px;
  padding: 12px 16px;
  border: 2px solid var(--border);
  border-radius: 20px;
  outline: none;
  background: #fff;
  transition: border-color 0.15s ease;
  min-width: 0;
  /* Auto-grow textarea */
  resize: none;
  font-family: inherit;
  line-height: 1.4;
  min-height: 44px;            /* matches icon-btn height for single-line state */
  max-height: 160px;           /* roughly 6 lines — then the textarea scrolls */
  overflow-y: auto;
  box-sizing: border-box;
  -webkit-appearance: none;    /* kill iOS default textarea styling */
}

#chat-input:focus {
  border-color: var(--purple);
}

/* Buttons stay on the bottom row of the expanding textarea */
.chat-form-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s ease, background 0.2s ease;
}

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

.send-btn {
  background: var(--purple);
}

.mic-btn {
  background: #9E9EB0;
}

.mic-btn.recording {
  background: var(--danger);
}

.mic-btn.hidden { display: none; }
