.app-shell {
  position: relative;
  z-index: 2;
  max-width: 980px;
  margin: 0 auto;
  height: 100%;
  padding: calc(0.55rem + env(safe-area-inset-top)) clamp(0.8rem, 2.2vw, 1.4rem)
    calc(0.9rem + env(safe-area-inset-bottom));
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1fr;
  grid-template-areas:
    "top"
    "global"
    "guided"
    "history"
    "controls";
}

.top-bar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.2rem 0.1rem;
}

.top-bar h1 {
  margin: 0;
  font-weight: 700;
  letter-spacing: 0.03em;
  font-size: clamp(1.1rem, 2.6vw, 1.8rem);
}

.pill {
  border: 1px solid var(--line);
  background: rgba(171, 201, 213, 0.1);
  border-radius: 999px;
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
  color: var(--text-soft);
  transition: color 250ms, border-color 250ms, background 250ms;
}

/* GPS status indicators */
.gps-off       { color: var(--text-soft); }
.gps-searching { color: #f7c948; border-color: rgba(247, 201, 72, 0.35); }
.gps-ok        { color: var(--primary); border-color: rgba(24, 191, 125, 0.35); }
.gps-medium    { color: #f7931e; border-color: rgba(247, 147, 30, 0.35); }
.gps-low       { color: #da4141; border-color: rgba(218, 65, 65, 0.35); }
.gps-denied,
.gps-no-gps,
.gps-error     { color: #da4141; border-color: rgba(218, 65, 65, 0.35); background: rgba(218, 65, 65, 0.08); }

.global-panel {
  grid-area: global;
  padding: 0.85rem;
  border-radius: 18px;
}

.global-time {
  margin-top: 0.2rem;
  font-family: "Barlow Condensed", sans-serif;
  font-weight: 700;
  letter-spacing: 0.04em;
  font-size: clamp(3.1rem, 16vw, 6.8rem);
  line-height: 0.95;
  color: #8dffd2;
  font-variant-numeric: tabular-nums;
}

.global-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
}

.global-metrics article {
  border-radius: var(--radius-md);
  border: 1px solid rgba(165, 202, 214, 0.18);
  padding: 0.55rem;
  background: rgba(7, 22, 30, 0.48);
}

.metric-label {
  margin: 0;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--text-soft);
  letter-spacing: 0.08em;
}

.metric-value {
  margin: 0.25rem 0 0;
  font-size: 1.2rem;
  font-weight: 600;
}

.guided-panel {
  grid-area: guided;
  position: relative;
  overflow: hidden;
  padding: 0.95rem;
  min-height: 300px;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(247, 147, 30, 0.12) 0%, rgba(14, 28, 33, 0.88) 60%);
}

.guided-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

.template-name {
  margin: 0;
  color: #ffc784;
  font-size: 0.95rem;
}

.exercise-name {
  margin: 0.9rem 0 0;
  font-size: clamp(1.1rem, 3.8vw, 1.9rem);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.guided-time {
  margin: 0.1rem 0 0;
  font-family: "Barlow Condensed", sans-serif;
  font-size: clamp(3.4rem, 14vw, 5rem);
  color: #ffbe67;
  line-height: 0.95;
  font-variant-numeric: tabular-nums;
}

.guided-time.guided-prepared {
  color: #f2f6f7;
  font-size: clamp(2.6rem, 10vw, 4.2rem);
}

.guided-time.guided-countdown {
  color: #8dffd2;
  font-size: clamp(4rem, 18vw, 6.2rem);
}

.guided-confirm {
  margin: 0.55rem auto 0;
  width: min(340px, 78%);
}

.guided-confirm:not(.hidden) {
  display: block;
}

.exercise-progress-track,
.session-progress-track {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: rgba(152, 190, 204, 0.2);
  overflow: hidden;
}

.exercise-progress-fill,
.session-progress-fill {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  transition: width 220ms linear;
}

.exercise-progress-fill {
  background: linear-gradient(90deg, #ffdd91 0%, #f7931e 100%);
}

.session-progress-fill {
  background: linear-gradient(90deg, #6ff9be 0%, #18bf7d 100%);
}

.guided-actions-row {
  margin-top: 0.8rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.8rem;
}

.next-preview {
  margin: 0;
  color: var(--text-soft);
  font-size: 0.88rem;
}

.session-progress-wrap {
  margin-top: 0.65rem;
}

.session-time {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: var(--text-soft);
}

.countdown-overlay-global {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(0, 0, 0, 0.86);
  backdrop-filter: blur(4px);
  place-items: center;
}

.countdown-overlay-global:not(.hidden) {
  display: grid;
}

.countdown-overlay-global p {
  margin: 0;
  font-family: "Barlow Condensed", sans-serif;
  font-size: clamp(6rem, 24vw, 10rem);
  color: #8dffd2;
  text-shadow: 0 12px 30px rgba(24, 191, 125, 0.36);
}

.history-panel {
  grid-area: history;
  padding: 0.9rem;
  max-height: 230px;
  overflow: auto;
  border-radius: 18px;
}

.history-list {
  margin: 0.75rem 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.45rem;
}

.history-item,
.history-empty {
  border: 1px solid rgba(165, 202, 214, 0.16);
  border-radius: var(--radius-md);
  background: rgba(8, 20, 26, 0.45);
  padding: 0.5rem 0.65rem;
  font-size: 0.88rem;
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
}

.history-item.completado {
  border-left: 3px solid var(--primary);
}

.history-item.saltado {
  border-left: 3px solid var(--accent);
}

.history-item.detenido {
  border-left: 3px solid var(--danger);
}

.controls-panel {
  grid-area: controls;
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.55rem;
  padding: 0.8rem;
  position: sticky;
  bottom: 0.45rem;
  background: var(--bg-panel-strong);
  border-radius: 18px;
}

.template-dialog {
  width: min(500px, calc(100vw - 2rem));
  max-height: calc(100dvh - 3rem);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: #0c1d24;
  color: var(--text-main);
  padding: 0;
}

.template-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
}

.dialog-shell {
  margin: 0;
  padding: 1rem;
  display: grid;
  gap: 0.75rem;
}

.dialog-shell header h2 {
  margin: 0;
}

.dialog-shell header p {
  margin: 0.35rem 0 0;
  color: var(--text-soft);
}

.template-list {
  display: grid;
  gap: 0.55rem;
}

.template-card {
  text-align: left;
  padding: 0.7rem;
  border-radius: var(--radius-md);
  border: 1px solid rgba(170, 198, 209, 0.2);
  background: rgba(15, 34, 43, 0.8);
  color: inherit;
  cursor: pointer;
}

.template-card h3 {
  margin: 0;
}

.template-card p {
  margin: 0.28rem 0 0;
  color: var(--text-soft);
  font-size: 0.86rem;
}

.summary-kpis {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}

.summary-kpi-card {
  margin: 0;
  border: 1px solid rgba(170, 198, 209, 0.2);
  background: rgba(9, 27, 35, 0.7);
  border-radius: 12px;
  padding: 0.55rem;
}

.summary-label {
  margin: 0;
  font-size: 0.74rem;
  color: var(--text-soft);
  text-transform: uppercase;
}

.summary-value {
  margin: 0.15rem 0 0;
  font-size: 1.2rem;
  font-weight: 700;
}

.summary-guided {
  display: grid;
  gap: 0.55rem;
}

.summary-guided-card {
  border: 1px solid rgba(170, 198, 209, 0.2);
  background: rgba(16, 35, 45, 0.78);
  border-radius: 12px;
  padding: 0.6rem;
}

.summary-guided-card h3 {
  margin: 0;
  font-size: 0.98rem;
}

.summary-guided-card p {
  margin: 0.3rem 0 0;
  font-size: 0.84rem;
  color: #d9e5ea;
}

.summary-empty {
  margin: 0;
  padding: 0.7rem;
  border-radius: 12px;
  background: rgba(16, 35, 45, 0.5);
  color: var(--text-soft);
}

.dialog-shell menu {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: flex-end;
}

@media (min-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas:
      "top top"
      "global guided"
      "history history"
      "controls controls";
    overflow: hidden;
  }

  .history-panel {
    overflow-y: auto;
    min-height: 0;
  }

  .controls-panel {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ── MOBILE PORTRAIT: no-scroll layout ───────────────────── */
@media (max-width: 768px) {
  .app-shell {
    /* Abandon grid in favour of flex-column so history can fill remaining space */
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    gap: 0.42rem;
    padding:
      calc(0.38rem + env(safe-area-inset-top))
      0.72rem
      calc(0.38rem + env(safe-area-inset-bottom));
    /* reset grid-template-areas — not used in flex */
    grid-template-areas: none;
  }

  /* All sections need a grid-area reset when in flex context */
  .top-bar,
  .global-panel,
  .guided-panel,
  .history-panel,
  .controls-panel {
    grid-area: auto;
  }

  /* ── Top bar ── */
  .top-bar {
    flex-shrink: 0;
    padding: 0;
  }

  .top-bar h1 {
    font-size: 1.05rem;
  }

  /* ── Global panel ── */
  .global-panel {
    flex-shrink: 0;
    padding: 0.62rem;
  }

  .global-time {
    font-size: clamp(2.2rem, 13.5vw, 3.8rem);
  }

  .global-metrics {
    gap: 0.38rem;
    margin-top: 0.38rem;
  }

  .metric-value {
    font-size: 0.95rem;
  }

  /* ── Guided panel ── */
  .guided-panel {
    flex-shrink: 0;
    min-height: auto;
    padding: 0.65rem;
  }

  .exercise-name {
    margin: 0.45rem 0 0;
    font-size: clamp(0.92rem, 4vw, 1.25rem);
  }

  .guided-time {
    font-size: clamp(2.5rem, 11.5vw, 3.5rem);
  }

  .guided-time.guided-countdown {
    font-size: clamp(2.9rem, 14vw, 4.4rem);
  }

  .guided-time.guided-prepared {
    font-size: clamp(2rem, 9vw, 3rem);
  }

  .guided-actions-row {
    margin-top: 0.42rem;
    gap: 0.5rem;
  }

  .session-progress-wrap {
    margin-top: 0.38rem;
  }

  .exercise-progress-track,
  .session-progress-track {
    height: 8px;
  }

  /* ── History panel: fills all remaining space ── */
  .history-panel {
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    padding: 0.62rem;
  }

  .history-list {
    margin-top: 0.5rem;
  }

  .history-item,
  .history-empty {
    padding: 0.4rem 0.55rem;
    font-size: 0.82rem;
  }

  /* ── Controls panel: natural flow at bottom, no sticky ── */
  .controls-panel {
    flex-shrink: 0;
    position: static;
    bottom: auto;
    padding: 0.55rem;
    gap: 0.4rem;
    grid-template-columns: 1fr;
  }

  /* Compact buttons for mobile */
  .primary-btn,
  .secondary-btn,
  .accent-btn,
  .danger-btn {
    padding: 0.68rem 1rem;
    font-size: 0.87rem;
  }
}

/* ── Very small screens (< 390px) ─────────────────────────── */
@media (max-width: 389px) {
  .global-time {
    font-size: 12.5vw;
  }

  .guided-time {
    font-size: 11vw;
  }

  .guided-time.guided-countdown {
    font-size: 13vw;
  }

  .top-bar h1 {
    font-size: 0.95rem;
  }

  .primary-btn,
  .secondary-btn,
  .accent-btn,
  .danger-btn {
    padding: 0.58rem 0.85rem;
    font-size: 0.83rem;
  }
}
