*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0284c7;
  --radius: 8px;
}

html, body {
  margin: 0;
  padding: 0;
  background-color: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.6;
}

form, .grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 1.25rem;
}

/* Leveysluokat tarvittaessa */
.col-12 { grid-column: span 12; }
.col-8  {
  grid-column: span 12;
  @media (min-width: 960px) {
    grid-column: span 8;
  }
}
.col-6  {
  grid-column: span 12;
  @media (min-width: 960px) {
    grid-column: span 6;
  }
}
.col-4  {
  grid-column: span 12;
  @media (min-width: 960px) {
    grid-column: span 4;
  }
}
.col-3  {
  grid-column: span 12;
  @media (min-width: 960px) {
    grid-column: span 3;
  }
}
.m-col-6 {
  grid-column: span 6;
}

.text-align-right {
  text-align: right;
}

/* Mobiili-header kelluvana */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);

  .nav-button {
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    user-select: none;
    padding: 0 0.25rem 0.25rem 0.25rem;
    margin-top: -.1em;
  }

  /* Mobiili-headerin linkki ikonilla */
  .site-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--text);
    text-decoration: none;

    img {
      display: block;
      border-radius: 5px;
    }
  }
}

/* Navigaatio */
nav {
  display: none;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.25rem 1.5rem;
  position: sticky;
  top: 3.5rem;
  z-index: 99;
  max-height: calc(100vh - 3.5rem);
  overflow-y: auto;

  h2 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin: 1.25rem 0 0.5rem;
    border: none;
    padding: 0;

    &:first-of-type {
      margin-top: 0;
    }
  }

  ul {
    list-style: none;
    margin: 0;
    padding: 0;

    li {
      margin: 0.25rem 0;

      a {
        display: block;
        color: var(--text);
        text-decoration: none;
        padding: 0.35rem 0.5rem;
        border-radius: 6px;
        font-size: 0.95rem;

        &:hover {
          background: #f1f5f9;
        }

        &.active {
          background: #e0f2fe;
          color: var(--primary);
          font-weight: 600;
        }
      }
    }
  }
}

/* Checkbox-kytkin mobiilissa */
#nav-toggle:checked ~ nav {
  display: block;
}

ul, ol {
  padding-left: 1.25rem;
  margin: 0.5rem 0 1.25rem;

  li {
    margin-bottom: 0.5rem;
    color: #334155;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

/* Pääalue */
main {
  display: grid;
  grid-template-columns:
    minmax(1rem, 1fr)
    [main] minmax(0, 720px)
    minmax(1rem, 1fr);

  > * {
    grid-column: main;
    min-width: 0;
  }
}

h1 {
  font-size: 1.875rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.4;
  margin-bottom: 0.5rem;
}

h2 {
  font-size: 1.35rem;
  border-bottom: 2px solid var(--border);
  line-height: 1.4;
  padding-bottom: 0.3rem;
}

h3 {
  font-size: 1.05rem;
  margin: .75rem 0 0.5rem;
}

h1 + p, h2 + p, h3 + p {
  margin-top: 0;
}

p {
  margin-bottom: 1rem;
  color: #334155;
}

table {
  width: 100%;
}

/* Semanttinen Form: 12-kolumninen Grid */
form {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
  align-items: end;
  min-width: 0;
  margin-bottom: 1.5rem;

  /* Otsikot ja taulukot ottavat aina täyden leveyden */
  h2, table {
    grid-column: 1 / -1;
  }

  h2 {
    font-size: 1.15rem;
    margin: 1.25rem 0 0.25rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.25rem;
  }

  /* Oletuksena syöte-label: mobiili 12, deskari 6 */
  > label {
    grid-column: span 12;
    min-width: 0;

    @media (min-width: 600px) {
      grid-column: span 6;
    }
  }

  label {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 0.35rem 0.5rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--muted);
    margin: 0;
    min-width: 0;

    > span:not(.val) {
      grid-column: 1 / -1;
      font-weight: 600;
      color: var(--text);
    }

    > input {
      grid-column: 1;
      min-width: 0;
    }

    > select {
      grid-column: 1 / -1;
      min-width: 0;
    }

    /* Yksikköteksti: ei ikinä rivitystä */
    > :not(span):not(input):not(select) {
      grid-column: 2;
      white-space: nowrap;
    }

    /* Checkboxin tasoitus */
    &:has(input[type="checkbox"]) {
      display: flex;
      flex-direction: row;
      align-items: center;
      gap: 0.5rem;
      cursor: pointer;
      height: 42px;
    }
  }

  /* Input & Select */
  input[type="text"],
  input[type="number"],
  input[type="date"],
  select {
    width: 100%;
    height: 42px;
    font-size: 1rem;
    font-family: inherit;
    padding: 0 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    outline: none;
    transition: border-color 0.15s ease, background-color 0.15s ease;

    &:focus {
      border-color: var(--primary);
    }

    &:disabled {
      background-color: #f1f5f9;
      border-color: #cbd5e1;
      color: #94a3b8;
      cursor: not-allowed;
      user-select: none;

      &::placeholder {
        color: #cbd5e1;
      }
    }
  }

   /* Tuloslaatikko: Täysi leveys + oma 12-kolumnin sisägrid */
  fieldset {
    grid-column: 1 / -1;
    margin: 0.5rem 0 0;
    padding: 1rem 1.25rem;
    border: 1px solid #bae6fd;
    background: #f0f9ff;
    border-radius: 6px;
    min-width: 0;

    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 1rem 1.5rem;
    align-items: baseline;

    label {
      grid-column: span 12;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: baseline;
      gap: 0.75rem;
      font-size: 0.95rem;
      color: #0369a1;
      font-weight: 600;
      min-width: 0;

      .val {
        font-weight: 700;
        color: #0284c7;
        text-align: right;
        white-space: nowrap;

        output {
          font-family: inherit;
        }
      }
    }
    &.stacked-results label {
      flex-direction: column;
      align-items: flex-start;
      gap: 0.15rem;

      > span:first-child {
        font-size: 0.85rem;
        font-weight: 600;
        color: #0369a1;
      }

      .val {
        font-size: 1.25rem;
        font-weight: 700;
        color: #0284c7;
        text-align: left;
        white-space: normal;
      }
    }
  }

  /* Paljas semanttinen taulukko */
  table {
    border-collapse: collapse;
    margin-top: 0.5rem;

    td {
      padding: 0.45rem 0;
      font-size: 0.95rem;
      border-bottom: 1px dashed var(--border);

      &:last-child {
        text-align: right;
        font-weight: 600;
        color: var(--primary);
        white-space: nowrap;

        output {
          font-family: inherit;
          font-weight: 700;
        }
      }
    }

    tr:last-child td {
      border-bottom: 2px solid var(--text);
      font-weight: 700;
      color: var(--text);
      padding-top: 0.75rem;

      &:last-child {
        color: var(--text);
      }
    }
  }
}

/* Puhdas MathML ja itsenäiset kaavakontit */
p:has(> math:only-child) {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.65rem 0.85rem;
  margin: 0.5rem 0 1.25rem;
  background: #f1f5f9;
  border-radius: 6px;
  border: 1px solid var(--border);
  scrollbar-width: thin;

  math {
    font-size: 1.1em;
  }
}

math {
  font-family: inherit;
  white-space: nowrap;

  &:has(mfrac) {
    vertical-align: middle;
  }
}
/* Etusivun kategorialistaukset */
.index-categories {
  display: flex;
  flex-direction: column;
  gap: 2rem;
  margin-top: 1.5rem;
}

.category-block {
  h2 {
    font-size: 1.25rem;
    color: var(--text);
    margin-bottom: 0.75rem;
    border-bottom: 2px solid var(--border);
  }
}

.calculator-grid {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;

  @media (min-width: 600px) {
    grid-template-columns: repeat(2, 1fr);
  }

  li {
    margin: 0;

    a {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;
      padding: 1rem 1.15rem;
      background: var(--card-bg);
      border: 1px solid var(--border);
      border-radius: var(--radius);
      text-decoration: none;
      box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.03);
      transition: border-color 0.15s ease, transform 0.15s ease,
        box-shadow 0.15s ease;

      strong {
        color: var(--text);
        font-size: 1.05rem;
        font-weight: 600;
        margin-bottom: 0.35rem;
      }

      span {
        color: var(--primary);
        font-size: 0.85rem;
        font-weight: 500;
      }

      &:hover {
        border-color: var(--primary);
        transform: translateY(-1px);
        box-shadow: 0 4px 8px -2px rgba(2, 132, 199, 0.12);
        background: #fff;
      }
    }
  }
}

/* Deskarin brändilinkki ikonilla */
.nav-brand {
  display: none;

  @media (min-width: 956px) {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-weight: 700;
    font-size: 1.25rem;
    letter-spacing: -0.02em;
    color: var(--text);
    text-decoration: none;
    margin-bottom: 1.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);

    img {
      display: block;
      border-radius: 6px;
    }
  }
}
/* ==========================================================================
   Desktop Breakpoint (800px+)
   ========================================================================== */
@media (min-width: 960px) {
  header {
    display: none;
  }

  body {
    display: grid;
    grid-template-columns:
      minmax(2rem, 1fr)
      [nav] 220px
      [gap] 3.5rem
      [main] minmax(0, 680px)
      minmax(2rem, 1fr);
    padding: 4rem 1rem;
  }

  nav {
    display: block !important;
    grid-column: nav;
    background: transparent;
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 0 1.5rem 0 0;
    position: sticky;
    top: 3rem;
    align-self: start;
  }

  main {
    display: block;
    grid-column: main;
    padding: 0;

    > * {
      grid-column: auto;
    }
  }

  .val {
    font-size: 1.35rem;
  }
}
