:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --text: #333333;
  --text-strong: #1a1a1a;
  --muted: #666666;
  --faint: #999999;
  --border: #e0e0e0;
  --grid: #e6e6e6;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

  --accent: #1976d2;
  --accent-contrast: #ffffff;
  --btn: #e0e0e0;
  --btn-hover: #d0d0d0;
  --highlight: #e3f2fd;

  --power-on: #e8f5e9;
  --power-on-border: #c8e6c9;
  --off: #e65100;
  --off-strong: #bf360c;
  --off-text: #ffffff;

  --status-on-bg: #e8f5e9;
  --status-on-text: #1b5e20;
  --status-off-bg: #fbe9e7;
  --status-off-text: #bf360c;

  --notice-bg: #fff8e1;
  --notice-border: #ffe082;
  --notice-text: #6d4c00;

  --telegram: #2481cc;

  --label-w: 100px; /* width of the group column */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1c1f24;
    --text: #e3e3e3;
    --text-strong: #ffffff;
    --muted: #a0a4ab;
    --faint: #7d828a;
    --border: #2e333a;
    --grid: #2a2f36;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5);

    --accent: #64b5f6;
    --accent-contrast: #0d1b2a;
    --btn: #2a2f36;
    --btn-hover: #353b44;
    --highlight: #18293b;

    --power-on: #16301d;
    --power-on-border: #23452c;
    --off: #f2711c;
    --off-strong: #ff8a3d;
    --off-text: #1a0f00;

    --status-on-bg: #16301d;
    --status-on-text: #a5d6a7;
    --status-off-bg: #3b1f14;
    --status-off-text: #ffab91;

    --notice-bg: #3a3016;
    --notice-border: #6b5a1f;
    --notice-text: #ffe08a;
  }
}

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

[hidden] {
  display: none !important;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  padding: 20px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: inherit;
}

header {
  text-align: center;
  margin-bottom: 16px;
  flex-shrink: 0;
}

header h1 {
  font-size: 1.5rem;
  margin-bottom: 10px;
  color: var(--text-strong);
}

.metadata {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 4px 20px;
  font-size: 0.9rem;
  color: var(--muted);
}

.metadata strong {
  color: var(--text);
}

.notice {
  max-width: 720px;
  margin: 12px auto 0;
  padding: 8px 12px;
  background: var(--notice-bg);
  border: 1px solid var(--notice-border);
  border-radius: 6px;
  color: var(--notice-text);
  font-size: 0.85rem;
}

/* Day tabs */
.day-tabs {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.tab {
  padding: 10px 24px;
  border: none;
  background: var(--btn);
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  transition: background 0.2s ease;
}

.tab:hover {
  background: var(--btn-hover);
}

.tab.active {
  background: var(--accent);
  color: var(--accent-contrast);
}

/* Group selector */
.group-selector {
  background: var(--surface);
  padding: 14px 20px;
  border-radius: 8px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}

.group-selector-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  min-height: 30px;
}

.group-selector-head p {
  font-size: 0.9rem;
  color: var(--muted);
}

.group-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.group-btn {
  padding: 8px 16px;
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.group-btn:hover {
  border-color: var(--accent);
}

.group-btn.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.group-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-telegram {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 6px;
  background: var(--telegram);
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  transition: filter 0.2s ease;
}

.btn-telegram:hover {
  filter: brightness(1.1);
}

.btn-telegram:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-clear {
  padding: 6px 14px;
  border: none;
  background: var(--btn);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--muted);
  transition: background 0.2s ease;
}

.btn-clear:hover {
  background: var(--btn-hover);
  color: var(--text);
}

.tab:focus-visible,
.group-btn:focus-visible,
.btn-clear:focus-visible,
.scroll-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Status of the selected groups: what happens now */
.group-status {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.group-status:empty {
  display: none;
}

.group-status .hint {
  font-size: 0.85rem;
  color: var(--faint);
}

.status-card {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 10px;
  padding: 8px 12px;
  border-radius: 6px;
  border-left: 4px solid;
  font-size: 0.9rem;
}

.status-card.on {
  background: var(--status-on-bg);
  border-color: var(--status-on-text);
  color: var(--status-on-text);
}

.status-card.off {
  background: var(--status-off-bg);
  border-color: var(--off);
  color: var(--status-off-text);
}

.status-group {
  font-weight: 700;
}

.status-text {
  font-weight: 600;
}

.status-detail {
  font-size: 0.8rem;
  opacity: 0.85;
}

/* Schedule wrapper with scroll buttons */
.schedule-wrapper {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
}

.scroll-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 20;
  width: 36px;
  height: 36px;
  border: none;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 1.2rem;
  border-radius: 50%;
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}

.scroll-btn.visible {
  opacity: 0.7;
  visibility: visible;
}

.scroll-btn.visible:hover {
  opacity: 1;
}

.scroll-left {
  left: calc(var(--label-w) + 8px);
}

.scroll-right {
  right: 8px;
}

/* Schedule container */
.schedule-container {
  background: var(--surface);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: auto;
  flex: 1;
  min-height: 0;
  cursor: grab;
}

.schedule-container.dragging {
  cursor: grabbing;
  user-select: none;
}

/* Timeline header: stays on top while the rows scroll */
.timeline-header {
  display: grid;
  grid-template-columns: var(--label-w) 1fr;
  border-bottom: 1px solid var(--border);
  width: max(800px, 100%);
  position: sticky;
  top: 0;
  z-index: 15;
  background: var(--surface);
}

.header-label {
  padding: 10px;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--muted);
  border-right: 1px solid var(--border);
  position: sticky;
  left: 0;
  z-index: 16;
  background: var(--surface);
}

.hours-row {
  display: flex;
  position: relative;
}

/* The hour label sits right after its grid line, so "14" marks 14:00 */
.hour-mark {
  flex: 1;
  padding: 10px 0 10px 3px;
  font-size: 0.75rem;
  color: var(--faint);
  border-left: 1px solid var(--grid);
}

.hour-mark:first-child {
  border-left: none;
}

.now-badge {
  position: absolute;
  top: 50%;
  left: calc(var(--now, 0) * 100%);
  transform: translate(-50%, -50%);
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 0.75rem;
  font-weight: 600;
  pointer-events: none;
}

/* Groups timeline */
.groups-timeline {
  width: max(800px, 100%);
  position: relative;
}

.group-row {
  display: grid;
  grid-template-columns: var(--label-w) 1fr;
  border-bottom: 1px solid var(--grid);
}

.group-row:last-child {
  border-bottom: none;
}

.group-row.highlighted {
  background: var(--highlight);
}

/* Dim the content, not the row: the sticky label must stay opaque over the scrolled blocks */
.group-row.dimmed .group-timeline,
.group-row.dimmed .group-label > span {
  opacity: 0.35;
}

/* Group column: stays on the left while the timeline scrolls */
.group-label {
  padding: 8px 10px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: sticky;
  left: 0;
  z-index: 12;
  background: var(--surface);
}

.group-row.highlighted .group-label {
  background: var(--highlight);
  box-shadow: inset 4px 0 0 var(--accent);
}

.group-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.group-total {
  font-size: 0.7rem;
  color: var(--faint);
  white-space: nowrap;
}

.group-timeline {
  position: relative;
  height: 50px;
  background: var(--power-on);
}

/* Hour grid lines */
.group-timeline::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    90deg,
    var(--grid) 0px,
    var(--grid) 1px,
    transparent 1px,
    transparent calc(100% / 24)
  );
  opacity: 0.8;
  pointer-events: none;
}

/* Outage blocks */
.outage-block {
  position: absolute;
  top: 6px;
  bottom: 6px;
  background: var(--off);
  border-radius: 3px;
  cursor: default;
  transition: background 0.2s ease, box-shadow 0.2s ease;
  container-type: inline-size;
  overflow: hidden;
}

.outage-block:hover {
  background: var(--off-strong);
  box-shadow: 0 2px 8px rgba(230, 81, 0, 0.4);
  z-index: 2;
}

.group-row.highlighted .outage-block {
  background: var(--off-strong);
}

/* Outage label - shown only when block is wide enough */
.outage-label {
  display: none;
  color: var(--off-text);
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

@container (min-width: 100px) {
  .outage-label {
    display: block;
  }
}

/* Current time indicator */
.current-time-line {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(var(--label-w) + (100% - var(--label-w)) * var(--now, 0));
  width: 0;
  border-left: 2px dashed var(--accent);
  z-index: 10;
  pointer-events: none;
}

/* No data message */
.no-data {
  padding: 40px;
  text-align: center;
  color: var(--faint);
  font-size: 1rem;
}

/* Legend */
.legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px 20px;
  margin-top: 12px;
  font-size: 0.8rem;
  color: var(--muted);
  flex-shrink: 0;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-color {
  width: 20px;
  height: 12px;
  border-radius: 3px;
}

.legend-color.power-on {
  background: var(--power-on);
  border: 1px solid var(--power-on-border);
}

.legend-color.power-off {
  background: var(--off);
}

.legend-color.now {
  width: 0;
  height: 14px;
  border-radius: 0;
  border-left: 2px dashed var(--accent);
}

/* Small or short screens: the page scrolls as a whole, the timeline scrolls only sideways */
@media (max-width: 768px), (max-height: 700px) {
  body {
    height: auto;
    min-height: 100dvh;
    overflow: auto;
  }

  .schedule-wrapper {
    flex: none;
  }

  .schedule-container {
    flex: none;
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
  }
}

/* Mobile */
@media (max-width: 768px) {
  :root {
    --label-w: 64px;
  }

  body {
    padding: 12px;
  }

  header h1 {
    font-size: 1.15rem;
  }

  .metadata {
    flex-direction: column;
    gap: 2px;
    font-size: 0.85rem;
  }

  .tab {
    flex: 1;
    max-width: 180px;
  }

  .group-selector {
    padding: 12px;
  }

  .group-buttons {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
  }

  .group-btn {
    padding: 8px 0;
  }

  .status-card {
    flex: 1 1 100%;
  }

  .schedule-container {
    cursor: auto;
  }

  .header-label {
    padding: 10px 8px;
  }

  .group-label {
    padding: 6px 8px;
  }

  .group-timeline {
    height: 44px;
  }
}
