@layer base, layout, components, utilities;

@layer base {
  :root {
    color-scheme: light dark;

    /* Semantic action tokens */
    --color-accent: oklch(60% 0.23 28);
    /* brand orange-red */
    --color-danger: oklch(65% 0.21 27);
    /* red — destructive / stop */
    --color-success: oklch(65% 0.18 150);
    /* green — positive / go */
    --color-info: oklch(38% 0.30 264);
    /* blue — informational */
    --color-warning: oklch(84% 0.19 90);
    /* gold — caution */

    /* Light/dark adaptive tokens */
    --color-bg: light-dark(oklch(99% 0 0), oklch(6% 0 0));
    --color-text: light-dark(oklch(6% 0 0), oklch(99% 0 0));
    --ride-text: light-dark(oklch(99% 0 0), oklch(99% 0 0));
    --light-text: light-dark(oklch(65% 0 0), oklch(40% 0 0));
    --input-bg: light-dark(oklch(99% 0 0), oklch(99% 0 0));
    --input-text: light-dark(oklch(6% 0 0), oklch(6% 0 0));
    --menu-bg: light-dark(oklch(76% 0 0), oklch(53% 0 0));
    --menu-text: light-dark(oklch(10% 0 0), oklch(99% 0 0));
    --menu-box-shadow: light-dark(oklch(6% 0 0 / 20%), oklch(80% 0 0 / 20%));
    --color-borders: light-dark(oklch(93% 0 0), oklch(59% 0 0));

    /* Component tokens */
    --dialog-backdrop: oklch(35% 0 0 / 70%);
    --slider-bg: oklch(76% 0 0);
    --slider-border: oklch(59% 0 0);
    --slider-thumb: oklch(31% 0 0);
    --disabled-bg: oklch(53% 0 0);
    --disabled-text: oklch(76% 0 0);
  }

  /* Fallback for browsers that don't support light-dark() */
  [data-theme="light"] {
    color-scheme: light;
    --color-bg: oklch(99% 0 0);
    --color-text: oklch(6% 0 0);
    --ride-text: oklch(99% 0 0);
    --light-text: oklch(65% 0 0);
    --input-bg: oklch(99% 0 0);
    --input-text: oklch(6% 0 0);
    --menu-bg: oklch(76% 0 0);
    --menu-text: oklch(10% 0 0);
    --menu-box-shadow: oklch(6% 0 0 / 20%);
    --color-borders: oklch(93% 0 0);
  }

  [data-theme="dark"] {
    color-scheme: dark;
    --color-bg: oklch(6% 0 0);
    --color-text: oklch(99% 0 0);
    --ride-text: oklch(99% 0 0);
    --light-text: oklch(40% 0 0);
    --input-bg: oklch(99% 0 0);
    --input-text: oklch(6% 0 0);
    --menu-bg: oklch(53% 0 0);
    --menu-text: oklch(99% 0 0);
    --menu-box-shadow: oklch(80% 0 0 / 20%);
    --color-borders: oklch(59% 0 0);
  }

  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  body,
  html {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-weight: 400;
    font-size: 18px;
    background-color: var(--color-bg);
    color: var(--color-text);
    -webkit-user-select: none;
    user-select: none;
    min-height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
  }

  h1 {
    font-size: 2rem;
    font-weight: 600;
    color: var(--color-accent);
    padding-bottom: 0.25rem;
  }
}

@layer layout {
  main {
    position: relative;
    height: 100%;
    width: 100%;
    overflow-y: auto;
    padding: 0;
    flex-wrap: nowrap;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  header,
  footer {
    font-size: 2rem;
    font-weight: 600;
    padding: 0.25rem;
    flex: 0 1 auto;
    width: inherit;
    justify-content: center;
  }

  .content-wrapper {
    flex: 0 1 auto;
    padding: 0;
    align-items: center;
    width: inherit;
    height: 100%;
    z-index: 1;
    display: flex;
    justify-content: center;
    flex-direction: column;
  }

  .content-wrapper>section {
    width: 100%;
    margin: 0;
    padding: 1%;
  }

  #ride_page {
    background: var(--input-text);
    color: var(--input-bg);
    height: 100%;
    border-top: 1px solid var(--color-borders);
    border-bottom: 1px solid var(--color-borders);
  }

  output {
    display: block;
  }

  #clock {
    font-size: 2.5rem;
  }

  #speed-value,
  #distance-value {
    font-weight: 600;
    font-size: 5rem;
    color: var(--light-text);
    margin: 0;
    margin-left: 3.5rem;
    padding: 0;
    height: 20%;
  }

  .grid-container {
    display: grid;
    grid-template-columns: 20% 1fr 20%;
    height: 40%;
    font-weight: 600;
  }

  .grid-item {
    justify-content: center;
    height: 100%;
    align-items: center;
    display: inline-grid;
  }
}

@layer components {

  /* --- Buttons --- */
  .round-button {
    width: 90%;
    height: 3rem;
    border-radius: 0.5rem;
    border: none;
    cursor: pointer;
    display: table-cell;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    margin-bottom: 0.65rem;
    color: white;
  }

  button:disabled,
  button[disabled] {
    background-color: var(--disabled-bg);
    cursor: not-allowed;
    color: var(--disabled-text);
    text-shadow: 0 1px 0 oklch(99% 0 0 / 30%), 0 -1px 0 oklch(6% 0 0 / 70%);
  }

  .mode-button {
    font-size: 8rem;
    height: 100%;
    line-height: 2.5rem;
    margin: 0;
    background: transparent;
    border: none;
    color: var(--ride-text);
  }

  /* --- Ride page --- */
  #ride-mode-label {
    font-size: 1.75rem;
    background: transparent;
    color: var(--ride-text);
  }

  .battery-status {
    font-size: 1.2rem;
    font-weight: 400;
    height: 20%;
    width: 50%;
    display: block;
    margin: 0 auto;
    padding-top: 5%;
  }

  progress[value] {
    width: 75%;
    height: 2.5rem;
    position: relative;
  }

  /* --- Trip summary web component --- */
  ride-summary {
    display: block;
    width: inherit;
    background-color: var(--color-text);
    padding: 0.25rem;
    border-radius: 0.5rem;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    margin: 0 auto;
    margin-top: 1.5rem;
  }

  .trip-summary-header {
    font-size: 1rem;
    color: var(--color-bg);
    margin-bottom: 0.5rem;
    border-bottom: 1px solid var(--color-borders);
  }

  .trip-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    color: var(--color-bg);
  }

  .trip-stats>div {
    text-align: center;
    padding: 0.5rem;
  }

  .trip-stats>div:nth-child(odd) {
    border-right: 1px solid var(--color-borders);
  }

  .trip-stats>div:nth-child(-n+2) {
    border-bottom: 1px solid var(--color-borders);
  }

  .trip-stats dt {
    font-size: 1rem;
    color: var(--light-text);
  }

  .trip-stats dd .units {
    font-size: 1rem;
    color: var(--light-text);
  }

  .trip-navigation {
    display: flex;
    justify-content: space-between;
    margin-top: 1rem;
  }

  .trip-navigation button {
    background-color: var(--disabled-bg);
    color: var(--color-text);
    border: none;
    padding: 0.25rem 1rem;
    cursor: pointer;
    border-radius: 0.25rem;
  }

  .trip-navigation button:hover {
    background-color: oklch(from var(--disabled-bg) calc(l + 0.15) c h);
  }

  .trip-navigation button:disabled {
    background-color: oklch(from var(--disabled-bg) calc(l + 0.15) c h);
    cursor: not-allowed;
  }

  /* --- Home page --- */
  #red-wheel {
    width: 200px;
    height: 150px;
    z-index: 2;
    position: relative;
    margin: 0 auto;
  }

  #red-wheel svg {
    stroke-width: 3;
    stroke: var(--color-text);
  }

  #spinner {
    transform-origin: 41px 82.5px;
    animation: loading-spinner 1s linear infinite;
    display: none;
  }

  #spinner.visible {
    display: block;
  }

  @keyframes loading-spinner {
    0% {
      transform: rotate(0deg);
    }

    100% {
      transform: rotate(360deg);
    }
  }

  /* 2rem at ≤16 chars; steps down 0.05rem per char beyond that; floors at 1rem (~36 chars) */
  #wheel-name {
    font-size: clamp(1rem, calc(2.8rem - var(--name-len, 16) * 0.05rem), 2rem);
  }

  #trip-date-range {
    font-size: clamp(0.9rem, calc(1.8rem - var(--name-len, 45) * 0.05rem), 0.9rem);
  }

  #status {
    margin: 0 1rem;
  }

  /* --- Settings nav --- */
  .settings-nav {
    float: right;
    margin-right: 1rem;
    z-index: 25;
  }

  #settings-button {
    background: none;
    color: var(--color-text);
    border: none;
    font-size: 2rem;
    cursor: pointer;
    padding: 0.25rem;
  }

  #settings-dropdown {
    display: none;
    position: fixed;
    right: 1rem;
    margin-top: -3rem;
    background-color: var(--menu-bg);
    box-shadow: 0 2px 5px var(--menu-box-shadow);
    border-radius: 0.5rem;
    list-style: none;
    max-width: 14rem;
    width: 90vh;
    text-align: left;
    z-index: 25;
  }

  #settings-dropdown[data-state="open"] {
    display: block;
  }

  #settings-dropdown li {
    padding: 0.5rem;
    cursor: pointer;
    border-bottom: 1px solid var(--color-borders);
    font-size: 1.1rem;
  }

  #settings-dropdown li:last-child {
    border-bottom: none;
  }

  #settings-dropdown button {
    color: var(--menu-text);
    background: transparent;
    border: none;
    width: 100%;
    text-align: left;
    font-size: 1.1rem;
    cursor: pointer;
  }

  #settings-dropdown button:disabled,
  #settings-dropdown modal-toggle button[disabled] {
    color: var(--color-bg);
  }

  .menu-close {
    font-size: 0.75rem;
    cursor: pointer;
  }

  /* --- Modals / Dialogs --- */
  #home_page h1 {
    justify-content: center;
  }

  modal-content section {
    flex: 1;
    overflow-y: auto;
    padding-bottom: 0.5rem;
  }

  dialog::backdrop {
    background-color: var(--dialog-backdrop);
  }

  dialog[open] {
    position: fixed;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--color-text);
    border-radius: 0.25rem;
    min-width: calc(100vw - 2rem);
    max-width: calc(100vw - 2rem);
    max-height: calc(100vh - 4rem);
    overflow: hidden;
    top: 3rem;
    left: 1rem;
    padding: 0.5rem;
  }

  dialog[open] p,
  dialog[open] ul {
    text-align: left;
    font-size: 0.85rem;
    padding-bottom: 0.5rem;
  }

  dialog[open] ul {
    padding-bottom: 0.5rem;
  }

  dialog[open] ul li {
    list-style-type: disc;
    display: list-item;
    padding-bottom: 0;
    text-indent: 0;
    margin-left: 1.5rem;
  }

  dialog[open] .round-button {
    /* width: 50%; */
    height: 2.5rem;
    border-radius: 0.5rem;
    margin: 0.5rem auto 0.25rem;
    flex-shrink: 0;
  }

  /* --- Settings forms --- */
  .settings-form {
    margin: 0 auto;
    margin-top: 0.75rem;
    margin-bottom: 1rem;
  }

  .settings-form fieldset {
    border: 1px solid var(--slider-border);
    border-radius: 0.25rem;
    padding: 0 0.5rem;
  }

  .settings-form input[type="text"]:read-only {
    border: none;
    width: fit-content;
    max-width: 16ch;
    background: transparent;
    color: var(--color-text);
  }

  .settings-form input[readonly]:focus {
    border: none;
    outline: none;
  }

  .settings {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }

  .settings .label,
  .settings .value {
    text-align: left;
    border-bottom: 2px solid oklch(73% 0 0);
    padding: 5px;
    align-content: left;
    background: transparent;
    color: var(--input-text);
  }

  .settings .value {
    align-content: right;
  }

  .field-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 0.5rem 0;
  }

  .field-group label {
    font-weight: 600;
    margin-bottom: 0.25rem;
    text-align: left;
    width: 100%;
  }

  .field-group.row {
    flex-direction: row;
    align-items: baseline;
    padding: 0;
  }

  .field-group.row label {
    width: max-content;
  }

  .field-group input,
  .field-group select {
    padding: 0.5rem;
    border: 1px solid var(--color-borders);
    border-radius: 0.25rem;
    width: 100%;
    background: var(--input-bg);
    color: var(--input-text);
  }

  /* --- Toggle switches --- */
  .switch-container {
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .switch-container input[type="checkbox"] {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
  }

  .switch {
    position: relative;
    display: inline-flex;
    width: 3.75rem;
    height: 2rem;
    cursor: pointer;
  }

  .field-group .switch {
    width: auto;
    min-width: 3.75rem;
    display: inline-block;
  }

  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--slider-bg);
    transition: 0.4s;
    border-radius: 2rem;
    border: 1px solid var(--slider-border);
    width: 100%;
    height: 100%;
  }

  .slider-labels {
    display: flex;
    width: 100%;
    font-size: 0.75rem;
  }

  .slider-labels span {
    flex: 1;
  }

  .slider-labels span:first-child {
    text-align: start;
  }

  .slider-labels span:last-child {
    text-align: end;
  }

  .slider:before {
    content: "";
    position: absolute;
    height: 1.5rem;
    width: 1.5rem;
    left: 0.25rem;
    bottom: 0.25rem;
    background-color: var(--slider-thumb);
    transition: 0.4s;
    border-radius: 50%;
  }

  .switch-container input:checked+.switch .slider {
    background-color: var(--color-success);
    border-color: oklch(from var(--color-success) calc(l - 0.15) c h);
  }

  .switch-container input:checked+.switch .slider:before {
    transform: translateX(26px);
    background-color: oklch(99% 0 0);
  }

  .slider.round {
    border-radius: 2rem;
  }

  /* --- Error / status toast --- */
  #error-toast {
    position: fixed;
    bottom: 4rem;
    left: 50%;
    transform: translateX(-50%);
    background: var(--color-danger);
    color: oklch(99% 0 0);
    padding: 0.5rem 1.5rem;
    border-radius: 0.5rem;
    font-size: 0.9rem;
    z-index: 100;
    max-width: 80%;
    text-align: center;
  }

  #error-toast[data-type="warning"] {
    background: var(--color-warning);
    color: var(--color-text);
  }

  #error-toast[data-type="info"] {
    background: var(--color-info);
  }

  /* --- Simulator banner --- */
  #sim-banner {
    background: var(--color-danger);
    color: oklch(99% 0 0);
    font-weight: 700;
    font-size: 0.85rem;
    letter-spacing: 0.1em;
    padding: 0.25rem;
    text-align: center;
    width: 100%;
  }
}

@layer utilities {
  [hidden] {
    display: none !important;
    visibility: hidden;
  }

  [data-variant="danger"] {
    background-color: var(--color-danger);
  }

  [data-variant="success"] {
    background-color: var(--color-success);
  }

  [data-variant="info"] {
    background-color: var(--color-info);
  }

  .accent {
    color: var(--color-accent);
    font-weight: 600;
  }

  .success-text {
    color: var(--color-success);
    font-weight: 600;
  }

  [data-level="high"] {
    color: var(--color-success);
  }

  [data-level="med"] {
    color: var(--color-warning);
  }

  [data-level="low"] {
    color: var(--color-danger);
  }

  .large-text {
    font-size: 1.5rem;
  }

  .light-text {
    font-weight: 300;
  }

  .units {
    font-size: 1.5rem;
    color: var(--light-text);
  }
}