:root {
  --bg: #f6f6f4; --panel: #fff; --line: #e4e3df; --text: #1c1c1a; --muted: #77756f;
  --accent: #2f5bd3; --danger: #c2372b;
  --s-new: #3d6fe0; --s-working: #d99a1e; --s-need_to_reply: #d0453a; --s-replied: #2e9d6a; --s-closed: #a3a19b;
  --radius: 8px; --shadow: 0 1px 2px rgba(0,0,0,.06);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #161615; --panel: #1f1f1d; --line: #33322f; --text: #ecebe7; --muted: #9a988f; --accent: #7a9bff; --shadow: none; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); font-size: 12px; }
.error { color: var(--danger); }
[hidden] { display: none !important; }

.btn { display: inline-block; border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.small { padding: 2px 8px; font-size: 12px; }

.login { max-width: 360px; margin: 18vh auto; text-align: center; padding: 0 16px; }

/* Layout */
.app { display: grid; grid-template-columns: 180px 1fr; min-height: 100vh; }
.side { border-right: 1px solid var(--line); background: var(--panel); padding: 16px 10px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 650; padding: 0 8px 14px; }
.side a[data-view] { color: var(--text); padding: 6px 8px; border-radius: 6px; }
.side a[data-view]:hover { background: var(--bg); }
.side a.active { background: var(--bg); font-weight: 600; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 4px; padding: 0 8px; }
#view { padding: 16px 20px; min-width: 0; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar h1 { font-size: 18px; margin: 0 12px 0 0; }
.toolbar input[type=search] { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 5px 10px; width: 220px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 5px 10px; cursor: pointer; }
.seg button.on { background: var(--text); color: var(--bg); }

/* Tavla */
.lane { margin-bottom: 18px; }
.lane > h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }
.cols { display: grid; grid-template-columns: repeat(5, minmax(200px, 1fr)); gap: 8px; overflow-x: auto; }
.col { background: rgba(127,127,127,.06); border-radius: var(--radius); padding: 6px; min-height: 80px; display: flex; flex-direction: column; gap: 6px; transition: background .1s; }
.col.over { background: rgba(47,91,211,.12); outline: 2px dashed var(--accent); outline-offset: -2px; }
.col-head { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 2px 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.col-head .n { color: var(--muted); font-weight: 400; }
.more { font-size: 12px; color: var(--muted); text-align: center; cursor: pointer; padding: 2px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 8px 9px; cursor: grab; box-shadow: var(--shadow); border-left: 3px solid var(--line); }
.card:hover { border-color: var(--muted); }
.card.dragging { opacity: .4; }
.card .subj { font-weight: 550; margin-bottom: 4px; overflow-wrap: anywhere; }
.card .f { font-size: 12px; color: var(--muted); display: flex; gap: 4px; }
.card .f b { font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card.in { border-left-color: var(--s-need_to_reply); }
.card.out { border-left-color: var(--s-replied); }

.popover { position: absolute; z-index: 20; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; box-shadow: 0 6px 24px rgba(0,0,0,.15); }
.popover label { display: flex; gap: 6px; align-items: center; padding: 2px 0; }

/* Ärendepanel */
#drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 100vw); background: var(--panel); border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(0,0,0,.12); overflow-y: auto; z-index: 10; }
.d-head { position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--line); padding: 14px 18px; z-index: 1; }
.d-head h2 { font-size: 17px; margin: 0 28px 6px 0; overflow-wrap: anywhere; }
.d-close { position: absolute; top: 10px; right: 12px; border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }
.d-meta { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.d-meta b { color: var(--text); font-weight: 500; }
.d-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.d-actions select { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 4px 6px; }
.d-body { padding: 14px 18px 40px; }
.d-body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 8px; }

.msg { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; max-width: 92%; }
.msg.in { margin-right: auto; }
.msg.out { margin-left: auto; background: rgba(46,157,106,.06); }
.msg.internal { margin: 0 auto 10px; background: rgba(217,154,30,.08); border-style: dashed; }
.msg-head { display: flex; justify-content: space-between; gap: 10px; padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 12px; }
.msg-head .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-body { padding: 8px 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-body.clamped { max-height: 260px; overflow: hidden; position: relative; }
.msg-body.clamped::after { content: ""; position: absolute; inset: auto 0 0 0; height: 40px; background: linear-gradient(transparent, var(--panel)); }
.msg .expand { font-size: 12px; padding: 0 10px 8px; cursor: pointer; color: var(--accent); }

/* Livscykel */
.life { display: flex; height: 14px; border-radius: 4px; overflow: hidden; background: var(--line); }
.life span { height: 100%; min-width: 2px; }
.life-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.life-legend i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 4px; }
.ev { font-size: 12px; color: var(--muted); padding: 2px 0; }
.ev b { color: var(--text); font-weight: 500; }

.gantt { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.g-row { display: grid; grid-template-columns: minmax(160px, 28%) 1fr; border-bottom: 1px solid var(--line); cursor: pointer; }
.g-row:last-child { border-bottom: 0; }
.g-row:hover { background: rgba(127,127,127,.05); }
.g-label { padding: 5px 10px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-right: 1px solid var(--line); }
.g-label .muted { margin-left: 4px; }
.g-track { position: relative; height: 26px; }
.g-seg { position: absolute; top: 7px; height: 12px; }
.g-seg:first-child { border-radius: 3px 0 0 3px; }
.g-tick { position: absolute; top: 4px; width: 2px; height: 18px; background: var(--text); opacity: .55; }
.g-tick.out { background: var(--s-replied); opacity: .9; }
.g-axis { display: grid; grid-template-columns: minmax(160px, 28%) 1fr; font-size: 11px; color: var(--muted); border-bottom: 1px solid var(--line); }
.g-axis .track { position: relative; height: 22px; }
.g-axis .track span { position: absolute; top: 4px; transform: translateX(-50%); white-space: nowrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; }
.stat .v { font-size: 18px; font-weight: 600; }
.stat .k { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }

/* Status */
.form { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; max-width: 640px; }
.form label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.form input[type=text], .form input[type=date], .form input[type=time], .form input[type=number], .form textarea, .form select { border: 1px solid var(--line); background: var(--bg); border-radius: 6px; padding: 6px 8px; color: var(--text); font-size: 14px; }
.form textarea { min-height: 90px; resize: vertical; }
.form .row { display: flex; gap: 10px; flex-wrap: wrap; }
.form .check { display: flex; gap: 6px; align-items: center; color: var(--text); font-size: 14px; }
.notice { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; max-width: 640px; }
.notice .h { display: flex; justify-content: space-between; gap: 10px; }
.pill { font-size: 11px; border-radius: 999px; padding: 1px 8px; background: var(--bg); border: 1px solid var(--line); }
.two { display: grid; grid-template-columns: minmax(0, 640px) minmax(0, 1fr); gap: 20px; align-items: start; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 8px 16px; }
  .brand { padding: 0 8px 0 0; }
  .side-foot { margin: 0 0 0 auto; flex-direction: row; }
  #view { padding: 12px 16px; }
  .two { grid-template-columns: 1fr; }
}
