:root {
  --bg: #f4f5f7; --surface: #fff; --fg: #1c1d21; --muted: #6b6f7a; --line: #e2e4e9;
  --accent: #2f6bff; --accent-fg: #fff; --me: #e8efff; --code: #f0f1f4; --danger: #d33;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #121316; --surface: #1b1c20; --fg: #ecedf0; --muted: #9a9ea8; --line: #2c2e34;
    --accent: #6f95ff; --accent-fg: #0d0f14; --me: #1f2840; --code: #24262c; }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
body { font: 16px/1.6 -apple-system, system-ui, "Hiragino Sans", sans-serif; -webkit-text-size-adjust: 100%; }
p { margin: 0.4em 0; }
.muted { color: var(--muted); font-size: 14px; }

button { font: inherit; font-size: 15px; padding: 7px 14px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--fg); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.ghost { background: transparent; }
button.wide { width: 100%; padding: 12px; }
input[type=password], select, textarea { font: inherit; font-size: 16px; color: var(--fg); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }

/* ログイン */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login[hidden] { display: none; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 24px; display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card details input { width: 100%; margin: 8px 0; }

/* 全体: PC は上タブ・スマホは下タブ */
#main:not([hidden]) { display: flex; flex-direction: column; height: 100dvh; max-width: 1100px; margin: 0 auto; }
.panels { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 16px; }
[data-panel] { min-height: 100%; }
.tabs { display: flex; gap: 4px; padding: 8px 16px 0; border-bottom: 1px solid var(--line); }
.tabs button { border: none; background: none; border-radius: 0; color: var(--muted); padding: 8px 12px;
  border-bottom: 2px solid transparent; display: flex; gap: 6px; align-items: center; }
.tabs button[aria-selected=true] { color: var(--fg); border-bottom-color: var(--accent); font-weight: 600; }
@media (max-width: 700px) {
  .tabs { order: 2; gap: 0; padding: 0 0 env(safe-area-inset-bottom); border-bottom: none; border-top: 1px solid var(--line);
    background: var(--surface); }
  .tabs button { flex: 1; flex-direction: column; gap: 0; font-size: 11px; padding: 6px 0; border-bottom: none; }
  .tabs button[aria-selected=true] { color: var(--accent); }
  .tabs .ico { font-size: 20px; line-height: 1.3; }
  .panels { padding: 12px; }
}

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px; }
.card h2 { font-size: 15px; margin: 2px 0 6px; }
.bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0; }
.kv { list-style: none; padding: 0; margin: 0; font-size: 14px; }
.kv li { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0 12px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.kv li:last-child { border-bottom: none; }
.kv li > span:first-child { overflow-wrap: anywhere; }
.account { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* ホーム */
.env p { margin: 2px 0; }
.devices { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr)); gap: 12px; }
.device { margin: 0; display: flex; flex-direction: column; gap: 6px; }
.device-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.device-head button { padding: 2px 10px; }
.device .buttons { display: flex; gap: 8px; }
.device .buttons button { flex: 1; min-height: 42px; }
.device .lock-state, .device .status { margin: 0; }
.ac-settings { display: flex; gap: 6px; flex-wrap: wrap; }
.ac-settings select { flex: 1; min-width: 80px; padding: 6px; }

/* カメラ */
.camera:not([hidden]) { display: flex; flex-direction: column; align-items: center; gap: 8px; }
#camera-img { width: 100%; max-height: calc(100dvh - 180px); object-fit: contain; background: #000; border-radius: var(--radius); min-height: 160px; }

/* チャット */
.chat:not([hidden]) { display: flex; gap: 16px; height: 100%; }
.history { width: 240px; flex-shrink: 0; overflow-y: auto; }
.history summary { display: none; }
.history ul { list-style: none; padding: 0; margin: 0; }
.history li { display: flex; border-radius: 8px; }
.history li.active { background: var(--me); }
.history .open { flex: 1; text-align: left; border: none; background: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .del { border: none; background: none; color: var(--muted); }
.conversation { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#chat-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#log { flex: 1; overflow-y: auto; padding: 4px 0; }
#form textarea { width: 100%; resize: none; display: block; }
.msg { padding: 8px 12px; border-radius: var(--radius); margin: 8px 0; overflow-wrap: anywhere; }
.msg.user { background: var(--me); white-space: pre-wrap; margin-left: 15%; }
.msg.assistant { background: var(--surface); border: 1px solid var(--line); }
.msg.error { color: var(--danger); white-space: pre-wrap; }
details.thinking { color: var(--muted); font-size: 13px; white-space: pre-wrap; margin: 4px 0; }
@media (max-width: 700px) {
  .chat:not([hidden]) { flex-direction: column; gap: 4px; }
  .history { width: auto; max-height: 40vh; }
  .history summary { display: list-item; color: var(--muted); font-size: 14px; cursor: pointer; }
  .msg.user { margin-left: 8%; }
}

/* Markdown（AI の応答） */
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p, .msg.assistant li { white-space: pre-wrap; }
.msg.assistant h3, .msg.assistant h4, .msg.assistant h5, .msg.assistant h6 { margin: 0.8em 0 0.3em; line-height: 1.4; }
.msg.assistant h3 { font-size: 1.15em; }
.msg.assistant h4 { font-size: 1.05em; }
.msg.assistant h5, .msg.assistant h6 { font-size: 1em; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.4em; margin: 0.4em 0; }
.msg.assistant code { background: var(--code); border-radius: 4px; padding: 0 4px; font-size: 0.9em; }
.msg.assistant pre { background: var(--code); border-radius: 8px; padding: 10px; overflow-x: auto; margin: 0; }
.msg.assistant pre code { background: none; padding: 0; white-space: pre; }
.msg.assistant .codeblock { position: relative; margin: 0.5em 0; }
.msg.assistant .copy { position: absolute; top: 4px; right: 4px; font-size: 12px; padding: 2px 8px; }
.msg.assistant blockquote { margin: 0.5em 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.msg.assistant .tablewrap { overflow-x: auto; margin: 0.5em 0; }
.msg.assistant table { border-collapse: collapse; font-size: 14px; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 4px 8px; }
.msg.assistant hr { border: none; border-top: 1px solid var(--line); }
.msg.assistant a { color: var(--accent); }
.opts { display: flex; gap: 12px; flex-wrap: wrap; }
.searching { list-style: none; padding: 0; margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.searching .error { color: var(--danger); }
.msg .sources { margin: 0.6em 0 0; padding: 0.5em 0 0 1.6em; border-top: 1px solid var(--line); font-size: 13px; }
.msg .sources a { color: var(--muted); overflow-wrap: anywhere; }
