/* Compact widget + modal UI.
   Primary:   #3a81c0, #bfe5fc, #2c5a8f, #39afe7
   Secondary: #c75ea3, #faa331, #5578de, #51a8b1 */

:root {
  color-scheme: light dark;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;

  --primary: #3a81c0;
  --primary-2: #bfe5fc;
  --primary-3: #2c5a8f;
  --primary-4: #39afe7;

  --secondary: #c75ea3;
  --secondary-2: #faa331;
  --secondary-3: #5578de;
  --secondary-4: #51a8b1;

  --text: #0b1220;
  --muted: rgba(11, 18, 32, 0.62);
  --stroke: rgba(11, 18, 32, 0.12);
  --surface: rgba(255, 255, 255, 0.88);
  --surface-solid: #ffffff;

  --shadow: 0 18px 50px rgba(44, 90, 143, 0.22);
  --shadow-soft: 0 10px 30px rgba(44, 90, 143, 0.16);

  --radius: 18px;
  --ease: cubic-bezier(0.2, 0.9, 0.25, 1);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body.is-modal-open .widget {
  opacity: 0;
  transform: scale(0.98);
  pointer-events: none;
  transition: opacity 160ms var(--ease), transform 160ms var(--ease);
}

body {
  margin: 0;
  color: var(--text);
  background: #f7fbff;
}

/* Standalone app shell. Not required when embedding just the widget on another page. */
.app-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
  /* background:
    radial-gradient(1000px 550px at 25% 25%, var(--primary-2), transparent 55%),
    radial-gradient(900px 500px at 85% 15%, rgba(199, 94, 163, 0.22), transparent 55%),
    radial-gradient(1000px 650px at 70% 85%, rgba(85, 120, 222, 0.22), transparent 60%),
    #f7fbff; */
}

.widget {
  width: 300px;
  height: 200px;
  border-radius: var(--radius);
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.75));
  /* border: 1px solid rgba(44, 90, 143, 0.14); */
  box-shadow: var(--shadow-soft);
  display: grid;
  grid-template-rows: auto 1fr;
  padding: 14px 14px 16px;
  position: relative;
  overflow: hidden;
}

.widget::before {
  content: '';
  position: absolute;
  inset: -40px;
  background:
    radial-gradient(220px 180px at 20% 20%, rgba(58, 129, 192, 0.35), transparent 60%),
    radial-gradient(240px 200px at 85% 15%, rgba(57, 175, 231, 0.28), transparent 60%),
    radial-gradient(260px 220px at 70% 85%, rgba(199, 94, 163, 0.22), transparent 65%);
  filter: blur(10px);
  pointer-events: none;
}

.widget__header {
  position: relative;
  z-index: 1;
}

.widget__header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.widget__lang {
  height: 28px;
  min-width: 44px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid rgba(44, 90, 143, 0.14);
  background: rgba(255, 255, 255, 0.75);
  color: var(--primary-3);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.06em;
  cursor: pointer;
}

.widget__lang:hover {
  background: rgba(191, 229, 252, 0.55);
}

.widget__title {
  margin: 0;
  font-size: 14px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--primary-3);
}

.widget__subtitle {
  margin: 6px 0 0;
  font-size: 11px;
  line-height: 1.2;
  color: var(--muted);
}

.go {
  position: relative;
  z-index: 1;
  align-self: center;
  justify-self: center;
  width: 92px;
  height: 92px;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  background:
    radial-gradient(circle at 30% 25%, rgba(191, 229, 252, 0.95), rgba(191, 229, 252, 0.2) 55%, transparent 60%),
    linear-gradient(145deg, var(--primary), var(--primary-4));
  box-shadow: 0 18px 42px rgba(58, 129, 192, 0.35);
  transform: translateZ(0);
  transition: transform 160ms var(--ease), box-shadow 200ms var(--ease), filter 200ms var(--ease);
}

.go:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 22px 54px rgba(58, 129, 192, 0.42);
}

.go:active {
  transform: translateY(1px) scale(0.99);
  filter: saturate(1.1);
}

.go__label {
  position: relative;
  display: inline-block;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: white;
  font-size: 18px;
  text-shadow: 0 8px 20px rgba(44, 90, 143, 0.38);
}

.go__ring {
  position: absolute;
  inset: -10px;
  border-radius: inherit;
  background: conic-gradient(from 180deg, rgba(57, 175, 231, 0.65), rgba(199, 94, 163, 0.42), rgba(85, 120, 222, 0.5), rgba(57, 175, 231, 0.65));
  filter: blur(10px);
  opacity: 0.9;
  animation: pulse 1.9s var(--ease) infinite;
  pointer-events: none;
}

@keyframes pulse {
  0% {
    transform: scale(0.98);
    opacity: 0.7;
  }
  50% {
    transform: scale(1.06);
    opacity: 0.95;
  }
  100% {
    transform: scale(0.98);
    opacity: 0.7;
  }
}

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(11, 18, 32, 0.45);
  backdrop-filter: blur(8px);
  opacity: 0;
  transition: opacity 220ms var(--ease);
}

.backdrop.is-open {
  opacity: 1;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  width: min(720px, calc(100vw - 24px));
  height: min(920px, calc(100dvh - 24px));
  transform: translate(-50%, -50%) scale(0.98);
  opacity: 0;
  border-radius: 22px;
  color: rgba(255, 255, 255, 0.92);
  background:
    radial-gradient(900px 520px at 10% 0%, rgba(58, 129, 192, 0.32), transparent 55%),
    radial-gradient(900px 520px at 95% 10%, rgba(199, 94, 163, 0.22), transparent 55%),
    radial-gradient(900px 560px at 70% 90%, rgba(85, 120, 222, 0.22), transparent 60%),
    linear-gradient(160deg, rgba(18, 28, 46, 0.98), rgba(8, 12, 22, 0.98));
  border: 1px solid rgba(191, 229, 252, 0.18);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr;
  transition: opacity 260ms var(--ease), transform 260ms var(--ease);
}

/* Theme handling: dark is default; light overrides below */
.modal[data-theme='light'] {
  color: var(--text);
  background: linear-gradient(165deg, rgba(255, 255, 255, 0.96), rgba(255, 255, 255, 0.88));
  border: 1px solid rgba(44, 90, 143, 0.16);
}

.modal.is-open {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(191, 229, 252, 0.14);
}

.modal[data-theme='light'] .modal__header {
  border-bottom: 1px solid rgba(44, 90, 143, 0.12);
}

.modal__actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.modal__title {
  margin: 0;
  font-size: 14px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.92);
}

.modal[data-theme='light'] .modal__title {
  color: var(--primary-3);
}

.modal__subtitle {
  margin: 6px 0 0;
  font-size: 11px;
  color: rgba(191, 229, 252, 0.62);
  user-select: text;
}

.modal[data-theme='light'] .modal__subtitle {
  color: var(--muted);
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid rgba(191, 229, 252, 0.18);
  background: rgba(11, 18, 32, 0.45);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  color: rgba(191, 229, 252, 0.92);
  transition: transform 160ms var(--ease), background 160ms var(--ease);
}

.icon-btn--lang {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.06em;
}

.modal[data-theme='light'] .icon-btn {
  border: 1px solid rgba(44, 90, 143, 0.14);
  background: rgba(255, 255, 255, 0.75);
  color: rgba(44, 90, 143, 0.9);
}

.icon-btn:hover {
  transform: translateY(-1px);
  background: rgba(58, 129, 192, 0.24);
}

.modal[data-theme='light'] .icon-btn:hover {
  background: rgba(191, 229, 252, 0.55);
}

.modal__body {
  display: grid;
  /* Default layout (mobile-first): stacked */
  grid-template-columns: 1fr;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  padding: 10px;
  min-height: 0;
}

/* All screens: keep the "mobile" stacked layout (left above right) */

/* Small screens: avoid clipped top/bottom; allow scrolling only when necessary */
@media (max-width: 760px) {
  .modal {
    top: 14px;
    left: 14px;
    right: 14px;
    bottom: 14px;
    width: auto;
    height: auto;
    max-height: calc(100dvh - 28px);
    transform: translate(0, 0) scale(0.98);
    overflow: auto;
  }

  /* On mobile final screen, keep the action button visible without scrolling. */
  .modal.is-final {
    overflow: hidden;
  }

  .modal.is-final .right {
    display: none;
  }

  .modal.is-final .modal__body {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .modal.is-final .left {
    padding: 10px;
    gap: 10px;
    overflow: hidden;
  }

  .modal.is-final .results {
    padding: 10px;
    gap: 6px;
    max-width: none;
  }

  .modal.is-final .results__row {
    padding-bottom: 6px;
  }

  .modal.is-final .results__actions {
    margin-top: 4px;
  }

  .modal.is-final .try-again {
    height: 34px;
  }

  .modal.is-open {
    transform: translate(0, 0) scale(1);
  }
}

.left,
.right {
  min-height: 0;
  min-width: 0;
}

.left {
  display: flex;
  /* flex-direction: column; */
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 10px 10px 12px;
  border-radius: 18px;
  background: linear-gradient(170deg, rgba(58, 129, 192, 0.18), rgba(11, 18, 32, 0.25));
  border: 1px solid rgba(191, 229, 252, 0.14);
  overflow: hidden;
}

/* Run-state visibility rules (left panel): gauge only while running, results only when finished */
.modal.is-running #final-results {
  display: none !important;
}

/* While running: desktop stays 50/50 (no special split) */

/* Running state: split body 50/50 between left (gauge) and right (charts) */
.modal.is-running .modal__body {
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
}

.modal.is-running .left {
  justify-content: center;
  align-self: stretch;
}

.modal.is-running #gauge {
  display: grid !important;
  margin-top: 0;
}

.modal.is-final #gauge {
  display: none !important;
}

.modal.is-final #final-results {
  display: grid !important;
}

/* When finished, hide the charts panel completely (all breakpoints) */
.modal.is-final .right {
  display: none !important;
}

.modal.is-final .modal__body {
  grid-template-columns: 1fr;
  grid-template-rows: minmax(0, 1fr);
}

/* Light theme: make the left panel white */
.modal[data-theme='light'] .left {
  background: #ffffff;
  border: 1px solid rgba(44, 90, 143, 0.12);
}

/* When showing final results (gauge is hidden), center the card so the button never drops out. */
.modal.is-final .left {
  justify-content: center;
}

.right {
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
}

.gauge {
  width: min(320px, 100%);
  height: auto;
  aspect-ratio: 1 / 1;
  max-height: 100%;
  display: grid;
  place-items: center;
  position: relative;
  margin-top: 6px;
}

.gauge__svg {
  width: 100%;
  height: 100%;
}

.gauge__track {
  fill: none;
  stroke: rgba(191, 229, 252, 0.14);
  stroke-width: 18;
  stroke-linecap: round;
}

.gauge__progress {
  fill: none;
  stroke: url(#gaugeGrad);
  stroke-width: 18;
  stroke-linecap: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  transition: stroke-dashoffset 120ms var(--ease);
}

/* Upload phase: make the progress arc orange */
.gauge.is-upload .gauge__progress {
  stroke: var(--secondary-2);
}

.gauge__marks line {
  stroke: rgba(191, 229, 252, 0.34);
  stroke-linecap: round;
}

.gauge__marks line.minor {
  stroke-width: 1.6;
  opacity: 0.42;
}

.gauge__marks line.major {
  stroke-width: 2.2;
  opacity: 0.64;
}

.gauge__marks text {
  fill: rgba(191, 229, 252, 0.7);
  font-size: 12px;
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: middle;
}

.modal[data-theme='light'] .gauge__marks text {
  fill: rgba(44, 90, 143, 0.82);
}

.modal[data-theme='light'] .gauge__marks line {
  stroke: rgba(44, 90, 143, 0.30);
}

.gauge__needle {
  stroke: rgba(191, 229, 252, 0.72);
  stroke-width: 4;
  transition: transform 120ms var(--ease);
}

.gauge__needle--max {
  stroke: rgba(255, 84, 101, 0.92);
  stroke-width: 3;
}

.modal[data-theme='light'] .gauge__needle {
  stroke: rgba(44, 90, 143, 0.78);
}

.gauge__needle circle {
  fill: rgba(191, 229, 252, 0.72);
}

.gauge__needle--max circle {
  fill: rgba(255, 84, 101, 0.92);
}

.modal[data-theme='light'] .gauge__needle circle {
  fill: rgba(44, 90, 143, 0.78);
}

.gauge__ticks text {
  fill: rgba(191, 229, 252, 0.62);
  font-size: 12px;
  font-weight: 700;
}

.gauge__readout {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  padding-bottom: 12px;
  text-align: center;
  pointer-events: none;
}

.gauge__value {
  font-size: clamp(30px, 4.2vw, 40px);
  font-weight: 400;
  letter-spacing: -0.02em;
  color: rgba(255, 255, 255, 0.92);
  line-height: 1;
  width: 100%;
  font-variant-numeric: tabular-nums;
}

.gauge__value.is-wide {
  font-size: clamp(24px, 4.6vw, 34px);
  letter-spacing: -0.01em;
}

.modal[data-theme='light'] .gauge__value {
  color: rgba(11, 18, 32, 0.92);
}

.gauge__unit {
  margin-top: -6px;
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.58);
}

.gauge__phase {
  margin-top: 6px;
}

.modal[data-theme='light'] .gauge__unit {
  color: rgba(44, 90, 143, 0.62);
}

.gauge__phase {
  margin-top: 8px;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.68);
}

.modal[data-theme='light'] .gauge__phase {
  color: rgba(44, 90, 143, 0.68);
}

.results {
  /* width: 70%; */
  max-width: none;
  display: grid;
  gap: 8px;
  padding: 12px;
  border-radius: 18px;
  background: rgba(11, 18, 32, 0.55);
  border: 1px solid rgba(191, 229, 252, 0.16);
  animation: pop 220ms var(--ease) both;
  margin-top: 0;
  margin-bottom: 2px;
  user-select: text;
}

.modal[data-theme='light'] .results {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(44, 90, 143, 0.12);
}

.modal[data-theme='light'] .results__row {
  border-bottom: 1px solid rgba(44, 90, 143, 0.12);
}

.modal[data-theme='light'] .results__row span {
  color: rgba(44, 90, 143, 0.78);
}

.modal[data-theme='light'] .results__row strong {
  color: rgba(11, 18, 32, 0.92);
}

@keyframes pop {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.results__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid rgba(191, 229, 252, 0.12);
  /* padding-bottom: 7px; */
}

.results__row:last-of-type {
  border-bottom: none;
  padding-bottom: 0;
}

.results__row span {
  font-size: 11px;
  /* letter-spacing: 0.08em; */
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.72);
}

.results__row strong {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.92);
}

.again {
  margin-top: 8px;
  justify-self: center;
  width: 54px;
  height: 54px;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  background: linear-gradient(145deg, var(--primary), var(--secondary-3));
  box-shadow: 0 16px 34px rgba(58, 129, 192, 0.28);
  transition: transform 160ms var(--ease);
}

.results__actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.try-again {
  height: 38px;
  width: min(220px, 100%);
  padding: 0 16px;
  border-radius: 999px;
  border: 1px solid rgba(191, 229, 252, 0.18);
  background: rgba(11, 18, 32, 0.45);
  cursor: pointer;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.92);
  transition: transform 160ms var(--ease), background 160ms var(--ease);
}

.icon-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

@media (max-width: 1020px) {
  .gauge {
    width: min(320px, 100%);
  }
}

@media (max-width: 920px) {
  .results {
    max-width: 500px;
  }
  .gauge {
    width: min(300px, 100%);
  }
}

@media (max-height: 760px) {
  .modal {
    height: min(640px, calc(100dvh - 22px));
  }
  .gauge {
    width: min(320px, 100%);
  }
}

/* Short viewports: shrink gauge so the arc never clips and the results button stays visible */
@media (max-height: 700px) {
  .modal {
    height: min(560px, calc(100dvh - 22px));
  }
  .gauge {
    width: min(300px, 100%);
  }
}

@media (max-height: 620px) {
  .modal {
    height: min(520px, calc(100dvh - 18px));
  }
  .gauge {
    width: min(270px, 100%);
  }
  .gauge__readout {
    padding-bottom: 10px;
  }
}

@media (max-height: 560px) {
  .modal {
    height: min(480px, calc(100dvh - 16px));
  }
  .gauge {
    width: min(250px, 100%);
  }
  .gauge__value {
    font-size: clamp(24px, 6vw, 34px);
  }
  .left {
    overflow: auto;
  }
}

.try-again:hover {
  transform: translateY(-1px);
  background: rgba(58, 129, 192, 0.24);
}

.modal[data-theme='light'] .try-again {
  border: 1px solid rgba(44, 90, 143, 0.18);
  background: rgba(255, 255, 255, 0.78);
  color: rgba(44, 90, 143, 0.92);
}

.modal[data-theme='light'] .try-again:hover {
  background: rgba(191, 229, 252, 0.55);
}

.again:hover {
  transform: translateY(-2px);
}

.again__label {
  color: white;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.chart-card {
  border-radius: 18px;
  background: rgba(11, 18, 32, 0.55);
  border: 1px solid rgba(191, 229, 252, 0.16);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
  min-width: 0;
}

.modal[data-theme='light'] .chart-card {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(44, 90, 143, 0.12);
  box-shadow: 0 10px 24px rgba(44, 90, 143, 0.12);
}

.modal[data-theme='light'] .chart-card__header h3 {
  color: rgba(44, 90, 143, 0.85);
}

.modal[data-theme='light'] .status {
  color: rgba(44, 90, 143, 0.7);
}

.modal[data-theme='light'] .mini {
  color: rgba(44, 90, 143, 0.75);
}

.modal[data-theme='light'] .mini__v {
  color: rgba(11, 18, 32, 0.86);
}

.modal[data-theme='light'] .chart-card__legend {
  color: rgba(44, 90, 143, 0.7);
}

.chart-card__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 12px 10px;
}

.chart-card__header h3 {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.82);
}

.status {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(191, 229, 252, 0.68);
}

.mini {
  display: inline-flex;
  gap: 10px;
  align-items: baseline;
  font-size: 11px;
  color: rgba(191, 229, 252, 0.7);
}

.mini__k {
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.85;
}

.mini__v {
  font-weight: 800;
  color: rgba(255, 255, 255, 0.92);
}

.chart-card__canvas {
  padding: 0 12px 12px;
  min-height: 0;
}

.chart-card__canvas canvas {
  width: 100% !important;
  height: 100% !important;
}

.chart-card__legend {
  display: flex;
  gap: 10px;
  padding: 0 12px 12px;
  font-size: 11px;
  color: rgba(191, 229, 252, 0.68);
}

.legend {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend::before {
  content: '';
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: var(--primary);
}

.legend--down::before {
  background: var(--secondary-4);
}

.legend--up::before {
  background: var(--secondary-2);
}

@media (max-width: 1500px) {
  .modal {
    height: min(820px, calc(100vh - 28px));
  }
  .right {
    grid-template-rows: 1fr 1fr;
  }

  /* Final results should be immediately visible on narrow screens. */
  .modal.is-final .right {
    display: none;
  }
}

@media (max-width: 380px) {
  .widget {
    width: min(300px, 92vw);
    height: 200px;
  }
  .modal {
    width: calc(100vw - 18px);
    height: calc(100vh - 18px);
    border-radius: 18px;
  }
  .modal__header {
    padding: 14px 14px;
  }
  .modal__body {
    padding: 12px;
    /* Even more emphasis on the gauge panel for very narrow screens. */
    grid-template-rows: 1.35fr 0.65fr;
  }
  .gauge__value {
    font-size: 40px;
  }
}

@media (max-width: 380px) {
  .modal.is-running .modal__body {
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  }
}

/* Keep the gauge layout consistent across devices (no mobile-only overrides). */
