:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --side: #17202e;
  --side-text: #c5cedb;
  --side-on: #25324a;
  --text: #1a2331;
  --muted: #5b6676;
  --line: #dfe3e9;
  --accent: #0a5fb4;
  --accent-text: #ffffff;
  --good-bg: #e0f3e7;
  --good: #14613a;
  --bad-bg: #fde6e4;
  --bad: #a3271d;
  --note-bg: #fff6dc;
  --note: #6b4e00;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10141a;
    --surface: #1a2029;
    --side: #0c1016;
    --side-text: #aab4c3;
    --side-on: #1d2735;
    --text: #e5e9ef;
    --muted: #98a3b3;
    --line: #2c3542;
    --accent: #72b4f5;
    --accent-text: #0b1420;
    --good-bg: #153423;
    --good: #8edcab;
    --bad-bg: #44201c;
    --bad: #f6a79f;
    --note-bg: #3a3112;
    --note: #f0d68a;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-underline-offset: 2px; }
code { font: 13px/1.4 ui-monospace, "Cascadia Mono", Consolas, monospace; overflow-wrap: anywhere; }
h1 { font-size: 1.55rem; line-height: 1.25; margin: 0 0 .9rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 2.2rem 0 .7rem; }
h2.lead { font-size: 1.25rem; margin-top: .2rem; }
h3 { font-size: .95rem; margin: 1.4rem 0 .5rem; }
section + section { margin-top: .5rem; }
[hidden] { display: none !important; }

/* --- Runko: sivupalkki + sisältö ------------------------------------------- */
body.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.side {
  background: var(--side); color: var(--side-text);
  display: flex; flex-direction: column; gap: 1.25rem;
  padding: 1.1rem .8rem; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: .6rem; padding: 0 .45rem; color: #fff; font-weight: 700; letter-spacing: .08em; text-decoration: none; }
.mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-text); letter-spacing: 0; }
.side nav { display: grid; gap: 2px; }
.side nav a { padding: .45rem .7rem; border-radius: 7px; color: var(--side-text); text-decoration: none; }
.side nav a:hover { background: var(--side-on); color: #fff; }
.side nav a.on { background: var(--side-on); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.navhead { margin: 1rem .7rem .25rem; font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; opacity: .65; }
.who { margin-top: auto; padding: .7rem .7rem 0; border-top: 1px solid var(--side-on); display: grid; gap: .2rem; }
.who .help { color: var(--side-text); text-decoration: none; padding-bottom: .55rem; margin-bottom: .45rem; border-bottom: 1px solid var(--side-on); }
.who .help:hover { color: #fff; }
.whoname { color: #fff; font-weight: 600; overflow-wrap: anywhere; text-decoration: none; }
.whoname:hover, .whoname.on { text-decoration: underline; }
.who .link { color: var(--side-text); text-align: left; }
.orgname { margin: -.6rem .45rem 0; color: #fff; font-weight: 600; overflow-wrap: anywhere; }
.badge { display: inline-block; min-width: 1.4em; padding: 0 .4em; border-radius: 999px; background: var(--bad); color: #fff; font-size: .72rem; font-weight: 700; text-align: center; }

main { padding: 1.75rem 2rem 4rem; max-width: 1240px; width: 100%; }
body.guest main { max-width: 430px; margin: 0 auto; padding: 4.5rem 1.25rem; }
body.guest h1 { letter-spacing: .08em; }

/* --- Yleiset osat ------------------------------------------------------------ */
.row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .9rem; }
.row h1 { margin: 0; }
.crumb { margin: 0 0 .3rem; color: var(--muted); font-size: .9rem; }
.crumb span { opacity: .6; margin: 0 .15rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pre { white-space: pre-wrap; }
.nowrap { white-space: nowrap; }
.wrap { overflow-wrap: anywhere; }
.count { display: inline-block; min-width: 1.6em; padding: 0 .45em; border-radius: 999px; background: var(--line); color: var(--muted); font-size: .78rem; font-weight: 600; text-align: center; vertical-align: middle; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.15rem; margin: 0 0 1rem; }
.card.danger { border-color: var(--bad); }
.card ol, .card ul { margin: .5rem 0; padding-left: 1.3rem; }
.card li { margin: .2rem 0; }
details.card > summary { cursor: pointer; font-weight: 600; }
details.card[open] > summary { margin-bottom: .9rem; padding-bottom: .7rem; border-bottom: 1px solid var(--line); }
details.card > form + p, details.card > form + form { margin-top: 1rem; }
.perms { list-style: none; padding-left: 0 !important; }
.note { background: var(--note-bg); color: var(--note); border-radius: 8px; padding: .55rem .85rem; margin: 0 0 1rem; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(165px, 1fr)); gap: .75rem; margin: 0 0 1.4rem; }
.stats > * { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; color: var(--muted); text-decoration: none; }
.stats a:hover { border-color: var(--accent); }
.stats b { display: block; font-size: 1.6rem; line-height: 1.2; color: var(--text); font-variant-numeric: tabular-nums; }

.facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: .45rem 1.5rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.tabs { display: flex; flex-wrap: wrap; gap: .25rem; border-bottom: 1px solid var(--line); margin: 0 0 1.25rem; }
.tabs a { padding: .5rem .9rem; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); }
.tabs a.on { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem 1rem; margin: 0 0 .7rem; }
.toolbar input[type="search"] { max-width: 320px; background: var(--surface); }

/* --- Taulukot ---------------------------------------------------------------- */
.tablewrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; margin: 0 0 1rem; }
table { width: 100%; border-collapse: collapse; }
main > table { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin: 0 0 1rem; }
th, td { text-align: left; padding: .55rem .85rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .76rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; white-space: nowrap; background: color-mix(in srgb, var(--bg) 55%, var(--surface)); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in srgb, var(--accent) 5%, transparent); }
table.small td { font-size: .9rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.actions { white-space: nowrap; text-align: right; }
.actions > * + * { margin-left: .8rem; }
.actions form { display: inline; }
meter { width: 110px; height: .8rem; vertical-align: middle; }

/* --- Lomakkeet --------------------------------------------------------------- */
.stack { display: grid; gap: .85rem; max-width: 640px; }
.grid3 { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: .8rem; }
form.card.stack, .stack:has(.grid3) { max-width: none; }
label { display: grid; gap: .3rem; font-weight: 600; font-size: .88rem; }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 7px; padding: .48rem .65rem; width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[type="checkbox"] { width: auto; }
input[readonly] { color: var(--muted); }
textarea { font: 13px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; resize: vertical; }
textarea.plain { font: inherit; }
.affix { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
.affix span { color: var(--muted); white-space: nowrap; }
form.inline { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
form.inline select { width: auto; min-width: 11rem; }
form.inline.wide select { flex: 1; min-width: 14rem; }
form.inline label { font-size: .95rem; }

button, .btn {
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; display: inline-block; line-height: 1.3;
  background: var(--accent); color: var(--accent-text); border: 1px solid var(--accent); border-radius: 7px; padding: .48rem 1rem;
}
button:hover, .btn:hover { filter: brightness(1.08); }
button.ghost, .btn.ghost { background: transparent; color: var(--accent); }
button.small { padding: .25rem .65rem; font-size: .85rem; }
button.warn { background: var(--bad); border-color: var(--bad); color: var(--surface); }
button.link { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 400; text-decoration: underline; text-underline-offset: 2px; }
button.link.warn { color: var(--bad); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.flash { padding: .65rem .95rem; border-radius: 8px; margin: 0 0 1.1rem; }
.flash.ok { background: var(--good-bg); color: var(--good); }
.flash.err { background: var(--bad-bg); color: var(--bad); }
.tag { display: inline-block; font-size: .76rem; line-height: 1.5; padding: 0 .5rem; border-radius: 999px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.tag.good { background: var(--good-bg); color: var(--good); border-color: transparent; }
.tag.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
tr.unread td:first-child > a { font-weight: 700; }
.clip { max-width: 46ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stack.wide { max-width: none; }
.msg { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--line); border-radius: var(--radius); padding: .8rem 1.1rem; margin: 0 0 .8rem; }
.msg.out { border-left-color: var(--accent); margin-left: 2.5rem; }
.msg header { display: flex; flex-wrap: wrap; align-items: baseline; gap: .3rem .8rem; }
.msg header time { margin-left: auto; font-size: .85rem; }
.msg p { margin: .2rem 0 0; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: .6rem; max-height: 32rem; overflow-y: auto; }
button:disabled { opacity: .6; cursor: progress; }
.chips { list-style: none; display: flex; flex-wrap: wrap; gap: .5rem; margin: 0 0 .75rem; padding: 0; }
.chips a { display: inline-block; padding: .3rem .8rem; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; text-decoration: none; }
.chips a:hover { border-color: var(--accent); }

@media (max-width: 860px) {
  body.app { display: block; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: .5rem 1rem; padding: .7rem .9rem; }
  .side nav { display: flex; flex-wrap: wrap; flex: 1 1 100%; order: 3; }
  .side nav a.on { box-shadow: none; }
  .navhead { display: none; }
  .who { margin: 0 0 0 auto; padding: 0; border: 0; display: flex; gap: .75rem; align-items: center; }
  main { padding: 1.1rem .95rem 3rem; }
  .grid3 { grid-template-columns: 1fr; }
  .facts { grid-template-columns: 1fr; gap: .1rem; }
  .facts dd { margin-bottom: .55rem; }
}

/* --- Oikeudet, jaot, portaali --------------------------------------------------- */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.perms-grid { border: 1px solid var(--line); border-radius: 8px; padding: .8rem 1rem; margin: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: .8rem 1.2rem; }
.perms-grid legend { padding: 0 .4rem; font-size: .88rem; font-weight: 600; color: var(--muted); }
.perms-grid small { display: block; font-weight: 400; }
.checks { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .4rem 1rem; }
details.card > summary .tag { margin-left: .4rem; font-weight: 400; }
details.card > summary .muted { font-weight: 400; }
.jump { display: flex; flex-wrap: wrap; gap: .4rem 1.1rem; margin: -.3rem 0 1rem; font-size: .92rem; }
.actions-row { display: flex; flex-wrap: wrap; gap: .6rem; margin: 0 0 1.4rem; }
.chips.static span { display: inline-block; padding: .3rem .8rem; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; }
.progress { list-style: none; display: flex; gap: .5rem; padding: 0; margin: 0 0 1.2rem; }
.progress li { flex: 1; display: flex; align-items: center; gap: .55rem; padding: .6rem .8rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); color: var(--muted); }
.progress li span { display: grid; place-items: center; width: 1.7rem; height: 1.7rem; border-radius: 50%; background: var(--line); color: var(--muted); font-weight: 700; font-size: .85rem; flex: none; }
.progress li.done { color: var(--text); }
.progress li.done span { background: var(--good-bg); color: var(--good); }
.progress li.now { color: var(--text); font-weight: 600; border-color: var(--accent); }
.progress li.now span { background: var(--accent); color: var(--accent-text); }
input[type="file"] { padding: .35rem; }
@media (max-width: 860px) {
  .grid2 { grid-template-columns: 1fr; }
  .progress { flex-direction: column; }
  .who .help { border: 0; padding: 0; margin: 0; }
  .orgname { margin: 0; }
}
