:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --accent2: #5ab4e0;
  --text: #e0e0e0;
  --text-sub: #a0a0b0;
  --ok: #4caf50;
  --error: #e05a5a;
  --warn: #f5a623;
  --radius: 8px;

  --surface-hover: #1e2a4a;
  --border: #2a3656;
  --text-muted: #a0a0b0;
  --space-1: 0.5rem;
  --space-2: 1rem;
  --space-3: 1.5rem;
  --space-4: 2rem;
  --shadow-card: 0 1px 3px rgba(0,0,0,0.3);
  --focus-ring: 0 0 0 3px rgba(90, 180, 224, 0.5);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Helvetica Neue', Arial, sans-serif;
  font-size: 15px;
}

nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
}
.nav-brand { font-size: 1.1rem; font-weight: bold; color: var(--accent); }
nav a { color: var(--text-sub); text-decoration: none; }
nav a:hover { color: var(--text); }

main { max-width: 900px; margin: 0 auto; padding: 1.5rem 1rem; }

h1 { font-size: 1.4rem; margin-bottom: 1.2rem; color: var(--text); }
h2 { font-size: 1.1rem; margin-bottom: 0.8rem; color: var(--text); }

.section { margin-top: 2rem; }

/* Status grid */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  border: 1px solid var(--surface2);
}
.card-label { font-size: 0.8rem; color: var(--text-sub); margin-bottom: 0.3rem; }
.card-value { font-size: 2rem; font-weight: bold; line-height: 1.1; }
.card-sub { font-size: 0.8rem; color: var(--text-sub); margin-top: 0.2rem; }

.status-on   { color: var(--ok); }
.status-off  { color: var(--text-sub); }
.status-warn { color: var(--warn); }
.status-cool { color: var(--accent2); }

/* センサー個別グリッド（minmax 小さめ）*/
.sensor-detail-grid {
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  margin-top: 0.5rem;
}
.sensor-temp { font-size: 1.5rem; }

/* stale センサー */
.card-stale { opacity: 0.55; }
.stale-badge {
  display: inline-block;
  font-size: 0.7rem;
  color: var(--warn);
  border: 1px solid var(--warn);
  border-radius: 3px;
  padding: 0 3px;
  line-height: 1.4;
  vertical-align: middle;
}

/* Alerts */
.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}
.alert-error { background: rgba(224,90,90,0.15); border: 1px solid var(--error); }

/* Buttons */
.btn {
  display: inline-block;
  padding: 0.5rem 1.2rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: opacity 0.2s;
}
.btn:hover { opacity: 0.85; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface2); color: var(--text); }
.btn-on { background: var(--ok); color: #fff; }
.btn-off { background: var(--surface2); color: var(--text-sub); }
.btn-danger { background: rgba(224,90,90,0.2); color: var(--error); }
.btn-small { padding: 0.3rem 0.7rem; font-size: 0.8rem; }
.btn-active { background: var(--accent2); color: var(--bg); }

/* Manual grid */
.manual-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
}
.manual-grid > div { display: flex; flex-direction: column; gap: 0.3rem; }
.manual-grid label { font-size: 0.8rem; color: var(--text-sub); }
.manual-grid input, .manual-grid select {
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  min-width: 90px;
}
.send-btn-wrap { padding-bottom: 0; }

/* Chart */
.chart-controls { margin-bottom: 0.5rem; display: flex; gap: 0.5rem; }
.chart-wrap { background: var(--surface); border-radius: var(--radius); padding: 1rem; max-height: 320px; }

/* Table */
table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
th, td { padding: 0.6rem 0.8rem; text-align: left; border-bottom: 1px solid var(--surface2); font-size: 0.9rem; }
th { background: var(--surface2); font-weight: 600; }
tr:last-child td { border-bottom: none; }
input[type="time"], input[type="number"], select {
  background: var(--bg);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 0.3rem 0.5rem;
  border-radius: 4px;
  font-size: 0.9rem;
}

/* Forms */
.form-row { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.8rem; }
.form-row label { font-size: 0.85rem; color: var(--text-sub); }
.form-row select { width: 100%; }
.form-actions { display: flex; gap: 0.8rem; margin-top: 1rem; flex-wrap: wrap; }
.help-text { color: var(--text-sub); font-size: 0.9rem; margin-bottom: 1rem; }

/* Messages */
.msg { margin-top: 0.6rem; font-size: 0.85rem; min-height: 1.2em; }
.msg-ok { color: var(--ok); }
.msg-error { color: var(--error); }

hr { border: none; border-top: 1px solid var(--surface2); margin: 1.5rem 0; }
code { background: var(--surface2); padding: 0.1em 0.4em; border-radius: 4px; font-family: monospace; }

.toggle-row { display: flex; gap: 1rem; align-items: center; }

/* 外出/帰宅ボタン */
.away-home-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.away-home-row .btn { flex: 1; min-width: 180px; padding: 0.8rem 1rem; font-size: 0.95rem; text-align: center; }
.btn-warn { background: rgba(245,166,35,0.15); color: var(--warn); border: 1px solid var(--warn); }
.btn-warn-active { background: var(--warn); color: var(--bg); font-weight: bold; }
.btn-ok { background: rgba(76,175,80,0.15); color: var(--ok); border: 1px solid var(--ok); }
.btn-ok-active { background: var(--ok); color: #fff; font-weight: bold; }

/* 制御方式バッジ / 履歴リンク */
.ctrl-type-badge {
  font-size: 0.8rem;
  color: var(--text-muted);
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 0.2rem 0.6rem;
}
.history-link {
  font-size: 0.8rem;
  color: var(--accent2);
  text-decoration: none;
  margin-left: 0.8rem;
  font-weight: normal;
}
.history-link:hover { text-decoration: underline; }

/* セットアップ: センサー複数選択 */
.sensor-check-list { display: flex; flex-direction: column; gap: 0.4rem; max-height: 220px; overflow-y: auto;
  background: var(--bg); border: 1px solid var(--surface2); border-radius: var(--radius); padding: 0.5rem 0.8rem; }
.sensor-check-list label { display: flex; align-items: center; gap: 0.5rem; font-size: 0.9rem; cursor: pointer; }
.sensor-check-list input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent2); }

/* アクセシビリティ: フォーカスリング */
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius);
}

/* トースト通知 */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.75rem 1.2rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  box-shadow: var(--shadow-card);
  z-index: 9999;
  display: none;
  max-width: 320px;
}
.toast.toast-ok { border-color: var(--ok); color: var(--ok); }
.toast.toast-error { border-color: var(--error); color: var(--error); }
.toast.visible { display: block; }

/* テーブル横スクロール用ラッパ */
.table-scroll { overflow-x: auto; }

/* レスポンシブ */
@media (max-width: 720px) {
  main { padding: 1rem 0.75rem; }
  nav { flex-wrap: wrap; gap: 0.5rem; }
  .status-grid { grid-template-columns: 1fr; }
  .manual-grid { flex-direction: column; }
  .away-home-row { flex-direction: column; }
  .away-home-row .btn { width: 100%; min-width: unset; }
  .toggle-row { flex-direction: column; align-items: stretch; }
  .chart-wrap { max-height: 240px; }
  table { font-size: 0.85rem; }
}

@media (min-width: 1100px) {
  main { max-width: 1100px; }
}

/* 履歴ページ: 大きめチャート */
.chart-wrap-lg {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem;
  height: 420px;
}
.chart-wrap-lg canvas { height: 100% !important; }

/* 制御方式バッジ（履歴テーブル内） */
.badge-ctrl {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: bold;
  border-radius: 3px;
  padding: 0 4px;
  line-height: 1.5;
}
.badge-p { background: rgba(90,180,224,0.25); color: var(--accent2); }
.badge-h { background: rgba(245,166,35,0.2);  color: var(--warn); }

/* テーブル内テキスト色 */
.ok-text    { color: var(--ok); }
.error-text { color: var(--error); }

@media (max-width: 720px) {
  .chart-wrap-lg { height: 260px; }
}
