/* ==========================================================================
   Kai — Dark Minimalist Design System & Flapping Avatar
   ========================================================================== */

:root {
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  /* Refined Modern Dark Glass Palette */
  --bg-base: #08090d;
  --bg-surface: rgba(17, 19, 26, 0.82);
  --bg-surface-elevated: #181b26;
  --bg-input: rgba(18, 21, 29, 0.9);
  --bg-message-user: linear-gradient(135deg, #222736 0%, #191c28 100%);
  --bg-message-ai: rgba(20, 23, 32, 0.85);

  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-medium: rgba(255, 255, 255, 0.14);
  --border-active: rgba(168, 85, 247, 0.45);

  --text-primary: #f1f2f6;
  --text-secondary: #9aa0b0;
  --text-tertiary: #5d6375;

  --accent-red: #e11d48;
  --accent-purple: #a855f7;
  --accent-indigo: #6366f1;
  --status-online: #22c55e;

  --radius-xs: 6px;
  --radius-sm: 9px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --radius-pill: 9999px;
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--bg-base);
  background-image: 
    radial-gradient(circle at 50% -10%, rgba(147, 51, 234, 0.09) 0%, transparent 60%),
    radial-gradient(circle at 10% 30%, rgba(99, 102, 241, 0.05) 0%, transparent 40%),
    radial-gradient(circle at 90% 80%, rgba(225, 29, 72, 0.04) 0%, transparent 50%);
  color: var(--text-primary);
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  position: relative;
}

/* ==========================================================================
   Minimalist Header
   ========================================================================== */

.app-header {
  height: 52px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(8, 9, 13, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 20;
  flex-shrink: 0;
}

.header-left, .header-center, .header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-logo {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #ffffff 40%, #c4b5fd 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.model-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 4px 10px;
  transition: border-color 0.2s;
}

.model-badge:hover {
  border-color: var(--border-medium);
}

.status-indicator {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--status-online);
  box-shadow: 0 0 8px rgba(34, 197, 94, 0.5);
}

.model-select {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  outline: none;
  padding-right: 14px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='none' stroke='%238c8f9a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' d='M1 1l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right center;
}

.model-select:hover {
  color: var(--text-primary);
}

.header-action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.header-action-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-medium);
  transform: translateY(-1px);
}

.header-action-btn.active {
  color: #f5d0fe;
  background: rgba(168, 85, 247, 0.12);
  border-color: rgba(168, 85, 247, 0.35);
  box-shadow: 0 0 12px rgba(168, 85, 247, 0.15);
}

.nav-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* ==========================================================================
   Main Container
   ========================================================================== */

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 840px;
  width: 100%;
  margin: 0 auto;
  padding: 14px 20px 18px;
  height: calc(100vh - 52px);
  overflow: hidden;
}

/* 1. TOP: Kai Avatar */
.avatar-section {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-bottom: 8px;
  flex-shrink: 0;
}

/* Ambient glow spotlight behind Kai */
.avatar-pedestal-glow {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 250px;
  height: 140px;
  background: radial-gradient(ellipse at 50% 50%, rgba(168, 85, 247, 0.16) 0%, rgba(99, 102, 241, 0.08) 45%, transparent 70%);
  filter: blur(28px);
  pointer-events: none;
  z-index: 0;
}

/* Locked, exact pixel dimensions */
.avatar-frame {
  position: relative;
  width: 220px;
  height: 242px; /* 220 * (1036 / 943) = 241.67px */
  margin: 0 auto;
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  z-index: 1;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), filter 0.2s ease;
}

.avatar-frame:hover {
  filter: brightness(1.1) drop-shadow(0 8px 16px rgba(168, 85, 247, 0.2));
  transform: scale(1.025);
}

.avatar-frame:active {
  transform: scale(0.985);
}

/* Mouth cavity inside the cut line */
.mouth-cavity {
  position: absolute;
  left: 37.22%;  /* 351 / 943 */
  top: 32.82%;   /* 340 / 1036 */
  width: 24.81%; /* 234 / 943 */
  height: 5.5%;
  background: #110305;
  border-radius: 0 0 8px 8px;
  z-index: 1;
  overflow: hidden;
}

.mouth-cavity .tongue {
  position: absolute;
  bottom: -3px;
  left: 20%;
  width: 60%;
  height: 70%;
  background: var(--accent-red);
  border-radius: 50% 50% 0 0;
}

/* Torso & jaw */
.kai-bottom {
  position: absolute;
  left: 0;
  top: 34.556%; /* 358 / 1036 */
  width: 100%;
  height: 65.444%; /* 678 / 1036 */
  object-fit: contain;
  z-index: 2;
  pointer-events: none;
}

/* Flapping top head */
.kai-top {
  position: absolute;
  left: 31.283%; /* 295 / 943 */
  top: 0;
  width: 36.585%; /* 345 / 943 */
  height: 34.556%; /* 358 / 1036 */
  object-fit: contain;
  z-index: 3;
  transform-origin: 85% 95%; /* back hinge for jaw flap */
  pointer-events: none;
  will-change: transform;
  transition: transform 0.05s ease-out;
}

/* Active Flapping State (ONLY active while speaking) */
.avatar-frame.is-flapping .kai-top {
  animation: canadianHingedFlap 0.13s infinite alternate cubic-bezier(0.15, 0.9, 0.2, 1.2);
}

@keyframes canadianHingedFlap {
  0% {
    transform: translateY(0px) rotate(0deg);
  }
  25% {
    transform: translateY(-10px) rotate(-1deg);
  }
  75% {
    transform: translateY(-32px) rotate(-3.5deg);
  }
  100% {
    transform: translateY(-36px) rotate(-3deg);
  }
}

/* Sassy status badge pill */
.status-pill {
  position: relative;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 6px;
  padding: 4px 12px;
  background: rgba(22, 25, 34, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  transition: all 0.2s ease;
}

.status-pill:hover {
  border-color: var(--border-medium);
  background: rgba(28, 32, 44, 0.85);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #a855f7;
  box-shadow: 0 0 6px #a855f7;
  animation: statusPulse 2s infinite ease-in-out;
}

@keyframes statusPulse {
  0%, 100% { opacity: 0.6; transform: scale(0.9); }
  50% { opacity: 1; transform: scale(1.15); }
}

.status-caption {
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: -0.01em;
}

/* ==========================================================================
   2. MIDDLE: Chat Display Area
   ========================================================================== */

.chat-display-area {
  flex: 1;
  min-height: 220px;
  background: var(--bg-surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  scroll-behavior: smooth;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

/* Custom Sleek Scrollbar */
.chat-display-area::-webkit-scrollbar,
.history-items::-webkit-scrollbar {
  width: 6px;
}
.chat-display-area::-webkit-scrollbar-track,
.history-items::-webkit-scrollbar-track {
  background: transparent;
}
.chat-display-area::-webkit-scrollbar-thumb,
.history-items::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-pill);
}
.chat-display-area::-webkit-scrollbar-thumb:hover,
.history-items::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}

.messages-flow {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
}

.empty-state {
  margin: auto;
  text-align: center;
  padding: 24px 16px;
  max-width: 520px;
}

.empty-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  background: rgba(168, 85, 247, 0.12);
  color: #d8b4fe;
  border: 1px solid rgba(168, 85, 247, 0.25);
  margin-bottom: 8px;
}

.empty-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.empty-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.55;
  margin-bottom: 18px;
}

.quick-prompts {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.prompt-chip {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 13px;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.prompt-chip:hover {
  background: rgba(168, 85, 247, 0.12);
  border-color: rgba(168, 85, 247, 0.35);
  color: #f3e8ff;
  transform: translateY(-1.5px);
  box-shadow: 0 4px 12px rgba(168, 85, 247, 0.15);
}

.prompt-chip:active {
  transform: translateY(0);
}

/* Message Rows */
.msg-entry {
  display: flex;
  flex-direction: column;
  width: 100%;
  animation: msgFadeIn 0.2s ease-out;
}

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

.msg-entry.user {
  align-items: flex-end;
}

.msg-entry.assistant {
  align-items: flex-start;
}

.msg-meta {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-tertiary);
  margin-bottom: 4px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.msg-entry.assistant .msg-meta {
  color: #c084fc;
}

.msg-body {
  max-width: 86%;
  border-radius: var(--radius-md);
  padding: 11px 16px;
  font-size: 13.5px;
  line-height: 1.6;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.msg-entry.user .msg-body {
  background: var(--bg-message-user);
  border: 1px solid var(--border-medium);
  border-bottom-right-radius: 4px;
  color: var(--text-primary);
}

.msg-entry.assistant .msg-body {
  background: var(--bg-message-ai);
  border: 1px solid var(--border-subtle);
  border-bottom-left-radius: 4px;
  color: var(--text-primary);
}

.msg-body p {
  margin-bottom: 8px;
}

.msg-body p:last-child {
  margin-bottom: 0;
}

.msg-body pre {
  background: #0d0e12 !important;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xs);
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: var(--font-mono);
  font-size: 12px;
}

.msg-body code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: rgba(255, 255, 255, 0.06);
  padding: 2px 4px;
  border-radius: 3px;
  color: #a5b4fc;
}

.msg-body pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

.msg-toolbar {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.action-link {
  background: transparent;
  border: none;
  font-size: 11px;
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
}

.action-link:hover {
  color: var(--text-secondary);
  background: rgba(255, 255, 255, 0.04);
}

/* Typing Indicator */
.typing-line {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

.typing-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing 1.2s infinite ease-in-out;
}

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

@keyframes typing {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.9); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* ==========================================================================
   3. BOTTOM: Input Bar
   ========================================================================== */

.input-section {
  width: 100%;
  margin-top: 12px;
  flex-shrink: 0;
}

.input-bar {
  width: 100%;
  background: var(--bg-input);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
  padding: 6px 10px 6px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.input-bar:focus-within {
  border-color: var(--border-active);
  box-shadow: 0 0 0 2px rgba(168, 85, 247, 0.2), 0 8px 28px rgba(0, 0, 0, 0.4);
}

.tool-btn {
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tool-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
  transform: translateY(-1px);
}

.tool-btn.listening {
  color: #ffffff;
  background: rgba(225, 29, 72, 0.25);
  border: 1px solid rgba(225, 29, 72, 0.5);
  box-shadow: 0 0 12px rgba(225, 29, 72, 0.4);
  animation: micPulse 1.2s infinite ease-in-out;
}

@keyframes micPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.08); opacity: 0.8; }
}

.input-textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13.5px;
  resize: none;
  max-height: 120px;
  padding: 6px 0;
}

.input-textarea::placeholder {
  color: var(--text-tertiary);
}

.send-btn, .stop-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-xs);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.send-btn {
  background: linear-gradient(135deg, #a855f7 0%, #6366f1 100%);
  color: #ffffff;
  box-shadow: 0 2px 10px rgba(168, 85, 247, 0.3);
}

.send-btn:hover {
  transform: translateY(-1px) scale(1.04);
  box-shadow: 0 4px 16px rgba(168, 85, 247, 0.45);
}

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

.stop-btn {
  background: rgba(225, 29, 72, 0.15);
  color: #fda4af;
  border: 1px solid rgba(225, 29, 72, 0.35);
}

.stop-btn:hover {
  background: rgba(225, 29, 72, 0.25);
  transform: translateY(-1px);
}

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

/* ==========================================================================
   Drawer & Modal
   ========================================================================== */

.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  z-index: 50;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
}

.drawer-overlay.hidden, .modal-backdrop.hidden {
  display: none !important;
}

.model-badge-static {
  padding: 6px 10px;
  background: var(--bg-base);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xs);
  color: var(--text-secondary);
  font-size: 12px;
  font-family: var(--font-mono);
}

.history-panel {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 260px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  z-index: 60;
  display: flex;
  flex-direction: column;
  padding: 16px;
}

.history-panel.hidden {
  display: none;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.panel-header h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.new-conversation-btn {
  padding: 8px 10px;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  border-radius: var(--radius-xs);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 12px;
  text-align: left;
}

.new-conversation-btn:hover {
  border-color: var(--border-medium);
}

.history-items {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-row {
  padding: 8px 10px;
  border-radius: var(--radius-xs);
  color: var(--text-secondary);
  font-size: 12.5px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-row:hover, .history-row.active {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
}

.panel-footer {
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
}

.subtle-btn {
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  font-size: 11.5px;
  cursor: pointer;
}

.subtle-btn:hover {
  color: var(--text-secondary);
}

/* Settings Dialog */
.settings-dialog {
  width: 90%;
  max-width: 420px;
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
  margin: auto;
  display: flex;
  flex-direction: column;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6);
}

.dialog-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h2 {
  font-size: 14px;
  font-weight: 600;
}

.dialog-content {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-row label {
  display: block;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.control-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.control-header span {
  font-size: 11.5px;
  color: var(--text-tertiary);
}

.form-select {
  width: 100%;
  padding: 6px 10px;
  background: var(--bg-base);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xs);
  color: var(--text-primary);
  font-size: 12.5px;
  outline: none;
}

.form-slider {
  width: 100%;
  accent-color: var(--text-primary);
}

.dialog-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  justify-content: flex-end;
}

.primary-btn {
  padding: 6px 14px;
  background: var(--text-primary);
  color: var(--bg-base);
  border: none;
  border-radius: var(--radius-xs);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:hover {
  background: #ffffff;
}
