:root{
  --bg: #0b1220;
  --bg2:#050a14;
  --card:#0a1324cc;
  --border: rgba(148,163,184,.18);
  --text:#e5e7eb;
  --muted:#94a3b8;
  --shadow: 0 18px 60px rgba(0,0,0,.55);
}

:root[data-theme="light"]{
  --bg:#f6f7fb;
  --bg2:#eef2ff;
  --card:#ffffffcc;
  --border: rgba(15,23,42,.14);
  --text:#0b1220;
  --muted:#475569;
  --shadow: 0 18px 60px rgba(2,6,23,.12);
}

*{ box-sizing:border-box; }
html, body { max-width: 100%; overflow-x: hidden; }

body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(56,189,248,.18), transparent 60%),
    radial-gradient(900px 500px at 90% 0%, rgba(168,85,247,.16), transparent 55%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg2) 100%);
  min-height:100vh;
}

/* Global safety: images and long text never break layout */
img { max-width: 100%; height: auto; }
a { color: inherit; }
td, th { overflow-wrap: anywhere; word-break: break-word; }

.wrap{
  max-width: 980px;
  margin: 0 auto;
  padding: 18px 16px;
}

.top{
  position: sticky;
  top:0;
  backdrop-filter: blur(14px);
  background: linear-gradient(180deg, rgba(2,6,23,.55), rgba(2,6,23,.15));
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

:root[data-theme="light"] .top{
  background: linear-gradient(180deg, rgba(255,255,255,.75), rgba(255,255,255,.35));
}

.top .wrap{
  display:flex; align-items:center; justify-content:space-between;
  gap:12px;
}

.brand{ display:flex; gap:12px; align-items:center; }
.logo{
  width:44px; height:44px; border-radius:16px;
  display:grid; place-items:center;
  background: rgba(56,189,248,.14);
  border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
}
.title{ font-size:1.05rem; font-weight:800; letter-spacing:.2px; }
.subtitle{ font-size:.9rem; color:var(--muted); margin-top:2px; }

.card{
  border: 1px solid var(--border);
  background: var(--card);
  box-shadow: var(--shadow);
  border-radius: 22px;
}

.hero{
  display:grid;
  grid-template-columns: 1.2fr .8fr;
  gap:16px;
  padding: 18px;
  overflow:hidden;
}

.hero h1{ margin:0 0 6px 0; font-size:1.55rem; }
.muted{ color: var(--muted); }

.facts{
  display:grid;
  grid-template-columns: repeat(3, minmax(0,1fr));
  gap:10px;
  margin-top:14px;
}

.fact{
  padding:10px 12px;
  border-radius:16px;
  background: rgba(2,6,23,.22);
  border: 1px solid var(--border);
}
:root[data-theme="light"] .fact{ background: rgba(15,23,42,.04); }

.k{ color:var(--muted); font-size:.78rem; margin-bottom:6px; }
.v{ font-weight:750; }
.mono{ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size:.85rem; }

.actions{ display:flex; gap:10px; flex-wrap:wrap; margin-top:14px; }

.btn{
  display:inline-flex; align-items:center; justify-content:center;
  gap:8px;
  padding: 10px 14px;
  border-radius: 16px;
  border: 1px solid var(--border);
  text-decoration:none;
  color: var(--text);
  background: rgba(2,6,23,.18);
}
:root[data-theme="light"] .btn{ background: rgba(15,23,42,.04); }

.btn.primary{
  background: rgba(56,189,248,.18);
  border-color: rgba(56,189,248,.35);
}
.btn.primary:hover{
  box-shadow: 0 0 26px rgba(56,189,248,.22);
}

.btn.ghost:hover{
  box-shadow: 0 0 22px rgba(168,85,247,.18);
}

.btn.danger{
  background:#5c1d1d;
  border:1px solid #8c2b2b;
}
.btn.danger:hover{
  background:#7a2525;
}

.themebtn{
  display:inline-flex; align-items:center; gap:8px;
  padding: 10px 12px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: rgba(2,6,23,.20);
  color: var(--text);
}
:root[data-theme="light"] .themebtn{ background: rgba(15,23,42,.04); }
.themebtn:hover{ box-shadow: 0 0 24px rgba(56,189,248,.18); cursor:pointer; }

.carimg{
  width:100%;
  height: 220px;
  object-fit: cover;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: rgba(2,6,23,.15);
}

.grid{
  margin-top: 14px;
  display:grid;
  grid-template-columns: repeat(2, minmax(0,1fr));
  gap: 12px;
}

.tile{
  position:relative;
  display:flex;
  gap:12px;
  padding: 14px;
  text-decoration:none;
  color: var(--text);
  overflow:hidden;
  transition: transform .12s ease, box-shadow .12s ease;
}

.tile:hover{
  transform: translateY(-2px);
  box-shadow: 0 22px 70px rgba(0,0,0,.62);
}

.tile-icon{
  width:44px; height:44px;
  border-radius:16px;
  display:grid; place-items:center;
  background: rgba(148,163,184,.10);
  border: 1px solid var(--border);
  flex: 0 0 auto;
}

.tile-title{ font-weight:850; letter-spacing:.2px; }
.tile-sub{ color: var(--muted); margin-top:4px; font-size:.92rem; line-height:1.25; }

.tile-accent{
  position:absolute;
  inset: -40px -40px auto auto;
  width: 160px;
  height: 160px;
  border-radius: 999px;
  opacity: .55;
}

.accent-blue{ background: radial-gradient(circle at 30% 30%, rgba(56,189,248,.55), transparent 60%); }
.accent-green{ background: radial-gradient(circle at 30% 30%, rgba(34,197,94,.45), transparent 60%); }
.accent-orange{ background: radial-gradient(circle at 30% 30%, rgba(249,115,22,.45), transparent 60%); }
.accent-purple{ background: radial-gradient(circle at 30% 30%, rgba(168,85,247,.45), transparent 60%); }

.footer{
  margin-top: 18px;
  padding: 12px 2px;
  text-align:center;
  opacity:.8;
}

.badge{
  display:inline-block;
  padding:6px 10px;
  border-radius:999px;
  font-size:.78rem;
  font-weight:700;
  border:1px solid var(--border);
  background: rgba(2,6,23,.18);
}
.badge.ok{ box-shadow: 0 0 18px rgba(34,197,94,.12); }
.badge.warn{ box-shadow: 0 0 18px rgba(249,115,22,.14); }
.badge.bad{ box-shadow: 0 0 18px rgba(239,68,68,.14); }
.badge.muted{ opacity:.75; }

/* =========================
   Tables (desktop defaults)
   ========================= */
.table-wrapper{
  width: 100%;
  overflow-x: auto;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: rgba(2,6,23,.12);
}

table{
  width: 100%;
  border-collapse: collapse;
  min-width: 640px;
}

th, td{
  padding: 10px 10px;
  border-bottom: 1px solid rgba(148,163,184,.12);
  vertical-align: top;
  font-size: .95rem;
}

th{
  text-align: left;
  color: rgba(229,231,235,.85);
  font-weight: 800;
  letter-spacing: .15px;
  background: rgba(2,6,23,.22);
  position: sticky;
  top: 0;
}

:root[data-theme="light"] th{
  color: rgba(15,23,42,.78);
  background: rgba(15,23,42,.04);
}

/* nicer date column */
td.time{
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* =========================
   Attachments (shared)
   ========================= */
.attach-chip{
  display:inline-flex;
  align-items:center;
  gap:8px;
  max-width: 100%;
  flex-wrap: wrap;
}

.attach-thumb{
  width: 64px;
  height: 48px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
  display:block;
}

/* Make sure attachment images never blow up */
td img{
  max-width: 100%;
}

/* =========================
   Etusivu: Viimeisimmät merkinnät -taulukko (desktop)
   ========================= */
.latest-table table{
  table-layout: fixed;
}
.latest-table th,
.latest-table td{
  vertical-align: top;
}
.latest-table th:nth-child(1),
.latest-table td:nth-child(1){ width: 120px; }  /* Pvm */
.latest-table th:nth-child(2),
.latest-table td:nth-child(2){ width: 100px; }  /* Km */
.latest-table th:nth-child(3),
.latest-table td:nth-child(3){ width: 170px; }  /* Kategoria */
.latest-table th:nth-child(4),
.latest-table td:nth-child(4){ width: auto; }   /* Otsikko */

.latest-table td:nth-child(2),
.latest-table td:nth-child(3){
  white-space: nowrap;
}

.latest-table td:nth-child(4){
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.25;
}

.latest-table td{
  padding-top: 12px;
  padding-bottom: 12px;
}

/* =========================
   Login hero layout
   ========================= */
.login-wrap{
  max-width: 980px;
  margin: 0 auto;
  display: grid;
  gap: 14px;
  grid-template-columns: 1.25fr 0.85fr;
  align-items: stretch;
}

.login-hero{
  border-radius: 22px;
  border: 1px solid var(--border);
  min-height: 420px;
  background:
    linear-gradient(180deg, rgba(2,6,23,.10), rgba(2,6,23,.45)),
    url("/static/login-hero.jpg") center/cover no-repeat;
  box-shadow: 0 0 40px rgba(0,0,0,.35);
}

.login-card{
  padding: 16px;
  border-radius: 22px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-error{
  padding: 10px;
  border-radius: 14px;
  border: 1px solid #8c2b2b;
  background: rgba(122,37,37,.12);
}

/* === Responsive tables: turn rows into cards on mobile (Safari-safe) === */
@media (max-width: 700px) {

  .table-wrapper{
    overflow: visible;
    border: none;
    background: transparent;
  }

  table.resp{
    width: 100%;
    border-collapse: separate;
    border-spacing: 0 10px;
    min-width: 0;
  }

  table.resp thead{
    display: none;
  }

  table.resp tbody tr{
    display: block;
    background: rgba(255,255,255,.03);
    border: 1px solid rgba(255,255,255,.10);
    border-radius: 16px;
    padding: 10px 12px;
    box-shadow: 0 0 22px rgba(0,0,0,.25);
  }

  /* ✅ FLEX instead of GRID: fixes iOS/Safari "vertical date" bug */
  table.resp tbody td{
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 6px 0;
    border: none;
    white-space: normal;
  }

  table.resp tbody td::before{
    content: attr(data-label);
    flex: 0 0 92px;            /* label width */
    color: var(--muted);
    font-size: .85rem;
    font-weight: 700;
    line-height: 1.2;
    padding-top: 2px;
  }

  /* arvot (oikea puoli) saa wrapata nätisti */
  table.resp tbody td > *{
    min-width: 0;
  }

  /* päivämäärä & km pysyy yhtenä rivinä */
  td.time{
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  /* linkit: wrapataan mutta ei pilkota kirjainta kerrallaan */
  a{
    overflow-wrap: anywhere;
    word-break: normal;
  }

  /* Liitekuvat mobiilissa: ei koskaan yli ruudun */
  img{
    max-width: 100%;
    height: auto;
  }

  .attach-chip{
    display:inline-flex;
    align-items:center;
    gap:8px;
    flex-wrap:wrap;
  }

  .attach-thumb{
    width: 44px;
    height: 44px;
    border-radius: 10px;
    object-fit: cover;
    border: 1px solid rgba(255,255,255,.12);
    flex: 0 0 auto;
  }
}

/* =========================
   Layout responsive
   ========================= */
@media (max-width: 860px){
  .login-wrap{
    grid-template-columns: 1fr;
  }
  .login-hero{
    min-height: 240px;
  }
  .login-card{
    padding: 14px;
  }
}

@media (max-width: 420px){
  .login-hero{
    min-height: 200px;
    border-radius: 18px;
  }
  .login-card{
    border-radius: 18px;
  }
}

@media (max-width: 900px){
  .latest-table th,
  .latest-table td{
    font-size: .92rem;
  }
  .latest-table th:nth-child(3),
  .latest-table td:nth-child(3){ width: 150px; }
}

@media (max-width: 820px){
  .hero{ grid-template-columns: 1fr; }
  .facts{ grid-template-columns: 1fr; }
  .grid{ grid-template-columns: 1fr; }
  .carimg{ height: 200px; }
}