:root {
  color-scheme: light dark;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #1a1f26;
  --muted: #5b6573;
  --border: #dde2e9;
  --focus: #2459c4;

  --locked: #14754a;       --locked-bg: #e2f4ea;
  --unlocked: #a45700;     --unlocked-bg: #fff0d6;
  --moved: #2a57ad;        --moved-bg: #e4ecfa;
  --unknown: #5b6573;      --unknown-bg: #eceff3;
  --error: #b3261e;        --error-bg: #fde9e7;
  --warn: #8a5300;         --warn-bg: #fff3d9;
  --success: #14754a;      --success-bg: #e2f4ea;
  --info: #2a57ad;         --info-bg: #e4ecfa;

  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1217;
    --surface: #171c23;
    --text: #e9edf2;
    --muted: #9aa5b3;
    --border: #2a323d;
    --focus: #7ba3f5;

    --locked: #5fd592;     --locked-bg: #11301f;
    --unlocked: #ffb347;   --unlocked-bg: #3a2708;
    --moved: #8db1f5;      --moved-bg: #16264a;
    --unknown: #9aa5b3;    --unknown-bg: #232a33;
    --error: #ff9a92;      --error-bg: #3d1713;
    --warn: #ffc56b;       --warn-bg: #3a2a0a;
    --success: #5fd592;    --success-bg: #11301f;
    --info: #8db1f5;       --info-bg: #16264a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--font);
  -webkit-text-size-adjust: 100%;
}

main {
  max-width: 480px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  display: grid;
  gap: 16px;
}

h1 { font-size: 1.25rem; margin: 0; letter-spacing: .01em; }
h2 { font-size: 1rem; margin: 0 0 8px; }
p { margin: 0; }
code { font: 0.875em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--unknown-bg); padding: .1em .35em; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .8125rem; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.pills { display: flex; gap: 6px; flex-wrap: wrap; }

.pill {
  font-size: .8125rem;
  font-weight: 600;
  padding: 3px 12px;
  border-radius: 999px;
  background: var(--unknown-bg);
  color: var(--unknown);
  white-space: nowrap;
}
.pill[data-tone="ok"]    { background: var(--success-bg); color: var(--success); }
.pill[data-tone="warn"]  { background: var(--warn-bg);    color: var(--warn); }
.pill[data-tone="error"] { background: var(--error-bg);   color: var(--error); }
.pill[data-tone="info"]  { background: var(--info-bg);    color: var(--info); }

/* ---- 表示の切り替え（鍵・その他 / Hub 3）と、それぞれのパネル */
.view-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--unknown-bg);
  border-radius: 14px;
}
.view-tab {
  min-height: 44px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.view-tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .18); }
.panel { display: grid; gap: 16px; align-content: start; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: 1.125rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.setup { border-color: var(--warn); }
.setup ul { margin: 0 0 8px; padding-left: 1.2em; color: var(--error); }
.setup ol { margin: 8px 0; padding-left: 1.4em; }

/* ---- 状態カード */
.lock {
  --accent: var(--unknown);
  --accent-bg: var(--unknown-bg);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  background: var(--accent-bg);
  border-color: transparent;
  transition: background-color .3s;
}
.lock[data-state="locked"]   { --accent: var(--locked);   --accent-bg: var(--locked-bg); }
.lock[data-state="unlocked"] { --accent: var(--unlocked); --accent-bg: var(--unlocked-bg); }
.lock[data-state="moved"]    { --accent: var(--moved);    --accent-bg: var(--moved-bg); }

.label { font-size: .8125rem; color: var(--muted); }
.state { font-size: 1.75rem; font-weight: 700; line-height: 1.3; color: var(--accent); }

.padlock { width: 84px; height: 84px; }
.padlock .body { fill: var(--accent); }
.padlock .keyhole { fill: var(--accent-bg); }
.padlock .shackle {
  fill: none;
  stroke: var(--accent);
  stroke-width: 7;
  stroke-linecap: round;
  transform-origin: 64px 44px;
  transition: transform .35s ease;
}
.lock[data-state="unlocked"] .shackle { transform: translateY(-9px) rotate(-26deg); }
.lock[data-state="moved"] .shackle    { transform: translateY(-4px) rotate(-12deg); }
.lock[data-state="unknown"] .shackle  { stroke-dasharray: 2 9; }

.icon-btn {
  align-self: start;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 12px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: rgba(127, 127, 127, .15); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- 操作ボタン */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  min-height: 56px;
  padding: 0 20px;
  border: 2px solid transparent;
  border-radius: 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: filter .15s, opacity .15s;
}
.btn:hover:not(:disabled) { filter: brightness(1.06); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.lock-btn   { background: var(--locked);   color: #fff; }
.unlock-btn { background: var(--unlocked); color: #fff; }
.ghost      { background: transparent; border-color: var(--border); color: var(--text); }
@media (prefers-color-scheme: dark) {
  .lock-btn, .unlock-btn { color: #0e1217; }
}

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---- 通知 */
.notice {
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--info-bg);
  color: var(--info);
  font-size: .9375rem;
}
.notice[data-tone="success"] { background: var(--success-bg); color: var(--success); }
.notice[data-tone="warn"]    { background: var(--warn-bg);    color: var(--warn); }
.notice[data-tone="error"]   { background: var(--error-bg);   color: var(--error); }

/* ---- 詳細 */
.info dl { margin: 0; display: grid; gap: 10px; }
.info dl > div { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.info dt { color: var(--muted); font-size: .875rem; }
.info dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
meter { width: 88px; height: 10px; vertical-align: middle; }

.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: .875rem; }
.log li { display: flex; gap: 10px; }
.log time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }

/* ---- ダイアログ */
dialog {
  width: min(92vw, 380px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog p { margin-bottom: 12px; }
dialog input {
  width: 100%;
  min-height: 44px;
  margin: 4px 0 12px;
  padding: 0 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.dialog-actions { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 10px; margin-top: 8px; }

/* ---- 機器の切り替え・追加 */
.devices { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.device-tabs { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .9375rem;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--locked); border-color: transparent; color: #fff; font-weight: 700; }
.chip.add { border-style: dashed; color: var(--muted); }
.empty-state { text-align: center; display: grid; gap: 12px; }
#readonly-note { display: grid; gap: 8px; justify-items: start; }
#readonly-note .chip { border-color: currentColor; background: transparent; color: inherit; font-weight: 700; }
main.empty #lock-card, main.empty .actions, main.empty .info, main.empty #readonly-note, main.empty #notice { display: none; }

.app-version { margin-top: 4px; text-align: center; font-size: .6875rem; color: var(--muted); font-variant-numeric: tabular-nums; user-select: all; }

/* ---- Hub 3 */
.hub-led { display: grid; gap: 14px; }
.hub-led [hidden] { display: none; }
.hub-sec { display: grid; gap: 8px; padding-top: 14px; border-top: 1px solid var(--border); }
.hub-sec h3 { margin: 0; font-size: .875rem; color: var(--muted); font-weight: 600; }
.hub-name-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.hub-name-row strong { font-size: 1.125rem; overflow-wrap: anywhere; }
.hub-name-row strong.placeholder { font-size: .9375rem; font-weight: 400; color: var(--muted); }
.link-btn.quiet { margin-top: 0; padding: 4px 0; color: var(--muted); }
.meta-chunk { display: inline-block; }   /* 補足行は、区切りごとにまとめて折り返す（単語の途中で切らない） */
.led-row { display: flex; align-items: center; gap: 12px; }
.led-row input[type="range"] { flex: 1; min-height: 44px; accent-color: var(--locked); }
.led-row output { min-width: 3.2em; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.led-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.led-actions .link-btn { margin-top: 0; color: var(--muted); }
.led-log summary { cursor: pointer; padding: 4px 0; }
.led-log .log { margin-top: 6px; }
select {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
dialog select { margin: 4px 0 12px; }
#biz-dialog .dialog-actions, #hub-name-dialog .dialog-actions { grid-auto-flow: row; grid-auto-columns: auto; }

.link-btn {
  margin-top: 8px;
  padding: 8px 0;
  border: 0;
  background: none;
  color: var(--error);
  font: inherit;
  font-size: .875rem;
  text-decoration: underline;
  cursor: pointer;
}

.scan { position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: #000; margin-bottom: 12px; }
.scan video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan::after { content: ""; position: absolute; inset: 16%; border: 3px solid rgba(255, 255, 255, .85); border-radius: 12px; pointer-events: none; }
.preview dl { margin: 0 0 8px; display: grid; gap: 8px; }
.preview dl > div { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.preview dt { color: var(--muted); font-size: .8125rem; }
.preview dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; word-break: break-all; }
#add-dialog .dialog-actions { grid-auto-flow: row; grid-auto-columns: auto; }
/* UUID は全文を見比べるので、1 行目として左揃え・小さめの字で折り返しを自然にする */
.preview dl > div:first-child { flex-direction: column; align-items: flex-start; gap: 2px; }
.preview dl > div:first-child dd { text-align: left; font-size: .8125rem; letter-spacing: 0; }
dialog { max-height: 92vh; overflow-y: auto; } /* 画面が低くても、下のボタンまでスクロールで届く */

@media (prefers-color-scheme: dark) {
  .chip[aria-pressed="true"] { color: #0e1217; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
