:root {
  --bg: #000;
  --fg: #fff;
  --accent-red: #ff1f2e;
  --card-bg: #0a0a0a;
  --card-border: #1a1a1a;
  --hinge: #000;
  --muted: #3a3a3a;
  --muted-2: #666;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  height: 100%;
  overscroll-behavior: none;
}

.app {
  width: 100vw;
  height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

/* ──────────── FLOATING FULLSCREEN BUTTON ──────────── */
.fs-btn {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--card-border);
  background: rgba(12,12,12,0.85);
  color: var(--fg);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  z-index: 100;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
}
.fs-btn:active { background: rgba(26,26,26,0.95); }

.fs-error {
  position: fixed;
  top: 72px;
  right: 16px;
  max-width: 280px;
  padding: 12px 14px;
  background: rgba(180, 40, 40, 0.95);
  color: #fff;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.4;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

/* ──────────── KONFIGURACJA ──────────── */
.config {
  width: 100%;
  height: 100%;
  max-width: 640px;
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
}
.config-inner {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 28px;
  padding: 24px 0;
  margin: auto;
}
.config-title {
  margin: 0;
  font-size: clamp(48px, 10vw, 88px);
  font-weight: 900;
  letter-spacing: -0.04em;
  line-height: 0.9;
  text-align: center;
}
.config-sub {
  text-align: center;
  color: var(--muted-2);
  font-size: 14px;
  letter-spacing: 0.02em;
  margin-top: -16px;
}
.config-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 24px;
  border: 1px solid var(--card-border);
  border-radius: 16px;
  background: #060606;
}
.config-label {
  font-size: 11px;
  letter-spacing: 0.2em;
  color: var(--muted-2);
  font-weight: 700;
}
.config-time {
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  color: var(--fg);
  border: 0;
  font-family: inherit;
  font-size: clamp(56px, 14vw, 96px);
  font-weight: 800;
  letter-spacing: -0.02em;
  text-align: center;
  font-variant-numeric: tabular-nums;
  outline: none;
  width: 100%;
  padding: 0;
}
.config-time::-webkit-calendar-picker-indicator {
  filter: invert(1);
  opacity: 0.4;
}

.config-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.config-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  background: #060606;
  border: 1px solid var(--card-border);
  border-radius: 12px;
}
.config-field label {
  font-size: 10px;
  letter-spacing: 0.18em;
  color: var(--muted-2);
  font-weight: 700;
}
.config-steppers {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.config-steppers button {
  width: 34px; height: 34px;
  border: 1px solid var(--card-border);
  background: #0e0e0e;
  color: var(--fg);
  font-size: 18px;
  font-weight: 700;
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}
.config-steppers button:active { background: #1a1a1a; }
.config-steppers span {
  font-size: 28px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  min-width: 40px;
  text-align: center;
}

.config-preview {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 18px;
  border: 1px dashed #222;
  border-radius: 12px;
  font-size: 13px;
}
.config-preview-row {
  display: flex;
  justify-content: space-between;
  color: var(--muted-2);
}
.config-preview-row b {
  color: var(--fg);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.config-start {
  appearance: none;
  border: 0;
  padding: 18px;
  background: var(--fg);
  color: var(--bg);
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.2em;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.config-start:active { transform: scale(0.98); }

/* ──────────── TIMER SCREEN ──────────── */
.timer-screen {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  padding: clamp(6px, 1vmin, 16px);
  gap: clamp(4px, 0.8vmin, 10px);
}

/* Górny pasek — 3 kolumny: lewo (Q), środek (odliczanie), prawo (koniec) */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 8px;
  gap: 12px;
}
.topbar-left { display: flex; align-items: center; gap: 18px; justify-self: start; }
.topbar-center { justify-self: center; }
.topbar-right { justify-self: end; display: flex; align-items: baseline; gap: 10px; color: var(--muted-2); }

.countdown-lbl {
  font-size: clamp(14px, 2.5vmin, 22px);
  letter-spacing: 0.2em;
  font-weight: 800;
  color: var(--fg);
  white-space: nowrap;
}
.seg-label {
  font-size: clamp(14px, 2.5vmin, 22px);
  font-weight: 800;
  letter-spacing: 0.2em;
  color: var(--fg);
}
.quarter-dots { display: flex; gap: 8px; }
.quarter-dots .dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #222;
}
.quarter-dots .dot.done { background: var(--muted-2); }
.quarter-dots .dot.active { background: var(--fg); box-shadow: 0 0 0 4px rgba(255,255,255,0.1); }

.end-time-label {
  font-size: 11px;
  letter-spacing: 0.2em;
  font-weight: 700;
}
.wake-indicator {
  font-size: clamp(14px, 2.2vmin, 20px);
  line-height: 1;
  align-self: center;
  opacity: 0.85;
}
.wake-indicator.wake-active { color: #4ade80; }
.wake-indicator.wake-failed { color: #f87171; animation: wake-blink 1.2s ease-in-out infinite; }
.wake-indicator.wake-unsupported { color: #fbbf24; }
.wake-indicator.wake-idle { color: var(--muted-2); }
@keyframes wake-blink {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}
.end-time {
  font-size: clamp(16px, 2.5vmin, 22px);
  font-weight: 800;
  color: var(--fg);
  font-variant-numeric: tabular-nums;
}

/* Karty */
.cards-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 2vmin, 24px);
  min-height: 0;
  flex: 1;
  width: 100%;
}
.card-pair {
  display: flex;
  gap: clamp(3px, 0.8vmin, 10px);
}
.colon {
  display: flex;
  flex-direction: column;
  gap: min(8vw, 22vh);
  flex-shrink: 0;
}
.colon span {
  display: block;
  width: min(2.5vw, 2.5vh);
  height: min(2.5vw, 2.5vh);
  background: var(--fg);
  border-radius: 50%;
}
.colon.paused span { animation: blink 1s step-end infinite; }
@keyframes blink { 50% { opacity: 0.2; } }

/* Pasek segmentów */
.segments-bar {
  display: flex;
  gap: 3px;
  height: 6px;
  padding: 0 8px;
}
.seg {
  position: relative;
  background: #1a1a1a;
  border-radius: 2px;
  overflow: hidden;
  min-width: 6px;
}
.seg span { display: none; }
.seg.break, .seg.halftime { background: #0f0f0f; }
.seg.seg-done { background: var(--muted-2); }
.seg.seg-active { background: var(--fg); }
.seg.break.seg-active, .seg.halftime.seg-active {
  background: repeating-linear-gradient(45deg, var(--fg) 0 4px, var(--muted-2) 4px 8px);
}

/* Sterowanie */
.controls {
  display: flex;
  gap: 8px;
  padding: 0 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.ctrl {
  appearance: none;
  border: 1px solid var(--card-border);
  background: #0c0c0c;
  color: var(--fg);
  font-family: inherit;
  font-size: clamp(12px, 1.8vmin, 15px);
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 12px 16px;
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
  min-width: 70px;
}
.ctrl:active { background: #1a1a1a; }
.ctrl-primary {
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
  flex: 1 1 200px;
  max-width: 300px;
  font-weight: 800;
}
.ctrl-primary.is-paused {
  background: #1a8a3f;
  color: var(--fg);
  border-color: #1a8a3f;
}
.ctrl-danger { color: #ff6b6b; }

/* ──────────── OSTATNIA MINUTA ──────────── */
.app.last-minute { --fg: var(--accent-red); }
.app.last-minute .flip-digit { color: var(--accent-red); }
.app.last-minute .colon span { background: var(--accent-red); }

/* ──────────── PRZERWA ──────────── */
.app.is-break { --fg: #22c55e; }
.app.is-break .flip-digit { color: #22c55e; font-weight: 900; }
.app.is-break .colon span { background: #22c55e; }

/* ──────────── FLIP CARD ──────────── */
/* Karta ma zajmować maksymalnie dużo przestrzeni.
   Układ: 4 karty + dwukropek w poziomie. Ograniczeniami są wysokość kontenera
   oraz szerokość / 4. Używamy CSS min() żeby wybrać mniejsze z ograniczeń. */
.flip-card {
  position: relative;
  background: transparent;
  /* Szerokość: viewport/4.8 (4 karty + dwukropek + odstępy) LUB wysokość/1.4 */
  width: min(22vw, 66vh);
  height: min(30vw, 88vh);
  max-width: 44vh;
  max-height: 62vh;
  border-radius: clamp(8px, 1.5vmin, 18px);
  font-family: inherit;
  font-weight: 900;
  perspective: 1400px;
  -webkit-perspective: 1400px;
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.flip-half {
  position: absolute;
  left: 0; right: 0;
  height: 50%;
  overflow: hidden;
  background: var(--card-bg);
  border: 1px solid #111;
  display: flex;
  align-items: center;
  justify-content: center;
}
.flip-top {
  top: 0;
  border-radius: clamp(10px, 2vmin, 20px) clamp(10px, 2vmin, 20px) 0 0;
  border-bottom: none;
  align-items: flex-end;
}
.flip-bottom {
  bottom: 0;
  border-radius: 0 0 clamp(10px, 2vmin, 20px) clamp(10px, 2vmin, 20px);
  border-top: none;
  align-items: flex-start;
}
.flip-static-old { z-index: 1; }

.flip-digit {
  font-size: min(22vw, 70vh);
  line-height: 1;
  color: var(--fg);
  font-weight: 800;
  letter-spacing: -0.05em;
  transform: translateY(50%);
  white-space: nowrap;
}
.flip-digit-bottom { transform: translateY(-50%); }

.flip-hinge {
  position: absolute;
  left: 6%; right: 6%;
  top: calc(50% - 1px);
  height: 2px;
  background: rgba(0,0,0,0.9);
  z-index: 3;
  pointer-events: none;
}

/* Jedna klapka, dwustronna. Zaczyna jako górna połowa na górze (rotateX 0°),
   opada wokół dolnej krawędzi (transform-origin: bottom center) do -180° —
   wtedy rewers znajduje się dokładnie w miejscu dolnej połowy. */
.flip-flap {
  position: absolute;
  left: 0; right: 0;
  top: 0;
  height: 50%;
  z-index: 2;
  transform-origin: bottom center;
  transform-style: preserve-3d;
  animation: flipCard var(--dur) cubic-bezier(0.5, 0.0, 0.4, 1) forwards;
  pointer-events: none;
}
.flip-flap-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--card-bg);
  border: 1px solid #111;
  display: flex;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.flip-flap-front {
  align-items: flex-end;
  border-radius: clamp(10px, 2vmin, 20px) clamp(10px, 2vmin, 20px) 0 0;
  border-bottom: none;
}
/* Rewers: obrócony o 180° wokół osi X, żeby był widoczny po obrocie klapki.
   Ze względu na obrót wokół dolnej krawędzi klapki, rewers "wisi" poniżej 
   zawiasu — czyli znajduje się w miejscu dolnej połowy karty. */
.flip-flap-back {
  align-items: flex-start;
  border-radius: 0 0 clamp(10px, 2vmin, 20px) clamp(10px, 2vmin, 20px);
  border-top: none;
  transform: rotateX(180deg);
  /* Rewers musi pokazywać dolną połowę cyfry. Cyfra w .flip-digit-bottom jest
     już translateY(-50%), więc widać dolną część. */
}

@keyframes flipCard {
  0% {
    transform: rotateX(0deg);
  }
  100% {
    transform: rotateX(-180deg);
  }
}

/* Lekki cień/przyciemnienie gdy klapka jest pionowa — dodaje głębi */
.flip-flap-front {
  animation: flipShadeFront var(--dur) cubic-bezier(0.5, 0.0, 0.4, 1) forwards;
}
.flip-flap-back {
  animation: flipShadeBack var(--dur) cubic-bezier(0.5, 0.0, 0.4, 1) forwards;
}
@keyframes flipShadeFront {
  0%   { filter: brightness(1); }
  50%  { filter: brightness(0.5); }
  100% { filter: brightness(0.5); }
}
@keyframes flipShadeBack {
  0%   { filter: brightness(0.5); }
  50%  { filter: brightness(0.5); }
  100% { filter: brightness(1); }
}

/* ──────────── RESPONSIVE ──────────── */
/* Portrait na telefonie: karty w pionie? Nie — zostawiamy w poziomie, mniejsze */
@media (orientation: portrait) and (max-width: 600px) {
  .topbar-left { gap: 10px; }
  .end-time-label { display: none; }
  .controls { gap: 6px; }
  .ctrl { padding: 10px 12px; min-width: 56px; font-size: 12px; }
  .ctrl-primary { flex: 1 1 100%; order: -1; }
}

/* Landscape na telefonie: bardzo niski viewport */
@media (orientation: landscape) and (max-height: 500px) {
  .timer-screen { padding: 8px; gap: 6px; }
  .topbar { padding: 0 4px; }
  .config-inner { gap: 18px; }
  .config-title { font-size: 42px; }
  .config-hero { padding: 14px; }
}

@media (orientation: landscape) and (max-height: 800px) {
  .config-inner { gap: 20px; }
  .config-title { font-size: clamp(36px, 7vh, 64px); }
  .config-time { font-size: clamp(44px, 10vh, 72px); }
}
