:root {
  --bg: #14121a; --bg2: #1c1924; --bg3: #262230; --line: #332e3f;
  --text: #ece8f2; --muted: #9b94a8; --accent: #f2a65a; --accent2: #e07a5f; --danger: #e5675c;
  --user: #2e2a3a; --radius: 14px;
  --font: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.5 var(--ui); }
body { overscroll-behavior: none; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }

.btn { text-decoration: none; display: inline-block; border: 1px solid var(--line); background: var(--bg3); padding: 9px 14px; border-radius: 10px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #4a4358; }
.btn.primary { background: var(--accent); color: #1b1410; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px; border-radius: 8px; line-height: 0; }
.icon-btn:hover { color: var(--text); background: var(--bg3); }
.icon-btn svg { width: 18px; height: 18px; }

input[type=text], input[type=password], input[type=search], input[type=number], textarea, select {
  width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 80px; }
label { display: block; font-weight: 600; margin: 16px 0 6px; }
.hint { color: var(--muted); font-size: 13px; font-weight: 400; margin-top: 4px; }
.err { color: var(--danger); min-height: 1.4em; margin-top: 10px; }

/* Innlogging / oppsett */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth form { width: min(420px, 100%); background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; padding: 28px; }
.auth h1 { font-family: var(--font); font-weight: 600; margin: 0 0 4px; font-size: 28px; }
.auth .btn { width: 100%; margin-top: 20px; padding: 12px; }

/* Toppfelt */
.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px; padding: 12px 20px;
  background: rgba(20,18,26,.92); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.logo { font-family: var(--font); font-size: 22px; font-weight: 600; text-decoration: none; color: var(--text); display: flex; align-items: center; gap: 8px; }
.logo img { width: 26px; height: 26px; }
.top .grow { flex: 1; }
.top input[type=search] { max-width: 320px; }

.page { max-width: 1100px; margin: 0 auto; padding: 24px 20px 60px; }
.page h2 { font-family: var(--font); font-weight: 600; font-size: 20px; margin: 28px 0 12px; }
.page h2:first-child { margin-top: 0; }

/* Karakterkort */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.card { display: flex; gap: 12px; padding: 14px; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius);
  cursor: pointer; text-decoration: none; color: inherit; transition: border-color .15s, transform .15s; min-width: 0; }
.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card .meta { min-width: 0; }
.card .name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .tag { color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .by { color: var(--muted); font-size: 12px; margin-top: 4px; }
.card.new { align-items: center; justify-content: center; border-style: dashed; color: var(--muted); min-height: 86px; }
.recent { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: thin; }
.recent .card { flex: 0 0 240px; }

.avatar { flex: none; width: 56px; height: 56px; border-radius: 50%; object-fit: cover; background: var(--bg3);
  display: grid; place-items: center; font-family: var(--font); font-size: 22px; font-weight: 600; color: #1b1410; }
.avatar.sm { width: 36px; height: 36px; font-size: 15px; }
.avatar.lg { width: 96px; height: 96px; font-size: 38px; }
.empty { color: var(--muted); padding: 30px 0; }

/* Skjema */
.form { max-width: 720px; }
.form .row { display: flex; gap: 14px; align-items: center; }
.form .actions { display: flex; gap: 10px; margin-top: 24px; flex-wrap: wrap; }
.switch { display: flex; align-items: center; gap: 10px; font-weight: 600; margin-top: 16px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
details.adv { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 10px; }
details.adv summary { cursor: pointer; color: var(--muted); }
.section { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; }
.section h3 { margin: 0 0 4px; font-family: var(--font); font-weight: 600; }
.section .btn { margin-top: 14px; }
.users { width: 100%; border-collapse: collapse; margin-top: 10px; }
.users td { padding: 8px 4px; border-top: 1px solid var(--line); }
.users td:last-child { text-align: right; }

/* Chat */
.chat-layout { display: grid; grid-template-columns: 280px 1fr; height: 100vh; height: 100dvh; }
.side { background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side .head { padding: 16px; display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; border-bottom: 1px solid var(--line); }
.side .head .name { font-family: var(--font); font-size: 20px; font-weight: 600; }
.side .head .tag { color: var(--muted); font-size: 13px; }
.side .head .btns { display: flex; gap: 8px; margin-top: 6px; flex-wrap: wrap; justify-content: center; }
.side .list { overflow-y: auto; flex: 1; padding: 8px; }
.side .item { display: block; padding: 9px 10px; border-radius: 10px; color: inherit; text-decoration: none; }
.side .item:hover { background: var(--bg3); }
.side .item.on { background: var(--bg3); box-shadow: inset 3px 0 0 var(--accent); }
.side .item .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side .item .d { color: var(--muted); font-size: 12px; }
.side .foot { padding: 10px; border-top: 1px solid var(--line); }

.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-top { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.chat-top .who { flex: 1; min-width: 0; }
.chat-top .who .n { font-weight: 650; }
.chat-top .who .m { color: var(--muted); font-size: 12px; }
.only-mobile { display: none; }
.msgs { flex: 1; overflow-y: auto; padding: 24px 16px 12px; }
.msgs .inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg .body { min-width: 0; flex: 1; }
.msg .who { font-weight: 650; font-size: 14px; margin-bottom: 2px; display: flex; gap: 8px; align-items: center; }
.msg .text { font-family: var(--font); font-size: 17px; line-height: 1.6; overflow-wrap: anywhere; }
.msg .text em { color: #b9b0c9; }
.msg .text code { font-family: ui-monospace, monospace; font-size: 14px; background: var(--bg3); padding: 1px 5px; border-radius: 5px; }
.msg.user { flex-direction: row-reverse; }
.msg.user .body { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .text { background: var(--user); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 85%; }
.msg .tools { display: flex; align-items: center; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity .15s; }
.msg:hover .tools, .msg.last .tools, .msg:focus-within .tools { opacity: 1; }
.msg .swipe { color: var(--muted); font-size: 13px; min-width: 34px; text-align: center; }
.msg .edit textarea { font-family: var(--font); font-size: 16px; min-height: 100px; }
.msg .edit .r { display: flex; gap: 8px; margin-top: 6px; justify-content: flex-end; }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); margin-left: 1px; }
@keyframes blink { 50% { opacity: 0; } }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bob 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bob { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }
.msg-error { color: var(--danger); background: rgba(229,103,92,.08); border: 1px solid rgba(229,103,92,.3); padding: 10px 14px; border-radius: 10px; font-size: 14px; }

.composer { padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer .box { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; background: var(--bg2);
  border: 1px solid var(--line); border-radius: 22px; padding: 6px 6px 6px 16px; }
.composer .box:focus-within { border-color: #4a4358; }
.composer textarea { border: 0; background: transparent; resize: none; min-height: 24px; max-height: 200px; padding: 8px 0; font-size: 16px; }
.composer .send { flex: none; width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: #1b1410; cursor: pointer; display: grid; place-items: center; }
.composer .send:disabled { opacity: .4; cursor: default; }
.composer .send svg { width: 18px; height: 18px; }
.composer .send.stop { background: var(--bg3); color: var(--text); border: 1px solid var(--line); }
.composer .note { max-width: 760px; margin: 6px auto 0; text-align: center; color: var(--muted); font-size: 12px; }

.menu { position: relative; }
.menu .pop { position: absolute; right: 0; top: 100%; background: var(--bg2); border: 1px solid var(--line); border-radius: 12px;
  padding: 6px; min-width: 200px; z-index: 20; box-shadow: 0 12px 30px rgba(0,0,0,.4); }
.menu .pop button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 8px; cursor: pointer; }
.menu .pop button:hover { background: var(--bg3); }
.menu .pop button.danger { color: var(--danger); }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--bg3); border: 1px solid var(--line);
  padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: 90vw; }
#toast.on { opacity: 1; transform: translateX(-50%); }

@media (max-width: 760px) {
  .chat-layout { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 30; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.5); }
  .only-mobile { display: inline-flex; }
  .top { padding: 10px 14px; flex-wrap: wrap; }
  .top input[type=search] { order: 5; max-width: none; flex-basis: 100%; }
  .page { padding: 18px 14px 50px; }
  .msg .tools { opacity: 1; }
  .msg .text { font-size: 16px; }
}
