/* PeriCom Test Terminal — styled on the fab4minds FabUI design tokens.
 *
 * Values are inlined copies of the generated FabUI custom properties, keeping the
 * original `--fab-ui-*` names so every colour stays traceable to its token. Source:
 *   source/FabUIStyleDictionary/src/assets/tokens/{core,shared}.json
 *   source/FabUIStyleDictionary/src/assets/tokens/themes/{light,dark}.json
 * They are inlined rather than imported because this app ships without a build
 * step and the compiled fabular.css is not checked in.
 *
 * Theme selectors follow the generator (src/tokens/buildTokens.js):
 *   `:root, .variant-light` for the default theme, `.variant-dark` for dark.
 * prefers-color-scheme picks dark only when no variant class is set explicitly.
 */

/* ------------------------------------------------------- shared scale tokens */

:root {
  /* shared.borderRadius */
  --fab-ui-shared-border-radius-radius-4: 8px;
  --fab-ui-shared-border-radius-radius-6: 12px;
  --fab-ui-shared-border-radius-radius-pill: 10000px;

  /* shared.spacing */
  --fab-ui-shared-spacing-sp-1: 2px;
  --fab-ui-shared-spacing-sp-2: 4px;
  --fab-ui-shared-spacing-sp-3: 6px;
  --fab-ui-shared-spacing-sp-4: 8px;
  --fab-ui-shared-spacing-sp-5: 12px;
  --fab-ui-shared-spacing-sp-6: 16px;
  --fab-ui-shared-spacing-sp-8: 24px;
  --fab-ui-shared-spacing-sp-11: 48px;

  /* shared.borderWidth */
  --fab-ui-shared-border-width-border-1: 1px;
  --fab-ui-shared-border-width-border-2: 2px;

  /* shared.dimension.height */
  --fab-ui-shared-dimension-height-h-36: 36px;

  /* core.fontFamily — bundled in the monorepo, falls back when not installed */
  --fab-ui-core-font-family-inter: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --fab-ui-core-font-family-monospaced: CommitMonof4m, "IBM Plex Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;

  /* core.fontWeight */
  --fab-ui-core-font-weight-regular: 400;
  --fab-ui-core-font-weight-medium: 500;
  --fab-ui-core-font-weight-semibold: 600;
  --fab-ui-core-font-weight-bold: 700;
}

/* ------------------------------------------------------------- light (default) */

:root,
.variant-light {
  --fab-ui-theme-color-background-primary: #EEF2F6;
  --fab-ui-theme-color-background-secondary: #FFFFFF;
  --fab-ui-theme-color-background-tertiary: #EEF2F6;
  --fab-ui-theme-color-background-quaternary: #E3E8EF;
  --fab-ui-theme-color-background-disabled: #EEF2F6;
  --fab-ui-theme-color-background-error: #FFE4E8;
  --fab-ui-theme-color-background-success: #D3F8DF;
  --fab-ui-theme-color-background-warning: #FDEAD7;

  --fab-ui-theme-color-text-primary: #121926;
  --fab-ui-theme-color-text-secondary: #4B5565;
  --fab-ui-theme-color-text-tertiary: #697586;
  --fab-ui-theme-color-text-white: #FFFFFF;
  --fab-ui-theme-color-text-accent: #1570EF;
  --fab-ui-theme-color-text-error: #F63D68;
  --fab-ui-theme-color-text-success: #16B364;
  --fab-ui-theme-color-text-warning: #EF6820;

  --fab-ui-theme-color-border-primary: #CDD5DF;
  --fab-ui-theme-color-border-secondary: #E3E8EF;
  --fab-ui-theme-color-border-tertiary: #EEF2F6;
  --fab-ui-theme-color-border-accent: #1570EF;
  --fab-ui-theme-color-border-error: #FD6F8E;
  --fab-ui-theme-color-border-success: #3CCB7F;
  --fab-ui-theme-color-border-warning: #F38744;

  --fab-ui-theme-color-icon-secondary: #364152;

  --fab-ui-theme-color-accent-blue: #53B1FD;

  --fab-ui-component-button-color-background-default-primary: #1570EF;
  --fab-ui-component-button-color-background-hover-primary: #175CD3;
  --fab-ui-component-button-color-text-primary: #FFFFFF;
  --fab-ui-component-button-color-background-default-secondary: #FFFFFF;
  --fab-ui-component-button-color-text-secondary: #4B5565;
  --fab-ui-component-button-color-border-secondary: #E3E8EF;
}

/* --------------------------------------------------------------------- dark */

.variant-dark {
  --fab-ui-theme-color-background-primary: #202939;
  --fab-ui-theme-color-background-secondary: #2B3545;
  --fab-ui-theme-color-background-tertiary: #4B5565;
  --fab-ui-theme-color-background-quaternary: #697586;
  --fab-ui-theme-color-background-disabled: #364152;
  --fab-ui-theme-color-background-error: #89123E;
  --fab-ui-theme-color-background-success: #084C2E;
  --fab-ui-theme-color-background-warning: #772917;

  --fab-ui-theme-color-text-primary: #F8FAFC;
  --fab-ui-theme-color-text-secondary: #F8FAFC;
  --fab-ui-theme-color-text-tertiary: #CDD5DF;
  --fab-ui-theme-color-text-white: #FFFFFF;
  --fab-ui-theme-color-text-accent: #2E90FA;
  --fab-ui-theme-color-text-error: #FD6F8E;
  --fab-ui-theme-color-text-success: #3CCB7F;
  --fab-ui-theme-color-text-warning: #F38744;

  --fab-ui-theme-color-border-primary: #364152;
  --fab-ui-theme-color-border-secondary: #697586;
  --fab-ui-theme-color-border-tertiary: #9AA4B2;
  --fab-ui-theme-color-border-accent: #1570EF;
  --fab-ui-theme-color-border-error: #FD6F8E;
  --fab-ui-theme-color-border-success: #3CCB7F;
  --fab-ui-theme-color-border-warning: #F38744;

  --fab-ui-theme-color-icon-secondary: #CDD5DF;

  --fab-ui-theme-color-accent-blue: #53B1FD;

  --fab-ui-component-button-color-background-default-primary: #1570EF;
  --fab-ui-component-button-color-background-hover-primary: #2E90FA;
  --fab-ui-component-button-color-text-primary: #FFFFFF;
  --fab-ui-component-button-color-background-default-secondary: #4B5565;
  --fab-ui-component-button-color-text-secondary: #F8FAFC;
  --fab-ui-component-button-color-border-secondary: transparent;
}

@media (prefers-color-scheme: dark) {
  :root:not(.variant-light) {
    --fab-ui-theme-color-background-primary: #202939;
    --fab-ui-theme-color-background-secondary: #2B3545;
    --fab-ui-theme-color-background-tertiary: #4B5565;
    --fab-ui-theme-color-background-quaternary: #697586;
    --fab-ui-theme-color-background-disabled: #364152;
    --fab-ui-theme-color-background-error: #89123E;
    --fab-ui-theme-color-background-success: #084C2E;
    --fab-ui-theme-color-background-warning: #772917;

    --fab-ui-theme-color-text-primary: #F8FAFC;
    --fab-ui-theme-color-text-secondary: #F8FAFC;
    --fab-ui-theme-color-text-tertiary: #CDD5DF;
    --fab-ui-theme-color-text-accent: #2E90FA;
    --fab-ui-theme-color-text-error: #FD6F8E;
    --fab-ui-theme-color-text-success: #3CCB7F;
    --fab-ui-theme-color-text-warning: #F38744;

    --fab-ui-theme-color-border-primary: #364152;
    --fab-ui-theme-color-border-secondary: #697586;
    --fab-ui-theme-color-border-tertiary: #9AA4B2;

    --fab-ui-theme-color-icon-secondary: #CDD5DF;

    --fab-ui-component-button-color-background-hover-primary: #2E90FA;
    --fab-ui-component-button-color-background-default-secondary: #4B5565;
    --fab-ui-component-button-color-text-secondary: #F8FAFC;
    --fab-ui-component-button-color-border-secondary: transparent;
  }
}

/* ------------------------------------------------------------------- base */

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--fab-ui-theme-color-background-primary);
  color: var(--fab-ui-theme-color-text-primary);
  font-family: var(--fab-ui-core-font-family-inter);
  font-size: 14px;
  line-height: 1.5;
}

h1, h2 { margin: 0; }
h1 { font-size: 18px; font-weight: var(--fab-ui-core-font-weight-semibold); }

h2 {
  font-size: 12px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fab-ui-theme-color-text-tertiary);
}

code {
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 0.92em;
  color: var(--fab-ui-theme-color-text-secondary);
}

/* ----------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--fab-ui-shared-spacing-sp-6);
  flex-wrap: wrap;
  padding: var(--fab-ui-shared-spacing-sp-5) var(--fab-ui-shared-spacing-sp-8);
  background: var(--fab-ui-theme-color-background-secondary);
  border-bottom: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-secondary);
}

.topbar__title p {
  margin: var(--fab-ui-shared-spacing-sp-1) 0 0;
  font-size: 12px;
  color: var(--fab-ui-theme-color-text-tertiary);
}

.topbar__state {
  display: flex;
  align-items: center;
  gap: var(--fab-ui-shared-spacing-sp-5);
  font-size: 13px;
  color: var(--fab-ui-theme-color-text-secondary);
}

.topbar__status {
  display: flex;
  align-items: center;
  gap: var(--fab-ui-shared-spacing-sp-4);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: var(--fab-ui-shared-border-radius-radius-pill);
  background: var(--fab-ui-theme-color-text-tertiary);
}
.dot--open       { background: var(--fab-ui-theme-color-text-success); }
.dot--connecting { background: var(--fab-ui-theme-color-text-warning); animation: pulse 1s ease-in-out infinite; }
.dot--closed     { background: var(--fab-ui-theme-color-text-tertiary); }
.dot--error      { background: var(--fab-ui-theme-color-text-error); }

@keyframes pulse { 50% { opacity: 0.35; } }

.badge {
  padding: var(--fab-ui-shared-spacing-sp-1) var(--fab-ui-shared-spacing-sp-4);
  border-radius: var(--fab-ui-shared-border-radius-radius-pill);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-secondary);
  font-size: 11px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--fab-ui-theme-color-text-tertiary);
}
.badge--ok {
  border-color: var(--fab-ui-theme-color-border-success);
  background: var(--fab-ui-theme-color-background-success);
  color: var(--fab-ui-theme-color-text-success);
}
.badge--warn {
  border-color: var(--fab-ui-theme-color-border-warning);
  background: var(--fab-ui-theme-color-background-warning);
  color: var(--fab-ui-theme-color-text-warning);
}
.badge--muted { color: var(--fab-ui-theme-color-text-tertiary); }

/* ----------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: var(--fab-ui-shared-spacing-sp-6);
  padding: var(--fab-ui-shared-spacing-sp-6) var(--fab-ui-shared-spacing-sp-8) var(--fab-ui-shared-spacing-sp-11);
  align-items: start;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

.column {
  display: flex;
  flex-direction: column;
  gap: var(--fab-ui-shared-spacing-sp-6);
  min-width: 0;
}

.card {
  background: var(--fab-ui-theme-color-background-secondary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-secondary);
  border-radius: var(--fab-ui-shared-border-radius-radius-6);
  padding: var(--fab-ui-shared-spacing-sp-6);
  display: flex;
  flex-direction: column;
  gap: var(--fab-ui-shared-spacing-sp-5);
}

.card[hidden] { display: none; }

/* ----------------------------------------------------------------- fields */

.field { display: flex; flex-direction: column; gap: var(--fab-ui-shared-spacing-sp-3); }
.field--row { flex-direction: row; gap: var(--fab-ui-shared-spacing-sp-4); align-items: flex-end; flex-wrap: wrap; }
.field--row > div {
  flex: 1 1 0;
  min-width: 110px;
  display: flex;
  flex-direction: column;
  gap: var(--fab-ui-shared-spacing-sp-3);
}

label {
  font-size: 11px;
  font-weight: var(--fab-ui-core-font-weight-medium);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fab-ui-theme-color-text-tertiary);
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--fab-ui-shared-spacing-sp-3);
  font-size: 13px;
  font-weight: var(--fab-ui-core-font-weight-regular);
  text-transform: none;
  letter-spacing: 0;
  color: var(--fab-ui-theme-color-text-primary);
  cursor: pointer;
}

input[type="text"], input[type="number"], select {
  width: 100%;
  padding: var(--fab-ui-shared-spacing-sp-4) var(--fab-ui-shared-spacing-sp-5);
  background: var(--fab-ui-theme-color-background-secondary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-primary);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  color: var(--fab-ui-theme-color-text-primary);
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 13px;
}

input:focus-visible, select:focus-visible, button:focus-visible {
  outline: var(--fab-ui-shared-border-width-border-2) solid var(--fab-ui-theme-color-border-accent);
  outline-offset: 1px;
}

input[type="checkbox"] { accent-color: var(--fab-ui-theme-color-border-accent); }

.hint {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--fab-ui-theme-color-text-tertiary);
}
.hint--mono {
  font-family: var(--fab-ui-core-font-family-monospaced);
  color: var(--fab-ui-theme-color-text-secondary);
  word-break: break-all;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  padding: var(--fab-ui-shared-spacing-sp-4) var(--fab-ui-shared-spacing-sp-6);
  background: var(--fab-ui-component-button-color-background-default-secondary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-component-button-color-border-secondary);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  color: var(--fab-ui-component-button-color-text-secondary);
  font-family: var(--fab-ui-core-font-family-inter);
  font-size: 13px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  line-height: 1.2;
  cursor: pointer;
  transition: background-color 120ms ease-out, border-color 120ms ease-out, opacity 120ms ease-out;
}
.btn:hover:not(:disabled) { border-color: var(--fab-ui-theme-color-border-primary); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn--primary {
  background: var(--fab-ui-component-button-color-background-default-primary);
  border-color: var(--fab-ui-component-button-color-background-default-primary);
  color: var(--fab-ui-component-button-color-text-primary);
}
.btn--primary:hover:not(:disabled) {
  background: var(--fab-ui-component-button-color-background-hover-primary);
  border-color: var(--fab-ui-component-button-color-background-hover-primary);
}

.btn--sm { padding: var(--fab-ui-shared-spacing-sp-3) var(--fab-ui-shared-spacing-sp-5); font-size: 12px; }
.btn--ghost { background: transparent; border-color: transparent; }
.btn--ghost:hover:not(:disabled) { background: var(--fab-ui-theme-color-background-tertiary); }
.btn--busy { opacity: 0.6; }

/* -------------------------------------------------------------- the scale */

.scale__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--fab-ui-shared-spacing-sp-5);
  flex-wrap: wrap;
}

.scale__readout {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: var(--fab-ui-shared-spacing-sp-4);
  padding: var(--fab-ui-shared-spacing-sp-2) 0;
}

.scale__value {
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: clamp(44px, 9vw, 76px);
  font-weight: var(--fab-ui-core-font-weight-bold);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: -0.02em;
}
.scale__value--negative { color: var(--fab-ui-theme-color-text-warning); }

.scale__unit {
  font-size: 22px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  color: var(--fab-ui-theme-color-text-secondary);
}

/* Track, fill and transition follow the FabUI pocket live-scale component:
 * source/FabUIStyleDictionary/src/tokens/sassTemplates/_pocketLiveScale.scss */
.scale__bar {
  position: relative;
  height: var(--fab-ui-shared-dimension-height-h-36);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  background: var(--fab-ui-theme-color-background-quaternary, var(--fab-ui-theme-color-background-tertiary));
  overflow: hidden;
}

.scale__bar-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--fab-ui-theme-color-accent-blue);
  transition: width 180ms ease-out, background-color 180ms ease-out;
}
.scale__bar-fill--unstable { background: #fdb022; }
.scale__bar-fill--negative { background: #fd6f8e; }

.scale__bar-overflow {
  position: absolute;
  inset: 0;
  border: var(--fab-ui-shared-border-width-border-2) solid var(--fab-ui-theme-color-border-error);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  pointer-events: none;
}

.scale__bar-legend {
  display: flex;
  justify-content: space-between;
  margin-top: calc(var(--fab-ui-shared-spacing-sp-3) * -1);
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 11px;
  color: var(--fab-ui-theme-color-text-tertiary);
}

.scale__stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--fab-ui-shared-spacing-sp-4);
  margin: 0;
}
.scale__stats > div {
  background: var(--fab-ui-theme-color-background-tertiary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-tertiary);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  padding: var(--fab-ui-shared-spacing-sp-4) var(--fab-ui-shared-spacing-sp-5);
}
.scale__stats dt {
  font-size: 10.5px;
  font-weight: var(--fab-ui-core-font-weight-medium);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fab-ui-theme-color-text-tertiary);
}
.scale__stats dd {
  margin: var(--fab-ui-shared-spacing-sp-1) 0 0;
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 16px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  font-variant-numeric: tabular-nums;
}

.scale__range { align-items: flex-end; }
.presets { display: flex; gap: var(--fab-ui-shared-spacing-sp-3); flex-wrap: wrap; flex: 2 1 auto; }

/* --------------------------------------------------------------- actions */

.actions {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: var(--fab-ui-shared-spacing-sp-4);
}

@media (max-width: 620px) {
  .actions { grid-template-columns: 1fr; }
  .scale__stats { grid-template-columns: 1fr; }
}

.actions .btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--fab-ui-shared-spacing-sp-1);
  padding: var(--fab-ui-shared-spacing-sp-5) var(--fab-ui-shared-spacing-sp-5);
  font-size: 15px;
}
.actions .btn small {
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 11px;
  font-weight: var(--fab-ui-core-font-weight-regular);
  text-transform: none;
  opacity: 0.75;
}

.error {
  margin: 0;
  padding: var(--fab-ui-shared-spacing-sp-4) var(--fab-ui-shared-spacing-sp-5);
  background: var(--fab-ui-theme-color-background-error);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-error);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  font-size: 13px;
  color: var(--fab-ui-theme-color-text-error);
}

/* --------------------------------------------------------------- results */

.lastweigh {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: var(--fab-ui-shared-spacing-sp-4);
}
.lastweigh > div {
  background: var(--fab-ui-theme-color-background-tertiary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-tertiary);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  padding: var(--fab-ui-shared-spacing-sp-4) var(--fab-ui-shared-spacing-sp-5);
}
.lastweigh span {
  display: block;
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  color: var(--fab-ui-theme-color-text-tertiary);
}
.lastweigh strong {
  display: block;
  margin-top: var(--fab-ui-shared-spacing-sp-1);
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 15px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  font-variant-numeric: tabular-nums;
}

.ok  { color: var(--fab-ui-theme-color-text-success); }
.bad { color: var(--fab-ui-theme-color-text-error); }

.table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 12.5px;
}
.table th {
  text-align: left;
  padding: var(--fab-ui-shared-spacing-sp-3) var(--fab-ui-shared-spacing-sp-4);
  font-size: 10.5px;
  font-weight: var(--fab-ui-core-font-weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fab-ui-theme-color-text-tertiary);
  border-bottom: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-primary);
}
.table td {
  padding: var(--fab-ui-shared-spacing-sp-3) var(--fab-ui-shared-spacing-sp-4);
  border-bottom: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-tertiary);
  font-variant-numeric: tabular-nums;
}
.table__empty td { color: var(--fab-ui-theme-color-text-tertiary); text-align: center; }

/* ------------------------------------------------------------------- log */

.log {
  height: 260px;
  overflow: auto;
  padding: var(--fab-ui-shared-spacing-sp-5);
  background: var(--fab-ui-theme-color-background-tertiary);
  border: var(--fab-ui-shared-border-width-border-1) solid var(--fab-ui-theme-color-border-tertiary);
  border-radius: var(--fab-ui-shared-border-radius-radius-4);
  font-family: var(--fab-ui-core-font-family-monospaced);
  font-size: 11.5px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
}

.log__line--out   { color: var(--fab-ui-theme-color-text-accent); }
.log__line--in    { color: var(--fab-ui-theme-color-text-success); }
.log__line--info  { color: var(--fab-ui-theme-color-text-tertiary); }
.log__line--error { color: var(--fab-ui-theme-color-text-error); }
