/* 文書 AI 検索 — スタイル */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1a1d21;
  --text-muted: #565d66;
  --accent: #2f6fed;
  --accent-soft: #e8f0ff;
  --danger: #c8372d;
  --success: #1f8b4c;
  --warning: #b7791f;
  /* バッジ等の淡い色地に載せる文字色。--success 等をそのまま使うと
     コントラストが AA(4.5:1) に届かないため、専用に暗い値を持つ。 */
  --on-tint-success: #14663a;
  --on-tint-accent: #1e4fbf;
  --on-tint-danger: #a3271e;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 12px rgb(16 24 40 / 6%);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --surface-2: #24282e;
    --border: #333941;
    --text: #e8eaed;
    --text-muted: #9aa2ad;
    --accent: #5c92ff;
    --accent-soft: #1e2a44;
    --danger: #f0736a;
    --success: #4ec27f;
    --warning: #e0a850;
    --on-tint-success: #7fdda6;
    --on-tint-accent: #9dbcff;
    --on-tint-danger: #ffa39b;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 14px rgb(0 0 0 / 25%);
  }
}

/*
 * hidden 属性を確実に効かせる。
 * ブラウザ標準の [hidden] { display: none } は作者スタイルの display に負けるため、
 * 例えば .overlay { display: grid } を定義すると hidden を付けても消えなくなる。
 */
[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family:
    system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo,
    sans-serif;
  font-size: 15px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  cursor: pointer;
}

/* ---------------------------------------------------------------- layout */

.app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 20px 16px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.sidebar-header h1 {
  margin: 0 0 4px;
  font-size: 17px;
  letter-spacing: 0.01em;
}

.sidebar-header p {
  margin: 0;
  color: var(--text-muted);
  font-size: 12.5px;
  line-height: 1.6;
}

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* ------------------------------------------------------------- dropzone */

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 18px 12px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text-muted);
  text-align: center;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: none;
}

.dropzone svg {
  width: 26px;
  height: 26px;
  margin-bottom: 2px;
  color: var(--accent);
}

.dropzone strong {
  color: var(--text);
  font-size: 13.5px;
}

.dropzone span {
  font-size: 12px;
}

.dropzone small {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.5;
  word-break: break-all;
}

/* ------------------------------------------------------------ file list */

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.section-title b {
  color: var(--text);
}

.ghost-button {
  padding: 3px 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: none;
  color: var(--text-muted);
  font-size: 12px;
}

.ghost-button:hover {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text);
}

.doc-list {
  flex: 1;
  min-height: 60px;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.doc {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: start;
  gap: 8px;
  padding: 9px 8px;
  border-radius: 8px;
}

.doc:hover {
  background: var(--surface-2);
}

.doc input[type="checkbox"] {
  margin-top: 4px;
  accent-color: var(--accent);
}

.doc-name {
  overflow: hidden;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 11.5px;
}

.doc-delete {
  padding: 2px 6px;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1;
  opacity: 1;
}

/* ホバーできる環境でだけ「普段は隠す」演出を行う。
   タッチ端末やキーボード操作では常に見えている必要がある。 */
@media (hover: hover) and (pointer: fine) {
  .doc-delete {
    opacity: 0;
  }

  .doc:hover .doc-delete,
  .doc:focus-within .doc-delete,
  .doc-delete:focus-visible {
    opacity: 1;
  }
}

.doc-delete:hover {
  background: var(--surface-2);
  color: var(--danger);
}

.badge {
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge.ready {
  background: color-mix(in srgb, var(--success) 18%, transparent);
  color: var(--on-tint-success);
}

.badge.working {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  color: var(--on-tint-accent);
}

.badge.error {
  background: color-mix(in srgb, var(--danger) 18%, transparent);
  color: var(--on-tint-danger);
}

.progress {
  grid-column: 2 / -1;
  height: 3px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-2);
}

.progress > i {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width 0.25s;
}

.doc-error {
  grid-column: 2 / -1;
  color: var(--danger);
  font-size: 11px;
  line-height: 1.5;
  word-break: break-word;
}

.scope {
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 12px;
}

.scope label {
  display: flex;
  gap: 7px;
  align-items: center;
  cursor: pointer;
}

.scope input {
  accent-color: var(--accent);
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 10.5px;
}

#model-info {
  overflow: hidden;
  line-height: 1.5;
  word-break: break-all;
}

/* -------------------------------------------------------------- messages */

.messages {
  flex: 1;
  min-height: 0;
  padding: 28px 24px 8px;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.messages > * {
  max-width: 780px;
  margin-inline: auto;
}

.empty-state {
  padding: 48px 0;
  text-align: center;
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 19px;
}

.empty-state p {
  max-width: 460px;
  margin: 0 auto 20px;
  color: var(--text-muted);
  font-size: 13.5px;
}

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

.sample {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12.5px;
}

.sample:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.message {
  margin-bottom: 22px;
}

.message.user .bubble {
  margin-left: auto;
  max-width: 85%;
  width: fit-content;
  padding: 10px 14px;
  border-radius: 14px 14px 4px 14px;
  background: var(--accent);
  color: #fff;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.assistant .bubble {
  padding: 2px 0;
  word-break: break-word;
}

.bubble h3,
.bubble h4 {
  margin: 16px 0 6px;
  font-size: 15px;
}

.bubble p {
  margin: 0 0 10px;
}

.bubble ul,
.bubble ol {
  margin: 0 0 10px;
  padding-left: 1.4em;
}

.bubble li {
  margin-bottom: 3px;
}

.bubble code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 0.88em;
}

.bubble pre {
  margin: 0 0 12px;
  padding: 12px 14px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.bubble pre code {
  padding: 0;
  background: none;
}

.bubble table {
  width: 100%;
  margin-bottom: 12px;
  border-collapse: collapse;
  font-size: 13px;
}

.bubble th,
.bubble td {
  padding: 5px 8px;
  border: 1px solid var(--border);
  text-align: left;
}

.cite {
  display: inline-block;
  min-width: 17px;
  height: 17px;
  margin: 0 2px;
  padding: 0 4px;
  border: none;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
  vertical-align: 1px;
}

.cite:hover {
  background: var(--accent);
  color: #fff;
}

.thinking {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  color: var(--text-muted);
  font-size: 13px;
}

.thinking i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.2s infinite ease-in-out;
}

.thinking i:nth-child(2) {
  animation-delay: 0.15s;
}

.thinking i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes pulse {
  0%,
  70%,
  100% {
    opacity: 0.25;
  }
  35% {
    opacity: 1;
  }
}

/* --------------------------------------------------------------- sources */

.sources {
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 13px;
}

.sources > summary {
  padding: 8px 12px;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.sources > summary:hover {
  color: var(--text);
}

.source {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}

.source[data-highlight="true"] {
  background: var(--accent-soft);
}

.source-head {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: baseline;
  margin-bottom: 4px;
  font-size: 12px;
}

.source-index {
  display: inline-block;
  min-width: 17px;
  padding: 0 4px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  text-align: center;
}

.source-file {
  font-weight: 600;
}

.source-heading {
  color: var(--text-muted);
}

.source-body {
  max-height: 170px;
  overflow-y: auto;
  color: var(--text-muted);
  font-size: 12.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}

/* -------------------------------------------------------------- composer */

.composer {
  padding: 10px 24px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer-inner {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 780px;
  margin-inline: auto;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.composer-inner:focus-within {
  border-color: var(--accent);
}

#input {
  flex: 1;
  max-height: 180px;
  padding: 6px 0;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  resize: none;
}

#input:focus {
  outline: none;
}

#send {
  display: grid;
  flex-shrink: 0;
  place-items: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
}

#send svg {
  width: 17px;
  height: 17px;
}

#send:disabled {
  background: var(--border);
  color: var(--text-muted);
  cursor: not-allowed;
}

.composer-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 780px;
  margin: 6px auto 0;
  color: var(--text-muted);
  font-size: 11.5px;
}

/* ----------------------------------------------------------------- login */

.overlay {
  position: fixed;
  z-index: 50;
  display: grid;
  place-items: center;
  background: var(--bg);
  inset: 0;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(340px, 90vw);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-card h1 {
  margin: 0;
  font-size: 18px;
}

.login-card p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.login-card input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}

.login-card button {
  padding: 9px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.error-text {
  color: var(--danger) !important;
  font-size: 12.5px;
}

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  z-index: 60;
  max-width: min(560px, 90vw);
  padding: 10px 16px;
  border-radius: 9px;
  background: #23262b;
  color: #fff;
  font-size: 13px;
  box-shadow: var(--shadow);
  transform: translateX(-50%);
  /* 通知は読むだけの要素。背後の「会話をクリア」等のクリックを奪わない。 */
  pointer-events: none;
}

.toast[data-kind="error"] {
  background: var(--danger);
}

/* ------------------------------------------------------------ responsive */

@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100dvh;
  }

  .sidebar {
    max-height: 46dvh;
    /* overflow が無いと中身があふれてチャット領域に重なり、
       ログアウトやスコープ切替が押せなくなる。 */
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .chat {
    min-height: 54dvh;
  }

  .messages,
  .composer {
    padding-inline: 14px;
  }
}

.diagnostics {
  max-height: 42vh;
  margin: 0;
  padding: 10px 12px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  user-select: text;
}

/* スクリーンリーダにだけ読ませる領域。 */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* 初期化に失敗したときの永続バナー。トーストと違い自動で消えない。 */
.init-error {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  margin: 12px 24px 0;
  padding: 10px 14px;
  border: 1px solid var(--danger);
  border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger);
  font-size: 13px;
}

.doc-actions {
  grid-column: 2 / -1;
  display: flex;
  gap: 6px;
  margin-top: 2px;
}

.doc-actions .ghost-button {
  border-color: var(--border);
}

/* 言語切り替え */
.footer-actions {
  display: flex;
  flex-shrink: 0;
  gap: 6px;
  align-items: center;
}

.lang-switch {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  color: var(--text-muted);
  font-size: 12px;
}

.lang-select {
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 12px;
}

.lang-select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ログイン画面の言語切り替えは中央寄せにする */
.login-card .lang-switch {
  justify-content: center;
}
