:root {
  color-scheme: light;
  --bg: #f6f7f4;
  --panel: #ffffff;
  --ink: #17211f;
  --muted: #5b6864;
  --line: #d7ded9;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --danger: #a33b35;
  --warning-bg: #fff4d7;
  --warning-ink: #59410d;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

.app {
  width: min(960px, 100%);
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  display: grid;
  gap: 14px;
}

.scanner-panel,
.ble-panel,
.settings,
.history {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.topbar,
.history-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.6rem, 7vw, 2.4rem);
  line-height: 1.05;
}

h2 {
  font-size: 1.05rem;
}

p,
small,
.section-note,
.history-head span {
  color: var(--muted);
}

#supportText {
  margin-top: 6px;
  line-height: 1.45;
}

.version-line {
  margin-top: 6px;
  color: var(--danger);
  font-size: 0.9rem;
  font-weight: 800;
}

.badge {
  flex: 0 0 auto;
  min-width: 72px;
  padding: 7px 10px;
  border-radius: 999px;
  background: #dcece8;
  color: #0b5d55;
  text-align: center;
  font-size: 0.88rem;
  font-weight: 700;
}

.badge.is-off {
  background: #ece7df;
  color: #6a5440;
}

.badge.is-alert {
  background: #f6d8d5;
  color: #7c2924;
}

.scan-zone {
  position: relative;
  margin-top: 12px;
  min-height: 190px;
  border: 2px dashed #9ab8b1;
  border-radius: 8px;
  display: grid;
  place-items: center;
  padding: 18px;
  background: #eef7f4;
  outline: none;
}

.scan-zone:focus,
.scan-zone.is-armed {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}

.capture-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.01;
  border: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
}

.scan-readout {
  pointer-events: none;
  width: 100%;
  text-align: center;
  display: grid;
  gap: 9px;
}

.scan-label {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
  letter-spacing: 0;
}

#latestValue {
  overflow-wrap: anywhere;
  font-size: clamp(2rem, 11vw, 4.25rem);
  line-height: 1;
}

.target-picker {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.target-button {
  min-height: 78px;
  display: grid;
  align-content: center;
  gap: 6px;
  text-align: left;
  padding: 12px;
}

.target-button span {
  color: var(--muted);
  font-size: 0.88rem;
}

.target-button strong {
  overflow-wrap: anywhere;
  font-size: 1.05rem;
}

.target-button.is-active {
  border-color: var(--accent);
  background: #e5f4ef;
  box-shadow: inset 0 0 0 1px var(--accent);
}

.actions {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

button {
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-weight: 750;
}

button:disabled {
  color: #9aa49f;
  background: #f1f3f0;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

button.danger {
  color: var(--danger);
}

.ble-panel {
  display: grid;
  gap: 12px;
}

.ble-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

@media (min-width: 520px) {
  .ble-actions {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.ble-form {
  display: grid;
  gap: 10px;
}

.ble-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
}

.ble-form input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #ffffff;
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9rem;
}

.ble-log {
  min-height: 92px;
  max-height: 180px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border-radius: 8px;
  background: #17211f;
  color: #e5f4ef;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 0.86rem;
}

.warning {
  margin-top: 14px;
  padding: 11px 12px;
  border-radius: 8px;
  background: var(--warning-bg);
  color: var(--warning-ink);
  line-height: 1.45;
}

.settings {
  display: grid;
  gap: 12px;
}

.settings label {
  display: grid;
  grid-template-columns: minmax(94px, 1fr) minmax(120px, 2fr) auto;
  align-items: center;
  gap: 10px;
  color: var(--muted);
}

.settings select,
.settings input[type="number"] {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #ffffff;
  color: var(--ink);
}

.settings .check {
  grid-template-columns: auto 1fr;
  justify-content: start;
  color: var(--ink);
}

.settings input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
}

.history-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.history-list li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfa;
}

.history-target {
  min-width: 28px;
  height: 28px;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  background: #dcece8;
  color: #0b5d55;
  font-size: 0.84rem;
  font-weight: 850;
}

.history-value {
  overflow-wrap: anywhere;
  font-weight: 800;
}

.history-time {
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
}

@media (min-width: 760px) {
  .app {
    grid-template-columns: 1.45fr 0.8fr;
    align-items: start;
  }

  .scanner-panel,
  .ble-panel,
  .history {
    grid-column: 1;
  }

  .settings {
    grid-column: 2;
    grid-row: 1 / span 2;
    position: sticky;
    top: 16px;
  }

  .actions {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 420px) {
  .topbar {
    display: grid;
  }

  .badge {
    justify-self: start;
  }

  .settings label {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .settings .check {
    grid-template-columns: auto 1fr;
  }
}
