/* Impeccable refinement: legible, touch-safe and responsive without changing app logic. */
:root {
  --surface-1: #111513;
  --surface-2: #191e1c;
  --surface-3: #222825;
  --ios-blue: #58a6ff;
  --focus-ring: rgba(88, 166, 255, 0.68);
}

html {
  min-width: 320px;
  color-scheme: dark;
  background: #070a09;
  -webkit-text-size-adjust: 100%;
}

body {
  min-width: 320px;
  color: #f5fbf8;
  background-color: #070a09;
  overscroll-behavior-y: contain;
}

::selection {
  color: #04100c;
  background: var(--accent);
}

button,
input,
select {
  touch-action: manipulation;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  filter: grayscale(0.35);
  opacity: 0.52;
}

.panel {
  background: rgba(22, 25, 24, 0.94);
  border-color: rgba(255, 255, 255, 0.13);
  box-shadow: 0 14px 38px rgba(0, 0, 0, 0.32);
}

.section-title {
  color: #bac3c0;
  font-weight: 750;
  letter-spacing: 0.09em;
}

.readout {
  background: linear-gradient(180deg, rgba(70, 242, 188, 0.10), rgba(255, 255, 255, 0.025));
}

.active-code {
  color: #f6fffc;
  letter-spacing: -0.04em;
}

.status {
  color: #c0c8c5;
  font-size: 13px;
}

.transport-icon svg {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.transport-icon path { fill: none; }

.protocol-runner { position: fixed; }
.protocol-runner-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  border-radius: 50%;
  font-size: 24px;
  line-height: 1;
  z-index: 2;
}

.meter {
  height: 8px;
  background: rgba(255, 255, 255, 0.10);
}

.wave-readout,
.wave-stage-info .stage-copy,
.stage-copy {
  color: #c1cbc7;
}

.stage-stat span {
  color: #bec9c5;
}

@media (pointer: coarse) {
  button,
  select,
  input:not([type="checkbox"]) {
    min-height: 44px;
  }

  button:active {
    transform: scale(0.975);
  }
}

@media (max-width: 700px) {
  html,
  body {
    width: 100%;
    height: auto;
    min-height: 100%;
    overflow-x: clip;
    overflow-y: auto;
  }

  body {
    padding: 0;
    background: #070a09;
  }

  .app {
    width: 100%;
    height: auto;
    min-height: 100dvh;
    margin: 0;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(18px, env(safe-area-inset-bottom));
    padding-left: max(8px, env(safe-area-inset-left));
    overflow: visible;
  }

  .grid {
    display: grid;
    width: 100%;
    height: auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto;
    gap: 10px;
  }

  .screen {
    order: 1;
    min-height: 0;
    overflow: visible;
    gap: 10px;
  }

  .controls {
    order: 2;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    overflow: visible;
  }

  .controls > .panel,
  .controls .panel:nth-child(1),
  .controls .panel:nth-child(2),
  .controls .panel:nth-child(3) {
    display: block;
    width: 100%;
    grid-column: 1;
  }

  .controls .panel:nth-child(4),
  .controls .panel:nth-child(5),
  .screen .panel:nth-child(3) {
    display: none;
  }

  body.mobile-setup-open .controls .panel:nth-child(5) {
    display: block;
  }

  .panel {
    overflow: hidden;
    border-radius: 16px;
  }

  .controls .panel-pad,
  .screen .panel-pad {
    padding: 12px;
  }

  .section-title {
    margin-bottom: 8px;
    font-size: 12px;
    line-height: 1.2;
  }

  button {
    min-height: 44px;
    font-size: 14px;
  }

  .readout {
    min-height: 82px;
    padding: 12px 14px;
  }

  .active-code {
    font-size: clamp(52px, 17vw, 72px);
    line-height: 0.95;
  }

  .freq-buttons {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    gap: 4px;
  }

  .freq-buttons button {
    min-width: 0;
    min-height: 46px;
    padding: 0 2px;
    border-radius: 11px;
    font-size: clamp(13px, 4vw, 16px);
    font-weight: 850;
  }

  .stage-section-head {
    align-items: flex-start;
    gap: 8px;
  }

  .stage-section-head .section-title {
    flex: 1 1 auto;
    padding-top: 5px;
    font-size: 11px;
  }

  .stage-head-actions {
    display: grid;
    flex: 0 1 208px;
    grid-template-columns: 40px repeat(2, minmax(0, 1fr));
    gap: 5px;
  }

  #stageInfoBtn,
  #exactToggleBtn,
  #oscToggleBtn,
  .stage-head-actions .stage-inline-info {
    width: auto;
    min-width: 0;
    min-height: 44px;
    padding-inline: 5px;
    font-size: 10px;
  }

  .therapy-stage,
  body.template-2 .therapy-stage,
  body.template-4 .therapy-stage,
  body.template-5 .therapy-stage,
  body.template-7 .therapy-stage,
  body.template-11 .therapy-stage {
    height: 224px;
    min-height: 224px;
    grid-template-columns: 56px minmax(72px, 1fr) minmax(118px, 0.9fr);
    gap: 5px;
    padding: 8px;
  }

  .therapy-stage .laser-device {
    display: grid;
    width: 52px;
  }

  .therapy-stage .beam-field {
    display: block;
    min-height: 168px;
  }

  .therapy-stage .human-field {
    display: grid;
    height: 202px;
  }

  .human-field .aura {
    height: 168px;
  }

  .wave-display {
    display: grid;
    grid-template-columns: minmax(122px, 0.8fr) minmax(0, 1.2fr);
    gap: 8px;
    margin-top: 10px;
  }

  .wave-stage-info {
    min-height: 82px;
    padding: 8px;
  }

  .wave-stage-info .stage-title {
    font-size: 14px;
  }

  .wave-stage-info .stage-copy {
    font-size: 11px;
  }

  .wave-stage-info .stage-stat span {
    min-height: 34px;
    font-size: 9px;
  }

  .wave-stage-info .stage-stat strong {
    font-size: 14px;
  }

  #waveCanvas {
    height: 96px;
  }

  .emission-adjust {
    padding: 10px;
    margin-bottom: 10px;
  }

  .adjust-row {
    grid-template-columns: 1fr 1fr 1.35fr;
    gap: 8px;
  }

  .adjust-row button,
  .transport button {
    min-height: 50px;
  }

#lightBtn {
    min-width: 0;
    padding-inline: 4px;
    font-size: 12px;
    white-space: nowrap;
  }

  .transport-icon { gap: 0; padding-inline: 4px; }
  .transport-icon svg { width: 23px; height: 23px; fill: currentColor; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .transport-icon path { fill: none; }
  .transport-icon span { display: none; }

  .protocol-runner { padding-top: 48px; }
  .protocol-runner-close { position:absolute;top:8px;right:8px;width:36px;min-width:36px;min-height:36px;padding:0;border-radius:50%;font-size:24px;line-height:1;z-index:2; }

  .transport {
    gap: 8px;
  }

  .special-buttons {
    gap: 8px;
  }

  .special-buttons button {
    min-height: 46px;
    padding-inline: 5px;
    font-size: 13px;
  }

  .info-actions {
    gap: 8px;
    margin-top: 8px;
  }

  .info-actions .info-open,
  .info-actions .ai-open,
  .info-actions button {
    min-height: 46px;
    padding-inline: 8px;
    font-size: 12px;
    overflow-wrap: normal;
  }

  .status {
    min-height: 30px;
    padding: 7px 3px 1px;
    font-size: 13px;
  }

  .meter {
    width: 110px;
  }

  #liteBadge,
  #licenseBadge {
    min-height: 44px;
    padding-inline: 12px;
    font-size: 11px;
  }

  .info-modal-card,
  .ai-modal-card,
  .setup-modal-card,
  .safety-modal-card,
  .protocol-modal-card {
    width: calc(100% - 16px);
    max-height: calc(100dvh - 20px);
    border-radius: 18px;
  }
}

@media (max-width: 359px) {
  .app {
    padding-inline: max(5px, env(safe-area-inset-left), env(safe-area-inset-right));
  }

  .controls .panel-pad,
  .screen .panel-pad {
    padding: 9px;
  }

  .freq-buttons {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
  }

  .freq-buttons button {
    min-height: 44px;
    font-size: 12px;
  }

  .therapy-stage,
  body.template-2 .therapy-stage,
  body.template-4 .therapy-stage,
  body.template-5 .therapy-stage,
  body.template-7 .therapy-stage,
  body.template-11 .therapy-stage {
    grid-template-columns: 50px minmax(65px, 1fr) minmax(106px, 0.9fr);
  }

  .special-buttons button,
  .info-actions button {
    font-size: 11px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
