:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #5f6670;
  --border: #d5d9df;
  --accent: #1f4e79;
  --accent-text: #ffffff;
  --accent-soft: #e6eef6;
  --brand-orange: #e29e78;
  --ok: #2e7d4f;
  --warn: #b7791f;
  --bad: #c0392b;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1d2126;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --border: #343a42;
    --accent: #6ea8dc;
    --accent-text: #0e1116;
    --accent-soft: #1f2b38;
    --ok: #4cb27a;
    --warn: #e0a64a;
    --bad: #e36a5d;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }

/* Yläpalkki */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; white-space: nowrap; }
.brand img { border-radius: 3px; }
.global-search { position: relative; flex: 1; max-width: 520px; }
.global-search input { width: 100%; }
.search-results { position: absolute; top: calc(100% + 4px); left: 0; right: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12); max-height: 70vh; overflow: auto; padding: 6px; }
.search-results h4 { margin: 8px 8px 4px; font-size: 0.75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.search-results a { display: block; padding: 8px; border-radius: 6px; color: var(--text); text-decoration: none; }
.search-results a:hover, .search-results a.active { background: var(--accent-soft); }
.search-results small { color: var(--muted); display: block; }
.user-menu { position: relative; margin-left: auto; }
.user-menu summary { cursor: pointer; list-style: none; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu .menu { position: absolute; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: 6px; min-width: 190px; display: flex; flex-direction: column; }
.user-menu .menu a, .user-menu .menu button { text-align: left; padding: 9px 10px; border-radius: 6px; color: var(--text); text-decoration: none; background: none; border: 0; font: inherit; cursor: pointer; }
.user-menu .menu a:hover, .user-menu .menu button:hover { background: var(--accent-soft); }
@media (max-width: 700px) {
  .topbar-inner { flex-wrap: wrap; }
  .brand span { display: none; }
  .global-search { order: 3; flex-basis: 100%; max-width: none; }
}

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }

/* Peruselementit */
h1 { font-size: 1.5rem; margin: 0 0 4px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.crumbs { font-size: 0.9rem; margin-bottom: 6px; }
.crumbs a { text-decoration: none; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .grid-2 { grid-template-columns: 1fr; } }

input, textarea, select { font: inherit; font-size: 16px; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; width: 100%; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
button, .button { font: inherit; font-size: 0.92rem; border-radius: 8px; padding: 9px 14px; cursor: pointer; border: 1px solid var(--accent); background: var(--surface); color: var(--accent); display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; white-space: nowrap; }
button.primary, .button.primary { background: var(--accent); color: var(--accent-text); }
button.ghost, .button.ghost { border-color: var(--border); color: var(--text); }
button.danger { border-color: var(--bad); color: var(--bad); }
button:disabled { opacity: .55; cursor: default; }

/* Tietolistat */
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.facts dt { color: var(--muted); font-size: 0.9rem; }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
@media (max-width: 500px) { dl.facts { grid-template-columns: 1fr; } dl.facts dd { margin-bottom: 6px; } }

table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 0.78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.list td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.list tr.clickable { cursor: pointer; }
table.list tr.clickable:hover td { background: var(--accent-soft); }
table.list td a { font-weight: 600; text-decoration: none; }
.table-wrap { overflow-x: auto; }
@media (max-width: 640px) {
  table.list.responsive thead { display: none; }
  table.list.responsive tr { display: block; border-bottom: 1px solid var(--border); padding: 8px 0; }
  table.list.responsive td { display: block; border: 0; padding: 2px 10px; }
  table.list.responsive td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: 0.85rem; }
}
.empty { padding: 24px; text-align: center; color: var(--muted); }

.badge { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; }
.badge.luonnos { color: var(--warn); border-color: var(--warn); }
.badge.valmis { color: var(--ok); border-color: var(--ok); }
.badge.bad { color: var(--bad); border-color: var(--bad); }

.list-filter { margin-bottom: 12px; }

/* Kirjautuminen */
.login-wrap { min-height: 80vh; display: grid; place-items: center; }
.login-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; }
.login-card .brand { margin-bottom: 18px; font-size: 1.1rem; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }
.form-error { color: var(--bad); font-size: 0.9rem; margin: 0; }
.notice { background: var(--accent-soft); border-radius: 8px; padding: 10px 12px; font-size: 0.92rem; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 1.1rem; background: var(--bg); border: 1px dashed var(--border); padding: 8px 12px; border-radius: 6px; user-select: all; display: inline-block; margin: 4px 0; }

/* Lomakeikkuna */
dialog { width: min(640px, 96vw); padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dialog-head { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.dialog-body { padding: 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; max-height: 65vh; overflow: auto; }
.dialog-body .span-2 { grid-column: span 2; }
.dialog-body h3 { grid-column: span 2; margin: 6px 0 0; font-size: 0.95rem; }
@media (max-width: 560px) { .dialog-body { grid-template-columns: 1fr; } .dialog-body .span-2, .dialog-body h3 { grid-column: auto; } }
#form-dialog-error { padding: 0 16px 8px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }

.loading { padding: 40px; text-align: center; color: var(--muted); }
.home-grid { grid-template-columns: 2fr 1fr; }
@media (max-width: 800px) { .home-grid { grid-template-columns: 1fr; } }
button.danger-solid { background: var(--bad); border-color: var(--bad); color: #fff; }

/* Valintaruudut lomakeikkunassa (lomakeoikeudet, näkyvyys) */
.checks { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin: 0; }
.checks legend { font-size: 0.85rem; color: var(--muted); padding: 0 4px; }
label.check-row { flex-direction: row; align-items: flex-start; gap: 10px; padding: 6px 0; font-size: 0.95rem; color: var(--text); cursor: pointer; }
label.check-row input { width: 18px; height: 18px; margin: 2px 0 0; flex: none; padding: 0; }
.small-btn { padding: 5px 10px; font-size: 0.82rem; }

/* Asiakashallinta */
.sched-tools { display: flex; gap: 10px; flex-wrap: wrap; }
.sched-tools input { flex: 1 1 280px; }
.sched-tools select { flex: 0 1 280px; width: auto; }
.sched-customer-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.sched-customer-head h2 { margin: 0; }
.sched-customer-head h2 a { text-decoration: none; }
.sched-site { border-top: 1px solid var(--border); padding: 10px 0 4px; }
.sched-site-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.sched-site-head a { font-weight: 600; text-decoration: none; }
.sched-empty { margin: 6px 0; }
.sched-table td { vertical-align: middle; padding: 6px 10px; }
.sched-table select, .sched-table input[type=date] { width: auto; min-width: 9.5em; padding: 6px 8px; }
.sched-table .row-actions { text-align: right; }
.sched-table tr.saved td { background: var(--accent-soft); transition: background .3s; }
.due { display: inline-flex; flex-direction: column; }
.due small { font-size: 0.78rem; }
.due.overdue { color: var(--bad); }
.due.soon { color: var(--warn); }
.due.none small { color: var(--muted); }
@media (max-width: 700px) {
  .sched-table td { padding: 4px 10px; }
  .sched-table .row-actions { text-align: left; }
}
@media (min-width: 701px) {
  .sched-table { table-layout: fixed; }
  .sched-table th:nth-child(1) { width: 34%; }
  .sched-table th:nth-child(2) { width: 17%; }
  .sched-table th:nth-child(3) { width: 19%; }
  .sched-table th:nth-child(4) { width: 18%; }
  .sched-table th:nth-child(5) { width: 12%; }
  .sched-table select, .sched-table input[type=date] { width: 100%; min-width: 0; }
}
