:root {
  --bg: #0d1117;
  --bg-2: #161b26;
  --surface: #1b2130;
  --surface-2: #232a3b;
  --border: #303950;
  --text: #eef2fb;
  --muted: #98a2ba;
  --accent: #5b8def;
  --accent-hover: #7aa3ff;
  --level: #3ecf8e;
  --level-glow: rgba(62, 207, 142, 0.55);
  --radius: 16px;
  --touch-min: 48px;
  /* Cap by height too so the dial still fits when the phone is turned sideways. */
  --vial-size: min(70vw, 58vh, 300px);
  --vial-travel: calc(var(--vial-size) * 0.5 - 32px);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

/* Keep the HTML `hidden` attribute authoritative over layout `display` rules
   (e.g. the permission panel below sets display:flex). */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(ellipse at top, #1a2236 0%, var(--bg) 60%);
  color: var(--text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-user-select: none;
  user-select: none;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 1rem 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.noscript {
  margin: 0;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  text-align: center;
}

.header h1 {
  margin: 0 0 0.25rem;
  font-size: clamp(1.6rem, 6vw, 2.2rem);
  letter-spacing: 0.01em;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: clamp(0.9rem, 3.2vw, 1.02rem);
  line-height: 1.45;
}

/* Permission gate (mainly iOS Safari, which requires a user gesture). */
.enable-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  align-items: flex-start;
}

.enable-panel p {
  margin: 0;
  color: var(--muted);
}

/* Digital readout */
.readout {
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg-2) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1rem 1rem;
  text-align: center;
}

.readout-main {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.4rem;
}

.tilt-value {
  font-size: clamp(2.8rem, 15vw, 4.2rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--text);
  transition: color 0.2s ease;
}

.tilt-unit {
  color: var(--muted);
  font-size: clamp(0.85rem, 3.5vw, 1rem);
}

.level-badge {
  margin: 0.6rem auto 0;
  display: inline-block;
  padding: 0.3rem 0.85rem;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: all 0.2s ease;
}

.axis-grid {
  margin: 1rem 0 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
}

.axis {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.55rem 0.4rem;
}

.axis dt {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.axis dd {
  margin: 0.2rem 0 0;
  font-size: 1.25rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Levels (tabbed: Surface / Horizontal / Vertical) */
.levels {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  min-height: 40px;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  transition:
    background 0.15s ease,
    color 0.15s ease;
}

.tab:hover[aria-selected="false"] {
  color: var(--text);
  background: var(--surface-2);
}

.tab[aria-selected="true"] {
  background: var(--accent);
  color: #061224;
}

.tab:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
}

.tab-icon {
  font-size: 1.05rem;
  line-height: 1;
}

.panels {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: var(--vial-size);
}

.panel {
  display: flex;
  justify-content: center;
  align-items: center;
}

.bullseye {
  position: relative;
  width: var(--vial-size);
  height: var(--vial-size);
  border-radius: 50%;
  background:
    radial-gradient(
      circle at 38% 32%,
      rgba(255, 255, 255, 0.16) 0%,
      rgba(255, 255, 255, 0) 45%
    ),
    radial-gradient(circle at 50% 50%, #1d2b3a 0%, #12202c 70%, #0c1620 100%);
  border: 2px solid rgba(120, 160, 210, 0.28);
  box-shadow:
    inset 0 4px 22px rgba(0, 0, 0, 0.55),
    0 10px 30px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

.ring {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 1.5px solid rgba(120, 160, 210, 0.32);
  pointer-events: none;
}

.ring-outer {
  width: 62%;
  height: 62%;
}

.ring-inner {
  width: 30%;
  height: 30%;
  border-color: var(--level);
  box-shadow: 0 0 12px rgba(62, 207, 142, 0.25);
}

.crosshair {
  position: absolute;
  top: 50%;
  left: 50%;
  background: rgba(120, 160, 210, 0.22);
  pointer-events: none;
}

.crosshair-h {
  width: 84%;
  height: 1.5px;
  transform: translate(-50%, -50%);
}

.crosshair-v {
  width: 1.5px;
  height: 84%;
  transform: translate(-50%, -50%);
}

.bubble {
  --bx: 0;
  --by: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background:
    radial-gradient(
      circle at 36% 32%,
      rgba(255, 255, 255, 0.9) 0%,
      rgba(255, 255, 255, 0.15) 30%,
      rgba(90, 200, 255, 0.25) 60%,
      rgba(60, 150, 210, 0.15) 100%
    );
  border: 2px solid rgba(180, 230, 255, 0.55);
  box-shadow: 0 0 18px rgba(120, 210, 255, 0.35);
  transform: translate(
    calc(-50% + var(--bx) * var(--vial-travel)),
    calc(-50% + var(--by) * var(--vial-travel))
  );
  transition:
    transform 0.12s ease-out,
    background 0.25s ease,
    box-shadow 0.25s ease,
    border-color 0.25s ease;
}

/* Tube vials */
.tube {
  position: relative;
  background:
    radial-gradient(
      circle at 30% 30%,
      rgba(255, 255, 255, 0.1),
      rgba(255, 255, 255, 0) 60%
    ),
    linear-gradient(180deg, #16232f, #0e1a24);
  border: 2px solid rgba(120, 160, 210, 0.28);
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.tube-h {
  --tube-len: min(440px, 90vw);
  --th-travel: calc(var(--tube-len) / 2 - 34px);
  width: var(--tube-len);
  height: 60px;
  border-radius: 30px;
}

.tube-v {
  --tube-len: min(300px, 55vh);
  --tv-travel: calc(var(--tube-len) / 2 - 34px);
  width: 60px;
  height: var(--tube-len);
  border-radius: 30px;
}

.tube-target {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1.5px solid var(--level);
  border-radius: 12px;
  opacity: 0.5;
  pointer-events: none;
}

.tube-h .tube-target {
  width: 64px;
  height: 100%;
}

.tube-v .tube-target {
  width: 100%;
  height: 64px;
}

.tube-bubble {
  --bx: 0;
  --by: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(
    circle at 36% 32%,
    rgba(255, 255, 255, 0.9) 0%,
    rgba(120, 210, 255, 0.28) 60%,
    rgba(60, 150, 210, 0.15) 100%
  );
  border: 2px solid rgba(180, 230, 255, 0.55);
  box-shadow: 0 0 12px rgba(120, 210, 255, 0.3);
  transition:
    transform 0.12s ease-out,
    background 0.25s ease,
    box-shadow 0.25s ease,
    border-color 0.25s ease;
}

.tube-h .tube-bubble {
  transform: translate(calc(-50% + var(--bx) * var(--th-travel)), -50%);
}

.tube-v .tube-bubble {
  transform: translate(-50%, calc(-50% + var(--by) * var(--tv-travel)));
}

/* Level state highlight */
body.is-level .bubble,
body.is-level .tube-bubble {
  background: radial-gradient(
    circle at 36% 32%,
    rgba(255, 255, 255, 0.95) 0%,
    rgba(120, 235, 180, 0.5) 55%,
    rgba(62, 207, 142, 0.35) 100%
  );
  border-color: rgba(150, 245, 200, 0.85);
  box-shadow: 0 0 26px var(--level-glow);
}

body.is-level .tilt-value {
  color: var(--level);
}

body.is-level .level-badge {
  color: #06231a;
  background: var(--level);
  border-color: var(--level);
  box-shadow: 0 0 18px var(--level-glow);
}

/* Controls */
.controls {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: center;
}

.btn {
  appearance: none;
  min-height: var(--touch-min);
  padding: 0.6rem 1.1rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    transform 0.05s ease;
}

.btn:hover:not(:disabled) {
  background: #2b344a;
  border-color: #40507040;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #061224;
}

.btn.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.status {
  margin: 0;
  min-height: 1.2em;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.hint {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
  opacity: 0.85;
}

@media (prefers-reduced-motion: reduce) {
  .bubble,
  .tube-bubble {
    transition: none;
  }
}

/* Short landscape (a phone turned sideways): put the readout on the left and the
   active level on the right so it stays visible without scrolling. */
@media (orientation: landscape) and (max-height: 640px) {
  .app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    column-gap: 1.5rem;
    row-gap: 0.55rem;
    max-width: 940px;
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
  }

  .header,
  .enable-panel,
  .readout,
  .controls,
  #status,
  .hint {
    grid-column: 1;
    width: 100%;
  }

  .levels {
    grid-column: 2;
    grid-row: 1 / span 12;
    align-self: center;
  }

  .subtitle {
    display: none;
  }
}
