/**
 * TES Universal Time Picker — system-wide custom clock UI (no native OS pickers).
 * Tokens fall back through theme → Connect → hard defaults.
 * Authoritative package: tes-universal-time-picker (not tes-ui-kit).
 *
 * Viewport: fit actual visible area (svh/dvh + visualViewport vars), not blind 100vh.
 * Safe areas: env(safe-area-inset-*) so Cancel/Set Time never sit under system UI.
 */

:root {
  --tes-tp-ink: var(--text, var(--tc-ink, #13203a));
  --tes-tp-muted: var(--muted, var(--tc-muted, #5d6b86));
  --tes-tp-primary: var(--primary, var(--tc-blue, #5b4bff));
  --tes-tp-primary-2: var(--primary-2, var(--tc-purple, #8b5cf6));
  --tes-tp-card: var(--card, var(--tc-surface, #ffffff));
  --tes-tp-border: var(--border, var(--tc-border, #e7e9f7));
  --tes-tp-shadow: var(--shadow, var(--tc-shadow-md, 0 10px 30px rgba(48, 58, 109, 0.08)));
  --tes-tp-radius: var(--radius-sm, var(--ds-radius-md, 14px));
  --tes-tp-radius-lg: var(--radius, var(--ds-radius-xl, 22px));
  --tes-tp-font: var(--ds-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif);
  /* Available height: visualViewport (JS) → svh/dvh → vh */
  --tes-tp-avail-h: 100vh;
  --tes-tp-avail-h: 100svh;
  --tes-tp-avail-h: 100dvh;
  --tes-tp-safe-top: env(safe-area-inset-top, 0px);
  --tes-tp-safe-right: env(safe-area-inset-right, 0px);
  --tes-tp-safe-bottom: env(safe-area-inset-bottom, 0px);
  --tes-tp-safe-left: env(safe-area-inset-left, 0px);
}

html.tes-tp-open,
html.tes-tp-open body {
  overflow: hidden;
  overscroll-behavior: none;
}

/* Field shell — storage input holds HH:MM; ONE display button is the visible UI */
.tes-time-picker-wrap {
  position: relative;
  display: block;
  width: 100%;
}

/* Storage: same form control, removed from visual painting (not a transparent overlay) */
.tes-time-picker-wrap > input.tes-time-picker-field,
.tes-time-picker-wrap > input.tes-time-picker-storage,
.tes-time-picker-wrap > input[data-tes-time-picker] {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  min-height: 0 !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  opacity: 0 !important;
  pointer-events: none !important;
  -webkit-text-fill-color: transparent !important;
  color: transparent !important;
  caret-color: transparent !important;
  background: transparent !important;
  box-shadow: none !important;
  outline: none !important;
}

/* Sole visible time value in the field */
.tes-time-picker-display,
button.tes-time-picker-display {
  box-sizing: border-box;
  display: block;
  width: 100%;
  min-height: 48px;
  margin: 0;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--tes-tp-border);
  border-radius: var(--tes-tp-radius);
  background: var(--tes-tp-card);
  color: var(--tes-tp-ink);
  font: inherit;
  font-family: var(--tes-tp-font);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-align: left;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tes-time-picker-display:focus,
button.tes-time-picker-display:focus {
  outline: 2px solid var(--tes-tp-primary);
  outline-offset: 2px;
  border-color: var(--tes-tp-primary);
}

.tes-time-picker-display.is-placeholder {
  font-weight: 600;
  color: var(--tes-tp-muted);
}

/* Legacy mirror class (v1.0.1) — never paint; JS migrates/removes duplicates */
.tes-time-picker-mirror {
  display: none !important;
}

/* Modal root — sized to visible viewport + safe areas (never under system nav) */
.tes-tp-root {
  position: fixed;
  z-index: 100050;
  box-sizing: border-box;
  top: var(--tes-tp-vv-top, 0px);
  left: var(--tes-tp-vv-left, 0px);
  width: var(--tes-tp-vv-width, 100%);
  height: var(--tes-tp-avail-h);
  max-height: var(--tes-tp-avail-h);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-top: max(0.35rem, var(--tes-tp-safe-top));
  padding-right: max(0px, var(--tes-tp-safe-right));
  padding-bottom: max(0.35rem, var(--tes-tp-safe-bottom));
  padding-left: max(0px, var(--tes-tp-safe-left));
  font-family: var(--tes-tp-font);
  /* Isolate from page scroll chaining */
  overscroll-behavior: none;
}

.tes-tp-root[hidden] {
  display: none !important;
}

.tes-tp-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(19, 32, 58, 0.48);
}

.tes-tp-dialog {
  position: relative;
  z-index: 1;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  width: min(26rem, 100%);
  max-width: 100%;
  /* Cap to available root content box (already minus safe insets) */
  max-height: min(40rem, 100%);
  margin: 0;
  padding: 0;
  overflow: hidden;
  border-radius: var(--tes-tp-radius-lg) var(--tes-tp-radius-lg) 0 0;
  background: var(--tes-tp-card);
  box-shadow: var(--tes-tp-shadow);
  color: var(--tes-tp-ink);
  animation: tes-tp-rise 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}

/* Scrollable body — keeps Cancel | Set Time pinned */
.tes-tp-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: clamp(0.65rem, 2.2vh, 1.15rem) clamp(0.75rem, 3vw, 1.15rem) 0.35rem;
}

@keyframes tes-tp-rise {
  from { transform: translateY(1.25rem); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}

@media (min-width: 640px) {
  .tes-tp-root {
    align-items: center;
    padding-top: max(1.5rem, var(--tes-tp-safe-top));
    padding-right: max(1.5rem, var(--tes-tp-safe-right));
    padding-bottom: max(1.5rem, var(--tes-tp-safe-bottom));
    padding-left: max(1.5rem, var(--tes-tp-safe-left));
  }
  .tes-tp-dialog {
    border-radius: var(--tes-tp-radius-lg);
    animation-name: tes-tp-pop;
  }
  @keyframes tes-tp-pop {
    from { transform: scale(0.96); opacity: 0.6; }
    to { transform: scale(1); opacity: 1; }
  }
}

.tes-tp-title {
  margin: 0 0 clamp(0.4rem, 1.5vh, 0.85rem);
  font-size: clamp(0.95rem, 2.4vh, 1.05rem);
  font-weight: 800;
  letter-spacing: -0.01em;
  text-align: center;
}

.tes-tp-display {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.2rem;
  margin: 0 0 clamp(0.45rem, 1.8vh, 1rem);
  padding: clamp(0.4rem, 1.4vh, 0.65rem) 0.5rem;
  border-radius: var(--tes-tp-radius);
  background: linear-gradient(135deg, rgba(91, 75, 255, 0.08), rgba(139, 92, 246, 0.08));
}

.tes-tp-disp-part {
  border: 0;
  background: transparent;
  color: var(--tes-tp-ink);
  font-size: clamp(1.65rem, 6.5vw, 2.75rem);
  font-weight: 800;
  letter-spacing: 0.04em;
  line-height: 1;
  padding: 0.25rem 0.35rem;
  border-radius: 10px;
  cursor: pointer;
}

.tes-tp-disp-part.is-active {
  background: var(--tes-tp-primary);
  color: #fff;
}

.tes-tp-disp-colon {
  font-size: clamp(1.65rem, 6.5vw, 2.75rem);
  font-weight: 800;
  opacity: 0.45;
}

.tes-tp-disp-period {
  font-size: clamp(1rem, 4vw, 1.45rem);
  margin-left: 0.35rem;
  letter-spacing: 0.06em;
}

.tes-tp-manual {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: clamp(0.4rem, 1.5vh, 0.65rem);
  align-items: end;
  margin-bottom: clamp(0.45rem, 1.8vh, 1rem);
}

.tes-tp-manual-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--tes-tp-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tes-tp-manual-field input {
  min-height: 48px;
  border: 1px solid var(--tes-tp-border);
  border-radius: var(--tes-tp-radius);
  padding: 0.55rem 0.7rem;
  font-size: 1.25rem;
  font-weight: 700;
  text-align: center;
  color: var(--tes-tp-ink);
  background: #fff;
}

.tes-tp-manual-field input:focus {
  outline: 2px solid var(--tes-tp-primary);
  outline-offset: 1px;
}

.tes-tp-ampm {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tes-tp-ampm button {
  min-width: 3.5rem;
  min-height: 44px;
  border: 1px solid var(--tes-tp-border);
  border-radius: 10px;
  background: #fff;
  color: var(--tes-tp-ink);
  font-weight: 800;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.tes-tp-ampm button.is-active {
  border-color: transparent;
  background: linear-gradient(135deg, var(--tes-tp-primary), var(--tes-tp-primary-2));
  color: #fff;
}

.tes-tp-clock-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(0.2rem, 1vh, 0.45rem);
  margin-bottom: clamp(0.35rem, 1.4vh, 0.75rem);
}

.tes-tp-clock {
  /* Large when space allows; shrinks with short viewports */
  --tes-tp-clock-size: clamp(10.5rem, min(78vw, 42vh), 17.5rem);
  --tes-tp-tick-radius: calc(var(--tes-tp-clock-size) * 0.349);
  width: var(--tes-tp-clock-size);
  height: var(--tes-tp-clock-size);
  max-width: 100%;
  border-radius: 50%;
  touch-action: none;
  outline: none;
  flex-shrink: 0;
}

.tes-tp-clock:focus-visible {
  box-shadow: 0 0 0 3px rgba(91, 75, 255, 0.35);
}

.tes-tp-clock-face {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background:
    radial-gradient(circle at 50% 50%, #fff 0 38%, transparent 39%),
    linear-gradient(145deg, #f4f5ff, #eef0ff);
  border: 1px solid var(--tes-tp-border);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8), var(--tes-tp-shadow);
}

.tes-tp-hand {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 3px;
  height: 32%;
  margin-left: -1.5px;
  margin-top: -32%;
  border-radius: 3px;
  background: linear-gradient(180deg, var(--tes-tp-primary), var(--tes-tp-primary-2));
  transform-origin: bottom center;
  transform: rotate(0deg);
  z-index: 2;
  pointer-events: none;
}

.tes-tp-center {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 12px;
  height: 12px;
  margin: -6px 0 0 -6px;
  border-radius: 50%;
  background: var(--tes-tp-primary);
  z-index: 3;
  pointer-events: none;
}

.tes-tp-ticks {
  position: absolute;
  inset: 0;
  z-index: 4;
}

.tes-tp-tick {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 2.5rem;
  height: 2.5rem;
  margin: -1.25rem 0 0 -1.25rem;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--tes-tp-ink);
  font-size: clamp(0.72rem, 2.4vw, 0.95rem);
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  transform: rotate(var(--tes-tp-tick-angle, 0deg))
    translateX(var(--tes-tp-tick-radius, 6.1rem))
    rotate(calc(-1 * var(--tes-tp-tick-angle, 0deg)));
}

.tes-tp-tick.is-selected {
  background: var(--tes-tp-primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(91, 75, 255, 0.35);
}

.tes-tp-tick[hidden] {
  display: none !important;
}

.tes-tp-clock-hint {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--tes-tp-muted);
  text-align: center;
}

.tes-tp-error {
  margin: 0 0 0.65rem;
  color: #b42318;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
}

/* Sticky footer — always above system gesture / home indicator area */
.tes-tp-actions {
  flex: 0 0 auto;
  position: sticky;
  bottom: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: clamp(0.45rem, 1.5vh, 0.65rem);
  margin: 0;
  padding: 0.65rem clamp(0.75rem, 3vw, 1.15rem) clamp(0.75rem, 2vh, 1.15rem);
  background: var(--tes-tp-card);
  box-shadow: 0 -10px 18px rgba(19, 32, 58, 0.04);
}

.tes-tp-btn {
  min-height: 48px;
  border-radius: var(--tes-tp-radius);
  border: 1px solid var(--tes-tp-border);
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
}

.tes-tp-btn-ghost {
  background: #fff;
  color: var(--tes-tp-ink);
}

.tes-tp-btn-primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--tes-tp-primary), var(--tes-tp-primary-2));
  color: #fff;
}

.tes-tp-btn:focus-visible {
  outline: 2px solid var(--tes-tp-primary);
  outline-offset: 2px;
}

/* Short portrait / large text — progressive shrink; clock stays usable */
@media (max-height: 720px) {
  .tes-tp-clock {
    --tes-tp-clock-size: clamp(9.5rem, min(72vw, 36vh), 15rem);
  }
}

@media (max-height: 620px) {
  .tes-tp-clock {
    --tes-tp-clock-size: clamp(8.25rem, min(64vw, 32vh), 12.5rem);
  }
  .tes-tp-manual-field input {
    min-height: 44px;
    font-size: 1.1rem;
    padding: 0.4rem 0.55rem;
  }
  .tes-tp-ampm button {
    min-height: 40px;
  }
  .tes-tp-tick {
    width: 2.15rem;
    height: 2.15rem;
    margin: -1.075rem 0 0 -1.075rem;
  }
}

@media (max-height: 520px) {
  .tes-tp-clock {
    --tes-tp-clock-size: clamp(7rem, min(58vw, 28vh), 10.5rem);
  }
  .tes-tp-clock-hint {
    display: none;
  }
  .tes-tp-title {
    margin-bottom: 0.3rem;
  }
  .tes-tp-btn {
    min-height: 44px;
  }
}

/* Landscape / very short — scroll body; keep actions reachable */
@media (max-height: 430px), (orientation: landscape) and (max-height: 500px) {
  .tes-tp-root {
    align-items: stretch;
  }
  .tes-tp-dialog {
    width: min(36rem, 100%);
    max-height: 100%;
    border-radius: var(--tes-tp-radius-lg);
  }
  .tes-tp-clock {
    --tes-tp-clock-size: clamp(6.25rem, min(42vw, 42vh), 9rem);
  }
  .tes-tp-disp-part,
  .tes-tp-disp-colon {
    font-size: clamp(1.35rem, 5vw, 1.85rem);
  }
  .tes-tp-actions {
    padding-top: 0.45rem;
    padding-bottom: 0.55rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tes-tp-dialog {
    animation: none;
  }
}
