/*
 * 高齢者向け UI の方針
 *  - 本文 20px 以上、操作ボタンは 28px 以上・高さ 72px 以上（押し間違い防止）
 *  - 色だけに頼らず、必ず文字でも状態を出す
 *  - コントラスト比 7:1 以上を目安
 *  - アニメーションは控えめ、prefers-reduced-motion を尊重
 */

:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --ink-soft: #4a4a4a;
  --brand: #1b3a5c;
  --brand-ink: #ffffff;
  --accent: #0f7a5a;
  --warn: #8a4b00;
  --danger: #9b1c1c;
  --line: #cfc9bd;
  --radius: 16px;
  --shadow: 0 2px 8px rgba(0, 0, 0, .12);
}

* { box-sizing: border-box; }

/* display を持つクラス（.btn 等）が hidden 属性に勝ってしまうのを防ぐ */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", Meiryo, system-ui, sans-serif;
  font-size: 20px;
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
}

/* ------------------------------------------------------------------ 共通部品 */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4em;
  min-height: 72px;
  padding: 0 1.2em;
  border: 2px solid transparent;
  border-radius: var(--radius);
  font: inherit;
  font-size: 1.4rem;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn:focus-visible { outline: 4px solid #b8860b; outline-offset: 3px; }
.btn:disabled { opacity: .5; cursor: default; }

.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-primary:active { background: #12293f; }

.btn-secondary { background: var(--surface); color: var(--brand); border-color: var(--brand); }
.btn-secondary:active { background: #e8eef4; }

.btn-quiet {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--line);
  min-height: 56px;
  font-size: 1.05rem;
}

.btn-huge { width: 100%; min-height: 88px; font-size: 1.6rem; }
.btn-small { min-height: 44px; font-size: .95rem; padding: 0 .9em; }

.error {
  margin: 0 0 1rem;
  padding: .8rem 1rem;
  border-radius: var(--radius);
  background: #fdecec;
  color: var(--danger);
  border: 2px solid #f0b4b4;
  font-weight: 700;
}

/* -------------------------------------------------------------------- ログイン */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-card {
  width: min(100%, 460px);
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 2rem 1.75rem 2.25rem;
}

.login-title {
  margin: 0 0 .5rem;
  font-size: 2.4rem;
  line-height: 1.25;
  color: var(--brand);
  text-align: center;
}

.login-lead {
  margin: 0 0 1.75rem;
  text-align: center;
  color: var(--ink-soft);
  font-size: 1.1rem;
}

.field { display: block; margin-bottom: 1.25rem; }
.field-label { display: block; margin-bottom: .35rem; font-weight: 700; }
.field input {
  width: 100%;
  min-height: 68px;
  padding: 0 .9rem;
  font: inherit;
  font-size: 1.4rem;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
}
.field input:focus { border-color: var(--brand); outline: 3px solid #cfe0ef; }

.checkline {
  display: flex;
  align-items: center;
  gap: .6rem;
  margin-bottom: 1.5rem;
  font-size: 1.05rem;
  color: var(--ink-soft);
}
.checkline input { width: 28px; height: 28px; }

/* ------------------------------------------------------------------ 会話画面 */

.app-body {
  display: flex;
  flex-direction: column;
  min-height: 100svh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 1rem;
}

.badge {
  display: inline-block;
  padding: .3em .9em;
  border-radius: 999px;
  font-size: .9rem;
  font-weight: 700;
  white-space: nowrap;
  border: 2px solid;
}
.badge-ok        { background: #e6f4ee; color: #0a5c43; border-color: #0f7a5a; }
.badge-connecting{ background: #fdf3e0; color: #7a4b00; border-color: #b8860b; }
.badge-error     { background: #fdecec; color: var(--danger); border-color: #d05a5a; }

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 1rem;
  text-align: center;
}

/* 状態オーブ: 待機=静止 / 聞取中=脈動 / 思考中=回転 / 発話中=拡縮 */
.orb {
  position: relative;
  width: 168px;
  height: 168px;
  display: grid;
  place-items: center;
  flex: none;
}
.orb-core {
  width: 108px;
  height: 108px;
  border-radius: 50%;
  background: var(--brand);
  transition: background .3s ease, transform .3s ease;
}
.orb-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 6px solid var(--line);
}
.orb[data-state="listening"] .orb-core { background: var(--accent); animation: pulse 2.4s ease-in-out infinite; }
.orb[data-state="hearing"]   .orb-core { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.orb[data-state="thinking"]  .orb-ring { border-top-color: var(--brand); animation: spin 1.2s linear infinite; }
.orb[data-state="speaking"]  .orb-core { background: #2f6ea5; animation: speak .9s ease-in-out infinite; }
.orb[data-state="reconnecting"] .orb-core { background: var(--warn); animation: pulse 1.6s ease-in-out infinite; }
.orb[data-state="error"]     .orb-core { background: var(--danger); }

@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@keyframes speak { 0%,100% { transform: scale(.94); } 50% { transform: scale(1.12); } }
@keyframes spin  { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .orb-core, .orb-ring { animation: none !important; }
}

.state-text {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--ink);
  min-height: 2.4em;
}

.bubbles { width: min(100%, 620px); display: flex; flex-direction: column; gap: .75rem; }

.bubble {
  margin: 0;
  padding: .9rem 1.1rem;
  border-radius: var(--radius);
  font-size: 1.2rem;
  text-align: left;
  word-break: break-word;
}
.bubble-user { background: #e8eef4; color: var(--ink); }
.bubble-ai   { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.app-footer {
  display: flex;
  flex-direction: column;
  gap: .75rem;
  padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
}
.app-footer .btn-small { align-self: flex-end; }

/* ------------------------------------------------------------------ 計測パネル */

.metrics {
  position: fixed;
  right: .75rem;
  bottom: .75rem;
  width: min(92vw, 340px);
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: .8rem .9rem;
  font-size: .85rem;
  line-height: 1.45;
  /* 検証用パネル。会話画面を完全に覆わないよう高さを制限する */
  max-height: 55svh;
  overflow-y: auto;
  z-index: 10;
}
.metrics-title { margin: 0 0 .5rem; font-size: .95rem; }
.metrics-table { width: 100%; border-collapse: collapse; }
.metrics-table th {
  text-align: left;
  font-weight: 400;
  color: var(--ink-soft);
  padding: .15rem 0;
}
.metrics-table td {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  padding: .15rem 0;
}
.metrics-total th, .metrics-total td { border-top: 1px solid var(--line); padding-top: .4rem; }
.metrics-meta { margin: .6rem 0 0; color: var(--ink-soft); font-size: .78rem; word-break: break-all; }
.metric-good { color: var(--accent); }
.metric-warn { color: var(--warn); }
.metric-bad  { color: var(--danger); }

@media (min-width: 700px) {
  html, body { font-size: 22px; }
  .orb { width: 220px; height: 220px; }
  .orb-core { width: 144px; height: 144px; }
}

/* ------------------------------------------------- 話し相手（声と性格）の選択 */

.voice-panel {
  position: fixed;
  left: 50%;
  bottom: .75rem;
  transform: translateX(-50%);
  width: min(94vw, 420px);
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  /* 会話画面を覆いすぎないよう、はみ出すぶんはパネル内で送る */
  max-height: 70svh;
  overflow-y: auto;
  z-index: 20;
}
.voice-title { margin: 0 0 .75rem; font-size: 1.15rem; }
.voice-list { list-style: none; margin: 0 0 .75rem; padding: 0; display: grid; gap: .5rem; }

.voice-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: .15rem;
  text-align: left;
  padding: .8rem .9rem;
  background: var(--surface);
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  cursor: pointer;
  /* 高齢者が押しやすいよう、指一本ぶんの高さを確保する */
  min-height: 60px;
}
.voice-item:hover { border-color: var(--brand); }
.voice-item.is-selected {
  border-color: var(--brand);
  background: var(--brand);
  color: var(--brand-ink);
}
.voice-name { font-size: 1.25rem; font-weight: 700; }
.voice-desc { font-size: .9rem; color: var(--ink-soft); }
.voice-item.is-selected .voice-desc { color: var(--brand-ink); opacity: .85; }
.voice-note { margin: 0 0 .75rem; font-size: .85rem; color: var(--ink-soft); }

/* ------------------------------------------------------------- 管理画面 */

/* 管理画面はインフォアイ側の職員が PC で使う。会話画面ほど大きくしない。 */
.admin-body {
  background: var(--bg);
  color: var(--ink);
  min-height: 100svh;
}
.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: .9rem 1.1rem;
  background: var(--brand);
  color: var(--brand-ink);
}
.admin-title { margin: 0; font-size: 1.15rem; }
.admin-header-right { display: flex; align-items: center; gap: .6rem; }
.admin-main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 1.1rem;
  display: grid;
  gap: 1.1rem;
}
.admin-card {
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}
.admin-h2 { margin: 0 0 .8rem; font-size: 1.05rem; }
.admin-h3 { margin: 1rem 0 .4rem; font-size: .95rem; }

.admin-notice {
  margin: 0;
  padding: .7rem .9rem;
  border-radius: var(--radius);
  border: 2px solid var(--accent);
  background: #e6f4ee;
  color: #0a5c43;
}
.admin-notice-error { border-color: #d05a5a; background: #fdecec; color: var(--danger); }

.admin-form { display: grid; gap: .7rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); align-items: end; }
.admin-field { display: grid; gap: .25rem; font-size: .9rem; }
.admin-field input {
  font: inherit;
  padding: .6rem .7rem;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
}
.admin-field input:focus { outline: 3px solid var(--brand); outline-offset: 1px; }

/* 列が多いので、狭い画面では表だけ横に送る */
.admin-table-wrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.admin-table th, .admin-table td {
  text-align: left;
  padding: .55rem .6rem;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.admin-table th { font-size: .82rem; color: var(--ink-soft); white-space: nowrap; }
.admin-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; }
.admin-actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
.admin-empty { color: var(--ink-soft); padding: 1rem .6rem; }
.admin-issued { font-size: .78rem; color: var(--ink-soft); }

.btn-linkish {
  background: none;
  border: none;
  color: var(--brand);
  text-decoration: underline;
  padding: 0;
  min-height: 0;
  font-weight: 700;
  cursor: pointer;
}

.admin-dialog {
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem;
  width: min(94vw, 620px);
  background: var(--surface);
  color: var(--ink);
}
.admin-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.admin-dialog-note { margin: 0 0 .6rem; font-size: .9rem; color: var(--ink-soft); }
.admin-dialog-buttons { display: flex; gap: .5rem; justify-content: flex-end; margin-top: .8rem; }
.admin-linkbox {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .85rem;
  padding: .6rem;
  border: 2px solid var(--line);
  border-radius: 10px;
  resize: vertical;
  background: var(--bg);
  color: var(--ink);
}

.admin-memory { max-height: 60svh; overflow-y: auto; font-size: .92rem; }
.admin-turn { border-left: 3px solid var(--line); padding-left: .7rem; margin: .5rem 0; }
.admin-turn-user { margin: .1rem 0; }
.admin-turn-ai { margin: .1rem 0; color: var(--ink-soft); }

/* /admin から戻されたときの案内 */
.login-note {
  margin: 0 0 .9rem;
  padding: .6rem .8rem;
  border: 2px solid var(--brand);
  border-radius: 10px;
  background: #eef3f8;
  color: var(--brand);
  font-size: .92rem;
}
