/* ==================== Reset & Base ==================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --indigo: #4F46E5;
  --indigo-dark: #3730A3;
  --indigo-light: #EEF2FF;
  --gray-50: #F9FAFB;
  --gray-100: #F3F4F6;
  --gray-200: #E5E7EB;
  --gray-400: #9CA3AF;
  --gray-600: #4B5563;
  --gray-900: #111827;
  --white: #FFFFFF;
  --red: #EF4444;
  --green: #10B981;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px rgba(0,0,0,.1), 0 4px 6px rgba(0,0,0,.05);
  --radius: 16px;
  --radius-sm: 8px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  -webkit-font-smoothing: antialiased;
}

/* ==================== Login Screen ==================== */
#login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #EEF2FF 0%, #E0E7FF 100%);
}

.login-card {
  background: var(--white);
  border-radius: 24px;
  padding: 48px 36px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}

.login-logo { margin-bottom: 20px; display: flex; justify-content: center; }

.avatar-circle {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--indigo) 0%, #7C3AED 100%);
  color: var(--white);
  font-size: 18px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.avatar-circle.large { width: 80px; height: 80px; font-size: 32px; }

.login-title { font-size: 28px; font-weight: 700; color: var(--gray-900); margin-bottom: 6px; }
.login-subtitle { font-size: 14px; color: var(--gray-400); margin-bottom: 32px; }

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

#password-input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: 16px;
  outline: none;
  transition: border-color .2s;
  background: var(--gray-50);
}
#password-input:focus { border-color: var(--indigo); background: var(--white); }

#login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--indigo) 0%, #7C3AED 100%);
  color: var(--white);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .2s;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
#login-btn:hover { opacity: .9; }
#login-btn:disabled { opacity: .6; cursor: not-allowed; }

.error-msg { color: var(--red); font-size: 13px; }

/* ==================== Chat Screen ==================== */
#chat-screen {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 768px;
  margin: 0 auto;
  background: var(--white);
  box-shadow: var(--shadow-md);
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  position: sticky; top: 0; z-index: 10;
}

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

.header-info { display: flex; flex-direction: column; }
.header-name { font-size: 16px; font-weight: 600; line-height: 1.2; }
.header-status { font-size: 11px; color: var(--green); }

.icon-btn {
  background: none; border: none; cursor: pointer;
  color: var(--gray-600); padding: 6px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.icon-btn:hover { background: var(--gray-100); }

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 85%;
  animation: msgIn .2s ease-out;
}

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

.msg.user { margin-left: auto; flex-direction: row-reverse; }
.msg.aoi  { margin-right: auto; }

.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--indigo) 0%, #7C3AED 100%);
  color: var(--white); font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.msg-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: linear-gradient(135deg, var(--indigo) 0%, #7C3AED 100%);
  color: var(--white);
  border-bottom-right-radius: 4px;
}

.msg.aoi .msg-bubble {
  background: var(--gray-100);
  color: var(--gray-900);
  border-bottom-left-radius: 4px;
}

/* Markdown inside bubbles */
.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin-bottom: 8px; }
.msg-bubble li { margin-bottom: 4px; }
.msg-bubble strong { font-weight: 700; }
.msg-bubble code {
  background: rgba(0,0,0,.08);
  padding: 1px 4px;
  border-radius: 4px;
  font-family: 'SF Mono', Menlo, monospace;
  font-size: 12px;
}
.msg-bubble pre {
  background: rgba(0,0,0,.08);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin: 8px 0;
}
.msg-bubble th, .msg-bubble td {
  padding: 6px 10px;
  text-align: left;
  border-bottom: 1px solid rgba(0,0,0,.1);
}
.msg-bubble th { font-weight: 600; background: rgba(0,0,0,.05); }
.msg.user .msg-bubble table th,
.msg.user .msg-bubble table td { border-color: rgba(255,255,255,.2); }
.msg.user .msg-bubble table th { background: rgba(255,255,255,.15); }

/* Typing indicator */
.typing-indicator {
  display: flex; align-items: center; gap: 4px;
  padding: 12px 16px;
}
.typing-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--gray-400);
  animation: bounce .8s infinite;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* Input Area */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--gray-200);
  background: var(--white);
}

#message-input {
  flex: 1;
  padding: 10px 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: 22px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color .2s;
  background: var(--gray-50);
}
#message-input:focus { border-color: var(--indigo); background: var(--white); }

.send-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--indigo) 0%, #7C3AED 100%);
  color: var(--white);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: opacity .2s, transform .1s;
}
.send-btn:hover:not(:disabled) { opacity: .9; transform: scale(1.05); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ==================== Drawer ==================== */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 50;
}

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 260px;
  background: var(--white);
  z-index: 51;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform .25s ease;
}

.drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--gray-200);
  font-size: 16px; font-weight: 600;
}

.drawer-list { list-style: none; padding: 8px 0; }

.drawer-item {
  width: 100%;
  background: none; border: none;
  padding: 14px 20px;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
  color: var(--gray-900);
  transition: background .15s;
}
.drawer-item:hover { background: var(--gray-50); }
.drawer-item[data-action="logout"] { color: var(--red); }

/* ==================== Utilities ==================== */
.hidden { display: none !important; }

.spinner {
  width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
