/**
 * CSS Variables
 */
:root {
  /* Colors */
  --background: #0a0e1a;
  --background-elevated: #111827;
  --foreground: #f3f4f6;
  --foreground-muted: #9ca3af;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.3);
  --muted: #6b7280;
  --border: rgba(255, 255, 255, 0.1);
  --message-bg: rgba(255, 255, 255, 0.05);
  --message-bg-sent: rgba(59, 130, 246, 0.15);

  /* Sizes */
  --fab: 48px;
  --gap: 12px;
  --rail-max: 680px;

  /* Safe area insets */
  --safe-left: 16px;
  --safe-right: 16px;
  
  /* Typography */
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", "Monaco", "Inconsolata", "Fira Code", monospace;
}

@supports (padding: max(0px)) {
  :root {
    --safe-left: max(12px, env(safe-area-inset-left));
    --safe-right: max(12px, env(safe-area-inset-right));
  }
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/**
 * Base Styles
 */
html,
body {
  height: 100%;
  background: linear-gradient(135deg, var(--background) 0%, #0d1117 100%);
  color: var(--foreground);
  font-family: var(--font-ui);
  text-align: center;
  display: block;
  padding: 2rem;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

@media (max-width: 500px) {
  html,
  body {
    padding: 0;
  }
  
  main {
    padding-top: 3rem;
  }
  
  .chat-modal {
    padding: 0.5rem 1rem 1rem;
    padding-top: max(0.5rem, env(safe-area-inset-top, 0.5rem));
  }
  
  .chat-header {
    margin-bottom: 0.5rem;
    padding-top: 0.25rem;
  }
  
  .chat-rail {
    flex: 1;
    min-height: 0;
  }
  
  #transcriptionBox {
    flex: 1;
    min-height: 0;
    height: auto;
    max-height: none;
  }
  
  .chat-modal footer {
    position: relative;
    margin-top: 0.5rem;
    padding-bottom: env(safe-area-inset-bottom, 0.5rem);
  }
}
main {
  display: flex;
  flex-direction: column;
  align-items: center;
}

h1 {
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--foreground);
  animation: fadeIn 1.5s ease-out;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
}

.highlight-word {
  color: var(--accent);
  font-weight: 700;
  padding: 0 0.25rem;
}

.pulse {
  background: linear-gradient(135deg, var(--accent) 0%, #60a5fa 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: bold;
  animation: pulse 2s infinite;
}

p {
  margin-top: 1rem;
  font-size: 1.1rem;
  color: var(--muted);
}

footer {
  position: absolute;
  bottom: 1rem;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--muted);
}

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

@keyframes pulse {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

/**
 * Chat Message Display
 */

#transcriptionBox {
  align-self: center;
  position: static;
  max-width: var(--rail-max);
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  height: 65vh;
  min-height: 300px;
  max-height: 700px;
  overflow-y: auto;
  background: rgba(17, 24, 39, 0.6);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 1.5rem;
  color: var(--foreground);
  font-size: 0.95rem;
  line-height: 1.6;
  text-align: left;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  scrollbar-width: thin;
  scrollbar-color: var(--muted) transparent;
}

#transcriptionBox::-webkit-scrollbar {
  width: 6px;
}

#transcriptionBox::-webkit-scrollbar-track {
  background: transparent;
}

#transcriptionBox::-webkit-scrollbar-thumb {
  background: var(--muted);
  border-radius: 3px;
}

#transcriptionBox > .message {
  margin-bottom: 0.75rem;
  padding: 0.875rem 1rem;
  border-radius: 16px;
  background: var(--message-bg);
  backdrop-filter: blur(10px);
  word-break: break-word;
  border: 1px solid var(--border);
  transition: all 0.2s ease;
  position: relative;
  animation: messageSlideIn 0.3s ease-out;
}

#transcriptionBox > .message:hover {
  background: rgba(255, 255, 255, 0.08);
  transform: translateX(2px);
}

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

/**
 * Modal Styles
 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  animation: fadeIn 0.2s ease-out;
}

.modal-content {
  position: relative;
  background: rgba(17, 24, 39, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 2.5rem;
  border-radius: 24px;
  width: 90%;
  max-width: 400px;
  text-align: center;
  border: 1px solid var(--border);
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  animation: modalSlideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-content input {
  width: 100%;
  padding: 0.875rem 1rem;
  margin: 0.5rem 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--foreground);
  font-family: var(--font-ui);
  font-size: 0.95rem;
  transition: all 0.2s ease;
}

.modal-content input:focus {
  outline: none;
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.modal-content input::placeholder {
  color: var(--foreground-muted);
}

.modal-content button {
  width: 100%;
  padding: 0.875rem 1.25rem;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  border: none;
  border-radius: 12px;
  color: white;
  cursor: pointer;
  margin-top: 0.75rem;
  font-weight: 600;
  font-size: 0.95rem;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.modal-content button:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}

.modal-content button:active {
  transform: translateY(0);
}

.error {
  color: red;
  font-size: 0.9rem;
  margin-top: 0.3rem;
}

.divider {
  margin: 1rem 0;
  border-top: 1px solid #333;
}

/**
 * Chat Interface
 */
.chat-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, var(--background) 0%, #0d1117 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(1rem, env(safe-area-inset-top, 1rem)) 2rem 2rem;
  z-index: 1000;
  justify-content: flex-start;
}

.chat-header {
  width: 100%;
  max-width: var(--rail-max);
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
  padding-top: 0.5rem;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.select-chat-button {
  display: none;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.select-chat-button.show {
  display: inline-flex;
}

.select-chat-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}

.select-chat-button svg {
  transition: transform 0.2s;
}

.select-chat-button:hover svg {
  transform: translateY(1px);
}

.chat-header span {
  color: var(--foreground);
  font-size: 1rem;
}

.chat-header button {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-weight: 500;
}

.chat-header button:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-1px);
}

.enter-chat-button {
  padding: 1rem 2rem;
  font-size: 1.1rem;
  font-weight: 600;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  border: none;
  border-radius: 14px;
  color: white;
  cursor: pointer;
  margin-top: 2rem;
  transition: all 0.3s ease;
  box-shadow: 0 8px 24px rgba(59, 130, 246, 0.4);
  position: relative;
  overflow: hidden;
}

.enter-chat-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.enter-chat-button:hover::before {
  left: 100%;
}

.enter-chat-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(59, 130, 246, 0.5);
}

.enter-chat-button:active {
  transform: translateY(0);
}

.close-modal {
  position: absolute;
  top: 10px;
  right: 12px;
  background: transparent !important;
  border: none !important;
  color: var(--muted) !important;
  font-size: 1.5rem !important;
  font-weight: bold;
  line-height: 1;
  padding: 0 !important;
  margin: 0 !important;
  width: auto !important;
  height: auto !important;
  display: inline !important;
  box-shadow: none !important;
  border-radius: 0 !important;
}

.close-modal:hover {
  color: var(--accent);
}

/**
 * Message Composition
 */
body.modal-open {
  overflow: hidden;
  height: 100%;
}

.compose {
  position: relative;
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}

.compose .field-wrap {
  position: relative;
  width: 100%;
  padding-bottom: calc(var(--fab) / 2 + 10px);
}

.compose textarea {
  flex: 1;
  resize: vertical;
  min-height: 3.2em;
  max-height: 35vh;
  line-height: 1.5;
  font-size: 16px;
  background: rgba(17, 24, 39, 0.6);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--foreground);
  font-family: var(--font-ui);
  padding: 1rem 3.5rem 1rem 1rem;
  width: 100%;
  transition: all 0.2s ease;
}

.compose textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: rgba(17, 24, 39, 0.8);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.compose textarea::placeholder {
  color: var(--foreground-muted);
}

.compose .field-wrap textarea#userText {
  padding-right: calc(var(--fab) * 2 + var(--gap) * 3);
  overflow-y: auto;
}

.compose .field-wrap .fab {
  position: absolute;
  bottom: 10px;
  width: var(--fab);
  height: var(--fab);
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  user-select: none;
  z-index: 2;
}

.compose .field-wrap .send-fab {
  right: var(--gap);
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  color: white;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
  transition: all 0.2s ease;
}

.compose .field-wrap .send-fab:hover {
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.5);
}

.compose .field-wrap .mic-fab {
  right: calc(var(--gap) + var(--fab) + var(--gap));
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  color: var(--foreground);
  border: 1px solid var(--border);
  transition: all 0.2s ease;
}

.compose .field-wrap .mic-fab:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px) scale(1.05);
}

.mic-fab.recording {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  color: white;
  z-index: 10;
  border: none;
  box-shadow:
    0 0 20px rgba(239, 68, 68, 0.6),
    0 0 40px rgba(239, 68, 68, 0.4),
    0 8px 24px rgba(239, 68, 68, 0.3);
  animation: micGlow 1.5s ease-in-out infinite;
  transition: all 0.3s ease;
}

@keyframes micGlow {
  0%,
  100% {
    box-shadow:
      0 0 15px rgba(239, 68, 68, 0.6),
      0 0 35px rgba(239, 68, 68, 0.4),
      0 8px 24px rgba(239, 68, 68, 0.3);
    transform: scale(1);
  }
  50% {
    box-shadow:
      0 0 25px rgba(239, 68, 68, 0.9),
      0 0 50px rgba(239, 68, 68, 0.7),
      0 8px 32px rgba(239, 68, 68, 0.5);
    transform: scale(1.05);
  }
}

.fab .icon {
  width: 22px;
  height: 22px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/**
 * Chat Layout
 */
.chat-rail {
  width: min(94vw, var(--rail-max));
  max-width: var(--rail-max);
  margin: 0 3px 0 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.chat-rail > .compose,
.chat-rail > #transcriptionBox {
  width: 100%;
}

.chat-rail > #transcriptionBox {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.highlight {
  outline: 2px solid var(--accent);
  transition: outline 0.6s ease-out;
}

/**
 * Chat Selector Modal (Admin Only)
 */
.chat-selector-modal {
  max-width: 500px;
  width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.chat-selector-modal h2 {
  margin-bottom: 1rem;
  color: var(--foreground);
}

.chat-selector-modal #chatSearchInput {
  width: 100%;
  padding: 0.875rem 1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--foreground);
  font-family: var(--font-ui);
  transition: all 0.2s ease;
}

.chat-selector-modal #chatSearchInput:focus {
  outline: none;
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.chat-selector-list {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow-y: auto;
  flex: 1;
  max-height: 60vh;
}

.chat-selector-item {
  display: flex;
  flex-direction: column;
  padding: 1rem;
  margin-bottom: 0.75rem;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: left;
}

.chat-selector-item:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--accent);
  transform: translateX(4px);
}

.chat-selector-item.active {
  background: rgba(59, 130, 246, 0.15);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.chat-selector-item.unread {
  border-left: 3px solid var(--accent);
}

.chat-selector-name {
  font-weight: 600;
  color: var(--foreground);
  margin-bottom: 0.3rem;
  font-size: 1rem;
}

.chat-selector-preview {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 0.3rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-selector-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: var(--muted);
}

.unread-badge {
  color: var(--accent);
  font-size: 1.2rem;
  margin-left: 0.5rem;
}

