/* kaite の画面の見た目（外部のフォント・画像は読まない）。
 *
 * 第一の画面はスマホの縦長。机に立てた端末を片手で操作するので、ボタンは大きく（高さ 52px 以上）、
 * 主な操作（撮る・送る）は画面の下に寄せて親指で届くようにする。iPad と PC では会話と撮影を
 * 横に並べる。文字は 16px 以上にする（iOS は 16px 未満の入力欄に触れると画面を拡大してしまう）。
 */

:root {
  --bg: #f6f4ef;
  --fg: #1d1d1f;
  --muted: #5f5f66;
  --card: #ffffff;
  --line: #d9d4c7;
  --accent: #1a5fd0;
  --accent-fg: #ffffff;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --warn-bg: #fff3d1;
  --warn-fg: #5c4300;
  --reply-bg: #f1f6ff;
  --radius: 12px;
  --tap: 52px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --fg: #ececf0;
    --muted: #a3a3ad;
    --card: #1d1d21;
    --line: #34343b;
    --accent: #5b9bff;
    --accent-fg: #0b0b0d;
    --danger: #ff8a80;
    --danger-bg: #3b1d1b;
    --warn-bg: #3a2f12;
    --warn-fg: #ffdf94;
    --reply-bg: #18233a;
  }
}

/* hidden 属性を、display を指定した class より常に優先させる（画面の切り替えは hidden だけで行う）。 */
[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP",
    "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 17px;
  line-height: 1.6;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

h1,
h2,
h3 {
  line-height: 1.3;
}

h2 {
  font-size: 1.15rem;
  margin: 0 0 12px;
}

h3 {
  font-size: 1rem;
  margin: 16px 0 8px;
}

code {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 0.95em;
  overflow-wrap: anywhere;
}

/* --- 上の帯と見出し ------------------------------------------------------- */

.banner {
  background: var(--warn-bg);
  color: var(--warn-fg);
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}

.banner p {
  margin: 0 0 6px;
}

.banner .cmd {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.banner code {
  background: rgba(0, 0, 0, 0.08);
  padding: 6px 8px;
  border-radius: 6px;
  user-select: all;
  -webkit-user-select: all;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 12px calc(24px + env(safe-area-inset-bottom));
}

.center {
  text-align: center;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.hint {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 6px 0;
}

.error {
  color: var(--danger);
  margin: 8px 0;
}

.error-box {
  background: var(--danger-bg);
  color: var(--fg);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 10px 0;
}

.error-box p {
  margin: 0 0 6px;
}

/* --- ボタンと入力 ----------------------------------------------------------- */

button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--fg);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation; /* ダブルタップでの拡大待ちを無くし、押した瞬間に反応させる */
  -webkit-tap-highlight-color: transparent;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

button:focus-visible,
.button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.big {
  width: 100%;
  min-height: var(--tap);
  font-size: 1.1rem;
}

.small {
  min-height: 36px;
  padding: 4px 12px;
  font-size: 0.95rem;
}

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

.secondary {
  background: var(--card);
}

.danger {
  color: var(--danger);
  background: transparent;
  border-color: transparent;
}

.ghost {
  background: transparent;
  border-color: transparent;
  min-height: 40px;
  padding: 4px 8px;
}

label {
  display: block;
  font-weight: 600;
  margin: 12px 0 4px;
}

input[type="text"],
input[type="password"],
textarea {
  width: 100%;
  font: inherit;
  font-size: 17px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
}

textarea {
  resize: vertical;
}

input[readonly] {
  opacity: 0.7;
}

fieldset {
  border: 0;
  padding: 0;
  margin: 0 0 16px;
}

legend {
  font-weight: 700;
  padding: 0;
  margin-bottom: 8px;
}

.btnrow {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0;
}

.btnrow > * {
  flex: 1 1 12em;
}

label.check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 400;
  min-height: 44px;
  margin: 0;
}

label.check input {
  width: 22px;
  height: 22px;
}

/* --- lens の選択と返し方 ------------------------------------------------------ */

.choices {
  display: grid;
  gap: 8px;
}

.choice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-weight: 400;
  cursor: pointer;
}

.choice input {
  width: 22px;
  height: 22px;
  margin-top: 2px;
  flex: none;
}

.choice:has(input:checked) {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent) inset;
}

.choice-text {
  display: flex;
  flex-direction: column;
}

.choice-name {
  font-weight: 700;
}

.choice-desc {
  color: var(--muted);
  font-size: 0.95rem;
}

.segmented {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.segmented label {
  flex: 1;
  margin: 0;
  position: relative;
}

/* ラジオの丸は隠し、ラベル全体を大きな押し場所にする（入力欄としては残すので操作と読み上げは保たれる）。 */
.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
}

.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 0 8px;
}

.segmented input:checked + span {
  background: var(--accent);
  color: var(--accent-fg);
}

.segmented input:focus-visible + span {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}

/* --- 一覧 ------------------------------------------------------------------- */

.list,
.problems {
  list-style: none;
  padding: 0;
  margin: 0 0 12px;
  display: grid;
  gap: 8px;
}

.list-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  min-height: var(--tap);
  font-weight: 400;
}

.item-title {
  font-weight: 700;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.item-sub {
  color: var(--muted);
  font-size: 0.95rem;
}

.badge {
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 8px;
  color: var(--muted);
}

.problems li {
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--danger-bg);
}

/* --- 会話 ------------------------------------------------------------------- */

.turns {
  list-style: none;
  padding: 0;
  margin: 0;
}

.turn-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9rem;
}

.turn.pending .turn-status::before {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-right: 6px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  border-right-color: transparent;
  animation: spin 0.9s linear infinite;
  vertical-align: -0.1em;
}

@media (prefers-reduced-motion: reduce) {
  .turn.pending .turn-status::before {
    animation: none;
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.status-failed {
  color: var(--danger);
  font-weight: 700;
}

.turn-image-link {
  display: block;
  margin: 8px 0;
}

.turn-image {
  display: block;
  max-width: 100%;
  max-height: 180px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  object-fit: contain;
}

.turn-note {
  margin: 6px 0;
  color: var(--muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* 返答は平文のまま読ませる（改行を保ち、長い式や URL でも横にはみ出さない）。 */
.turn-reply {
  background: var(--reply-bg);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 8px 0;
  font-size: 1.05rem;
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.turn-context {
  font-size: 0.9rem;
}

.turn-warnings {
  margin: 6px 0;
  padding: 8px 12px 8px 28px;
  background: var(--warn-bg);
  color: var(--warn-fg);
  border-radius: 10px;
  font-size: 0.95rem;
}

.turn-manual {
  margin-top: 8px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.manual-lead {
  font-weight: 700;
  margin: 4px 0;
}

.steps {
  margin: 4px 0 8px;
  padding-left: 1.6em;
}

.manual-text {
  margin-top: 8px;
}

.manual-status {
  color: var(--muted);
  margin: 6px 0 0;
}

/* --- 撮る ------------------------------------------------------------------- */

.frame {
  border-radius: 10px;
  overflow: hidden;
  background: #000;
  margin-bottom: 8px;
}

.frame video,
.frame img {
  display: block;
  width: 100%;
  max-height: 62vh;
  object-fit: contain;
  background: #000;
}

/* 主な操作は画面の下に貼り付け、紙から目を離さずに親指で押せるようにする。 */
.actionbar {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px -16px -16px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
  border-radius: 0 0 var(--radius) var(--radius);
}

.filebtn {
  position: relative;
  margin: 0;
  overflow: hidden;
}

/* ファイル選択の入力欄は見えなくするが、display: none にはしない（一部の端末でラベルからの
 * 呼び出しが効かなくなるため）。押し場所はラベル全体。 */
.filebtn input {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

details {
  margin: 8px 0;
}

summary {
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  color: var(--muted);
}

/* --- 広い画面（iPad の横向き・PC）: 会話と撮影を横に並べる ------------------------ */

@media (min-width: 900px) {
  main {
    max-width: 1180px;
  }

  .session-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
  }

  .col-capture {
    position: sticky;
    top: 64px;
  }

  .turns {
    max-height: none;
  }

  #view-home,
  #view-lenses {
    max-width: 760px;
    margin: 0 auto;
  }
}

/* --- ライブ（書画カメラで紙を見続ける） --------------------------------------------- */
/* 返事は紙から目を上げてすぐ読めるよう大きめの字にする。判定の状態は色だけでなく文で出す（色を見分けにくい人にも伝わる）。 */

.live-frame video {
  max-height: 48vh;
}

/* 紙の四隅を置く時に映像の上に重ねる。置いている間だけ指の操作を受け、画面のスクロールに取られないようにする。 */
.live-frame {
  position: relative;
}

.live-corners-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  touch-action: none;
}

.live-corners-overlay[data-editing="1"] {
  pointer-events: auto;
  cursor: crosshair;
}

.live-corners-edit {
  margin: 4px 0 8px;
}

.live-state {
  margin: 4px 0 8px;
  padding: 8px 10px;
  border-radius: 8px;
  border-left: 4px solid var(--line);
  background: var(--bg);
}

.live-state[data-state="clear"] {
  border-left-color: var(--accent);
}

.live-state[data-state="blocked"] {
  border-left-color: var(--warn-fg);
  background: var(--warn-bg);
  color: var(--warn-fg);
}

.live-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px 0 8px;
}

.live-reply {
  min-height: 3.2em;
  margin: 0;
  padding: 12px;
  font-size: 1.25rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border-radius: 10px;
  background: var(--reply-bg);
}

.live-reply[data-state="silent"] {
  font-size: 0.95rem;
  color: var(--muted);
  background: transparent;
}

.live-reply:empty::before {
  content: "書けた所まで送ると、ここに返事が出ます。";
  color: var(--muted);
  font-size: 0.95rem;
}

.notice {
  background: var(--warn-bg);
  color: var(--warn-fg);
}

.region {
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.region-text {
  margin: 0 0 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.region-edit {
  width: 100%;
}

.live-history-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

@media (min-width: 900px) {
  .live-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
  }

  .col-live-camera {
    position: sticky;
    top: 64px;
  }
}
