/* ===== OPP Helper – style ===== */

:root {
  --green: #1f8f3a;
  --yellow: #d8a400;
  --red: #c62828;
  --gray: #4a4a4a;
  --bg: #111;
  --fg: #fff;
  --fg-dim: rgba(255, 255, 255, 0.72);
  --panel: rgba(0, 0, 0, 0.28);
  --btn: #2b2b2b;
  --btn-primary: #1565c0;
  --btn-danger: #b71c1c;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:active { filter: brightness(0.85); }

/* ===== ekrany ===== */
.screen {
  display: none;
  position: fixed;
  inset: 0;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  flex-direction: column;
  background: var(--bg);
}
.screen.active { display: flex; }

/* ===== EKRAN POMIARU ===== */
.screen--measure {
  transition: background-color 150ms linear;
}
.screen--measure[data-zone="green"]  { background: var(--green); }
.screen--measure[data-zone="yellow"] { background: var(--yellow); }
.screen--measure[data-zone="red"]    { background: var(--red); }
.screen--measure[data-zone="none"]   { background: var(--gray); }

.gps-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 96px 10px 18px;
  min-height: 64px;
  font-size: clamp(16px, 2.4vh, 24px);
  font-weight: 700;
  letter-spacing: 0.02em;
  background: var(--panel);
  text-transform: uppercase;
}
.gps-dot {
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #888;
  flex: 0 0 auto;
  box-shadow: 0 0 0 3px rgba(0,0,0,0.25);
}
.gps-dot[data-q="good"] { background: #43ff7a; }
.gps-dot[data-q="fair"] { background: #ffd54a; }
.gps-dot[data-q="poor"] { background: #ff7a3d; }
.gps-dot[data-q="lost"] { background: #ff2b2b; animation: blink 0.6s steps(2, start) infinite; }
.gps-dot[data-q="none"] { background: #888; }
@keyframes blink { to { visibility: hidden; } }
.gps-text { flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gps-gaps { flex: 0 0 auto; opacity: 0.9; font-weight: 600; }
.gps-gaps:empty { display: none; }

.btn-icon {
  position: absolute;
  top: calc(var(--safe-top) + 4px);
  right: calc(var(--safe-right) + 8px);
  width: 80px; height: 80px;
  border-radius: 50%;
  background: rgba(0,0,0,0.35);
  display: flex; align-items: center; justify-content: center;
  z-index: 2;
}
.btn-icon svg { width: 52px; height: 52px; fill: var(--fg); }
.screen--measure[data-state="measuring"] .btn-settings { display: none; }

.measure-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  min-height: 0;
}
.big-label {
  font-size: clamp(16px, 2.6vh, 28px);
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--fg-dim);
}
.big-value {
  font-size: clamp(96px, 26vh, 260px);
  font-weight: 800;
  line-height: 0.95;
  letter-spacing: -0.03em;
  text-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.big-unit {
  font-size: clamp(18px, 3vh, 32px);
  font-weight: 700;
  color: var(--fg-dim);
  margin-top: 6px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(100%, 720px);
  margin-top: clamp(14px, 3vh, 36px);
}
.stat {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 10px 8px;
  text-align: center;
}
.stat-label {
  font-size: clamp(12px, 1.8vh, 18px);
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--fg-dim);
}
.stat-value {
  font-size: clamp(24px, 4.6vh, 52px);
  font-weight: 800;
  line-height: 1.1;
  white-space: nowrap;
}
.stat-value small { font-size: 0.45em; font-weight: 700; color: var(--fg-dim); }

/* w stanie gotowości: brak "aktualna" (jest dużą liczbą) i brak czasu */
.screen--measure[data-state="ready"] .stat--current,
.screen--measure[data-state="ready"] .stat--time { display: none; }
.screen--measure[data-state="measuring"] .stats { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 560px) {
  .screen--measure[data-state="measuring"] .stats { grid-template-columns: repeat(2, 1fr); }
  .screen--measure[data-state="ready"] .stats { grid-template-columns: repeat(2, 1fr); }
}

.btn-main {
  flex: 0 0 auto;
  height: max(26vh, 150px);
  margin: 10px 12px calc(10px + var(--safe-bottom));
  border-radius: 28px;
  font-size: clamp(44px, 9vh, 96px);
  font-weight: 900;
  letter-spacing: 0.08em;
  background: rgba(0,0,0,0.42);
  color: var(--fg);
  box-shadow: inset 0 0 0 4px rgba(255,255,255,0.35);
}
.screen--measure[data-state="measuring"] .btn-main {
  background: rgba(0,0,0,0.6);
  box-shadow: inset 0 0 0 6px rgba(255,255,255,0.7);
}
.btn-main:disabled { opacity: 0.5; }

/* ===== layout poziomy na ekranie pomiaru ===== */
@media (orientation: landscape) and (max-height: 560px) {
  .screen--measure {
    display: none;
    grid-template-columns: 1fr minmax(220px, 34vw);
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "bar bar"
      "main btn";
  }
  .screen--measure.active { display: grid; }
  .gps-bar { grid-area: bar; }
  .measure-main { grid-area: main; padding: 0 12px; }
  .btn-main {
    grid-area: btn;
    height: auto;
    margin: 10px calc(12px + var(--safe-right)) calc(10px + var(--safe-bottom)) 0;
    font-size: clamp(36px, 10vh, 72px);
  }
  .btn-icon {
    top: calc(var(--safe-top) + 4px);
    right: calc(var(--safe-right) + max(220px, 34vw) + 10px);
    width: 56px; height: 56px;
  }
  .btn-icon svg { width: 36px; height: 36px; }
  .big-value { font-size: clamp(80px, 34vh, 200px); }
  .stats { margin-top: 8px; gap: 8px; }
  .stat { padding: 6px 6px; }
}

/* ===== wspólne: ustawienia, historia, szczegóły ===== */
.screen-header {
  padding: 14px 20px;
  background: rgba(255,255,255,0.06);
}
.screen-header h1 {
  margin: 0;
  font-size: clamp(22px, 3.4vh, 36px);
  letter-spacing: 0.1em;
  font-weight: 800;
}

.screen-footer {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 12px 12px calc(12px + var(--safe-bottom));
}
.screen-footer > .btn-primary:only-child { grid-column: 1 / -1; }
.app-info {
  grid-column: 1 / -1;
  text-align: center;
  font-size: 13px;
  color: var(--fg-dim);
  min-height: 1em;
}

.btn-primary, .btn-secondary, .btn-danger {
  min-height: 96px;
  border-radius: var(--radius);
  font-size: clamp(26px, 4vh, 40px);
  font-weight: 800;
  letter-spacing: 0.08em;
}
.btn-primary { background: var(--btn-primary); }
.btn-secondary { background: var(--btn); }
.btn-danger { background: var(--btn-danger); }

/* ===== USTAWIENIA ===== */
.settings-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vh, 32px);
  padding: 16px;
}
.setting-label {
  font-size: clamp(16px, 2.6vh, 26px);
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--fg-dim);
}
.target-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  width: min(100%, 720px);
}
.btn-step {
  min-height: 120px;
  border-radius: var(--radius);
  background: var(--btn);
  font-size: clamp(36px, 6vh, 64px);
  font-weight: 800;
}
.target-value {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 130px;
  font-size: clamp(72px, 14vh, 140px);
  font-weight: 900;
  line-height: 1;
}
.target-value small {
  font-size: clamp(16px, 2.4vh, 24px);
  font-weight: 700;
  color: var(--fg-dim);
  margin-top: 4px;
}
.presets {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(100%, 720px);
}
.btn-preset {
  min-height: 84px;
  border-radius: var(--radius);
  background: var(--btn);
  font-size: clamp(26px, 4.4vh, 44px);
  font-weight: 800;
}
.btn-preset.active {
  background: var(--btn-primary);
  box-shadow: inset 0 0 0 4px rgba(255,255,255,0.5);
}
@media (orientation: landscape) and (max-height: 560px) {
  .presets { grid-template-columns: repeat(6, 1fr); }
  .btn-step { min-height: 84px; }
  .btn-primary, .btn-secondary, .btn-danger { min-height: 72px; }
}

/* ===== HISTORIA ===== */
.history-main {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px;
  min-height: 0;
}
.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 2px 14px;
  align-items: center;
  min-height: 84px;
  padding: 12px 16px;
  background: rgba(255,255,255,0.08);
  border-radius: var(--radius);
  width: 100%;
  text-align: left;
}
.history-item:active { background: rgba(255,255,255,0.18); }
.history-date { font-size: clamp(18px, 2.6vh, 24px); font-weight: 800; min-width: 0; }
.history-range { display: block; font-size: 0.8em; font-weight: 700; color: var(--fg-dim); }
.history-meta { font-size: clamp(14px, 2vh, 18px); color: var(--fg-dim); }
.history-avg {
  grid-row: 1 / span 2;
  grid-column: 2;
  font-size: clamp(34px, 5vh, 52px);
  font-weight: 900;
  padding: 4px 14px;
  border-radius: 12px;
  min-width: 110px;
  text-align: center;
}
.history-avg small { font-size: 0.4em; font-weight: 700; margin-left: 2px; }
.zone-green  { background: var(--green); }
.zone-yellow { background: var(--yellow); }
.zone-red    { background: var(--red); }
.history-empty {
  text-align: center;
  padding: 48px 16px;
  font-size: 22px;
  color: var(--fg-dim);
}
.history-empty.hidden { display: none; }

/* ===== SZCZEGÓŁY ===== */
.detail-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.detail-summary {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  padding: 10px 12px;
  align-items: stretch;
}
.detail-avg {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 18px;
  border-radius: var(--radius);
  min-width: 150px;
}
.detail-avg-value { font-size: clamp(48px, 8vh, 80px); font-weight: 900; line-height: 1; }
.detail-avg-unit { font-size: 14px; font-weight: 700; color: var(--fg-dim); margin-top: 4px; }
.detail-avg-limit { font-size: 14px; font-weight: 700; color: var(--fg-dim); }
.detail-grid {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  font-size: clamp(14px, 2vh, 18px);
  align-content: center;
}
.detail-grid dt { color: var(--fg-dim); font-weight: 700; white-space: nowrap; }
.detail-grid dd { margin: 0; font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 560px) {
  .detail-summary { grid-template-columns: 1fr; }
  .detail-avg { flex-direction: row; gap: 14px; padding: 8px 14px; }
  .detail-avg-value { font-size: clamp(40px, 6vh, 64px); }
}
.detail-map {
  flex: 1 1 auto;
  min-height: 200px;
  margin: 0 12px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #222;
  position: relative;
  user-select: none;
}
.map-placeholder {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--fg-dim);
  font-size: 18px;
  text-align: center;
  padding: 20px;
}
.leaflet-container { background: #2a2a2a; font: inherit; }
@media (orientation: landscape) and (max-height: 560px) {
  .detail-main { flex-direction: row; }
  .detail-summary {
    grid-template-columns: 1fr;
    width: 42vw;
    flex: 0 0 auto;
    overflow-y: auto;
    align-content: start;
    min-height: 0;
  }
  .detail-avg { flex-direction: row; gap: 14px; padding: 6px 12px; }
  .detail-avg-value { font-size: clamp(36px, 9vh, 56px); }
  .detail-map { margin: 10px 12px 10px 0; }
}

/* ===== DIALOG ===== */
.confirm {
  border: 0;
  border-radius: 22px;
  background: #1e1e1e;
  color: var(--fg);
  padding: 24px;
  width: min(92vw, 520px);
}
.confirm::backdrop { background: rgba(0,0,0,0.7); }
.confirm p { margin: 0 0 20px; font-size: 26px; font-weight: 700; text-align: center; }
.confirm-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ===== dostępność ===== */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
