:root {
  color-scheme: light;
  --bg: #f8f4f4;
  --surface: #ffffff;
  --surface-2: #f5ecee;
  --surface-3: #fff8f8;
  --text: #18212b;
  --muted: #74646a;
  --line: #ead4d8;
  --accent: #e60012;
  --accent-2: #b00014;
  --accent-3: #6f2530;
  --warn: #b35a00;
  --danger: #a73232;
  --shadow: 0 14px 32px rgba(24, 33, 43, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Meiryo UI", system-ui, sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1280px, calc(100% - 32px));
  margin: 0 auto;
  padding: 20px 0 44px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0 22px;
}

.brand {
  min-width: 0;
}

.status-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.82rem;
}

.status-meta .sep {
  color: var(--line);
}

.state-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}

.state-dot.ok {
  background: var(--accent);
}

.state-dot.warn {
  background: var(--warn);
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.45rem, 3vw, 2.1rem);
  letter-spacing: 0;
}

h2 {
  font-size: 1.1rem;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.icon-button span {
  font-size: 1.25rem;
}

.icon-button.active {
  border-color: rgba(230, 0, 18, 0.35);
  background: rgba(230, 0, 18, 0.1);
  color: var(--accent);
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.kebab {
  position: relative;
}

.menu {
  position: absolute;
  z-index: 30;
  top: calc(100% + 6px);
  right: 0;
  display: grid;
  gap: 2px;
  min-width: 180px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-size: 0.92rem;
  text-align: left;
}

.menu-item:hover {
  background: var(--surface-2);
}

.menu-item.toggle-row {
  color: var(--muted);
  cursor: pointer;
}

#logoutButton {
  color: var(--danger);
  font-weight: 600;
}

.settings-overlay {
  position: fixed;
  z-index: 60;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(24, 33, 43, 0.28);
}

.settings-sheet {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(760px, 100%);
  max-height: min(820px, calc(100dvh - 36px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.settings-head,
.settings-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.settings-foot {
  justify-content: flex-end;
  border-top: 1px solid var(--line);
  border-bottom: 0;
}

.settings-foot button {
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--danger);
  font-weight: 700;
}

.settings-body {
  display: grid;
  gap: 12px;
  overflow: auto;
  padding: 12px;
}

.settings-group {
  display: grid;
  gap: 8px;
}

.settings-group h3 {
  font-size: 0.95rem;
}

.settings-subgroup {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-3);
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 6px;
}

.settings-check {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  min-height: 36px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.settings-check input {
  width: 17px;
  height: 17px;
  accent-color: var(--accent);
}

.settings-check strong {
  overflow-wrap: anywhere;
  font-size: 0.86rem;
}

.settings-check span {
  color: var(--muted);
  font-size: 0.72rem;
}

.setup-panel,
.login-panel,
.device-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.setup-panel {
  margin-bottom: 16px;
  padding: 18px;
  border-color: rgba(182, 95, 0, 0.35);
  background: #fff8ed;
}

.login-panel {
  max-width: 520px;
  margin: 34px auto;
  padding: 22px;
  box-shadow: var(--shadow);
}

.login-form {
  display: grid;
  gap: 16px;
}

.login-form p {
  margin-top: 6px;
  color: var(--muted);
  line-height: 1.6;
}

.login-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

.login-row input {
  min-height: 42px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: var(--surface);
  color: var(--text);
}

.login-row button {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  padding: 0 16px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.login-error {
  min-height: 20px;
  color: var(--danger) !important;
}

.setup-panel p {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.7;
}

.setup-panel pre {
  overflow-x: auto;
  margin: 14px 0 0;
  padding: 12px;
  border-radius: 6px;
  background: #1f2724;
  color: #f1f8f4;
}

.hidden {
  display: none !important;
}

/* パネルボード：環境・操作・リモコン等を1つの並べ替え可能なグリッドに。 */
.panel-board {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
  gap: 14px;
  align-items: start;
}

.panel {
  min-width: 0;
  align-self: start;
}

/* デバイス一覧は中身が多いので広めに使う（収まらなければ自動で1カラム）。 */
.device-panel {
  grid-column: span 2;
}

@media (max-width: 820px) {
  .device-panel {
    grid-column: auto;
  }
}

.panel-heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.section-kicker {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.segmented button {
  min-width: 74px;
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 0.86rem;
}

.segmented button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 4px 12px rgba(25, 39, 35, 0.08);
}

.device-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}

.sensor-highlights {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px;
}

.sensor-card {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  min-height: 102px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.sensor-badge {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 50%;
  background: #ffe7ea;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
}

.sensor-badge.warn {
  background: #fff2df;
  color: var(--warn);
}

.sensor-body {
  min-width: 0;
}

.sensor-label {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.sensor-value {
  display: block;
  margin-top: 2px;
  overflow-wrap: anywhere;
  font-size: 1.38rem;
  line-height: 1.05;
}

.sensor-name {
  margin-top: 5px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.82rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sensor-meta {
  display: flex;
  grid-column: 1 / -1;
  flex-wrap: wrap;
  gap: 5px;
}

.sensor-meta span,
.state-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 21px;
  max-width: 100%;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
}

.control-list,
.remote-list {
  display: grid;
  gap: 8px;
}

.controls-panel .control-list {
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
}

.control-card {
  position: relative;
  display: grid;
  gap: 9px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.control-heading {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.control-heading strong,
.control-heading span {
  display: block;
}

.control-heading strong {
  overflow-wrap: anywhere;
}

.control-heading div > span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.78rem;
}

.control-heading .state-pill {
  display: inline-flex;
}

.state-pill.ok {
  background: rgba(230, 0, 18, 0.1);
  color: var(--accent);
}

.state-pill.warn {
  background: rgba(179, 90, 0, 0.12);
  color: var(--warn);
}

.control-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.device-card {
  position: relative;
  display: flex;
  min-height: 188px;
  flex-direction: column;
  gap: 9px;
  padding: 11px;
  box-shadow: var(--shadow);
}

.card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-top h3 {
  overflow-wrap: anywhere;
  font-size: 1rem;
}

.device-type {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.82rem;
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  max-width: 120px;
  overflow: hidden;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pill.ok {
  background: rgba(230, 0, 18, 0.1);
  color: var(--accent);
}

.pill.warn {
  background: rgba(182, 95, 0, 0.12);
  color: var(--warn);
}

.quick-values {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.value-tile {
  min-height: 50px;
  padding: 8px;
  border-radius: 8px;
  background: var(--surface-2);
}

.value-tile span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
}

.value-tile strong {
  display: block;
  margin-top: 3px;
  overflow-wrap: anywhere;
  font-size: 1rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 30px;
}

.actions button,
.scene-item button,
.control-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  line-height: 1;
  text-align: center;
}

.actions button,
.control-actions button {
  padding: 0 9px;
}

/* 単発アクション（押す等）はアクセント枠で示す。状態の「点灯」とは別扱い。 */
.actions button.primary,
.control-actions button.primary {
  border-color: rgba(230, 0, 18, 0.45);
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
}

/* 現在の状態と一致するボタンだけ赤に点灯。 */
.actions button.active,
.control-actions button.active {
  border-color: rgba(230, 0, 18, 0.3);
  background: var(--accent);
  color: #fff;
}

.actions button:disabled,
.scene-item button:disabled,
.control-actions button:disabled,
.icon-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.actions button.pending,
.control-actions button.pending {
  opacity: 1;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(230, 0, 18, 0.18);
  animation: pendingPulse 0.9s ease-in-out infinite;
}

@keyframes pendingPulse {
  50% {
    box-shadow: 0 0 0 2px rgba(230, 0, 18, 0.04);
  }
}

details {
  margin-top: auto;
  color: var(--muted);
  font-size: 0.8rem;
}

summary {
  cursor: pointer;
}

details pre {
  overflow: auto;
  max-height: 180px;
  margin: 8px 0 0;
  padding: 10px;
  border-radius: 6px;
  background: #1f2724;
  color: #f1f8f4;
  font-size: 0.75rem;
}

.scene-list,
.event-log {
  display: grid;
  gap: 8px;
}

.scene-item {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  background: var(--surface-2);
}

.scene-item strong {
  overflow-wrap: anywhere;
}

.scene-item button {
  width: 34px;
  padding: 0;
  color: var(--accent-2);
}

.log-entry {
  padding: 10px;
  border-left: 3px solid var(--line);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.remote-item {
  position: relative;
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.remote-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.remote-heading strong {
  overflow-wrap: anywhere;
}

.remote-heading span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.74rem;
}

.remote-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.remote-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  line-height: 1;
  text-align: center;
}

.remote-actions button.primary {
  border-color: rgba(230, 0, 18, 0.45);
  color: var(--accent);
  font-weight: 600;
}

.remote-actions button.pending {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(230, 0, 18, 0.18);
  animation: pendingPulse 0.9s ease-in-out infinite;
}

.remote-actions button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.log-entry.error {
  border-left-color: var(--danger);
  color: var(--danger);
}

.empty {
  padding: 20px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.orderable {
  outline: 1px dashed rgba(230, 0, 18, 0.45);
  outline-offset: 2px;
  cursor: grab;
}

.orderable.dragging {
  opacity: 0.55;
}

.orderable.drag-over {
  outline-color: var(--accent-2);
  box-shadow: 0 0 0 3px rgba(46, 100, 181, 0.16), var(--shadow);
}

.order-controls {
  position: absolute;
  z-index: 2;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 4px;
}

.order-controls button {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--accent-2);
  font-weight: 700;
  line-height: 1;
}

/* パネル見出しの並べ替えボタンは見出し内にインライン配置する。 */
.order-controls.inline {
  position: static;
  margin-left: auto;
}

/* 並べ替え中のパネルは枠を強調して掴める範囲を分かりやすくする。 */
body.reorder-mode .panel.orderable {
  padding: 8px;
  border-radius: 10px;
}

body.reorder-mode .sensor-card,
body.reorder-mode .control-card,
body.reorder-mode .device-card,
body.reorder-mode .remote-item {
  padding-top: 44px;
}

body.reorder-mode .scene-item {
  padding-right: 78px;
}

body.reorder-mode .scene-item .order-controls {
  right: 56px;
}

@media (max-width: 700px) {
  .app-shell {
    width: min(100% - 12px, 1180px);
    padding: 8px 0 24px;
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
    gap: 8px;
    padding: 6px 0 12px;
  }

  h1 {
    font-size: 1.3rem;
  }

  h2 {
    font-size: 0.98rem;
  }

  .status-meta {
    gap: 4px;
    margin-top: 3px;
    font-size: 0.72rem;
  }

  .toolbar {
    width: auto;
    gap: 6px;
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
    padding-top: 2px;
  }

  .brand {
    grid-column: 1;
    grid-row: 1;
    padding-right: 6px;
  }

  .settings-overlay {
    align-items: end;
    padding: 0;
  }

  .settings-sheet {
    width: 100%;
    max-height: 88dvh;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 8px 8px 0 0;
  }

  .settings-head,
  .settings-foot,
  .settings-body {
    padding: 10px;
  }

  .preset-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .settings-check {
    min-height: 34px;
    padding: 6px 7px;
  }

  .settings-check strong {
    font-size: 0.78rem;
  }

  .settings-check span {
    font-size: 0.64rem;
  }

  .icon-button {
    width: 34px;
    height: 34px;
  }

  .icon-button span {
    font-size: 1.05rem;
  }

  .panel-board {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .panel-heading {
    gap: 6px;
    margin-bottom: 6px;
  }

  .section-kicker {
    font-size: 0.62rem;
  }

  .segmented {
    width: 100%;
    padding: 2px;
  }

  .segmented button {
    min-width: 0;
    min-height: 28px;
    font-size: 0.78rem;
  }

  .sensor-highlights,
  .device-list,
  .controls-panel .control-list,
  .remote-list,
  .scene-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .sensor-card {
    grid-template-columns: minmax(0, 1fr);
    gap: 5px;
    min-height: 0;
    padding: 8px;
  }

  .sensor-badge {
    display: none;
  }

  .sensor-label,
  .value-tile span,
  .device-type,
  .control-heading div > span,
  .remote-heading span {
    font-size: 0.66rem;
  }

  .sensor-value {
    font-size: 1.08rem;
  }

  .sensor-name {
    margin-top: 3px;
    font-size: 0.68rem;
  }

  .sensor-meta {
    gap: 3px;
  }

  .sensor-meta span,
  .state-pill,
  .pill {
    min-height: 19px;
    padding: 0 6px;
    font-size: 0.64rem;
  }

  .control-card,
  .device-card,
  .remote-item {
    gap: 6px;
    padding: 8px;
  }

  .device-card {
    min-height: 0;
  }

  .card-top,
  .control-heading,
  .remote-heading {
    gap: 6px;
  }

  .card-top h3,
  .control-heading strong,
  .remote-heading strong,
  .scene-item strong {
    font-size: 0.86rem;
    line-height: 1.25;
  }

  .pill {
    max-width: 72px;
  }

  .quick-values {
    gap: 4px;
  }

  .value-tile {
    min-height: 42px;
    padding: 6px;
  }

  .value-tile strong {
    font-size: 0.88rem;
  }

  .actions,
  .control-actions,
  .remote-actions {
    gap: 4px;
  }

  .actions button,
  .scene-item button,
  .control-actions button,
  .remote-actions button {
    min-height: 28px;
    border-radius: 7px;
    font-size: 0.78rem;
  }

  .actions button,
  .control-actions button,
  .remote-actions button {
    padding: 0 7px;
  }

  .scene-item {
    min-height: 40px;
    gap: 6px;
    padding: 7px;
  }

  .scene-item button {
    width: 30px;
  }

  details {
    display: none;
  }

  .login-row {
    grid-template-columns: 1fr;
  }
}
