:root {
  --primary: #ff8c42;
  --primary-light: #fff3e6;
  --secondary: #4ea8de;
  --accent: #ffd166;
  --success: #06d6a0;
  --danger: #ef476f;
  --text-main: #2b2d42;
  --text-muted: #64748b;
  --card-bg: #ffffff;
  --border-radius-lg: 24px;
  --border-radius-md: 18px;
  --border-radius-sm: 10px;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Nunito", sans-serif;

  /* Safe Area Insets for iOS Fullscreen Notch / Dynamic Island / Clock & Home Indicator */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Standalone / PWA Fullscreen Mode (Added to iOS Home Screen) */
@media all and (display-mode: standalone) {
  :root {
    --safe-top: max(env(safe-area-inset-top, 0px), 48px);
    --safe-bottom: max(env(safe-area-inset-bottom, 0px), 20px);
  }
}

/* iOS WebKit standalone specific */
@supports (-webkit-touch-callout: none) {
  @media all and (display-mode: standalone) {
    :root {
      --safe-top: max(env(safe-area-inset-top, 0px), 50px);
    }
  }
}

html.ios-standalone {
  --safe-top: max(env(safe-area-inset-top, 0px), 50px);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: var(--font-family);
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background: #f8fafc;
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Chat App Layout Container - Gemini App Style */
#kidChatApp {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: #f8fafc;
}

/* Header - Fixed with iOS Safe Area support */
.kid-header {
  background: #ffffff;
  padding-top: calc(12px + max(var(--safe-top), env(safe-area-inset-top, 0px)));
  padding-right: calc(16px + max(var(--safe-right), env(safe-area-inset-right, 0px)));
  padding-bottom: 12px;
  padding-left: calc(16px + max(var(--safe-left), env(safe-area-inset-left, 0px)));
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
  border-bottom: 1px solid #f1f5f9;
  flex-shrink: 0;
  z-index: 100;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar-badge {
  font-size: 32px;
  background: var(--primary-light);
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(255, 140, 66, 0.25);
  animation: bounce 2s infinite ease-in-out;
  flex-shrink: 0;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.brand-title h1 {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1.2;
}

.brand-title p {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.current-child-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text-main);
}

.child-badge-avatar {
  font-size: 1.1rem;
}

.logout-btn {
  background: #fff1f2;
  border: 1px solid #fecdd3;
  color: #e11d48;
  padding: 7px 14px;
  border-radius: 20px;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
  outline: none;
}

.logout-btn:hover {
  background: #e11d48;
  border-color: #e11d48;
  color: #ffffff;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(225, 29, 72, 0.2);
}

.logout-btn:active {
  transform: translateY(0);
}

/* Status Bar - Always Fixed right below Header */
.status-bar {
  background: #fff8f0;
  border-bottom: 1px solid #ffe8d1;
  padding: 8px calc(20px + var(--safe-right)) 8px calc(20px + var(--safe-left));
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  font-weight: 600;
  flex-shrink: 0;
  z-index: 90;
}

.quota-badge, .bedtime-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.quota-badge {
  color: var(--primary);
}

.bedtime-badge {
  color: #475569;
}

.status-icon {
  font-size: 1.05rem;
}

/* Chat Container - Only message list scrolls (Gemini Style) */
.chat-main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  padding: 0;
  position: relative;
}

.messages-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 20px calc(16px + var(--safe-right)) 20px calc(16px + var(--safe-left));
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.message-row {
  display: flex;
  gap: 12px;
  max-width: 88%;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message-row.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-row.bot {
  align-self: flex-start;
  flex-direction: row;
}

.msg-avatar {
  font-size: 26px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.msg-avatar.bot-avatar {
  background: #fff3e6;
  border: 2px solid #ffd8a8;
}

.msg-avatar.child-avatar {
  background: #e0f2fe;
  border: 2px solid #bae6fd;
}

.msg-content-wrapper {
  display: flex;
  flex-direction: column;
}

/* Message Bubble - Increased font size for children */
.msg-bubble {
  padding: 14px 20px;
  border-radius: var(--border-radius-md);
  font-size: 1.15rem; /* Larger font size for kids */
  line-height: 1.6;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  word-break: break-word;
}

.message-row.user .msg-bubble {
  background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
  color: white;
  border-top-right-radius: 4px;
}

.message-row.bot .msg-bubble {
  background: white;
  color: var(--text-main);
  border-top-left-radius: 4px;
  border: 1px solid #e2e8f0;
}

.message-row.bot.flagged .msg-bubble {
  background: #fff5f5;
  border: 1px solid #fed7d7;
  color: #c53030;
}

.bot-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 6px;
}

.speak-btn {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  padding: 5px 12px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: #475569;
  transition: all 0.2s;
}

.speak-btn:hover {
  background: #f1f5f9;
  color: var(--primary);
  border-color: var(--primary);
}

/* Typing Indicator */
.typing-bubble {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 14px 20px;
  background: white;
  border-radius: var(--border-radius-md);
  border-top-left-radius: 4px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.dot {
  width: 8px;
  height: 8px;
  background: #94a3b8;
  border-radius: 50%;
  animation: dotPulse 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes dotPulse {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Chat Footer - Always Fixed at Bottom (Gemini Style) */
.chat-footer {
  flex-shrink: 0;
  background: linear-gradient(to top, #f8fafc 85%, rgba(248, 250, 252, 0) 100%);
  padding: 8px calc(16px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(16px + var(--safe-left));
  width: 100%;
  z-index: 100;
}

.input-wrapper {
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.input-container {
  background: white;
  border-radius: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 6px 8px 6px 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 2px solid #e2e8f0;
  transition: all 0.2s ease;
}

.input-container:focus-within {
  border-color: var(--primary);
  box-shadow: 0 4px 24px rgba(255, 140, 66, 0.2);
}

/* Chat Input - Increased font size for kids */
.chat-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 1.15rem; /* Larger font size for kids */
  line-height: 1.4;
  padding: 10px 4px;
  color: var(--text-main);
  background: transparent;
  min-height: 24px;
}

.chat-input::placeholder {
  color: #94a3b8;
  font-size: 1.05rem;
}

/* Microphone Button - Always Visible & Interactive */
.mic-btn {
  background: #f1f5f9;
  border: none;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #475569;
  transition: all 0.2s;
  flex-shrink: 0;
}

.mic-btn:hover {
  background: #e2e8f0;
  color: var(--primary);
  transform: scale(1.05);
}

.mic-btn.listening {
  background: var(--danger);
  color: white;
  animation: pulseMic 1.2s infinite;
}

@keyframes pulseMic {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 71, 111, 0.5); }
  70% { transform: scale(1.1); box-shadow: 0 0 0 12px rgba(239, 71, 111, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 71, 111, 0); }
}

.send-btn {
  background: var(--primary);
  color: white;
  border: none;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.send-btn:hover {
  background: #f77f00;
  transform: scale(1.06);
}

.send-btn:active {
  transform: scale(0.96);
}

.send-btn:disabled {
  background: #cbd5e0;
  cursor: not-allowed;
  transform: none;
}

/* Bedtime Overlay */
.bedtime-screen {
  background: #1a202c;
  color: white;
  border-radius: var(--border-radius-lg);
  padding: 40px 20px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  margin: 40px auto;
  max-width: 600px;
}

.bedtime-avatar {
  font-size: 72px;
  margin-bottom: 16px;
  animation: floating 3s ease-in-out infinite;
}

@keyframes floating {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.bedtime-screen h2 {
  font-size: 1.5rem;
  margin-bottom: 12px;
  color: var(--accent);
}

.bedtime-screen p {
  font-size: 1.05rem;
  line-height: 1.6;
  max-width: 500px;
  margin: 0 auto;
  color: #cbd5e0;
}

/* Footer Disclaimer */
.disclaimer {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 6px 10px 0 10px;
}

/* Toast Notification */
.toast-notification {
  position: fixed;
  bottom: calc(90px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: white;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 0.95rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  z-index: 1000;
  max-width: 90%;
  text-align: center;
  animation: slideToastUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes slideToastUp {
  from { opacity: 0; transform: translate(-50%, 15px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ===================================================
   PLEX-STYLE USER SELECTION & PIN AUTHENTICATION
=================================================== */

.plex-screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 50% 25%, #182236 0%, #0a0d14 100%);
  color: #f8fafc;
  display: flex;
  flex-direction: column;
  z-index: 1000;
  overflow-y: auto;
}

.plex-header {
  padding-top: calc(16px + max(var(--safe-top), env(safe-area-inset-top, 0px)));
  padding-right: calc(24px + max(var(--safe-right), env(safe-area-inset-right, 0px)));
  padding-bottom: 16px;
  padding-left: calc(24px + max(var(--safe-left), env(safe-area-inset-left, 0px)));
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(10, 13, 20, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.plex-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: #f8fafc;
}

.plex-brand-tag {
  background: rgba(229, 160, 13, 0.15);
  color: #f59e0b;
  border: 1px solid rgba(229, 160, 13, 0.3);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.plex-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}

.plex-nav-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #cbd5e1;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.plex-nav-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
  border-color: rgba(255, 255, 255, 0.25);
}

.plex-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
}

.plex-title {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
  color: #ffffff;
}

.plex-subtitle {
  font-size: 1rem;
  color: #94a3b8;
  margin-bottom: 48px;
}

.plex-profiles-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 28px;
  max-width: 900px;
  margin: 0 auto;
}

.plex-profile-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  width: 140px;
  transition: all 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  text-decoration: none;
  outline: none;
}

.plex-avatar-box {
  width: 120px;
  height: 120px;
  border-radius: 22px;
  background: #141b2b;
  border: 3px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 54px;
  position: relative;
  transition: all 0.25s ease-in-out;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.plex-profile-card:hover .plex-avatar-box,
.plex-profile-card:focus .plex-avatar-box {
  transform: translateY(-8px) scale(1.06);
  border-color: #f59e0b;
  box-shadow: 0 16px 36px rgba(245, 158, 11, 0.3);
}

.plex-lock-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(10, 13, 20, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #f59e0b;
}

.plex-profile-name {
  margin-top: 14px;
  font-size: 1.05rem;
  font-weight: 700;
  color: #f8fafc;
  letter-spacing: -0.01em;
  transition: color 0.2s;
}

.plex-profile-card:hover .plex-profile-name {
  color: #f59e0b;
}

.plex-profile-meta {
  font-size: 0.8rem;
  color: #64748b;
  margin-top: 3px;
}

/* PLEX PIN MODAL */
.plex-pin-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(5, 8, 15, 0.88);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1100;
  padding: calc(20px + max(var(--safe-top), env(safe-area-inset-top, 0px))) 20px calc(20px + max(var(--safe-bottom), env(safe-area-inset-bottom, 0px))) 20px;
}

.plex-pin-box {
  background: #111827;
  border: 1px solid #243048;
  border-radius: 20px;
  padding: 32px 28px;
  max-width: 380px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.6);
  position: relative;
}

.plex-pin-box.shake {
  animation: shake 0.4s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-10px); }
  40%, 80% { transform: translateX(10px); }
}

.plex-pin-avatar {
  width: 72px;
  height: 72px;
  border-radius: 16px;
  background: #1a2336;
  border: 2px solid #334155;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  margin-bottom: 12px;
}

.plex-pin-name {
  font-size: 1.25rem;
  font-weight: 700;
  color: #f8fafc;
}

.plex-pin-prompt {
  font-size: 0.85rem;
  color: #94a3b8;
  margin-top: 4px;
  margin-bottom: 24px;
}

.plex-dots-container {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
}

.plex-pin-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #475569;
  background: transparent;
  transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.plex-pin-dot.filled {
  background: #f59e0b;
  border-color: #f59e0b;
  box-shadow: 0 0 12px rgba(245, 158, 11, 0.6);
  transform: scale(1.15);
}

.plex-pin-dot.error {
  background: #ef4444;
  border-color: #ef4444;
  box-shadow: 0 0 12px rgba(239, 68, 68, 0.6);
}

.plex-numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 270px;
  margin-bottom: 18px;
}

.numpad-btn {
  height: 56px;
  border-radius: 14px;
  background: #1a2336;
  border: 1px solid #243048;
  color: #f8fafc;
  font-size: 1.35rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease-in-out;
  outline: none;
  user-select: none;
}

.numpad-btn:hover {
  background: #28354f;
  border-color: #3b4d70;
  transform: translateY(-2px);
}

.numpad-btn:active {
  transform: translateY(1px);
  background: #141b2b;
}

.numpad-btn.action-btn {
  font-size: 0.95rem;
  color: #94a3b8;
}

.plex-pin-error {
  font-size: 0.82rem;
  color: #ef4444;
  min-height: 20px;
  margin-bottom: 8px;
  text-align: center;
}

.plex-pin-back-btn {
  background: none;
  border: none;
  color: #94a3b8;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  transition: color 0.15s;
}

.plex-pin-back-btn:hover {
  color: #f8fafc;
  background: rgba(255, 255, 255, 0.05);
}

/* Switch user button in chat header */
.switch-user-btn {
  background: rgba(255, 140, 66, 0.12);
  border: 1px solid rgba(255, 140, 66, 0.3);
  color: var(--primary);
  padding: 7px 12px;
  border-radius: 10px;
  font-size: 0.84rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
  text-decoration: none;
}

.switch-user-btn:hover {
  background: var(--primary);
  color: white;
}
