:root {
  --shr-main: #0077c7;
  --shr-main-dark: #005ea0;
  --shr-danger: #e01e5a;
  --shr-warning: #f5b800;
  --shr-success: #00885a;
  --shr-text: #23221f;
  --shr-text-grey: #6b6b6b;
  --shr-border: #d6d6d6;
  --shr-border-strong: #b7b7b7;
  --shr-bg: #f7f7f7;
  --shr-white: #fff;
  --shr-blue-bg: #e8f4fb;
  --shr-red-bg: #fff0f3;
  --shr-yellow-bg: #fff8d9;
  --shr-green-bg: #eaf8f1;
  --shr-focus: 0 0 0 3px rgba(0, 119, 199, 0.28);
  --shr-shadow: 0 2px 8px rgba(35, 34, 31, 0.12);
  --shr-radius: 6px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  overscroll-behavior-y: none;
}
body {
  background: var(--shr-bg);
  color: var(--shr-text);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }
.screen { min-height: 100dvh; }
.muted { color: var(--shr-text-grey); }
.err {
  color: var(--shr-danger);
  min-height: 1.3em;
  font-size: 0.875rem;
  margin: 8px 0 0;
}

.login-card {
  width: min(420px, calc(100vw - 32px));
  margin: 14vh auto 0;
  background: var(--shr-white);
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
  padding: 24px;
  box-shadow: var(--shr-shadow);
}
.login-card h1 {
  margin: 0 0 8px;
  font-size: 1.5rem;
  line-height: 1.35;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  padding: max(12px, env(safe-area-inset-top)) 24px 12px;
  background: var(--shr-white);
  border-bottom: 1px solid var(--shr-border);
}
.brand {
  color: var(--shr-main);
  font-size: 1.25rem;
  font-weight: 700;
}
.statebar {
  margin-top: 2px;
  font-size: 0.8125rem;
  color: var(--shr-text-grey);
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

.view {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 24px 24px 112px;
}

.hero {
  background: var(--shr-white);
  border: 1px solid var(--shr-border);
  border-left: 6px solid var(--shr-main);
  border-radius: var(--shr-radius);
  padding: 20px;
  box-shadow: var(--shr-shadow);
}
.hero-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.home-state {
  margin-top: 2px;
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.2;
}
.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.metric {
  background: #fafafa;
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
  padding: 14px;
}
.metric-label {
  color: var(--shr-text-grey);
  font-size: 0.8125rem;
}
.metric-value {
  margin-top: 4px;
  color: var(--shr-text);
  font-size: 1.25rem;
  font-weight: 700;
}

.section-title {
  margin: 28px 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.section-title h2 {
  margin: 0;
  font-size: 1.125rem;
  line-height: 1.4;
}

.quick-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}
.scene-btn,
.action-btn {
  min-height: 72px;
  text-align: left;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  background: var(--shr-white);
}
.scene-btn span,
.action-btn span {
  color: var(--shr-text-grey);
  font-size: 0.8125rem;
}

.card,
.rule,
.device-card,
.history-item {
  background: var(--shr-white);
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
  padding: 16px;
}

.room { margin-bottom: 22px; }
.room h2 {
  margin: 0 0 10px;
  font-size: 1rem;
}
.device-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}
.device-card {
  min-height: 128px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 12px;
}
.device-title {
  font-weight: 700;
  overflow-wrap: anywhere;
}
.device-sub {
  margin-top: 4px;
  color: var(--shr-text-grey);
  font-size: 0.875rem;
}
.device-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.rule {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 16px;
  align-items: start;
}
.rule.disabled { background: #f0f0f0; }
.rule-main {
  min-width: 0;
  cursor: pointer;
}
.rule-title {
  font-weight: 700;
  margin-bottom: 4px;
}
.rule-sub {
  color: var(--shr-text-grey);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}
.rule-actions {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  justify-items: end;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--shr-border-strong);
  background: #f5f5f5;
  color: var(--shr-text);
  font-size: 0.75rem;
  font-weight: 700;
}
.chip.ok {
  border-color: #80c4a9;
  background: var(--shr-green-bg);
  color: var(--shr-success);
}
.chip.warn {
  border-color: #dcc200;
  background: var(--shr-yellow-bg);
  color: #735d00;
}
.chip.err {
  border-color: #ee8dab;
  background: var(--shr-red-bg);
  color: var(--shr-danger);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.history-item {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
}
.history-dot {
  width: 10px;
  height: 10px;
  margin-top: 7px;
  border-radius: 50%;
  background: var(--shr-success);
}
.history-dot.err { background: var(--shr-danger); }
.history-time {
  margin-top: 2px;
  color: var(--shr-text-grey);
  font-size: 0.8125rem;
}

.bottomnav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 12;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 8px max(12px, env(safe-area-inset-left)) max(10px, env(safe-area-inset-bottom));
  background: var(--shr-white);
  border-top: 1px solid var(--shr-border);
  box-shadow: 0 -2px 8px rgba(35, 34, 31, 0.08);
}
.navitem {
  border: none;
  background: transparent;
  color: var(--shr-text-grey);
  padding: 10px 6px;
  font-size: 0.8125rem;
  border-radius: var(--shr-radius);
}
.navitem.sel {
  background: var(--shr-blue-bg);
  color: var(--shr-main-dark);
  font-weight: 700;
}

.switch {
  position: relative;
  width: 44px;
  height: 24px;
  flex: none;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  inset: 0;
  background: #9b9b9b;
  border-radius: 999px;
}
.slider::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: var(--shr-white);
  border-radius: 50%;
  transition: transform 0.15s ease;
}
.switch input:focus-visible + .slider { box-shadow: var(--shr-focus); }
.switch input:checked + .slider { background: var(--shr-main); }
.switch input:checked + .slider::before { transform: translateX(20px); }

button,
input,
select,
textarea {
  font: inherit;
}
button {
  cursor: pointer;
  border-radius: var(--shr-radius);
  border: 1px solid var(--shr-border-strong);
  padding: 9px 14px;
  background: var(--shr-white);
  color: var(--shr-text);
  font-weight: 700;
  min-height: 40px;
}
button:hover { background: #f5f5f5; }
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: none;
  box-shadow: var(--shr-focus);
  border-color: var(--shr-main);
}
button.primary {
  background: var(--shr-main);
  border-color: var(--shr-main);
  color: var(--shr-white);
}
button.primary:hover { background: var(--shr-main-dark); }
button.ghost {
  background: transparent;
  border: none;
  color: var(--shr-main);
}
button.small {
  min-height: 34px;
  padding: 6px 10px;
  font-size: 0.875rem;
}
button.danger {
  color: var(--shr-danger);
  border-color: var(--shr-danger);
  background: var(--shr-white);
}
button.danger:hover { background: var(--shr-red-bg); }
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.iconbtn {
  min-height: 32px;
  padding: 5px 8px;
  background: transparent;
}
.fab {
  position: fixed;
  right: 20px;
  bottom: calc(82px + env(safe-area-inset-bottom));
  z-index: 13;
  min-width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--shr-main);
  color: var(--shr-white);
  border: none;
  box-shadow: var(--shr-shadow);
  font-size: 1.75rem;
  line-height: 1;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  color: var(--shr-text);
  background: var(--shr-white);
  border: 1px solid var(--shr-border-strong);
  border-radius: var(--shr-radius);
  padding: 9px 12px;
  min-height: 40px;
}
textarea {
  resize: vertical;
  min-height: 96px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.875rem;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: stretch;
  justify-content: flex-end;
  background: rgba(35, 34, 31, 0.45);
}
.confirm-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(35, 34, 31, 0.45);
}
.confirm-box {
  width: min(360px, 100%);
  background: var(--shr-white);
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
  box-shadow: var(--shr-shadow);
  padding: 20px;
}
.confirm-title {
  color: var(--shr-text);
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.5;
}
.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}
.sheet {
  width: min(720px, 100%);
  max-height: 100dvh;
  background: var(--shr-white);
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 16px rgba(35, 34, 31, 0.18);
}
.sheet-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--shr-border);
}
.sheet-head strong {
  font-size: 1.125rem;
}
.sheet-body {
  overflow-y: auto;
  padding: 20px 24px calc(28px + env(safe-area-inset-bottom));
}
.field { margin-bottom: 16px; }
.field > label {
  display: block;
  color: var(--shr-text);
  font-size: 0.875rem;
  font-weight: 700;
  margin-bottom: 6px;
}
.row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.row > * { min-width: 0; }
.checkline {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
  font-weight: 700;
}
.checkline input {
  width: auto;
  min-height: 0;
}
.subhead {
  color: var(--shr-text);
  font-size: 0.875rem;
  font-weight: 700;
  margin: 4px 0 8px;
}
.summary {
  background: var(--shr-blue-bg);
  border: 1px solid #9dcfe9;
  border-radius: var(--shr-radius);
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 0.875rem;
}
.cancel-box {
  border-top: 1px solid var(--shr-border);
  margin-top: 14px;
  padding-top: 14px;
}
.cancel-box .muted {
  margin: 0 0 10px;
  font-size: 0.875rem;
}
.cancel-box .condition-row {
  margin-top: 8px;
}
.editor-section {
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
  padding: 16px;
  margin-top: 16px;
  background: #fafafa;
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.section-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.section-line > select,
.section-line > button {
  width: auto;
  flex: none;
}
.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.condition-row,
.action-row {
  display: grid;
  gap: 8px;
  align-items: start;
  padding: 12px;
  background: var(--shr-white);
  border: 1px solid var(--shr-border);
  border-radius: var(--shr-radius);
}
.condition-row {
  grid-template-columns: minmax(140px, 1.2fr) minmax(120px, 0.8fr) minmax(140px, 1fr) auto;
}
.action-row {
  grid-template-columns: minmax(120px, 0.7fr) minmax(0, 1fr) auto auto;
}
.condition-row > *,
.action-row > *,
.action-row [data-action-detail] > * {
  min-width: 0;
}
.action-row [data-action-detail] {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.action-row [data-action-detail].device-action-detail {
  grid-template-columns: minmax(140px, 0.55fr) minmax(0, 1.45fr);
}
.action-row [data-device-action-detail] {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.action-row [data-device-action-detail] > * {
  min-width: 0;
}
.row-tools {
  display: flex;
  gap: 4px;
}
.addline {
  width: 100%;
  background: var(--shr-white);
  border: 1px dashed var(--shr-border-strong);
  color: var(--shr-main);
  text-align: left;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(92px + env(safe-area-inset-bottom));
  z-index: 40;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--shr-text);
  color: var(--shr-white);
  padding: 10px 16px;
  border-radius: var(--shr-radius);
  box-shadow: var(--shr-shadow);
  font-size: 0.875rem;
}

@media (width < 752px) {
  .topbar {
    padding-left: 16px;
    padding-right: 16px;
  }
  .view {
    padding: 16px 12px 104px;
  }
  .metric-grid,
  .quick-grid,
  .device-grid,
  .form-grid,
  .condition-row,
  .action-row,
  .action-row [data-action-detail],
  .action-row [data-device-action-detail] {
    grid-template-columns: 1fr;
  }
  .rule {
    grid-template-columns: 1fr;
  }
  .rule-actions {
    grid-template-columns: auto auto auto auto;
    justify-content: start;
    justify-items: start;
  }
  .modal {
    align-items: flex-end;
  }
  .sheet {
    width: 100%;
    max-height: 94dvh;
    border-radius: 8px 8px 0 0;
  }
  .sheet-head {
    padding: 12px 14px;
  }
  .sheet-body {
    padding: 14px 14px calc(24px + env(safe-area-inset-bottom));
  }
  .section-line {
    align-items: stretch;
    flex-direction: column;
  }
  .section-line > select,
  .section-line > button,
  .condition-row [data-remove],
  .action-row [data-remove] {
    width: 100%;
  }
}
