*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html {
  height: 100%;
  height: 100dvh;
  touch-action: manipulation; /* prevent double-tap zoom */
}
body {
  font-family: 'Noto Sans JP', sans-serif;
  background: #f0f2f5;
  color: #1a1a2e;
  /* Make body a flex column so .chat-app fills remaining space */
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  /* iOS safe area padding */
  padding: env(safe-area-inset-top) 0 0 0;
}

/* Top Navigation */
.top-nav {
  background: #1e293b;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  flex-shrink: 0;
}
.top-nav-inner {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
}
.top-nav-brand a {
  color: #e2e8f0;
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.top-nav-brand a:hover { color: #fff; }
.top-nav-links {
  display: flex;
  align-items: center;
  gap: 2px;
}
.nav-link {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: #94a3b8;
  text-decoration: none;
  font-size: 0.72rem;
  padding: 6px 10px;
  border-radius: 6px;
  transition: all 0.15s;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
}
.nav-link:hover { color: #e2e8f0; background: rgba(255,255,255,0.06); }
.nav-link svg { opacity: 0.6; flex-shrink: 0; }
.nav-debug-btn { color: #60a5fa; }
.nav-debug-btn:hover { color: #93c5fd; background: rgba(96,165,250,0.08); }

/* Language Selector */
.lang-selector {
  position: relative;
}
.lang-toggle {
  color: #e2e8f0 !important;
  font-weight: 500;
  min-width: 0;
}
.lang-toggle span {
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  vertical-align: middle;
}
.lang-toggle:hover { color: #fff !important; }
.lang-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 1000;
  background: #1e293b;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  padding: 4px;
  min-width: 150px;
  max-height: 340px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}
.lang-dropdown.open { display: block; }
.lang-option {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: #cbd5e1;
  font-size: 0.78rem;
  padding: 7px 12px;
  border-radius: 5px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
}
.lang-option:hover { background: rgba(255,255,255,0.08); color: #fff; }
.lang-option.active {
  background: rgba(96,165,250,0.15);
  color: #93c5fd;
  font-weight: 600;
}
/* Font size toggle */
.font-toggle-btn {
  font-weight: 800 !important;
  font-size: 0.8rem !important;
  min-width: 32px;
  text-align: center;
}
.font-toggle-btn.large-active { color: #93c5fd !important; background: rgba(96,165,250,0.12); }

/* Large font mode */
body.font-large .message-content { font-size: 1.1rem !important; line-height: 1.9 !important; }
body.font-large .message-content li, body.font-large .suggestion { font-size: 1rem !important; }
body.font-large .message-disclaimer { font-size: 0.85rem !important; }
body.font-large .chat-form input { font-size: 18px !important; }
body.font-large .message-sources { font-size: 0.88rem !important; }

/* Documents dropdown menu */
.nav-docs-menu { position: relative; }
.nav-docs-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 1000;
  background: #1e293b;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  padding: 4px;
  min-width: 160px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}
.nav-docs-dropdown.open { display: block; }
.docs-link {
  display: block;
  color: #cbd5e1;
  text-decoration: none;
  font-size: 0.78rem;
  padding: 7px 12px;
  border-radius: 5px;
  transition: background 0.15s;
}
.docs-link:hover { background: rgba(255,255,255,0.08); color: #fff; }

/* API Notice */
.api-notice {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 6px 20px;
  font-size: 0.7rem;
  color: #92400e;
  background: #fef3c7;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.api-notice svg { flex-shrink: 0; stroke: #92400e; }

/* Debug Panel */
.debug-panel {
  background: #0f172a;
  border-bottom: 2px solid #1e40af;
  flex-shrink: 0;
  overflow-y: auto;
  max-height: 40vh;
}
.debug-panel-inner {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px 20px;
}
.debug-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.debug-card {
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 8px;
  padding: 12px 14px;
}
.debug-card-title {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #60a5fa;
  font-weight: 600;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.debug-card-title svg { opacity: 0.7; }
.debug-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 0;
  font-size: 0.72rem;
  color: rgba(255,255,255,0.55);
}
.debug-val {
  color: #34d399;
  font-weight: 500;
  font-family: 'Courier New', monospace;
  font-size: 0.72rem;
}
.debug-val.warn { color: #fbbf24; }
.debug-val.ok { color: #34d399; }
.debug-val.info { color: #93c5fd; }
.debug-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.debug-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #34d399;
  display: inline-block;
}
.debug-dot.warn { background: #fbbf24; }

/* App Layout */
.chat-app {
  display: flex;
  flex-direction: column;
  /* Fill all remaining space after top-nav, api-notice, debug-panel */
  flex: 1;
  min-height: 0;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  background: #fff;
  box-shadow: 0 0 40px rgba(0,0,0,0.08);
}

/* Header */
.chat-header {
  background: linear-gradient(135deg, #1a5276, #2e86c1);
  color: white;
  flex-shrink: 0;
}
.chat-header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
}
.chat-header-logo {
  display: flex;
  align-items: center;
  gap: 12px;
}
.chat-avatar {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: rgba(255,255,255,0.2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.9rem;
}
.chat-header h1 { font-size: 1rem; font-weight: 700; }
.chat-header-sub { font-size: 0.75rem; opacity: 0.8; margin-top: 2px; }
.new-chat-btn {
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4);
  background: none;
  color: white;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
  line-height: 1;
}
.new-chat-btn:hover { background: rgba(255,255,255,0.2); border-color: white; }

.ai-badge {
  font-size: 0.65rem;
  background: rgba(255,255,255,0.2);
  padding: 3px 8px;
  border-radius: 4px;
  font-weight: 500;
}

/* Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.message-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.7rem; font-weight: 700;
  flex-shrink: 0;
}
.message.bot .message-avatar { background: #e8f4fd; color: #1a5276; }
.message.user .message-avatar { background: #1a5276; color: white; }

.message-content {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.7;
  overflow-wrap: anywhere;
  word-break: break-word;
  min-width: 0;
  hyphens: auto;
  -webkit-hyphens: auto;
}
.message.bot .message-content {
  background: #f5f7fa;
  border-bottom-left-radius: 4px;
}
.message.user .message-content {
  background: #1a5276;
  color: white;
  border-bottom-right-radius: 4px;
}

.message-content p { margin-bottom: 8px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content ul { margin: 8px 0 8px 20px; }
.message-content li { margin-bottom: 4px; }

/* Sources */
.message-sources {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid rgba(0,0,0,0.08);
  font-size: 0.78rem;
}
.message-sources-label {
  color: #888;
  font-weight: 500;
  margin-bottom: 4px;
}
.message-sources a {
  display: block;
  color: #2e86c1;
  text-decoration: none;
  padding: 3px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.message-sources a:hover { text-decoration: underline; white-space: normal; word-break: break-all; }

/* Relevance */
.message-relevance {
  margin-top: 6px;
  font-size: 0.7rem;
  color: #aaa;
}

/* Feedback */
.message-feedback {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  align-items: center;
}
.feedback-btn {
  background: none;
  border: 1px solid #ddd;
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.2s;
  color: #666;
}
.feedback-btn:hover { border-color: #2e86c1; color: #2e86c1; }
.feedback-btn.selected { background: #2e86c1; color: white; border-color: #2e86c1; }
.feedback-btn.selected-bad { background: #e74c3c; color: white; border-color: #e74c3c; }

/* Disclaimer */
.message-disclaimer {
  margin-top: 12px;
  padding: 10px 12px;
  background: #fff3cd;
  border-radius: 8px;
  font-size: 0.75rem;
  color: #856404;
  line-height: 1.6;
}

/* Suggestions */
.suggestion-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 10px 0;
}
.suggestion {
  background: white;
  border: 1px solid #ddd;
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: all 0.2s;
  color: #1a5276;
  font-family: inherit;
}
.suggestion:hover {
  background: #e8f4fd;
  border-color: #2e86c1;
}

/* Debug badges on messages */
.message-debug {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  padding-top: 6px;
  border-top: 1px dashed rgba(0,0,0,0.08);
}
.debug-label-tag {
  display: inline-block;
  font-size: 0.55rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 1px 6px;
  border-radius: 3px;
  background: #334155;
  color: #e2e8f0;
  font-family: 'Courier New', monospace;
}
.debug-badge {
  display: inline-block;
  font-size: 0.62rem;
  padding: 1px 7px;
  border-radius: 10px;
  background: #e8f4fd;
  color: #1a5276;
  font-weight: 500;
  font-family: 'Courier New', monospace;
}
.debug-badge.warn {
  background: #fef3c7;
  color: #92400e;
}

/* Blocked message */
.message-blocked {
  background: #fef2f2 !important;
  border: 1px solid #fecaca;
}
.message-blocked .blocked-icon { color: #dc2626; margin-right: 4px; }

/* Loading */
.message-loading .dots {
  display: inline-flex; gap: 4px;
}
.message-loading .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #aaa;
  animation: bounce 1.4s ease-in-out infinite;
}
.message-loading .dot:nth-child(2) { animation-delay: 0.2s; }
.message-loading .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Input Area */
.chat-input-area {
  flex-shrink: 0;
  border-top: 1px solid #eee;
  background: #fff;
  /* Add padding for iOS home indicator (safe area) */
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.chat-input-inner {
  padding: 12px 20px 8px;
  /* Ensure send button is not cut off on the right */
  padding-right: max(20px, env(safe-area-inset-right, 20px));
  padding-left: max(20px, env(safe-area-inset-left, 20px));
}
.pii-warning {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: #fff3cd;
  border-radius: 8px;
  font-size: 0.75rem;
  color: #856404;
  margin-bottom: 8px;
}
.chat-form {
  display: flex;
  gap: 8px;
  width: 100%;
}
.chat-form input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 24px;
  font-size: 16px; /* 16px未満だとiOSが自動ズームする */
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}
.chat-form input:focus { border-color: #2e86c1; }
.chat-form button {
  width: 40px; height: 40px;
  min-width: 40px;
  border-radius: 50%;
  border: none;
  background: #1a5276;
  color: white;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}
.chat-form button:hover { background: #2e86c1; }
.chat-form button:disabled { background: #ccc; cursor: not-allowed; }
.chat-footer-note {
  text-align: center;
  font-size: 0.7rem;
  color: #aaa;
  padding: 6px 0 4px;
}
.chat-footer-note a { color: #2e86c1; text-decoration: none; }

/* Responsive */
@media (max-width: 600px) {
  .chat-app { max-width: 100%; }
  .message { max-width: 95%; }
  .message-content { word-break: break-word; overflow-wrap: break-word; }
  .message-sources a { word-break: break-all; }
  .chat-header-inner { padding: 10px 14px; }
  .chat-messages { padding: 10px; gap: 12px; }
  .chat-input-inner {
    padding: 10px 14px 6px;
    padding-right: max(14px, env(safe-area-inset-right, 14px));
    padding-left: max(14px, env(safe-area-inset-left, 14px));
  }
  .top-nav-inner { padding: 0 10px; }
  .top-nav-brand a { font-size: 0.72rem; }
  .nav-link { padding: 6px 6px; font-size: 0.65rem; }
  .nav-docs-dropdown { right: -10px; }
  .api-notice { font-size: 0.6rem; padding: 4px 10px; }
  .debug-grid { grid-template-columns: 1fr; }
  .message-content { font-size: 0.85rem; padding: 10px 12px; }
  .suggestion-list { gap: 6px; }
  .suggestion { font-size: 0.75rem; padding: 5px 10px; }
  .lang-dropdown { right: -10px; min-width: 130px; }
  .lang-toggle span { max-width: 60px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: middle; }
}
