/* Pure Home tiimi – ulkoasu. Mobiili ensin; samat värit kuin laskurissa ja myynnissä. */

:root {
  --bg: #f3f4ef;
  --surface: #ffffff;
  --surface-muted: #f8f9f6;
  --text: #17231c;
  --text-soft: #3a4740;
  --muted: #6b766f;
  --border: #e1e5dd;
  --border-strong: #cdd4ca;
  --primary: #1f5b3f;
  --primary-dark: #163a2a;
  --primary-soft: #e5efe8;
  --accent: #93b33a;
  --accent-soft: #f0f5df;
  --danger: #b3261e;
  --danger-soft: #fbecea;
  --warning: #8a5a00;
  --warning-soft: #fff5dc;
  --warning-border: #efd38f;
  --blue: #24527a;
  --blue-soft: #e4eef8;
  --bubble-mine: #dcebd9;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 34, 24, .05), 0 4px 16px rgba(16, 34, 24, .05);
  --focus: 0 0 0 3px rgba(147, 179, 58, .45);
  --topbar-h: 54px;

  font-family: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "SF Pro Text", Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); }
a { color: var(--primary); text-underline-offset: 2px; }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0 0 .75rem; letter-spacing: -.015em; }
h2 { font-size: 1.1rem; margin: 0 0 .6rem; }
.icon { flex: none; vertical-align: -3px; }
.icon.down { transform: rotate(90deg); }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: 6px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-size: .88rem; margin: .3rem 0 0; }
.hint.flush { margin: 0 0 1rem; }
.hint.centered { text-align: center; }
.error { color: var(--danger); font-size: .9rem; margin: .3rem 0 0; }
.notice { background: var(--warning-soft); border: 1px solid var(--warning-border); color: var(--warning); padding: .6rem .8rem; border-radius: var(--radius-sm); }
[hidden] { display: none !important; }

/* ---------- Yläpalkki ---------- */
.topbar { position: sticky; top: 0; z-index: 50; background: linear-gradient(180deg, #1a4532 0%, var(--primary-dark) 100%); color: #fff; padding-top: env(safe-area-inset-top); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 0 .75rem; display: flex; align-items: center; gap: .75rem; min-height: var(--topbar-h); }
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; text-decoration: none; font-weight: 700; flex: 1; }
.brand-mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--primary-dark); }
.brand-text em { font-style: normal; font-weight: 500; color: #c9dc8f; }
.topnav { display: flex; gap: .15rem; }
.usermenu-link { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 8px; color: rgba(255,255,255,.88); background: none; border: 0; cursor: pointer; }
.usermenu-link:hover { background: rgba(255,255,255,.1); color: #fff; }
form.inline { display: inline; margin: 0; }

/* ---------- Asettelu ---------- */
.container { max-width: 1100px; margin: 0 auto; padding: 1rem .75rem 2rem; }
.container.narrow { max-width: 600px; }
.container.list-page { max-width: 680px; padding-top: .5rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: .75rem; }
.page-head h1 { margin: 0; }
.flash { padding: .7rem .9rem; border-radius: var(--radius-sm); margin-bottom: 1rem; background: var(--blue-soft); color: var(--blue); }
.flash-success { background: var(--primary-soft); color: var(--primary); }
.flash-error { background: var(--danger-soft); color: var(--danger); }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .5rem; }
.error-code { font-size: 2.5rem; font-weight: 800; color: var(--border-strong); margin: 0; }

/* ---------- Painikkeet ja kentät ---------- */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: .55rem 1rem; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.button:hover { border-color: var(--primary); }
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.button.primary:hover { background: var(--primary-dark); }
.button.small { min-height: 36px; padding: .3rem .7rem; font-size: .9rem; }
.button.wide { width: 100%; }
.button:disabled { opacity: .55; cursor: default; }
.icon-button { display: grid; place-items: center; width: 42px; height: 42px; flex: none; border-radius: 10px; border: 0; background: none; color: var(--text-soft); cursor: pointer; text-decoration: none; }
.icon-button:hover { background: var(--surface-muted); color: var(--primary); }
.field { margin-bottom: .9rem; border: 0; padding: 0; min-width: 0; }
.field label, .field legend { display: block; font-weight: 600; margin-bottom: .3rem; color: var(--text-soft); }
.key::after { content: " •"; color: var(--accent); }
input[type=text], input[type=email], input[type=password], input[type=search], select, textarea {
  width: 100%; font: inherit; padding: .65rem .75rem; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: #fff; color: var(--text); min-height: 46px;
}
textarea { resize: vertical; }
.has-error input { border-color: var(--danger); }
.field-grid { display: grid; gap: 0 1rem; grid-template-columns: 1fr; }
@media (min-width: 700px) { .field-grid { grid-template-columns: 1fr 1fr; } }
.radio-list { display: grid; gap: .4rem; }
.radio-row { display: flex; align-items: center; gap: .7rem; padding: .65rem .75rem; border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; min-height: 48px; }
.radio-row input { width: 22px; height: 22px; accent-color: var(--primary); flex: none; }
.radio-row:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.segmented { display: flex; border: 1px solid var(--border-strong); border-radius: 10px; overflow: hidden; }
.segmented label { flex: 1; margin: 0; }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented span { display: grid; place-items: center; min-height: 42px; padding: .3rem .5rem; font-size: .88rem; font-weight: 600; text-align: center; cursor: pointer; color: var(--text-soft); }
.segmented label + label span { border-left: 1px solid var(--border-strong); }
.segmented input:checked + span { background: var(--primary-soft); color: var(--primary-dark); }
.password-box { display: flex; gap: .5rem; align-items: center; margin: 1rem 0; }
.password-box input { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 1.2rem; letter-spacing: .05em; }
.more summary { cursor: pointer; color: var(--primary); font-weight: 600; }
.help { margin-top: 1rem; }
.help p { color: var(--text-soft); font-size: .93rem; }
.tag { display: inline-flex; align-items: center; gap: .2rem; font-size: .78rem; padding: .05rem .45rem; border-radius: 999px; background: var(--surface-muted); border: 1px solid var(--border); color: var(--text-soft); }

/* ---------- Kirjautuminen ---------- */
.auth-page .container { padding-top: 3rem; }
.login-brand { text-align: center; margin-bottom: 1.25rem; }
.login-brand .brand-mark { width: 52px; height: 52px; margin: 0 auto .6rem; border-radius: 14px; }
.brand-title { margin: 0; }
.brand-title span { color: var(--accent); font-weight: 500; }
.login-brand p { color: var(--muted); margin: .2rem 0 0; }

/* ---------- Keskustelulista ---------- */
.push-hint { display: flex; align-items: center; gap: .75rem; justify-content: space-between; background: var(--accent-soft); border: 1px solid var(--accent); border-radius: var(--radius); padding: .6rem .8rem; margin: .5rem 0; font-size: .93rem; }
.list-head { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1rem .25rem .35rem; font-weight: 700; }
.list-head.with-action { display: flex; align-items: center; justify-content: space-between; }
.list-head.with-action h2 { font-size: inherit; margin: 0; }
.list-head .button { text-transform: none; letter-spacing: 0; }
.spaces { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.spaces li + li { border-top: 1px solid var(--border); }
.spaces .empty { padding: .9rem; }
.space-row { display: flex; align-items: center; gap: .7rem; padding: .7rem .8rem; color: var(--text); text-decoration: none; min-height: 64px; }
.space-row:hover { background: var(--surface-muted); }
.space-row[aria-current="page"] { background: var(--primary-soft); }
.space-icon { display: grid; place-items: center; width: 40px; height: 40px; flex: none; border-radius: 12px; background: var(--primary-soft); color: var(--primary); }
.space-icon.kind-kohde { background: var(--accent-soft); color: #5d7420; }
.space-main { display: grid; flex: 1; min-width: 0; }
.space-top { display: flex; align-items: baseline; gap: .5rem; }
.space-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.space-time { font-size: .78rem; color: var(--muted); flex: none; }
.space-preview { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.has-unread .space-name { font-weight: 800; }
.has-unread .space-preview { color: var(--text-soft); }
.has-unread .space-time { color: var(--primary); font-weight: 700; }
.badge { display: grid; place-items: center; min-width: 22px; height: 22px; padding: 0 .4rem; border-radius: 999px; background: var(--primary); color: #fff; font-size: .78rem; font-weight: 700; flex: none; }
.badge.mention { background: var(--accent); color: var(--primary-dark); }
.list-foot { margin: .8rem .25rem; }
.list-foot a { text-decoration: none; }

/* ---------- Haku ---------- */
.search-form { display: flex; gap: .5rem; margin-bottom: 1rem; }
.search-results { list-style: none; margin: 0; padding: 0; }
.search-results li + li { border-top: 1px solid var(--border); }
.search-results a { display: grid; gap: .15rem; padding: .7rem .2rem; color: var(--text); text-decoration: none; }
.search-results a:hover { background: var(--surface-muted); }
.sr-meta { font-size: .85rem; color: var(--muted); }
.sr-text { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- Ylläpito ---------- */
.filter { display: flex; gap: .4rem; overflow-x: auto; margin-bottom: 1rem; padding-bottom: .2rem; }
.filter a { white-space: nowrap; padding: .4rem .85rem; border-radius: 999px; border: 1px solid var(--border-strong); background: #fff; text-decoration: none; color: var(--text-soft); }
.filter a[aria-current="page"] { background: var(--primary); border-color: var(--primary); color: #fff; }
.admin-list { list-style: none; margin: .25rem 0 0; padding: 0; }
.admin-list li { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; justify-content: space-between; padding: .7rem 0; }
.admin-list li + li { border-top: 1px solid var(--border); }
.admin-list li.inactive .admin-main { opacity: .55; }
.admin-main { display: grid; gap: .15rem; flex: 1 1 240px; min-width: 0; }
.row-actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.row-actions form { margin: 0; }

/* ======================================================================
   Keskustelunäkymä. Puhelimessa koko näyttö: otsikko ylhäällä, viestit
   välissä ja kirjoituskenttä alhaalla. Leveällä näytöllä lista vasemmalla.
   ====================================================================== */
.page-space .topbar { display: none; }
.page-space { overflow: hidden; }
.chat-layout { display: flex; height: 100vh; height: 100dvh; }
.chat-sidebar { display: none; }
.chat { position: relative; display: flex; flex-direction: column; flex: 1; min-width: 0; height: 100%; background: var(--bg); }

.chat-head { display: flex; align-items: center; gap: .5rem; padding: calc(.4rem + env(safe-area-inset-top)) .5rem .4rem; background: var(--surface); border-bottom: 1px solid var(--border); z-index: 5; }
.chat-head .space-icon { width: 36px; height: 36px; border-radius: 10px; }
.chat-title { flex: 1; min-width: 0; }
.chat-title h1 { font-size: 1.05rem; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { margin: 0; font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-flash { margin: .5rem .75rem 0; }
.space-brief { margin: .5rem .75rem 0; background: var(--accent-soft); border: 1px solid #dbe6b6; border-radius: var(--radius-sm); padding: .4rem .7rem; font-size: .92rem; }
.space-brief summary { cursor: pointer; font-weight: 600; color: var(--primary-dark); }
.space-brief p { margin: .4rem 0 .2rem; white-space: pre-wrap; color: var(--text-soft); }

.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: .5rem .6rem 1rem; -webkit-overflow-scrolling: touch; }
.messages:focus { box-shadow: none; }
.load-older { text-align: center; margin: .5rem 0; }
.loading { text-align: center; }
.day-sep { display: flex; justify-content: center; margin: 1rem 0 .5rem; position: sticky; top: .25rem; z-index: 2; pointer-events: none; }
.day-sep span { background: rgba(255,255,255,.92); border: 1px solid var(--border); border-radius: 999px; padding: .1rem .7rem; font-size: .78rem; font-weight: 600; color: var(--muted); }
.unread-sep { display: flex; align-items: center; gap: .5rem; margin: .8rem 0; color: var(--primary); font-size: .8rem; font-weight: 700; }
.unread-sep::before, .unread-sep::after { content: ""; flex: 1; height: 1px; background: var(--accent); }

.msg { display: flex; gap: .5rem; align-items: flex-end; margin-top: .55rem; max-width: 100%; }
.msg.cont { margin-top: .12rem; }
.msg .avatar { display: grid; place-items: center; width: 32px; height: 32px; flex: none; border-radius: 50%; background: var(--primary-soft); color: var(--primary-dark); font-size: .75rem; font-weight: 700; }
.msg.cont .avatar { visibility: hidden; height: 0; }
.msg.mine { flex-direction: row-reverse; }
.msg.mine .avatar { display: none; }
.bubble { position: relative; max-width: min(78%, 560px); background: var(--surface); border: 1px solid var(--border); border-radius: 14px 14px 14px 4px; padding: .4rem .65rem .3rem; box-shadow: 0 1px 1px rgba(16,34,24,.04); min-width: 0; }
.msg.cont .bubble { border-radius: 14px 14px 14px 4px; }
.msg.mine .bubble { background: var(--bubble-mine); border-color: #c7dcc2; border-radius: 14px 14px 4px 14px; }
.msg.mention .bubble { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.msg.highlight .bubble { animation: flash 2s ease-out; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 4px rgba(147,179,58,.7); } 100% { box-shadow: none; } }
.msg-who { display: block; font-size: .8rem; font-weight: 700; color: var(--primary); margin-bottom: .05rem; }
.msg-who small { font-weight: 500; color: var(--muted); margin-left: .25rem; }
.msg.mine .msg-who, .msg.cont .msg-who { display: none; }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; }
.msg-body .mention-tag { font-weight: 700; color: var(--primary); }
.msg-body .mention-tag.me { background: var(--accent-soft); border-radius: 4px; padding: 0 .15rem; }
.msg-meta { display: flex; justify-content: flex-end; gap: .35rem; font-size: .72rem; color: var(--muted); margin-top: .1rem; }
.msg.deleted .msg-body { font-style: italic; color: var(--muted); }
.msg-actions { align-self: center; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions, .msg:focus-within .msg-actions, .msg.menu-open .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: .55; } }
.msg-actions .icon-button { width: 32px; height: 32px; }
.msg-menu { position: absolute; z-index: 20; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow); padding: .25rem; min-width: 150px; list-style: none; margin: 0; }
.msg-menu button { display: block; width: 100%; text-align: left; border: 0; background: none; font: inherit; padding: .55rem .7rem; border-radius: 6px; cursor: pointer; color: var(--text); }
.msg-menu button:hover { background: var(--surface-muted); }
.msg-menu button.danger { color: var(--danger); }

.quote { display: block; width: 100%; text-align: left; font: inherit; border: 0; border-left: 3px solid var(--accent); background: rgba(0,0,0,.035); border-radius: 4px; padding: .2rem .5rem; margin: .1rem 0 .3rem; font-size: .85rem; color: var(--text-soft); text-decoration: none; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.quote strong { color: var(--primary); margin-right: .3rem; }

.att-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: .25rem; margin: .2rem 0 .25rem; }
.att-grid.single { grid-template-columns: 1fr; }
.att-img { display: block; padding: 0; border: 0; background: var(--surface-muted); border-radius: 8px; overflow: hidden; cursor: zoom-in; aspect-ratio: 1; }
.att-grid.single .att-img { aspect-ratio: auto; max-height: 320px; }
.att-img img { display: block; width: 100%; height: 100%; object-fit: cover; }
.att-grid.single .att-img img { height: auto; max-height: 320px; object-fit: contain; }
.att-file { display: flex; align-items: center; gap: .5rem; padding: .45rem .55rem; margin: .2rem 0; border: 1px solid var(--border); border-radius: 8px; background: rgba(255,255,255,.7); color: var(--text); text-decoration: none; font-size: .9rem; min-width: 0; }
.att-file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.att-file small { color: var(--muted); flex: none; }

.jump-new { position: absolute; right: .9rem; bottom: 86px; z-index: 6; display: flex; align-items: center; gap: .3rem; border: 0; border-radius: 999px; padding: .45rem .8rem; background: var(--primary); color: #fff; font: inherit; font-size: .85rem; font-weight: 600; box-shadow: var(--shadow); cursor: pointer; }

.archived-bar { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; justify-content: center; padding: .7rem .75rem calc(.7rem + env(safe-area-inset-bottom)); background: var(--warning-soft); border-top: 1px solid var(--warning-border); color: var(--warning); }

/* Kirjoituskenttä */
.composer { position: relative; background: var(--surface); border-top: 1px solid var(--border); padding: .4rem .4rem calc(.4rem + env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: .15rem; }
.composer textarea { flex: 1; min-height: 44px; max-height: 40vh; resize: none; border-radius: 22px; padding: .6rem .9rem; line-height: 1.35; margin: 0 .2rem; }
.composer .attach { cursor: pointer; }
.send-button { display: grid; place-items: center; width: 46px; height: 46px; flex: none; border-radius: 50%; border: 0; background: var(--primary); color: #fff; cursor: pointer; }
.send-button:disabled { opacity: .5; cursor: default; }
.send-button .icon { margin-left: -2px; }
.reply-bar, .edit-bar { display: flex; align-items: center; gap: .5rem; border-left: 3px solid var(--accent); background: var(--surface-muted); border-radius: 6px; padding: .25rem .25rem .25rem .6rem; margin: 0 .2rem .35rem; font-size: .88rem; }
.reply-bar > div, .edit-bar > div { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar strong { color: var(--primary); margin-right: .35rem; }
.file-chips { list-style: none; display: flex; gap: .4rem; overflow-x: auto; margin: 0 .2rem .35rem; padding: 0; }
.file-chips li { position: relative; flex: none; display: flex; align-items: center; gap: .35rem; border: 1px solid var(--border-strong); border-radius: 10px; padding: .25rem 1.9rem .25rem .3rem; background: #fff; font-size: .82rem; max-width: 220px; }
.file-chips img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; }
.file-chips span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chips button { position: absolute; right: .1rem; top: 50%; transform: translateY(-50%); width: 28px; height: 28px; border: 0; border-radius: 50%; background: none; cursor: pointer; color: var(--muted); font-size: 1.1rem; }
.composer-error { margin: 0 .4rem .35rem; }
.mention-pop { position: absolute; left: .6rem; bottom: calc(100% - .2rem); z-index: 30; list-style: none; margin: 0; padding: .25rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow); min-width: 200px; }
.mention-pop li { padding: .5rem .7rem; border-radius: 6px; cursor: pointer; }
.mention-pop li small { color: var(--muted); margin-left: .35rem; }
.mention-pop li[aria-selected="true"], .mention-pop li:hover { background: var(--primary-soft); }

/* Kuvan katselu */
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(10, 18, 14, .92); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: env(safe-area-inset-top) .5rem env(safe-area-inset-bottom); }
.lightbox img { max-width: 100%; max-height: calc(100% - 70px); object-fit: contain; }
.lightbox-bar { display: flex; gap: .5rem; margin-top: .75rem; }

/* Leveä näyttö: lista vasemmalla */
@media (min-width: 900px) {
  .page-space .topbar { display: block; }
  .chat-layout { height: calc(100vh - var(--topbar-h)); height: calc(100dvh - var(--topbar-h)); max-width: 1400px; margin: 0 auto; }
  .chat-sidebar { display: block; width: 340px; flex: none; overflow-y: auto; padding: 0 .75rem 1rem; border-right: 1px solid var(--border); }
  .chat-sidebar .spaces { border-radius: var(--radius-sm); }
  .back-link { display: none; }
  .chat-head { padding-top: .4rem; }
  .messages { padding: .75rem 1.25rem 1rem; }
  .bubble { max-width: min(70%, 620px); }
}
