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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  min-height: 100svh; /* mobiiliturvallinen */
  font-family: Arial, sans-serif;
  background: #f8fafc;
}

/* PÄÄKONTAINERI */
#appRoot {
  width: 100%;
  min-height: 100svh;
  display: block;
}

/* =========================================================
   0. RESET & PERUSASETUKSET
========================================================= */

:root {
  --app-width: 450px;
}

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

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}


html {
  overflow-x: hidden;
}

body {
  font-family: Arial, sans-serif;
  margin: 0;
  background: #f8fafc;
  overflow-x: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
}

/* =========================================================
   1. YLEISET KOMPONENTIT & ANIMAATIOT
========================================================= */

button.unsaved {
  animation:
    pulse 1.2s ease-out infinite,
    blink-bg 1s linear infinite;
}

@keyframes blink-bg {
  0%   { background:#fde68a; }
  50%  { background:#f59e0b; }
  100% { background:#fde68a; }
}

@keyframes pulse {
  0% {box-shadow: 0 0 0 0 rgba(255,140,0,0.7);}
  70% {box-shadow: 0 0 0 10px rgba(255,140,0,0);}
  100% {box-shadow: 0 0 0 0 rgba(255,140,0,0);}
}

@keyframes blink {
  0% { background:#fde68a; }
  50% { background:#f59e0b; }
  100% { background:#fde68a; }
}

button.loading {
  background: repeating-linear-gradient(
    45deg,
    #fde68a,
    #fde68a 10px,
    #f59e0b 10px,
    #f59e0b 20px
  );
  animation: progress 0.8s linear infinite;
}

@keyframes progress {
  from { background-position: 0 0; }
  to   { background-position: 40px 0; }
}


.login-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: sans-serif;
  z-index: 9999;
}

.login-box {
  background: white;
  padding: 30px;
  border-radius: 10px;
  width: 300px;
}

.login-input {
  width: 100%;
  padding: 8px;
  margin-bottom: 10px;
  box-sizing: border-box;
}

.login-remember {
  font-size: 14px;
}

.login-button {
  width: 100%;
  padding: 10px;
  margin-top: 15px;
  cursor: pointer;
}

.login-error {
  color: red;
  font-size: 13px;
}

.loading-overlay{
    position:fixed;
    inset:0;

    display:none;
    justify-content:center;
    align-items:center;
    
    background: rgba(248,250,252,0.60);
    backdrop-filter: blur(2px);
    pointer-events:none;
    z-index:99999;
}

.loading-overlay canvas{
    width:320px;
    max-width:80vw;
    height:auto;
}

.loading-box{

    display:flex;
    flex-direction:column;
    align-items:center;

}

.loading-text{

    margin-top:12px;

    font-size:18px;
    font-weight:600;

    color:#1f2937;

    text-shadow:
        0 1px 2px rgba(255,255,255,.9);

}
/* =========================================================
   2. ALOITUSNÄYTTÖ – RAKENNE
========================================================= */

.start-view {
  min-height: 100%;
  padding-top: 40px;
  background: #f3f4f6;
  overflow-x: hidden;
}

.start-card {
  width: var(--app-width);
  max-width: 100%;
  background: #111827;
  color: white;
  padding: 16px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0 auto;
  gap: 4px;
  overflow-x: hidden;
  position: relative;
}

.logout-btn {
  position: absolute;
  top: 8px;
  right: 8px;

  width: 30px;
  height: 30px;

  padding: 0;
  margin: 0;

  border: none;
  border-radius: 6px;

  background: transparent;
  color: #94a3b8;

  font-size: 24px;
  line-height: 30px;

  cursor: pointer;
}

.logout-btn:hover {
  background: #374151;
  color: white;
}

.start-logo {
  width: 400px;
  height: auto;
  margin-bottom: 4px;
}

.start-title {
  font-size: 1.8em;
  font-weight: 600;
  text-align: center;
  margin-top: 0;
}

.start-version {
  font-size: 0.6em;
  opacity: 0.7;
  margin-left: 6px;
}


/* =========================================================
   3. ALOITUSNÄYTTÖ – TOIMINTONAPIT
========================================================= */

.start-actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
  justify-content: center;
}

.start-actions button {
  width: 36px;
  height: 36px;
  font-size: 1.2em;
  margin-left: 4px;
  border-radius: 8px;
  border: none;
  cursor: pointer;

  background: #e5e7eb;   /* vaalea harmaa */
  color: #111827;
}

.back-btn {
  margin-left: 4px;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  background: #e5e7eb;
  color: #111;
  font-size: 20px;
  cursor: pointer;
}

.start-actions button:hover {
  background: #cbd5f5;
}

.back-btn:hover {
  background: #d1d5db;
}

#lisaaLennokkiBtn.unsaved {
  animation: blink 1s infinite;
}

/* =========================================================
   4. ALOITUSNÄYTTÖ – LENNOKKITAULUKKO
========================================================= */

.start-table-wrapper {
  margin-top: 14px;
  width: 100%;
  max-width: 100%;

  max-height: 169px;         
  min-height: 169px;

  overflow-y: auto;          
  overflow-x: hidden;
  border: 1px solid #ccc;
}

.start-table {
  width: 100%;
  max-width: 100%;
  border-collapse: separate;  
  border-spacing: 0;
  background: white;
  color: #111827;
  font-size: 0.9em;
  border-radius: 0px;
  overflow: clip;
  table-layout: fixed;
}

.start-table thead {
  box-shadow: 0 2px 0 #334155;    
}


/* =========================================================
   5. ALOITUSTAULUKON SOLUT & SARAKKEET
========================================================= */

.start-table th {
  background: #e5e7eb;        /* sama kuin napit */
  color: #111827;
  font-weight: 600;
  padding: 4px 6px;         /* tähän pieni säätö */
  border-bottom: 1px solid #334155;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.start-table td {
  padding: 6px 6px;
  border-bottom: 1px solid #1e293b;
  text-overflow: ellipsis;
  white-space: normal;
  overflow: visible;
}

.start-table thead th {
  position: sticky;     
  top: 0;
  z-index: 5;
  background: #e5e7eb;
}

/* ALOITUSTAULUKON SARAKKEIDEN LEVEYS */

.start-table th:nth-child(1),
.start-table td:nth-child(1) {
  width: 50%;        /* Lennokki */
}

.start-table th:nth-child(2),
.start-table td:nth-child(2) {
  width: 15%;        /* Paino */
}

.start-table th:nth-child(3),
.start-table td:nth-child(3) {
  width: 15%;        /* Pp */
}

.start-table th:nth-child(4),
.start-table td:nth-child(4) {
  width: 20%;        /* Pvm */
}

.col-name {
  width: 60%;
  min-width: 220px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-num {
  width: 18%;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.start-table td.col-num:nth-child(3) {
  width: 14%;          /* Pp */
}

.col-date {
  width: 8%;
  text-align: center;
}

.start-table tr:hover {
  background:#f3f4f6; 
}

.start-table tbody tr {
  height: auto;   /* tasainen korkeus */
  cursor: pointer;
  box-sizing: border-box; 
}

.start-table tbody tr:hover {
  background: #e0f2fe;
  outline: 1px solid transparent; /* 🔑 estää layout-shiftin */
}

.start-table tr.selected {
  background: #bae6fd;
  font-weight: 600;
}

.start-del-btn {
  background: transparent;
  border: none;
  color: #94a3b8;
  font-size: 1.1em;
  cursor: pointer;
}

.start-del-btn:hover {
  color: #ef4444;
}

.start-table th.col-num,
.start-table th.col-date {
  text-align: center;
}

.start-table td.col-num {
  text-align: right;
}
.start-table th.col-name,
.start-table td.col-name {
  text-align: left;
  padding-left: 10px;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.start-table td.col-date {
  text-align: center;
}

.start-table tr.empty-row td {
  background: #f9fafb;
  border-bottom: 1px solid #1e293b;
}

.start-table tr.empty-row:hover {
  background: inherit;
}

.start-table tr.empty-row input {
  cursor: default;
}

.new-lennokki-cell {
  text-align: left;
  padding: 6px 8px;
  overflow: visible;
}

.new-lennokki-cell input {
  width: 100%;
  text-align: left;
}


.new-plane-row label {
  font-size: 0.75em;
  color: #475569;
  display: block;
  margin-bottom: 2px;
}

.new-plane-row select {
  width: 100%;
  font-size: 0.85em;
  padding: 4px;
  border-radius: 4px;
  border: 1px solid #cbd5e1;
}

.uusi-malli-select {
  font-size: 0.85em;
  padding: 4px 6px;
  border-radius: 4px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;

  width: 140px;        /* ⬅ EI 100% */
  flex-shrink: 0;     /* ⬅ ei kutistu */
}

.new-row td > :not(.new-lennokki-row) {
  display: none;
}

.new-lennokki-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.new-lennokki-row input {
  flex: 1;            /* nimi vie kaiken vapaan tilan */
  min-width: 0;      /* 🔑 estää flex-yli-leveyden */
  text-align: left;
}
.new-lennokki-row select {
  width: 140px;         /* kiinteä, selkeä */
  flex-shrink: 0;
}

.start-table tr.new-row {
  height: auto;
}


/* =========================================================
   6. SOVELLUSNÄKYMÄ – RAKENNE
========================================================= */

.app {
  max-width: var(--app-width);
  margin: 0 auto;
  background: white;
  border-left: 1px solid #e5e7eb;
  border-right: 1px solid #e5e7eb;
  overflow-x: hidden;  
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.table-wrapper thead th {     
  position: sticky;
  top: 0;
  background: #e5e7eb;
  z-index: 10;
  border-bottom: 2px solid #9ca3af;
}

.table-wrapper {
  flex: 1;
  overflow-y: auto;
}


/* =========================================================
   7. YLÄPALKKI & NAVIGAATIO
========================================================= */

.topbar {
  background: #0f172a;
  color: white;
  padding: 10px 10px;
  min-height: 60px;  
  display: flex;                 
  justify-content: space-between;
  align-items: flex-start;
}

.topbar .left {
  display: flex;
  flex-direction: column;         /* otsikko ylhäällä, napit alla */
}

.topbar .right {
  display: flex;
  align-items:center;
  gap: 12px;          /* 👈 TÄMÄ siirtää selectiä irti logosta */
}

.titlebar {
  display: flex;
  align-items: center;
  margin-bottom: 14px;   /* 👈 tämä kasvattaa tilaa YLÄPUOLELTA nappeja */
}

.logo {
  height: 28px;
  width: auto;
  display: block;
}

.title {
  font-weight: bold;
}

.nav {
  margin-top: 6px;
  display: flex;
  gap: 10px;
}

.nav button {
  background: #e5e7eb;
  color: #111827;
  margin-left: 4px;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}

.nav button.active {
  background: #9ca3af;
}

.nav button.active {
  background: #38bdf8;   /* vaalea sininen */
  color: #0f172a;
  font-weight: bold;
}

#lennokkiSelect {
  min-width: 110px;
  padding: 6px 8px;
  font-size: 0.9em;
}


/* =========================================================
   8. OSAT, RYHMITTELY & YHTEENVETO – TAULUKOT
========================================================= */

table {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
}

thead th {
  position: sticky;  /*4.*/
  top: 0;
  background: #e5e7eb;
  z-index: 10;
  border-bottom: 2px solid #9ca3af;
}

th, td {
  border: 1px solid #d1d5db;
  padding: 8px 6px;      /* ⬅ kasvattaa rivikorkeuden */
  font-size: 14px;
  line-height: 1.4;     /* ⬅ mahdollistaa 2 riviä siististi */
  overflow: hidden;
  white-space: normal;  /* ⬅ sallii rivityksen */
}

/* Numerosolujen fontti 120 % */
td.col-num input {
  width: 100%;
  font-size: 1.2em;   /* = 120 % */
  text-align: right
}

th.col-nro, td.col-nro { width: 5ch; text-align: center; }
th.col-osa, td.col-osa { width: 21ch; }
th.col-num, td.col-num { width: 15ch; text-align: right; }
th.col-kok, td.col-kok { width: 24ch; }

/* Varsi sarake leveämmäksi */
th.col-num:nth-child(4),
td.col-num:nth-child(4) {
  width: 20ch;
}


/* ===============================
   YHTEENVETO TAULUKON SARAKKEET
   =============================== */

/* Yhteenveto: Yhteensä-rivi */
tr.yhteensa td {
  background: #e5e7eb;        /* sama vaalea harmaa kuin otsikoissa */
  font-weight: bold;
  border-top: 2px solid #9ca3af;
}

#yhteenvetoView table {
  table-layout: fixed;
  width: 100%;
}

#yhteenvetoView th:nth-child(1),
#yhteenvetoView td:nth-child(1) {
  width: 35%;
  text-align: left;
}

#yhteenvetoView th:nth-child(2),
#yhteenvetoView td:nth-child(2) {
  width: 20%;
  text-align: right;
}

#yhteenvetoView th:nth-child(3),
#yhteenvetoView td:nth-child(3) {
  width: 25%;
  text-align: right;
}

#yhteenvetoView th:nth-child(4),
#yhteenvetoView td:nth-child(4) {
  width: 20%;
  text-align: right;
}

/* ===== OSARYHMÄT ===== */

.osa-group {
  margin-bottom: 6px;
}

.osa-group-header {
  font-size: 0.75em;
  font-weight: 600;
  text-transform: uppercase;
  color: #475569;
  padding: 4px 6px;
  cursor: pointer;
  user-select: none;
  background: #f1f5f9;
  border-radius: 4px;
}

/* ===============================
   RYHMÄOTSIKON MUUTOSVILKUTUS
   =============================== */

.osa-group-header.unsaved {
  animation: blinkHeader 1s infinite;
}
@keyframes blinkHeader {  
  0% {background-color: #fff3cd;                  
  }
  50% {background-color: #ffe066;
  }
  100% {background-color: #fff3cd;
  }

}
.osa-group-header:hover {
  background: #e2e8f0;
}

.osa-group-body {
  margin-top: 4px;
}

.osa-group-header td {
  padding: 6px;
  position: relative;
}

.group-arrow {
  display: inline-block;
  width: 14px;
  margin-right: 6px;
  text-align: center;
  font-weight: bold;
}

.group-massa {
  float: right;
  font-weight: 600;
  color: inherit;   /* sama väri kuin otsikolla */
  margin-left: 12px;
  text-transform:none;
}

.group-name {
  margin-left: 6px;
}

.new-plane-row {
  margin-top: 6px;
}

/* STICKY TALLENNA */

.editor-footer{
  position: sticky;
  bottom: 0;
  background: white;
  border-top: 1px solid #d1d5db;
  padding: 10px;
  text-align: center;
  z-index: 20;
}

.editor-footer button{
  background: #e5e7eb;
  color: #111827;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 14px;
}

.editor-footer button:hover{
  background:#cbd5f5;
}

/* =========================================================
   9. INPUTIT, SELECTIT & FOKUS
========================================================= */

input {
  width: 100%;
  border: none;
  text-align: right;
  font-size: 16px;
  background: transparent;
}

.input-text {
  text-align: left;
}

select { 
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

td input:focus,
td select:focus {
  outline: none;
  box-shadow: inset 0 0 0 2px #2563eb;
  background: #fef3c7;
}

input.error {
  background: #fee2e2;
}

/* Kokoonpano-valitsin siistiksi (Chrome + Firefox) */
td.col-kok select {
  width: 100%;
  border: none;                 /* poistaa reunukset */
  background-color:  #f3f4f6;   /* vaalea harmaa */
  padding: 6px 4px;
  padding-right: 20px;
  font-size: inherit;
  appearance: none;             /* Chrome */
  -webkit-appearance: none;     /* Chrome / Android */
  -moz-appearance: none;        /* Firefox */
}

/* Kevyt nuoli oikeaan reunaan (valinnainen mutta suositeltu) */
td.col-kok {
  position: relative;
}
td.col-kok::after {
  content: "▾";
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: #6b7280;
  font-size: 0.9em;
}

td.col-kok select:focus {
  background-color: #e5e7eb;
  outline: none;
}


/* =========================================================
   10. AKTIIVISET RIVIT & VISUAALISET TILAT
========================================================= */

tbody tr.active-row td {
  background: #e0f2fe !important;
}

.varsi-pos { color:#2563eb; }
.varsi-neg { color:#dc2626; }


/* =========================================================
   11. MODAALIT & TOASTIT
========================================================= */

.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1000;
}

.modal-content {
  background: white;
  margin: 5dvh auto;
  max-width: 420px;
  max-height: 90dvh;
  overflow-y: auto;
  border-radius: 8px;
  padding: 16px;
}

.modal .close {
  float: right;
  font-size: 20px;
  cursor: pointer;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #065f46;   /* tumma vihreä */
  color: white;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 2000;
}

.toast.show {
  opacity: 1;
}


/* =========================================================
   12. MOBIILI
========================================================= */

@media (max-width: 600px) {
  .ohje h2 { margin-top: 0.6em; }
  .ohje h3 { margin-top: 0.5em; }
  .ohje p  { margin-bottom: 0.5em; }
  td.col-num input:focus,
  td.col-num input:focus-visible {
    background: #fde68a;
    font-size: 1.4em;        /* vielä isompi */
    padding: 10px 4px;       /* isompi kosketusalue */
  }

  select {
    padding: 10px 4px;
    font-size: 1.1em;
  }
  /* Numerokentät */
  td.col-num input {
    min-height: 48px;        /* 👈 sormen kokoinen */
    padding: 8px 6px;
    font-size: 1.4em;        /* 👈 selvästi isompi */
    box-sizing: border-box;
  }

  /* Kokoonpano-valitsin */
  td.col-kok select {
    min-height: 48px;
    padding: 8px 6px;
    font-size: 1.2em;
    box-sizing: border-box;
  }

  /* Taulukon rivit väljemmiksi */
  td {
    padding-top: 12px;
    padding-bottom: 12px;
  }
}


/* =========================================================
   13. SELAINKOHTAISET KORJAUKSET (Firefox)
========================================================= */

@-moz-document url-prefix() {

  .col-name {
    width: 48%;     /* lisää tilaa Lennokille */
    min-width: 220px;
  }

  .col-num {
    width: 28%;     /* hieman pienemmäksi vain Firefoxissa */
  }
}


/* =========================================================
   14. OHJE & HELP-NÄKYMÄ
========================================================= */
.ohje-print {
  margin: 12px 0 20px 0;
  text-align: right;
}

.ohje-print button {
  padding: 8px 14px;
  border: none;
  border-radius: 6px;
  background: #e5e7eb;
  color: #111827;
  cursor: pointer;
  font-size: 0.95em;
}

.ohje-print button:hover {
  background: #d1d5db;
}

@media print {
  @page {
    size: A4 portrait;
    margin: 10mm;
  }

  html,
  body {
    width: 100%;
    height: auto;
    min-height: 0;
    overflow: visible !important;
  }

  #appRoot {
    width: 100%;
    min-height: 0;
    height: auto;
    overflow: visible !important;
  }

  .ohje-print {
    display: none !important;
  }

  #ohjeTeksti {
    width: 100% !important;
    max-width: none !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    padding: 0 !important;
  }

  .ohje {
    width: 100% !important;
    max-width: none !important;
    height: auto !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    color: #000;
    background: #fff;
  }

  .toc {
    overflow: visible !important;
    page-break-after: auto;
  }

  .ohje h2,
  .ohje h3 {
    color: #000;
  }

  .ohje a {
    color: #000;
    text-decoration: none;
  }

  .ohje p {
    max-width: none;
  }
}

.help-btn {
  font-size: 18px;
  padding: 4px 10px;
}

pre {
  white-space: pre-wrap;
  font-family: inherit;
}

.ohje {
  font-size: 1.0em;
  line-height: 1.1;
}

#ohjeTeksti h2 { 
  font-size: 1.2em;
  margin-top: 1.0em;
  margin-bottom: 0.4em;
}

#ohjeTeksti h3 { 
  font-size: 1.0em;
  margin-top: 1.0em;
  margin-bottom: 0.3em;
}

#ohjeTeksti p {
  margin-top: 0.1em;
  margin-bottom: 0.4em;
}

#ohjeTeksti {
  max-height: 70vh;     /* ei yli näytön */
  overflow-y: auto;    /* pystyscroll */
  padding: 12px;
  scroll-behavior: smooth;
}

.sisallys {
  padding-left: 16px;
  margin-bottom: 16px;
}

.sisallys a {
  text-decoration: none;
  color: #2563eb;
  font-size: 0.9em;
}

.sisallys a:hover {
  text-decoration: underline;
}

.toc {
  padding-left: 18px;
  margin-bottom: 12px;
}

.toc ul {
  margin: 0;              /* 🔥 TÄRKEIN */
  padding-left: 16px;
}

.toc li {
  margin: 2px 0;
  padding: 0;
  line-height: 1.2;   /* ← TÄRKEIN */
}

.toc a {
  display: block;
  padding: 2px 0;       /* Kevyt klikkausalue */
  text-decoration: none;
  color: #2563eb;
  font-size: 0.9em;
}
