@font-face {
  font-family: 'TD Mono';
  src: url('fonts/JetBrainsMonoNL-Regular.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'TD Mono';
  src: url('fonts/JetBrainsMonoNL-Bold.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}

:root {
  --a: #3dff7a;           /* aksentfarge (fosfor) */
  --bg: #030503;
  --bg2: #070b07;
  --red: #ff4d4d;
  --amber: #ffb000;
  --font: 'TD Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --fg: color-mix(in srgb, var(--a) 82%, #fff);
  --dim: color-mix(in srgb, var(--a) 52%, var(--bg));
  --faint: color-mix(in srgb, var(--a) 16%, var(--bg));
  --line: color-mix(in srgb, var(--a) 34%, var(--bg));
  --glow: 0 0 6px color-mix(in srgb, var(--a) 55%, transparent);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --console-h: 44px;
  color-scheme: dark;
}
html[data-theme='amber']   { --a: #ffb000; --bg: #060400; --bg2: #0c0802; }
html[data-theme='cyan']    { --a: #39e6ff; --bg: #02050a; --bg2: #050a12; }
html[data-theme='mono']    { --a: #e8e8e8; --bg: #050505; --bg2: #0b0b0b; }
html[data-theme='magenta'] { --a: #ff4fd8; --bg: #070208; --bg2: #0d050e; }
html.noglow { --glow: none; }

.c-green   { --a: #3dff7a; }
.c-amber   { --a: #ffb000; }
.c-cyan    { --a: #39e6ff; }
.c-red     { --a: #ff4d4d; }
.c-magenta { --a: #ff4fd8; }
.c-white   { --a: #ececec; }
.c-blue    { --a: #6f9bff; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.4;
  color: var(--fg);
  text-shadow: var(--glow);
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}
input, textarea, select, button { font: inherit; color: inherit; text-shadow: inherit; }
input, textarea { -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }
.dim { color: var(--dim); }
.err { color: var(--red); --a: var(--red); }
.ok { color: var(--a); }
a { color: var(--a); }

/* ---------- CRT-effekt ---------- */
#crt {
  position: fixed; inset: 0; pointer-events: none; z-index: 100;
  background:
    repeating-linear-gradient(to bottom, transparent 0, transparent 2px, rgba(0, 0, 0, .28) 3px, transparent 4px),
    radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, .55) 100%);
  mix-blend-mode: multiply;
}
html.noscan #crt { background: radial-gradient(ellipse at center, transparent 60%, rgba(0, 0, 0, .4) 100%); }

.screen { min-height: 100%; }
body.booting .screen { opacity: 0; }
.screen { animation: power-on .35s ease-out; }
@keyframes power-on {
  0% { opacity: 0; transform: scaleY(.02); filter: brightness(3); }
  60% { opacity: 1; transform: scaleY(1.01); }
  100% { transform: none; filter: none; }
}

/* ---------- knapper ---------- */
.btn, .tbtn {
  background: transparent; border: 1px solid var(--line); color: var(--a);
  padding: 10px 14px; cursor: pointer; letter-spacing: .04em; border-radius: 0;
  transition: background .08s, color .08s;
  touch-action: manipulation;
}
.btn:active, .tbtn:active, .btn.primary { background: var(--a); color: var(--bg); text-shadow: none; }
.btn.primary:active { filter: brightness(1.3); }
.btn.danger { --a: var(--red); color: var(--red); border-color: color-mix(in srgb, var(--red) 45%, var(--bg)); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .4; }
.tbtn { border: none; padding: 8px 6px; min-width: 40px; }

/* ---------- innlogging ---------- */
#login { display: grid; place-items: center; padding: 24px; padding-top: calc(24px + var(--sat)); }
.login-box { width: 100%; max-width: 340px; display: grid; gap: 18px; }
.ascii { margin: 0; color: var(--a); font-size: 15px; line-height: 1.15; }
.prompt-line { display: flex; gap: 10px; align-items: center; border-bottom: 1px dashed var(--line); padding: 8px 0; }
.prompt-line input { flex: 1; min-width: 0; background: none; border: 0; outline: 0; caret-color: var(--a); font-size: 16px; }

/* ---------- topp ---------- */
#top {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
  padding: calc(6px + var(--sat)) 12px 0;
  border-bottom: 1px solid var(--faint);
}
.title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#title { font-size: 15px; font-weight: 700; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ps1-user { color: var(--a); }
.cursor { animation: blink 1.05s steps(1) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.top-actions { display: flex; flex-shrink: 0; }
body.editing #btnEdit { background: var(--a); color: var(--bg); text-shadow: none; }

#statusbar {
  display: flex; gap: 14px; overflow-x: auto; scrollbar-width: none; font-size: 11px;
  padding: 2px 0 8px; color: var(--dim); white-space: nowrap;
}
#statusbar::-webkit-scrollbar { display: none; }
.st { display: inline-flex; gap: 5px; align-items: center; }
.st b { font-weight: 400; }
.st.online b { color: var(--a); }
.st.connecting b { color: var(--amber); animation: blink .8s steps(1) infinite; }
.st.error b, .st.offline b { color: var(--red); }

#tabs { display: flex; gap: 0; overflow-x: auto; scrollbar-width: none; margin: 0 -12px; padding: 0 12px; }
#tabs::-webkit-scrollbar { display: none; }
#tabs button {
  background: none; border: 0; color: var(--dim); padding: 8px 10px 9px; cursor: pointer; white-space: nowrap;
  border-bottom: 2px solid transparent; text-transform: uppercase; font-size: 12px; letter-spacing: .06em;
}
#tabs button.on { color: var(--a); border-bottom-color: var(--a); }

/* ---------- rutenett ---------- */
#grid {
  --cols: var(--cols-base, 2);
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
  gap: 20px 12px;
  padding: 22px 12px calc(var(--console-h) + 28px + var(--sab));
  max-width: 1100px; margin: 0 auto;
}
@media (min-width: 700px) { #grid { --cols: calc(var(--cols-base, 2) + 1); } }
@media (min-width: 1000px) { #grid { --cols: calc(var(--cols-base, 2) + 2); } }

.card {
  position: relative; display: flex; flex-direction: column; justify-content: center;
  min-height: 108px; padding: 18px 12px 12px;
  background: var(--bg2); border: 1px solid var(--line); color: var(--a);
  text-align: left; cursor: pointer; border-radius: 0;
  touch-action: manipulation;
  transition: background .07s, color .07s, transform .07s, border-color .2s;
}
.card.wide { grid-column: 1 / -1; }
.card .label {
  position: absolute; top: -.75em; left: 8px; max-width: calc(100% - 16px);
  padding: 0 6px; background: var(--bg); color: var(--a);
  font-size: 11px; letter-spacing: .08em; text-transform: uppercase;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card .foot {
  margin-top: 8px; font-size: 11px; color: var(--dim); min-height: 1.4em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card .foot.err { color: var(--red); }
.card .big { font-size: 20px; font-weight: 700; letter-spacing: .03em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .icon { font-size: 22px; line-height: 1; margin-bottom: 6px; text-shadow: none; }

/* knapp-widget */
.w-button .big::before { content: '> '; color: var(--dim); }
.w-button:active, .w-button.pressing {
  background: var(--a); color: var(--bg); text-shadow: none; transform: scale(.975);
}
.w-button:active .big::before, .w-button:active .label, .w-button:active .foot,
.w-button.pressing .big::before, .w-button.pressing .foot { color: var(--bg); }
.w-button:active .label, .w-button.pressing .label { background: var(--a); color: var(--bg); }

/* bekreft-hold */
.card .hold {
  position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: var(--a);
}
.card.holding .hold { width: 100%; transition: width var(--hold, 700ms) linear; }
.card.holding { transform: scale(.985); }

/* pending / ok / feil */
.card.busy .foot::after { content: ' █'; animation: blink .4s steps(1) infinite; }
.card.flash-ok { animation: flash-ok .45s ease-out; }
.card.flash-err { animation: flash-err .6s ease-out; border-color: var(--red); }
@keyframes flash-ok { 0% { background: var(--a); color: var(--bg); } 100% { background: var(--bg2); } }
@keyframes flash-err {
  0%, 100% { transform: translateX(0); } 20% { transform: translateX(-5px); } 40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); } 80% { transform: translateX(3px); }
}

/* bryter */
.sw { display: flex; border: 1px solid var(--line); width: max-content; margin-top: 2px; }
.sw span { padding: 6px 12px; font-weight: 700; font-size: 13px; color: var(--dim); }
.sw span + span { border-left: 1px solid var(--line); }
.w-switch.on .sw .s-on { background: var(--a); color: var(--bg); text-shadow: none; }
.w-switch.off .sw .s-off { background: var(--faint); color: var(--fg); }
.w-switch.on { border-color: var(--a); box-shadow: inset 0 0 18px color-mix(in srgb, var(--a) 14%, transparent); }
.w-switch:active { transform: scale(.975); }
.w-switch.unknown .sw { border-style: dashed; }

/* sensor */
.w-sensor { cursor: default; }
.w-sensor .val { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.w-sensor .num { font-size: 30px; font-weight: 700; line-height: 1.05; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-sensor .num.txt { font-size: 17px; white-space: normal; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.w-sensor .unit { color: var(--dim); font-size: 13px; }
.w-sensor.warn { --a: var(--red); border-color: var(--red); }
.w-sensor.warn .label::before { content: '⚠ '; }
.w-sensor.warn .label { animation: blink 1.2s steps(1) infinite; }
.spark { font-size: 12px; letter-spacing: -1px; color: var(--dim); margin-top: 4px; height: 1.2em; overflow: hidden; white-space: nowrap; }
.w-sensor.stale .num { color: var(--dim); }

/* glidebryter */
.w-slider { cursor: default; }
.w-slider .big { font-size: 24px; line-height: 1.1; }
.w-slider .big small { font-size: 13px; color: var(--dim); font-weight: 400; margin-left: 4px; }
.w-slider input[type=range] { width: 100%; margin-top: 4px; }
input[type=range] {
  -webkit-appearance: none; appearance: none; flex: 1; height: 34px; background: transparent; margin: 0; touch-action: pan-y;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 10px; border: 1px solid var(--line);
  background: linear-gradient(to right, var(--a) 0 var(--p, 0%), transparent var(--p, 0%));
}
input[type=range]::-moz-range-track { height: 10px; border: 1px solid var(--line); background: transparent; }
input[type=range]::-moz-range-progress { height: 10px; background: var(--a); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 28px; margin-top: -10px; background: var(--bg); border: 2px solid var(--a);
  box-shadow: var(--glow);
}
input[type=range]::-moz-range-thumb { width: 18px; height: 26px; background: var(--bg); border: 2px solid var(--a); border-radius: 0; }

/* redigeringsmodus */
body.editing .card { border-style: dashed; cursor: pointer; }
body.editing .card:active { transform: none; background: var(--bg2); color: var(--a); }
.edit-bar { display: none; }
body.editing .card .foot { visibility: hidden; }
body.editing .edit-bar {
  display: flex; position: absolute; right: -1px; bottom: -1px; background: var(--bg);
  border: 1px solid var(--line); z-index: 2;
}
.edit-bar button { background: none; border: 0; color: var(--a); padding: 6px 8px; min-width: 32px; cursor: pointer; font-size: 13px; }
.edit-bar button:active { background: var(--a); color: var(--bg); }
.card.add {
  border-style: dashed; color: var(--dim); align-items: center; text-align: center; font-size: 14px;
}
.card.add:active { background: var(--faint); }
.empty { grid-column: 1 / -1; color: var(--dim); white-space: pre-wrap; font-size: 13px; line-height: 1.6; }
.empty .btn { margin-top: 14px; }

/* ---------- konsoll ---------- */
#console {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg) 96%, transparent);
  border-top: 1px solid var(--line);
  display: flex; flex-direction: column;
  height: calc(55vh + var(--sab)); max-height: calc(100% - 60px);
  padding-bottom: var(--sab);
  transition: height .2s ease;
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
#console.collapsed { height: calc(var(--console-h) + var(--sab)); }
.console-head {
  display: flex; gap: 10px; align-items: center; height: var(--console-h); flex-shrink: 0;
  background: none; border: 0; padding: 0 12px; text-align: left; color: var(--a); cursor: pointer; font-size: 12px;
}
.console-head #consoleLast { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#console.collapsed #logView, #console.collapsed #cmdForm { display: none; }
#logView {
  flex: 1; overflow-y: auto; padding: 0 12px 6px; font-size: 12px; line-height: 1.5;
  -webkit-user-select: text; user-select: text; overscroll-behavior: contain;
}
.ln { white-space: pre-wrap; word-break: break-word; }
.ln .t { color: var(--dim); }
.ln.tx .k { color: var(--a); }
.ln.rx .k { color: #39e6ff; }
.ln.ok .k { color: var(--a); }
.ln.warn, .ln.warn .k { color: var(--amber); }
.ln.err, .ln.err .k { color: var(--red); }
.ln.cmd { color: var(--fg); }
.ln.out { color: var(--fg); }
.ln.info .k { color: var(--dim); }
#cmdForm {
  display: flex; gap: 8px; align-items: center; padding: 8px 12px; border-top: 1px dashed var(--faint); flex-shrink: 0;
}
#cmd { flex: 1; background: none; border: 0; outline: 0; font-size: 16px; caret-color: var(--a); min-width: 0; }
#cmd::placeholder { color: var(--faint); }
.ps1 { color: var(--a); font-weight: 700; }

/* ---------- ark (modal) ---------- */
.sheet { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .7); }
.sheet-panel {
  position: relative; width: 100%; max-width: 620px; max-height: calc(100% - 24px - var(--sat));
  display: flex; flex-direction: column;
  background: var(--bg); border: 1px solid var(--line); border-bottom: 0;
  animation: sheet-up .18s ease-out;
}
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-panel { border-bottom: 1px solid var(--line); max-height: 88vh; }
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
.sheet-head {
  display: flex; justify-content: space-between; align-items: center; padding: 6px 6px 6px 14px;
  border-bottom: 1px solid var(--faint); color: var(--a); font-weight: 700; text-transform: uppercase; letter-spacing: .06em; font-size: 13px;
}
.sheet-body { overflow-y: auto; padding: 14px; overscroll-behavior: contain; }
.sheet-foot { display: flex; gap: 10px; padding: 10px 14px calc(10px + var(--sab)); border-top: 1px solid var(--faint); }
.sheet-foot:empty { display: none; }
.sheet-foot .btn { flex: 1; }

/* skjema */
.f { display: grid; gap: 4px; margin-bottom: 14px; }
.f > span { font-size: 11px; color: var(--dim); text-transform: uppercase; letter-spacing: .07em; }
.f input:not([type=checkbox]), .f select, .f textarea {
  width: 100%; background: var(--bg2); border: 1px solid var(--faint); outline: 0; padding: 10px; font-size: 16px;
  border-radius: 0; caret-color: var(--a); color: var(--fg); -webkit-appearance: none; appearance: none;
}
.f select {
  background-image: linear-gradient(45deg, transparent 50%, var(--a) 50%), linear-gradient(135deg, var(--a) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 28px;
}
.f textarea { min-height: 70px; resize: vertical; font-size: 14px; }
.f input:focus, .f select:focus, .f textarea:focus { border-color: var(--a); }
.f input::placeholder, .f textarea::placeholder { color: color-mix(in srgb, var(--a) 26%, var(--bg)); text-shadow: none; }
.f small { color: var(--dim); font-size: 11px; line-height: 1.45; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.chk { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; cursor: pointer; }
.chk input { appearance: none; -webkit-appearance: none; width: 20px; height: 20px; border: 1px solid var(--line); margin: 0; display: grid; place-items: center; flex-shrink: 0; }
.chk input:checked::after { content: '■'; color: var(--a); font-size: 13px; line-height: 1; }
fieldset { border: 1px solid var(--faint); margin: 6px 0 16px; padding: 14px 12px 4px; }
legend { padding: 0 6px; color: var(--a); font-size: 12px; text-transform: uppercase; letter-spacing: .07em; }
.seg { display: flex; flex-wrap: wrap; border: 1px solid var(--line); margin-bottom: 14px; }
.seg button { flex: 1; background: none; border: 0; padding: 9px 4px; color: var(--dim); cursor: pointer; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; min-width: 64px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--a); color: var(--bg); text-shadow: none; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatches button { width: 34px; height: 34px; border: 1px solid var(--line); background: var(--a); cursor: pointer; }
.swatches button.none { background: repeating-linear-gradient(45deg, var(--bg2) 0 4px, var(--faint) 4px 8px); }
.swatches button.on { outline: 2px solid var(--fg); outline-offset: 2px; }
.list-item {
  display: flex; align-items: center; gap: 10px; padding: 12px 10px; border: 1px solid var(--faint); margin-bottom: 10px; cursor: pointer;
}
.list-item:active { background: var(--faint); }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .sub { color: var(--dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.codebox {
  background: var(--bg2); border: 1px dashed var(--line); padding: 10px; font-size: 12px; word-break: break-all;
  -webkit-user-select: all; user-select: all; white-space: pre-wrap; margin: 4px 0 8px;
}
.result { font-size: 12px; white-space: pre-wrap; word-break: break-word; max-height: 160px; overflow: auto; margin: 0 0 12px; }
.menu-list button { display: block; width: 100%; text-align: left; margin-bottom: 8px; }
.hint { color: var(--dim); font-size: 12px; line-height: 1.55; margin: 0 0 14px; }
hr { border: 0; border-top: 1px dashed var(--faint); margin: 16px 0; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; top: calc(12px + var(--sat)); transform: translate(-50%, calc(-100% - 40px)); z-index: 90;
  visibility: hidden;
  background: var(--bg); border: 1px solid var(--a); color: var(--a); padding: 10px 14px; font-size: 13px;
  max-width: calc(100% - 24px); transition: transform .2s ease, visibility 0s linear .2s; pointer-events: none;
}
#toast.show { transform: translate(-50%, 0); visibility: visible; transition: transform .2s ease; }
#toast.err { --a: var(--red); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
