/* ============================================================
   久嵜くんアシスタント – 木漏れ日テーマ (Komorebi Light Theme)
   ============================================================ */

/* ---------- Design Tokens ---------- */
:root {
  --bg-base: #f6f3ec;
  --bg-surface: rgba(255, 255, 255, 0.82);
  --bg-elevated: rgba(255, 255, 255, 0.65);
  --bg-hover: rgba(255, 255, 255, 0.95);

  --text-primary: #2c3a2e;
  --text-secondary: #4a5c4e;
  --text-muted: #8a9a8c;

  --accent: #5a9a6a;
  --accent-hover: #4a8458;
  --accent-dim: rgba(90, 154, 106, 0.10);
  --accent-glow: rgba(90, 154, 106, 0.20);

  --gold: #c4a44a;
  --gold-dim: rgba(196, 164, 74, 0.12);

  --success: #5a9a6a;
  --warning: #c4a44a;
  --error: #c45a5a;

  --border: rgba(90, 120, 90, 0.10);
  --border-strong: rgba(90, 120, 90, 0.18);

  --shadow-sm: 0 1px 4px rgba(60, 80, 60, 0.06);
  --shadow-md: 0 4px 16px rgba(60, 80, 60, 0.08);
  --shadow-lg: 0 8px 32px rgba(60, 80, 60, 0.10);
  --shadow-glass: 0 8px 32px rgba(60, 80, 60, 0.06), inset 0 1px 0 rgba(255,255,255,0.6);

  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans JP",
    "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  --font-mono: "SF Mono", "Cascadia Code", "Consolas", monospace;
}

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

html {
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body {
  font-family: var(--font-sans);
  color: var(--text-primary);
  height: 100dvh;
  overflow: hidden;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;

  /* 木漏れ日背景 — Unsplash photo with overlay */
  background: var(--bg-base);
  background-image:
    linear-gradient(180deg, rgba(246, 243, 236, 0.55) 0%, rgba(246, 243, 236, 0.70) 100%),
    url('https://images.unsplash.com/photo-1448375240586-882707db888b?w=1920&q=80&auto=format&fit=crop');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  padding: 0 12px;
  overflow-x: hidden;
}

/* ---------- Header ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  margin-top: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: var(--shadow-glass);
}

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

.logo {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  font-weight: 800;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 3px 12px rgba(90, 154, 106, 0.3);
}

.header-title {
  font-size: 16px;
  font-weight: 800;
  line-height: 1.3;
  color: #1a1a1a;
}

.header-sub {
  font-size: 12px;
  color: #2a2a2a;
  font-weight: 600;
  margin-top: 1px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.role-select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  padding: 6px 28px 6px 10px;
  outline: none;
  cursor: pointer;
  backdrop-filter: blur(8px);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a9a8c' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  transition: border-color 0.15s;
}

.role-select:hover,
.role-select:focus {
  border-color: var(--accent);
}

.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  backdrop-filter: blur(8px);
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--accent);
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

/* ---------- Messages area ---------- */
.messages {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 0;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.messages::-webkit-scrollbar { width: 5px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb {
  background: rgba(90, 120, 90, 0.15);
  border-radius: 10px;
}

/* ---------- Message bubbles ---------- */
.msg {
  animation: msgIn 0.35s ease-out;
  max-width: 100%;
  padding: 0 4px;
}

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

/* -- User message -- */
.msg-user {
  display: flex;
  justify-content: flex-end;
  margin: 8px 0;
}

.msg-user-bubble {
  background: linear-gradient(135deg, rgba(90, 154, 106, 0.12), rgba(90, 154, 106, 0.06));
  border: 1px solid rgba(90, 154, 106, 0.20);
  border-radius: var(--radius-lg) var(--radius-lg) 6px var(--radius-lg);
  padding: 11px 18px;
  max-width: 80%;
  color: var(--text-primary);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  backdrop-filter: blur(6px);
}

/* -- Bot message -- */
.msg-bot {
  display: flex;
  gap: 10px;
  margin: 8px 0;
  align-items: flex-start;
}

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
  box-shadow: 0 2px 8px rgba(90, 154, 106, 0.25);
}

.msg-body { flex: 1; min-width: 0; }

.msg-label {
  font-size: 12px;
  color: #2a2a2a;
  margin-bottom: 4px;
  font-weight: 700;
}

.msg-content {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 6px var(--radius-lg) var(--radius-lg) var(--radius-lg);
  padding: 14px 18px;
  line-height: 1.75;
  color: #1a1a1a;
  word-break: break-word;
  overflow-wrap: anywhere;
  overflow-x: hidden;
  min-width: 0;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow-sm);
}

/* Markdown inside bot messages */
.msg-content h2, .msg-content h3, .msg-content h4 {
  color: var(--text-primary);
  margin: 16px 0 8px;
  line-height: 1.4;
}
.msg-content h2 {
  font-size: 1.15rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}
.msg-content h3 { font-size: 1.05rem; }
.msg-content h4 { font-size: 1rem; color: var(--text-secondary); }
.msg-content p { margin: 6px 0; }
.msg-content ul, .msg-content ol { margin: 6px 0; padding-left: 22px; }
.msg-content li { margin: 3px 0; }
.msg-content li + li { margin-top: 4px; }
.msg-content strong { color: var(--text-primary); font-weight: 700; }
.msg-content em { color: var(--accent); font-style: italic; }
.msg-content hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

.msg-content code.inline-code {
  background: rgba(90, 154, 106, 0.08);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  font-family: var(--font-mono);
  font-size: 0.88em;
  color: var(--accent-hover);
}

.msg-content pre.code-block {
  background: rgba(44, 58, 46, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin: 8px 0;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.88em;
  line-height: 1.5;
  color: var(--text-secondary);
}

.msg-content a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }

.msg-content blockquote {
  border-left: 3px solid var(--accent);
  padding: 6px 12px;
  margin: 8px 0;
  color: var(--text-muted);
  background: var(--accent-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* -- Streaming cursor -- */
.streaming-cursor {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursorBlink 0.7s infinite;
}
@keyframes cursorBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* -- Loading indicator -- */
.msg-loading {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
}

.loading-text {
  font-size: 13px;
  color: var(--text-muted);
  animation: loadingPulse 2s ease-in-out infinite;
}
@keyframes loadingPulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.typing-dots { display: flex; gap: 5px; }
.typing-dots span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: dotBounce 1.4s infinite ease-in-out both;
}
.typing-dots span:nth-child(1) { animation-delay: 0s; }
.typing-dots span:nth-child(2) { animation-delay: 0.16s; }
.typing-dots span:nth-child(3) { animation-delay: 0.32s; }
@keyframes dotBounce {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* -- Citations -- */
.msg-citations { margin-top: 8px; }
.msg-citations summary {
  font-size: 12px; color: var(--text-muted);
  cursor: pointer; padding: 6px 0; user-select: none; list-style: none;
}
.msg-citations summary::before { content: "▸ "; }
.msg-citations[open] summary::before { content: "▾ "; }
.msg-citations summary:hover { color: var(--accent); }

.citations-list { display: grid; gap: 6px; margin-top: 6px; }
.citation-item {
  background: rgba(90, 154, 106, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}
.citation-title { color: var(--text-secondary); font-weight: 600; }
.citation-source { opacity: 0.7; font-size: 11px; }

/* -- Follow-up chips -- */
.followup-area {
  margin-top: 10px;
  animation: followupIn 0.4s ease-out 0.15s both;
}
@keyframes followupIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.followup-label {
  font-size: 12px; color: var(--text-muted);
  margin-bottom: 6px; display: flex; align-items: center; gap: 4px;
}

.followup-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.followup-chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  padding: 6px 14px;
  font-size: 12.5px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  line-height: 1.4;
  backdrop-filter: blur(6px);
}
.followup-chip:hover {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(90, 154, 106, 0.12);
}

/* -- Feedback -- */
.msg-feedback {
  display: flex; align-items: center; gap: 6px;
  margin-top: 10px; padding-top: 8px;
  border-top: 1px solid var(--border);
}
.feedback-label { font-size: 12px; color: var(--text-muted); margin-right: 4px; }
.feedback-btn {
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: transparent;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  transition: all 0.15s;
  color: var(--text-muted);
  padding: 0;
}
.feedback-btn:hover { background: var(--bg-hover); border-color: var(--accent); }
.feedback-btn.selected {
  background: var(--accent-dim);
  border-color: var(--accent);
  transform: scale(1.1);
}
.feedback-btn.faded { opacity: 0.3; pointer-events: none; }
.feedback-thanks {
  font-size: 12px; color: var(--success);
  animation: fadeIn 0.3s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* -- Error -- */
.msg-error .msg-content {
  border-color: rgba(196, 90, 90, 0.25);
  background: rgba(196, 90, 90, 0.05);
  color: var(--error);
}

/* ============================================================
   Welcome screen
   ============================================================ */
.welcome {
  text-align: center;
  padding: 32px 20px;
  animation: msgIn 0.5s ease-out;
}

.welcome-icon {
  width: 60px;
  height: 60px;
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  font-weight: 800;
  font-size: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px;
  box-shadow: 0 4px 20px rgba(90, 154, 106, 0.3);
}

.welcome h2 {
  font-size: 20px;
  font-weight: 800;
  margin-bottom: 6px;
  color: #1a1a1a;
}

.welcome > p {
  color: #2a2a2a;
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 28px;
  line-height: 1.6;
}

.welcome-categories {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  max-width: 620px;
  margin: 0 auto;
  text-align: left;
}

.welcome-category {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  transition: all 0.2s;
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-sm);
}
.welcome-category:hover {
  border-color: rgba(90, 154, 106, 0.3);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.category-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.category-icon { font-size: 18px; line-height: 1; }
.category-label { font-size: 13px; font-weight: 700; color: var(--text-primary); }

.category-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  padding: 6px 13px;
  font-size: 12.5px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  font-family: inherit;
  backdrop-filter: blur(6px);
}
.chip:hover {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(90, 154, 106, 0.12);
}

/* -- Status badge -- */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 4px 10px;
  margin-top: 20px;
  backdrop-filter: blur(8px);
}
.status-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--success);
}
.status-dot.offline { background: var(--error); }

/* ============================================================
   Input area
   ============================================================ */
.input-area { padding: 8px 0 14px; }

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 6px 6px 6px 18px;
  transition: all 0.2s;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow-sm);
}
.input-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow), var(--shadow-md);
}

.input-field {
  flex: 1;
  background: transparent;
  border: none; outline: none;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 16px; /* 16px以上でiOS自動ズーム防止 */
  line-height: 1.5;
  resize: none;
  padding: 8px 0;
  max-height: 150px;
}
.input-field::placeholder { color: var(--text-muted); }

.btn-send {
  width: 40px; height: 40px;
  border-radius: var(--radius-md);
  border: none;
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
  box-shadow: 0 2px 10px rgba(90, 154, 106, 0.3);
}
.btn-send:hover:not(:disabled) {
  transform: scale(1.05);
  box-shadow: 0 4px 16px rgba(90, 154, 106, 0.35);
}
.btn-send:disabled { opacity: 0.35; cursor: not-allowed; transform: none; box-shadow: none; }

.input-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
  opacity: 0.6;
}

/* ============================================================
   Login overlay
   ============================================================ */
.login-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  overflow: auto;

  background: var(--bg-base);
  background-image:
    linear-gradient(180deg, rgba(246, 243, 236, 0.20) 0%, rgba(246, 243, 236, 0.35) 100%),
    url('https://images.unsplash.com/photo-1448375240586-882707db888b?w=1920&q=80&auto=format&fit=crop');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}

.login-card {
  background: rgba(255, 255, 255, 0.30);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: var(--radius-xl);
  padding: 44px 40px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.06);
  animation: msgIn 0.5s ease-out;
}

.login-logo {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  font-weight: 800;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 18px;
  box-shadow: 0 4px 24px rgba(90, 154, 106, 0.3);
}

.login-title {
  font-size: 23px;
  font-weight: 800;
  margin-bottom: 8px;
  color: #1a1a1a;
}

.login-desc {
  font-size: 14px;
  color: #1a1a1a;
  font-weight: 700;
  line-height: 1.6;
  margin-bottom: 28px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  text-align: left;
}

.login-field { display: flex; flex-direction: column; gap: 6px; }

.login-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.login-input {
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 11px 14px;
  font-size: 16px; /* 16px以上でiOS自動ズーム防止 */
  color: var(--text-primary);
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  backdrop-filter: blur(6px);
}
.login-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.login-input::placeholder { color: var(--text-muted); }

.login-error {
  font-size: 13px;
  color: var(--error);
  text-align: center;
  margin: 0;
}

.login-btn {
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #6aaa7a, #4a8a5a);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  box-shadow: 0 3px 14px rgba(90, 154, 106, 0.3);
  margin-top: 4px;
}
.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 5px 20px rgba(90, 154, 106, 0.35);
}
.login-btn:active { transform: translateY(0); }

/* -- Login divider -- */
.login-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 6px 0;
  color: var(--text-muted);
  font-size: 12px;
}
.login-divider::before,
.login-divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--border);
}

/* -- Emergency button on login screen -- */
.emergency-btn {
  width: 100%;
  padding: 12px;
  border: 2px solid #1a1a1a;
  border-radius: var(--radius-md);
  background: rgba(0, 0, 0, 0.08);
  color: #1a1a1a;
  font-size: 14px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
}
.emergency-btn:hover {
  background: rgba(196, 90, 90, 0.12);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(196, 90, 90, 0.15);
}

/* ============================================================
   Emergency overlay & panel
   ============================================================ */
.emergency-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  overflow-y: auto;
  padding: 20px;
  animation: msgIn 0.3s ease-out;

  background: var(--bg-base);
  background-image:
    linear-gradient(180deg, rgba(246, 243, 236, 0.60) 0%, rgba(246, 243, 236, 0.75) 100%),
    url('https://images.unsplash.com/photo-1448375240586-882707db888b?w=1920&q=80&auto=format&fit=crop');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}

.emergency-panel { max-width: 680px; margin: 0 auto; }

.emergency-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.emergency-header h2 { font-size: 22px; font-weight: 700; color: var(--text-primary); }

.emergency-back-btn {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  font-family: inherit;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.15s;
  backdrop-filter: blur(8px);
}
.emergency-back-btn:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--text-primary);
}

.emergency-cards { display: flex; flex-direction: column; gap: 12px; }

.emergency-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  cursor: pointer;
  transition: all 0.2s;
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-sm);
}
.emergency-card:hover {
  border-color: rgba(196, 90, 90, 0.3);
  box-shadow: 0 3px 14px rgba(196, 90, 90, 0.08);
}
.emergency-card.ecard-open {
  border-color: var(--error);
  cursor: default;
}

.ecard-icon { font-size: 28px; margin-bottom: 8px; }
.emergency-card h3 { font-size: 17px; font-weight: 700; margin-bottom: 4px; color: var(--text-primary); }
.ecard-hint { font-size: 12px; color: var(--text-muted); }

.ecard-detail {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  animation: msgIn 0.3s ease-out;
}
.ecard-detail h4 {
  font-size: 14px; font-weight: 700;
  color: var(--text-primary);
  margin: 14px 0 8px;
}
.ecard-detail h4:first-child { margin-top: 0; }
.ecard-detail ol, .ecard-detail ul {
  padding-left: 22px; margin: 6px 0;
  line-height: 1.8; color: var(--text-secondary);
}
.ecard-detail li { margin: 4px 0; }
.ecard-detail strong { color: var(--error); font-weight: 700; }
.ecard-detail p {
  margin: 8px 0; font-size: 13px;
  color: var(--text-muted); line-height: 1.6;
}

/* -- User badge in header -- */
.user-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  padding: 6px 14px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  backdrop-filter: blur(6px);
}
.user-badge:empty { display: none; }

/* -- Admin log in chat -- */
.admin-log-content h3 { color: #1a1a1a; margin: 12px 0 6px; font-size: 14px; font-weight: 700; }
.admin-log-content h3:first-child { margin-top: 0; }
.admin-log-content ul { padding-left: 18px; }
.admin-log-content li { margin: 3px 0; line-height: 1.6; color: #2a2a2a; }

/* ---------- Responsive: Tablet (768px以下) ---------- */
@media (max-width: 768px) {
  .app { padding: 0 8px; }

  .welcome-categories {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .msg-user-bubble { max-width: 85%; }
  .msg-content { padding: 12px 14px; }

  .login-card { padding: 36px 28px; max-width: 400px; }

  .emergency-panel { padding: 0 8px; }
  .emergency-card { padding: 16px 18px; }
}

/* ---------- Responsive: Mobile (480px以下) ---------- */
@media (max-width: 480px) {
  html { font-size: 14px; }

  .app { padding: 0 6px; }

  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 10px 14px;
    margin-top: 8px;
    border-radius: var(--radius-md);
  }
  .header-right {
    width: 100%;
    justify-content: space-between;
  }
  .header-title { font-size: 15px; }
  .header-sub { font-size: 11px; }

  .messages { padding: 10px 0; gap: 2px; }

  .msg-user { margin: 5px 0; }
  .msg-user-bubble {
    max-width: 92%;
    padding: 9px 14px;
    border-radius: var(--radius-md) var(--radius-md) 4px var(--radius-md);
  }

  .msg-bot { margin: 5px 0; gap: 8px; }
  .msg-avatar { width: 28px; height: 28px; font-size: 12px; border-radius: 8px; }
  .msg-content {
    padding: 11px 14px;
    border-radius: 4px var(--radius-md) var(--radius-md) var(--radius-md);
    font-size: 14px;
  }
  .msg-label { font-size: 11px; }

  .welcome { padding: 16px 6px; }
  .welcome-icon { width: 48px; height: 48px; font-size: 22px; }
  .welcome h2 { font-size: 17px; }
  .welcome > p { font-size: 13px; margin-bottom: 20px; }
  .welcome-categories { grid-template-columns: 1fr; gap: 8px; }
  .welcome-category { padding: 12px 14px; }
  .category-label { font-size: 12px; }

  .chip, .followup-chip {
    font-size: 12px;
    padding: 6px 12px;
  }

  .input-area { padding: 6px 0 10px; }
  .input-row {
    padding: 4px 4px 4px 14px;
    border-radius: var(--radius-md);
  }
  .input-field { font-size: 16px; padding: 7px 0; }
  .btn-send { width: 38px; height: 38px; }
  .input-hint { font-size: 10px; }

  /* Login */
  .login-card {
    padding: 28px 20px;
    border-radius: var(--radius-lg);
  }
  .login-logo { width: 52px; height: 52px; font-size: 24px; }
  .login-title { font-size: 20px; }
  .login-desc { font-size: 13px; margin-bottom: 22px; }
  .login-input { padding: 10px 12px; font-size: 16px; }
  .login-btn { padding: 12px; font-size: 15px; }
  .emergency-btn { font-size: 13px; padding: 11px; }

  /* Emergency */
  .emergency-overlay { padding: 14px; }
  .emergency-header h2 { font-size: 19px; }
  .emergency-card { padding: 14px 16px; }
  .emergency-card h3 { font-size: 15px; }
  .ecard-icon { font-size: 24px; }
  .ecard-detail ol, .ecard-detail ul { line-height: 1.7; }

  /* Feedback */
  .msg-feedback { gap: 4px; }
  .feedback-btn { width: 28px; height: 28px; font-size: 13px; }

  /* Citations */
  .citation-item { padding: 6px 10px; font-size: 11px; }

  /* User badge */
  .user-badge { font-size: 11px; padding: 5px 10px; }
}

/* ---------- Tap targets: ensure 44px min for touch ---------- */
@media (pointer: coarse) {
  .chip, .followup-chip {
    min-height: 40px;
    display: inline-flex;
    align-items: center;
  }

  .feedback-btn {
    width: 36px;
    height: 36px;
  }

  .btn-icon {
    width: 40px;
    height: 40px;
  }

  .emergency-card {
    padding: 18px 20px;
  }

  .login-input {
    min-height: 44px;
  }
}

/* ---------- Pull to Refresh indicator ---------- */
#pull-indicator {
  position: fixed;
  top: -50px;
  left: 50%;
  transform: translateX(-50%) translateY(-60px);
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  padding: 8px 20px;
  font-size: 13px;
  font-weight: 600;
  color: #2a2a2a;
  z-index: 9999;
  transition: transform 0.2s ease, opacity 0.2s ease;
  opacity: 0;
  pointer-events: none;
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* ---------- Safe area for notched phones ---------- */
@supports (padding-top: env(safe-area-inset-top)) {
  .login-overlay,
  .emergency-overlay {
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
  }

  .input-area {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .header {
    margin-top: max(10px, env(safe-area-inset-top));
  }
}
