:root {
  color-scheme: dark;
  --bg: #0b0f17;
  --surface: rgba(18, 24, 35, .92);
  --surface-2: rgba(28, 37, 53, .92);
  --surface-3: #222d40;
  --border: #334056;
  --text: #f3f6fb;
  --muted: #98a5b8;
  --accent: #74a9ff;
  --accent-2: #ba98ff;
  --danger: #ff7d88;
  --success: #6ee7a7;
  --rail-width: 330px;
  --overlay: .58;
  --bg-blur: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input, textarea, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
.background-layer {
  position: fixed; inset: -30px; z-index: 0;
  background-position: center; background-size: cover; background-repeat: no-repeat;
  filter: blur(var(--bg-blur));
}
.background-layer::after {
  content: ""; position: absolute; inset: 0; background: rgba(7, 10, 16, var(--overlay));
}
.app {
  position: relative; z-index: 1; width: 100%; height: 100%;
  display: grid; grid-template-columns: var(--rail-width) 6px 1fr;
  padding-top: var(--safe-top); padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom); padding-left: var(--safe-left);
}
.rail {
  min-width: 0; overflow: hidden; display: grid; grid-template-rows: auto auto 1fr auto;
  background: var(--surface); backdrop-filter: blur(20px); border-right: 1px solid var(--border);
}
.rail-header, .topbar, .modal-header { display: flex; align-items: center; }
.rail-header { min-height: 68px; gap: 11px; padding: 11px 14px; border-bottom: 1px solid var(--border); }
.brand-mark, .hero-mark, .floating-bubble {
  display: grid; place-items: center; font-weight: 900; color: #09101d;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.brand-mark { width: 39px; height: 39px; border-radius: 13px; font-size: 18px; flex: 0 0 auto; }
.brand-copy { display: flex; flex-direction: column; min-width: 0; }
.brand-copy strong { letter-spacing: .08em; }
.brand-copy span { color: var(--muted); font-size: 10px; }
.rail-actions { display: grid; gap: 6px; padding: 12px; border-bottom: 1px solid var(--border); }
.nav-btn, .primary, .secondary, .icon-btn, .send-btn {
  border-radius: 12px; cursor: pointer; color: var(--text); transition: .15s ease;
}
.nav-btn { min-height: 42px; text-align: left; padding: 8px 12px; border: 1px solid transparent; background: transparent; }
.nav-btn:hover, .nav-btn.active { background: var(--surface-2); border-color: var(--border); }
.primary { border: 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #09101d; font-weight: 850; padding: 10px 15px; }
.secondary { border: 1px solid var(--border); background: var(--surface-2); padding: 9px 13px; }
.primary:hover, .secondary:hover { transform: translateY(-1px); }
.wide { width: 100%; }
.icon-btn { width: 39px; height: 39px; border: 1px solid transparent; background: transparent; font-size: 18px; flex: 0 0 auto; }
.icon-btn:hover { border-color: var(--border); background: var(--surface-2); }
.rail-panel { overflow: auto; padding: 10px; }
.rail-footer { padding: 10px; display: grid; gap: 7px; border-top: 1px solid var(--border); }
.conversation-item, .source-item {
  display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center;
  border: 1px solid transparent; border-radius: 12px; padding: 9px;
}
.conversation-item:hover, .conversation-item.active, .source-item:hover { background: var(--surface-2); border-color: var(--border); }
.item-main { min-width: 0; cursor: pointer; }
.item-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 650; }
.item-meta { color: var(--muted); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-actions { display: flex; gap: 2px; }
.tiny-btn { width: 30px; height: 30px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); cursor: pointer; }
.tiny-btn:hover { background: var(--surface-3); color: var(--text); }
.tiny-btn.danger:hover { color: var(--danger); }
.splitter { cursor: col-resize; background: transparent; position: relative; touch-action: none; }
.splitter::after { content: ""; position: absolute; top: 0; bottom: 0; left: 2px; width: 1px; background: var(--border); }
.chat-pane { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto 1fr auto; background: rgba(7, 10, 16, .22); }
.topbar {
  min-height: 68px; gap: 9px; padding: 9px 12px; background: var(--surface); backdrop-filter: blur(20px); border-bottom: 1px solid var(--border);
}
.topbar-title { min-width: 0; display: flex; flex-direction: column; flex: 1; }
.topbar-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title span { color: var(--muted); font-size: 10px; }
.model-select { min-width: 130px; max-width: 240px; height: 39px; border: 1px solid var(--border); border-radius: 11px; background: var(--surface-2); color: var(--text); padding: 0 9px; }
.messages { overflow-y: auto; padding: 22px clamp(12px, 4vw, 44px) 34px; scroll-behavior: smooth; }
.welcome { min-height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; color: var(--muted); }
.hero-mark { width: 82px; height: 82px; border-radius: 25px; font-size: 38px; box-shadow: 0 20px 70px rgba(116,169,255,.2); }
.welcome h1 { color: var(--text); margin: 18px 0 5px; font-size: clamp(22px, 4vw, 32px); }
.welcome p { max-width: 520px; margin: 0 0 20px; }
.welcome-actions { display: flex; gap: 9px; flex-wrap: wrap; justify-content: center; }
.message-row { display: flex; margin: 0 auto 15px; max-width: 920px; }
.message-row.user { justify-content: flex-end; }
.message-bubble {
  max-width: min(88%, 760px); padding: 11px 13px; border: 1px solid var(--border);
  border-radius: 16px; background: var(--surface); backdrop-filter: blur(14px); box-shadow: 0 9px 30px rgba(0,0,0,.13);
}
.message-row.user .message-bubble { background: rgba(39, 57, 85, .95); border-bottom-right-radius: 5px; }
.message-row.assistant .message-bubble { border-bottom-left-radius: 5px; }
.message-meta { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 10px; margin-bottom: 6px; }
.copy-btn { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 0; }
.message-content { overflow-wrap: anywhere; }
.message-content p { margin: 0 0 9px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content pre { overflow-x: auto; padding: 12px; background: #080b11; border: 1px solid #2c3648; border-radius: 10px; }
.message-content code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.message-content :not(pre) > code { background: #273144; padding: 2px 5px; border-radius: 5px; }
.message-content a { color: var(--accent); }
.typing::after { content: "▋"; animation: blink 1s steps(2,start) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.composer-wrap { padding: 8px clamp(10px, 3vw, 28px) 10px; background: var(--surface); backdrop-filter: blur(20px); border-top: 1px solid var(--border); }
.composer { max-width: 920px; margin: auto; display: flex; align-items: flex-end; gap: 7px; background: rgba(14, 19, 28, .94); border: 1px solid var(--border); border-radius: 17px; padding: 8px; }
#promptInput { flex: 1; min-height: 31px; max-height: 170px; resize: none; border: 0; outline: 0; background: transparent; color: var(--text); padding: 6px; }
.send-btn { width: 40px; height: 40px; border: 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #09101d; font-weight: 900; }
.composer-meta { max-width: 920px; margin: 5px auto 0; display: flex; justify-content: space-between; color: var(--muted); font-size: 9px; }
.selected-source-bar { max-width: 920px; margin: 0 auto 6px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 11px; background: var(--surface-2); font-size: 11px; }
.floating-bubble {
  position: fixed; z-index: 50; width: 62px; height: 62px; border: 0; border-radius: 21px;
  right: calc(18px + var(--safe-right)); bottom: calc(24px + var(--safe-bottom));
  font-size: 25px; box-shadow: 0 16px 50px rgba(0,0,0,.42); touch-action: none; cursor: grab;
}
.floating-bubble i { position: absolute; right: 5px; bottom: 5px; width: 11px; height: 11px; border-radius: 50%; background: var(--success); border: 2px solid #111827; }
.app.minimized .rail, .app.minimized .splitter, .app.minimized .chat-pane { visibility: hidden; }
.scrim { display: none; position: fixed; z-index: 25; inset: 0; background: rgba(0,0,0,.5); }
.panel-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.field { width: 100%; color: var(--text); background: rgba(9, 13, 20, .9); border: 1px solid var(--border); border-radius: 11px; padding: 10px 11px; outline: none; }
.field:focus { border-color: var(--accent); }
.import-row { display: flex; gap: 6px; margin: 8px 0; }
.small-button { font-size: 11px; padding: 7px 9px; cursor: pointer; }
.source-list { display: grid; gap: 3px; }
.hidden { display: none !important; }
.modal { border: 0; padding: 0; border-radius: 18px; background: transparent; color: var(--text); width: min(780px, calc(100% - 24px)); max-height: calc(100dvh - 24px); }
.modal::backdrop { background: rgba(0,0,0,.62); backdrop-filter: blur(5px); }
.modal-card { background: #151c29; border: 1px solid var(--border); border-radius: 18px; padding: 18px; overflow-y: auto; max-height: calc(100dvh - 24px); }
.compact-card { width: min(520px, 100%); margin: auto; display: grid; gap: 13px; }
.modal-header { justify-content: space-between; gap: 10px; border-bottom: 1px solid var(--border); padding-bottom: 12px; margin-bottom: 16px; }
.modal-header h2 { margin: 0; } .modal-header p { margin: 2px 0 0; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.form-grid label, .compact-card label { display: grid; gap: 6px; font-weight: 650; }
.form-grid small { color: var(--muted); font-weight: 400; }
.full { grid-column: 1 / -1; }
.range-line { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.appearance-section { border-top: 1px solid var(--border); padding-top: 14px; }
.appearance-section h3 { margin: 0; }
.appearance-section p { color: var(--muted); margin-top: 3px; }
.background-controls { display: flex; gap: 7px; flex-wrap: wrap; }
.background-preview { min-height: 150px; display: grid; place-items: center; overflow: hidden; border: 1px dashed var(--border); border-radius: 13px; color: var(--muted); background: #0b1018; }
.background-preview img { width: 100%; height: 190px; object-fit: cover; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 17px; }
.result { color: var(--muted); min-height: 22px; margin-top: 8px; }
.result.error { color: var(--danger); }
.compact-only { display: none; }

@media (max-width: 760px) {
  .app { display: block; padding: 0; }
  .rail {
    position: fixed; z-index: 30; inset: 0 auto 0 0; width: min(88vw, 360px);
    transform: translateX(-103%); transition: transform .2s ease; padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
  }
  .rail.open { transform: translateX(0); }
  .scrim.open { display: block; }
  .splitter { display: none; }
  .chat-pane { height: 100dvh; padding-top: var(--safe-top); padding-bottom: var(--safe-bottom); }
  .compact-only { display: inline-grid; }
  .brand-copy { flex: 1; }
  .topbar { min-height: 62px; }
  .model-select { max-width: 42vw; min-width: 0; }
  .messages { padding: 16px 10px 24px; }
  .message-bubble { max-width: 94%; }
  .composer-wrap { padding: 7px 8px calc(8px + var(--safe-bottom)); }
  .composer-meta span:first-child { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .full { grid-column: auto; }
  .modal { width: calc(100% - 14px); max-height: calc(100dvh - 14px); }
  .modal-card { padding: 14px; max-height: calc(100dvh - 14px); }
}

@media (max-width: 390px) {
  .topbar-title span { display: none; }
  .model-select { width: 105px; }
  .topbar { gap: 4px; padding-inline: 6px; }
  .icon-btn { width: 36px; height: 36px; }
}

/* Viewport Segments API: fysisk segmentert foldbar skjerm */
@media (horizontal-viewport-segments: 2) {
  .app {
    grid-template-columns:
      env(viewport-segment-width 0 0)
      env(viewport-segment-left 1 0)
      env(viewport-segment-width 1 0);
  }
  .rail { grid-column: 1; }
  .splitter { grid-column: 2; cursor: default; }
  .chat-pane { grid-column: 3; }
}
