:root {
  color-scheme: dark;
  --bg: #111316;
  --panel: #191d22;
  --panel-2: #21262d;
  --text: #f2f4f7;
  --muted: #9aa4af;
  --accent: #63c5b8;
  --line: #343b44;
  --danger: #ff7a70;
  --warn: #e6c266;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

button {
  color: inherit;
  font: inherit;
}

.topbar {
  min-height: 50px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(17, 19, 22, 0.94);
  position: sticky;
  top: 0;
  z-index: 5;
  backdrop-filter: blur(16px);
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: 18px;
  line-height: 1.1;
  font-weight: 700;
}

#status {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.layout {
  width: 100%;
  height: 100%;
  min-height: 0;
  margin: 0;
  padding: 0;
}

.view[hidden] {
  display: none !important;
}

.view-grid {
  height: 100%;
  min-height: 0;
}

.camera-grid {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 2px;
  background: #050607;
}

.camera-tile {
  min-width: 0;
  min-height: 0;
  border: 0;
  background: #050607;
  border-radius: 0;
  overflow: hidden;
  position: relative;
}

.camera-tile.offline {
  outline: 2px solid #604a48;
  outline-offset: -2px;
}

.camera-button {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: #050607;
  cursor: pointer;
  text-align: left;
}

.camera-button iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  pointer-events: none;
  background: #000;
}

.camera-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 34px;
  padding: 14px 10px 8px;
  border-top: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.82), rgba(0, 0, 0, 0));
  color: var(--text);
  font-size: 14px;
  line-height: 1.2;
  pointer-events: none;
}

.camera-name span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.camera-name small {
  flex: 0 0 auto;
  color: var(--accent);
  font-size: 12px;
}

.camera-tile.offline .camera-name small {
  color: var(--warn);
}

.view-detail {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  grid-template-rows: auto 1fr;
  background: #050607;
}

.detail-open .topbar {
  display: none;
}

.detail-toolbar {
  min-height: 62px;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 10px max(10px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) minmax(128px, auto);
  gap: 10px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: rgba(17, 19, 22, 0.96);
}

.icon-button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  font-size: 34px;
  line-height: 1;
  cursor: pointer;
}

.detail-meta {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.detail-meta strong,
.detail-meta span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-meta span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.1;
}

.quality {
  display: grid;
  grid-template-columns: repeat(2, minmax(58px, 1fr));
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
}

.quality-button {
  min-height: 36px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}

.quality-button.active {
  background: var(--accent);
  color: #07110f;
  font-weight: 700;
}

#detail-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: #000;
  min-height: 0;
}

@media (max-width: 760px) {
  .detail-toolbar {
    grid-template-columns: 44px minmax(0, 1fr) auto;
  }

  .quality {
    grid-column: 2 / -1;
  }
}

@media (max-width: 560px) {
  body {
    grid-template-rows: minmax(0, 1fr);
    overflow: hidden;
  }

  .topbar {
    display: none;
  }

  h1 {
    font-size: 15px;
  }

  #status {
    margin-top: 1px;
    font-size: 10px;
  }

  .view-grid {
    overflow: hidden;
    overscroll-behavior: contain;
    background: #050607;
  }

  .camera-grid {
    height: 100%;
    min-height: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(2, auto);
    align-content: start;
    gap: 2px;
    padding: 2px;
  }

  .camera-tile {
    aspect-ratio: 16 / 9;
    min-height: auto;
  }

  .camera-name {
    min-height: 26px;
    padding: 10px 8px 6px;
    font-size: 12px;
  }

  .camera-name small {
    font-size: 10px;
  }

  .detail-toolbar {
    min-height: 52px;
    grid-template-columns: 38px minmax(0, 1fr) auto;
    gap: 6px;
    padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) 6px max(6px, env(safe-area-inset-left));
  }

  .icon-button {
    width: 38px;
    height: 38px;
    font-size: 28px;
  }

  .quality {
    gap: 4px;
    padding: 3px;
  }

  .quality-button {
    min-height: 30px;
    font-size: 12px;
  }

}

@media (max-height: 560px) and (orientation: landscape) {
  body {
    grid-template-rows: minmax(0, 1fr);
    overflow: hidden;
  }

  .topbar {
    display: none;
  }

  .view-grid {
    overflow: hidden;
    overscroll-behavior: contain;
    background: #050607;
  }

  .camera-grid {
    height: 100%;
    min-height: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    gap: 2px;
    padding: 2px;
  }

  .camera-tile {
    min-height: 0;
    aspect-ratio: auto;
  }

  .camera-name {
    min-height: 24px;
    padding: 9px 8px 5px;
    font-size: 12px;
  }

  .camera-name small {
    font-size: 10px;
  }

  .detail-toolbar {
    min-height: 48px;
    grid-template-columns: 38px minmax(0, 1fr) auto;
    gap: 6px;
    padding: max(5px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 5px max(8px, env(safe-area-inset-left));
  }

  .icon-button {
    width: 38px;
    height: 38px;
    font-size: 28px;
  }

  .quality {
    gap: 4px;
    padding: 3px;
  }

  .quality-button {
    min-height: 30px;
    font-size: 12px;
  }
}
