:root {
  --bg: #eef4f8;
  --surface: #ffffff;
  --surface-soft: #f6fafc;
  --text: #102638;
  --muted: #607080;
  --line: #d7e2ea;
  --navy: #0d355d;
  --blue: #1769a6;
  --ok: #157f5b;
  --ok-bg: #e4f5ef;
  --caution: #b96800;
  --caution-bg: #fff1d8;
  --warning: #bd2f23;
  --warning-bg: #ffe5e1;
  --stopped: #52606d;
  --stopped-bg: #edf0f2;
  --shadow: 0 18px 42px rgba(13, 53, 93, 0.13);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
  overscroll-behavior-y: none;
}

body {
  margin: 0;
  min-height: 100%;
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.6;
  background:
    linear-gradient(180deg, #063777 0, #0d4d98 168px, #eef4f8 169px),
    var(--bg);
  overscroll-behavior-y: none;
}

.pull-refresh-indicator {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: 50%;
  z-index: 20;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: var(--navy);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
  font-size: 1.5rem;
  font-weight: 900;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -48px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.pull-refresh-indicator.visible {
  opacity: 1;
}

.pull-refresh-indicator.ready {
  color: #0b64c0;
  background: #ffffff;
}

.pull-refresh-indicator.refreshing {
  color: var(--navy);
  background: #ffffff;
  animation: refresh-spin 760ms linear infinite;
}

@keyframes refresh-spin {
  from {
    transform: translate(-50%, 6px) rotate(0deg);
  }

  to {
    transform: translate(-50%, 6px) rotate(360deg);
  }
}

button,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(100%, 720px);
  margin: 0 auto;
  padding: max(7px, env(safe-area-inset-top)) 14px 18px;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  padding: 9px 2px 12px;
  color: #ffffff;
}

.section-label {
  margin: 0;
  color: var(--blue);
  font-size: 0.86rem;
  font-weight: 700;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.2rem, 4.0vw, 1.4rem);
  line-height: 1.15;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 1.25rem;
  line-height: 1.25;
}

.view-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  padding: 12px 18px 0;
  background: var(--surface);
}

.view-tab {
  position: relative;
  min-height: 26px;
  padding-bottom: 6px;
  border: 0;
  border-bottom: 4px solid transparent;
  color: var(--muted);
  background: transparent;
  font-size: 0.95rem;
  font-weight: 900;
}

.view-tab.active {
  color: var(--navy);
  border-bottom-color: #0b64c0;
}

.view-tab.disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.level-mode .image-frame,
.level-mode .show-history-button,
.level-mode .history-time,
.level-mode .timeline-controls,
.level-mode .timeline-footer {
  display: none;
}

.auto-refresh {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 7px;
  margin-top: 1px;
  padding: 5px 9px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px;
  color: #ffffff;
  background: rgba(255, 255, 255, 0.12);
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}

.pulse-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #f28c28;
  box-shadow: 0 0 0 5px rgba(242, 140, 40, 0.18);
}

main {
  display: grid;
  gap: 12px;
}

.overview,
.latest-section,
.history-section {
  border: 1px solid rgba(215, 226, 234, 0.95);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.overview {
  padding: 0;
  overflow: hidden;
}

.overview-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(0, 0.92fr);
  align-items: center;
  min-height: 52px;
}

.camera-row {
  display: grid;
  gap: 2px;
  padding: 6px 14px;
  border-right: 1px solid var(--line);
}

.camera-select-label,
.time-label,
.history-time span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.camera-select {
  width: 100%;
  min-height: 28px;
  border: 0;
  border-radius: 0;
  color: var(--text);
  background: transparent;
  font-size: clamp(1.18rem, 4.7vw, 1.5rem);
  font-weight: 900;
}

.status-panel {
  display: grid;
  justify-items: center;
  gap: 3px;
  padding: 5px 10px;
  text-align: center;
}

.status-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.1;
  white-space: nowrap;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 70px;
  min-height: 28px;
  padding: 3px 9px;
  border-radius: 999px;
  font-weight: 800;
  white-space: nowrap;
}

.status-icon {
  margin-right: 7px;
  font-size: 0.75rem;
}

.status-ok {
  color: var(--ok);
  background: var(--ok-bg);
}

.status-caution {
  color: var(--caution);
  background: var(--caution-bg);
}

.status-warning,
.status-error {
  color: var(--warning);
  background: var(--warning-bg);
}

.status-stopped,
.status-checking {
  color: var(--stopped);
  background: var(--stopped-bg);
}

.latest-section,
.history-section {
  overflow: hidden;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 15px;
}

.primary-button,
.secondary-button,
.control-button,
.icon-button {
  min-height: 46px;
  border: 0;
  border-radius: 8px;
  font-weight: 800;
}

.primary-button {
  padding: 0 15px;
  color: #ffffff;
  background: var(--navy);
}

.secondary-button {
  padding: 0 15px;
  color: var(--navy);
  background: #dcebf4;
}

.image-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: #d8e4ec;
}

.image-frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #0f2233;
}

.image-frame img[src=""],
.image-frame img:not([src]) {
  visibility: hidden;
}

.image-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  color: var(--muted);
  text-align: center;
  background: repeating-linear-gradient(
    -45deg,
    #edf4f8,
    #edf4f8 12px,
    #e4edf3 12px,
    #e4edf3 24px
  );
}

.image-frame.has-image .image-fallback {
  display: none;
}

.last-updated-line {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 3px 6px;
  color: #ffffff;
  border-bottom: 0;
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
}

.time-icon {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border: 2px solid rgba(255, 255, 255, 0.82);
  border-radius: 50%;
  color: #ffffff;
  font-size: 0.9rem;
}

.error-message {
  margin: 0;
  padding: 12px 15px;
  color: var(--warning);
  background: var(--warning-bg);
  font-weight: 700;
}

.level-panel {
  padding: 12px 10px 0;
  background: var(--surface);
}

.level-summary {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0 10px;
  align-items: baseline;
  padding: 9px 12px;
  border: 1px solid #d5e4f0;
  border-radius: 8px 8px 0 0;
  background: #f5f9fc;
}

.level-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.level-summary strong {
  color: var(--navy);
  font-size: clamp(1.35rem, 6vw, 2rem);
  line-height: 1;
}

.level-summary span:last-child {
  grid-column: 1 / -1;
  color: #526475;
  font-size: 0.76rem;
  font-weight: 700;
}

.level-chart-wrap {
  position: relative;
  height: 260px;
  border: 1px solid #d5e4f0;
  border-top: 0;
  border-radius: 0 0 8px 8px;
  background: linear-gradient(180deg, #ffffff 0%, #f3f8fc 100%);
}

.level-chart-wrap canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.level-empty-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 16px;
  color: var(--muted);
  font-weight: 800;
  text-align: center;
}

.level-empty-message[hidden] {
  display: none;
}

.show-history-button {
  display: block;
  width: calc(100% - 20px);
  min-height: 46px;
  margin: 12px 10px 10px;
  border: 0;
  border-radius: 8px;
  color: #ffffff;
  background: var(--navy);
  font-weight: 900;
}

.show-history-button[hidden] {
  display: none;
}

.show-history-button:disabled {
  opacity: 0.68;
  cursor: wait;
}

.timeline-panel {
  margin: 12px 10px 10px;
  overflow: hidden;
  border: 1px solid #d5e4f0;
  border-radius: 8px;
  background: linear-gradient(180deg, #f8fbfe 0%, #eef6fb 100%);
}

.history-range {
  display: grid;
  gap: 6px;
  padding: 10px 12px 2px;
}

.history-range-label {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
}

.range-segments {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.range-segment {
  min-height: 40px;
  border: 1px solid #cfe0ed;
  border-radius: 8px;
  color: var(--navy);
  background: #edf5fb;
  font-size: 0.9rem;
  font-weight: 900;
}

.range-segment.active {
  color: #ffffff;
  border-color: var(--navy);
  background: var(--navy);
}

.history-range-description {
  margin: 0;
  color: #526475;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.35;
}

.history-time {
  display: grid;
  justify-items: center;
  gap: 7px;
  padding: 10px 12px 4px;
}

.history-time strong {
  color: #063b89;
  font-size: clamp(0.98rem, 4.6vw, 1.34rem);
  line-height: 1.15;
}

.history-time span {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 14px;
  border: 1px solid #bad3eb;
  border-radius: 999px;
  color: #0c5ba6;
  background: #dcebf9;
  font-weight: 900;
}

.timeline-controls {
  display: grid;
  grid-template-columns: 56px 1fr;
  align-items: center;
  gap: 14px;
  padding: 8px 16px 6px;
}

.round-control {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  color: #ffffff;
  background: linear-gradient(135deg, #0f5fb5, #073b82);
  box-shadow: 0 8px 18px rgba(13, 53, 93, 0.24);
  font-size: 1.35rem;
  font-weight: 900;
  margin-top: -33px;
}

.timeline-area {
  min-width: 0;
}

.history-seek {
  width: 100%;
  accent-color: #0d4fa3;
}

/* Chrome, Safari, Edge, Opera */
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; /* デフォルトの解除 */
  appearance: none;
  width: 30px;  /* つまみの幅 */
  height: 30px; /* つまみの高さ */
  border-radius: 50%; /* 丸くする */
  cursor: pointer;
}

/* Firefox */
input[type="range"]::-moz-range-thumb {
  width: 30px;  /* つまみの幅 */
  height: 30px; /* つまみの高さ */
  border-radius: 50%;
  border: 2px solid #ffffff;
  background-color: rgb(150, 194, 255);
  cursor: pointer;
}

.timeline-labels {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  min-height: 24px;
  margin-top: 3px;
  color: #4f5e6f;
  font-size: 1.38rem;
  font-weight: 800;
}

.timeline-footer {
  display: none;
  /* display: flex; */
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px 13px 86px;
  color: var(--navy);
  font-weight: 900;
}

@media (max-width: 440px) {
  .app-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  .section-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .auto-refresh {
    width: fit-content;
  }

  .primary-button,
  .secondary-button {
    width: 100%;
  }

  .view-tabs {
    padding-left: 10px;
    padding-right: 10px;
  }

  .view-tab {
    min-height: 26px;
    font-size: 0.9rem;
  }

  .last-updated-line {
    align-items: center;
    font-size: 0.62rem;
  }

  .timeline-controls {
    grid-template-columns: 52px 1fr;
    gap: 12px;
    padding-left: 14px;
    padding-right: 14px;
  }

  .round-control {
    width: 52px;
    height: 52px;
  }

  .timeline-footer {
    padding-left: 78px;
  }

  .timeline-labels {
    font-size: 0.86rem;
  }

  .history-range {
    padding-left: 10px;
    padding-right: 10px;
  }

  .range-segment {
    min-height: 38px;
    font-size: 0.84rem;
  }
}
