/* レイアウト: 左に会話の一覧、右に会話。幅が狭い画面では 2 画面に切り替わる */
:root {
  --bg: #eef1f6; --panel: #ffffff; --panel-2: #f5f7fb; --ink: #172033; --muted: #62708a; --line: #dce2ec;
  --accent: #284f8f; --accent-text: #284f8f; --accent-ink: #ffffff; --bubble-in: #ffffff;
  --ok: #2e8b5a; --warn: #b7791f; --danger: #b3422f;
  --term-bg: #0d131d; --term-ink: #d7e0f0; --term-dim: #7f8da6; --term-ok: #6fd19b; --term-err: #f09a86; --term-line: #232d3d;
  --font-body: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans JP", "Meiryo", system-ui, sans-serif;
  --font-display: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Noto Sans Mono CJK JP", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-ui-theme="light"]) {
    --bg: #0f131a; --panel: #161b24; --panel-2: #1b212c; --ink: #e6eaf2; --muted: #8d99af; --line: #272f3d;
    --accent: #3a62ae; --accent-text: #93b4f0; --bubble-in: #1f2634;
    --ok: #5cc08a; --warn: #e0a84a; --danger: #ef8a76;
    color-scheme: dark;
  }
}
:root[data-ui-theme="dark"] {
  --bg: #0f131a; --panel: #161b24; --panel-2: #1b212c; --ink: #e6eaf2; --muted: #8d99af; --line: #272f3d;
  --accent: #3a62ae; --accent-text: #93b4f0; --bubble-in: #1f2634;
  --ok: #5cc08a; --warn: #e0a84a; --danger: #ef8a76;
  color-scheme: dark;
}
:root[data-size="small"] { font-size: 90%; }
:root[data-size="large"] { font-size: 112%; }
:root[data-font="mincho"] body { font-family: var(--font-display); }
:root[data-font="mono"] body { font-family: var(--font-mono); }

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font-body);
  font-size: 0.9375rem; line-height: 1.6; -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 2px; }
[hidden] { display: none !important; }
#root { height: 100%; }

/* ---- ログイン画面 ---- */
.auth {
  min-height: 100%; display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.auth-card {
  width: min(100%, 24rem); padding: 24px 20px 20px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
}
.wordmark {
  margin: 0; font-family: var(--font-display); font-weight: 700; font-size: 1.6rem;
  letter-spacing: 0.08em; color: var(--accent-text);
}
.auth-lead { margin: 4px 0 16px; font-size: 0.85rem; color: var(--muted); }
.tabs { display: flex; gap: 4px; padding: 3px; margin-bottom: 14px; border-radius: 999px; background: var(--panel-2); }
.tabs button { flex: 1; padding: 6px 10px; border: 0; border-radius: 999px; background: transparent; font-size: 0.85rem; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.fld { display: block; margin-bottom: 12px; }
.fld > span { display: block; margin-bottom: 4px; font-size: 0.78rem; color: var(--muted); }
.field {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); font: inherit;
}
.hint { margin: 3px 0 0; font-size: 0.72rem; color: var(--muted); }
.err { min-height: 1.4em; margin: 4px 0 8px; font-size: 0.82rem; color: var(--danger); }
.btn {
  padding: 8px 16px; border: 1px solid var(--line); border-radius: 999px;
  background: transparent; font-size: 0.85rem;
}
.btn:hover { border-color: var(--accent-text); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 700; }
.btn.wide { width: 100%; padding: 10px 16px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.danger { color: var(--danger); border-color: var(--danger); }

/* ---- 全体 ---- */
.app { height: 100%; display: grid; grid-template-columns: 300px minmax(0, 1fr); }
.side {
  min-height: 0; min-width: 0; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.side-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 12px; padding: 18px 16px 12px; }
.side-head .wordmark { flex: 0 1 auto; min-width: 0; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.head-actions { display: flex; align-items: center; gap: 4px; margin-left: auto; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.status[data-kind="live"]::before { background: var(--ok); }
.status[data-kind="error"]::before { background: var(--danger); }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center; flex: none;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink); }

.rooms { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 8px 16px; }
.group-label { margin: 14px 8px 6px; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.12em; color: var(--muted); }
.room {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 8px;
  border: 0; border-radius: 12px; background: transparent; text-align: left;
}
.room:hover { background: var(--panel-2); }
.room[aria-current="true"] { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.room .txt { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.room .nm { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room .sb { font-size: 0.75rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  flex: none; min-width: 20px; padding: 0 6px; border-radius: 999px; text-align: center;
  background: var(--accent); color: var(--accent-ink); font-size: 0.72rem; font-weight: 700; line-height: 20px;
}
.ava {
  flex: none; width: 40px; height: 40px; border-radius: 50%; object-fit: cover;
  display: grid; place-items: center; background: var(--panel-2);
  color: #fff; font-weight: 700; font-size: 1rem; overflow: hidden;
}
.ava.all { background: var(--accent); color: var(--accent-ink); font-family: var(--font-display); font-size: 1.05rem; }
.empty-people { margin: 6px 8px; font-size: 0.82rem; color: var(--muted); }

/* ---- コンソール ---- */
.term { flex: none; margin: 0 8px 8px; border-radius: 12px; background: var(--term-bg); color: var(--term-ink); overflow: hidden; }
.term-head {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px; border: 0; background: transparent; color: var(--term-dim);
  font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.08em; text-align: left;
}
.term-head svg { transition: transform 0.15s; }
.term-head[aria-expanded="false"] svg { transform: rotate(-90deg); }
.term-body { border-top: 1px solid var(--term-line); }
.term-out { height: 150px; overflow-y: auto; padding: 8px 12px; font-family: var(--font-mono); font-size: 0.78rem; line-height: 1.55; }
:root[data-term="small"] .term-out { height: 90px; }
:root[data-term="large"] .term-out { height: 260px; }
.tl { white-space: pre-wrap; overflow-wrap: anywhere; }
.tl.dim { color: var(--term-dim); } .tl.ok { color: var(--term-ok); } .tl.err { color: var(--term-err); }
.term-form { display: flex; align-items: center; gap: 8px; padding: 6px 12px 8px; border-top: 1px solid var(--term-line); }
.term-form label { font-family: var(--font-mono); font-size: 0.8rem; color: var(--term-ok); }
.term-form input { flex: 1; min-width: 0; padding: 4px 0; border: 0; background: transparent; color: var(--term-ink); font-family: var(--font-mono); font-size: 0.8rem; }
.term-form input:focus-visible { outline: none; }
.term-form:focus-within { box-shadow: inset 0 0 0 1px var(--accent-text); }

/* ---- 会話 ---- */
.thread { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); padding-top: env(safe-area-inset-top); }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.back { display: none; margin-left: -8px; }
.thread-head h2 { margin: 0; font-size: 1.05rem; line-height: 1.3; }
.thread-head p { margin: 0; font-size: 0.78rem; color: var(--muted); }
.head-txt { min-width: 0; }
.scroller { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; }
.msgs { max-width: 46rem; margin: 0 auto; display: flex; flex-direction: column; min-height: 100%; }
:root[data-width="narrow"] .msgs { max-width: 34rem; }
:root[data-width="wide"] .msgs { max-width: 64rem; }
.day { align-self: center; margin: 14px 0 6px; padding: 2px 12px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); font-size: 0.72rem; color: var(--muted); }
:root[data-days="off"] .day { display: none; }
.msg { display: flex; align-items: flex-end; gap: 8px; margin-top: 3px; }
.msg.start { margin-top: 12px; }
.msg.own { justify-content: flex-end; }
.msg .ava { width: 32px; height: 32px; font-size: 0.85rem; }
.msg .spacer { flex: none; width: 32px; }
:root[data-avatars="off"] .msg .ava, :root[data-avatars="off"] .msg .spacer { display: none; }
.col { min-width: 0; max-width: min(34rem, 82%); display: flex; flex-direction: column; align-items: flex-start; }
.msg.own .col { align-items: flex-end; }
.who { margin: 0 0 2px 4px; font-size: 0.75rem; color: var(--muted); }
.line { display: flex; align-items: flex-end; gap: 6px; max-width: 100%; }
.bubble {
  min-width: 0; padding: 8px 13px; border-radius: 18px 18px 18px 6px;
  background: var(--bubble-in); border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.own .bubble { border-radius: 18px 18px 6px 18px; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
:root[data-bubble="square"] .bubble, :root[data-bubble="square"] .msg.own .bubble { border-radius: 6px; }
:root[data-density="compact"] .msg { margin-top: 1px; }
:root[data-density="compact"] .msg.start { margin-top: 7px; }
:root[data-density="compact"] .bubble { padding: 4px 11px; }
.bubble.gone, .msg.own .bubble.gone { color: var(--muted); background: transparent; border: 1px dashed var(--line); }
.stampimg { display: block; width: min(120px, 100%); height: auto; aspect-ratio: 1; object-fit: contain; }
.meta { flex: none; display: flex; flex-direction: column; align-items: flex-end; font-size: 0.68rem; line-height: 1.4; color: var(--muted); font-variant-numeric: tabular-nums; }
.msg.other .meta { align-items: flex-start; }
:root[data-times="off"] .meta > span:first-child { display: none; }
.del { padding: 0; border: 0; background: transparent; font-size: 0.68rem; color: var(--muted); text-decoration: underline; opacity: 0; }
.msg:hover .del, .del:focus-visible, .del.armed { opacity: 1; }
.del.armed { color: var(--danger); }
@media (hover: none) { .del { opacity: 0.8; } }
.empty { margin: auto; max-width: 20rem; text-align: center; color: var(--muted); font-size: 0.9rem; }
.empty strong { display: block; margin-bottom: 4px; font-family: var(--font-display); font-size: 1.15rem; color: var(--ink); }

/* ---- 入力欄・スタンプ ---- */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; max-height: 140px; padding: 9px 14px;
  border: 1px solid var(--line); border-radius: 20px; background: var(--bg); line-height: 1.5;
}
.composer textarea::placeholder { color: var(--muted); }
.stamp-open { width: 40px; height: 40px; }
.send { flex: none; height: 40px; padding: 0 18px; border: 0; border-radius: 20px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.send:disabled { opacity: 0.5; }
.flash { margin: 0; padding: 6px 16px; font-size: 0.8rem; text-align: center; background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.tray { flex: none; max-height: 240px; overflow-y: auto; padding: 10px 16px 12px; background: var(--panel); border-top: 1px solid var(--line); }
.tray-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; font-size: 0.78rem; color: var(--muted); }
.tray-head button { padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; background: transparent; font-size: 0.78rem; }
.tray-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(68px, 1fr)); gap: 8px; }
.stamp-btn { position: relative; aspect-ratio: 1; padding: 4px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2); }
.stamp-btn:hover { border-color: var(--accent-text); }
.stamp-btn img { width: 100%; height: 100%; object-fit: contain; }
.stamp-btn.add { color: var(--accent-text); font-size: 0.75rem; line-height: 1.3; border-style: dashed; background: transparent; }
.stamp-btn .x { position: absolute; top: 2px; right: 2px; padding: 0 6px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 0.68rem; line-height: 1.5; }
.tray-empty { margin: 0 0 8px; font-size: 0.82rem; color: var(--muted); }

/* ---- 設定・編集 ---- */
.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet.editor { z-index: 21; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(10, 14, 22, 0.55); }
.sheet-panel {
  position: relative; width: min(100%, 28rem); max-height: 88%; overflow-y: auto;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); border-radius: 18px 18px 0 0;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line);
}
@media (min-width: 760px) { .sheet { align-items: center; } .sheet-panel { border-radius: 18px; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h2 { margin: 0; font-size: 1.1rem; }
.sheet-note { margin: 8px 0 8px; font-size: 0.82rem; color: var(--muted); }
.grp { min-width: 0; margin: 0; padding: 12px 0 4px; border: 0; }
.grp legend { float: left; width: 100%; padding: 0; margin: 0 0 8px; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.08em; color: var(--muted); }
.grp legend + * { clear: both; }
.seg, .swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label, .swatches label { position: relative; }
.seg input, .swatches input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.seg span { display: block; padding: 6px 13px; border: 1px solid var(--line); border-radius: 999px; font-size: 0.85rem; }
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.swatches span { display: block; width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--line); }
.swatches input:checked + span { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--ink); }
.seg input:focus-visible + span, .swatches input:focus-visible + span { outline: 2px solid var(--accent-text); outline-offset: 3px; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.toggle .tt { min-width: 0; display: flex; flex-direction: column; }
.toggle .th { font-size: 0.75rem; color: var(--muted); }
.toggle input { flex: none; appearance: none; -webkit-appearance: none; position: relative; width: 40px; height: 24px; margin: 0; border-radius: 12px; background: var(--line); cursor: pointer; }
.toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: var(--panel); }
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { left: 19px; }
.who-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.who-row .ava { width: 56px; height: 56px; font-size: 1.3rem; }
.ed-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.ed-row .field { flex: 1 1 12rem; width: auto; }
.ed-row label { font-size: 0.8rem; color: var(--muted); }
.ed-row input[type=range] { flex: 1; min-width: 120px; }
.tip { margin: 8px 0 0; font-size: 0.78rem; color: var(--muted); }
.ed-canvas { display: block; width: min(100%, 256px); aspect-ratio: 1; margin: 12px auto 8px; border-radius: 16px; background: var(--panel-2); border: 1px solid var(--line); touch-action: none; cursor: grab; }
.ed-canvas.text { cursor: default; }
.ed-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.file-btn { position: relative; display: inline-block; cursor: pointer; }
.file-btn input { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; }
.file-btn:focus-within { outline: 2px solid var(--accent-text); outline-offset: 2px; }

/* ---- 動き ---- */
@keyframes msg-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes msg-pop { 0% { opacity: 0; transform: translateY(12px) scale(0.9); } 60% { opacity: 1; transform: translateY(-2px) scale(1.03); } 100% { opacity: 1; transform: none; } }
@keyframes sheet-in { from { opacity: 0; transform: translateY(22px); } to { opacity: 1; transform: none; } }
:root[data-motion="normal"] .msg.fresh { animation: msg-in 0.22s ease-out both; }
:root[data-motion="lively"] .msg.fresh { animation: msg-pop 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.2) both; }
:root[data-motion="normal"] .sheet-panel { animation: sheet-in 0.22s ease-out both; }
:root[data-motion="lively"] .sheet-panel { animation: sheet-in 0.38s cubic-bezier(0.2, 0.9, 0.3, 1.2) both; }
:root:not([data-motion="off"]) .room, :root:not([data-motion="off"]) .del, :root:not([data-motion="off"]) .send { transition: background 0.15s, opacity 0.15s, transform 0.12s; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---- 画面幅 ---- */
@media (min-width: 760px) {
  :root[data-side="narrow"] .app { grid-template-columns: 240px minmax(0, 1fr); }
  :root[data-side="wide"] .app { grid-template-columns: 380px minmax(0, 1fr); }
}
@media (max-width: 759px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .app[data-view="list"] .thread { display: none; }
  .app[data-view="thread"] .side { display: none; }
  .back { display: grid; }
  .side { border-right: 0; }
}
