/* ── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 14px; line-height: 1.5; }

/* ── Design tokens ──────────────────────────────────────────────────────────── */
:root {
  /* Farver */
  --purple:      #7F77DD;
  --purple-dk:   #534AB7;
  --purple-lt:   #EEEDFE;
  --green:       #639922;
  --green-lt:    #EAF3DE;
  --red:         #E24B4A;
  --red-lt:      #FCEBEB;
  --amber:       #BA7517;
  --amber-lt:    #FAEEDA;
  --blue:        #378ADD;
  --blue-lt:     #E6F1FB;

  /* Light mode surfaces */
  --bg:          #f5f4f0;
  --surface:     #ffffff;
  --surface-2:   #f1efe8;
  --surface-3:   #e8e6de;
  --border:      rgba(0,0,0,0.10);
  --border-md:   rgba(0,0,0,0.18);
  --border-dk:   rgba(0,0,0,0.28);

  /* Light mode text */
  --text:        #1a1a18;
  --text-2:      #4a4a47;
  --text-3:      #888780;
  --text-4:      #b4b2a9;

  /* Layout */
  --sidebar-w:   224px;
  --topbar-h:    52px;
  --radius-sm:   6px;
  --radius-md:   10px;
  --radius-lg:   14px;
  --radius-xl:   20px;

  /* Transitions */
  --t:           150ms ease;
}

[data-theme="dark"] {
  --bg:        #111113;
  --surface:   #1c1c1f;
  --surface-2: #242428;
  --surface-3: #2e2e33;
  --border:    rgba(255,255,255,0.08);
  --border-md: rgba(255,255,255,0.14);
  --border-dk: rgba(255,255,255,0.22);
  --text:      #f0ede6;
  --text-2:    #b8b5ae;
  --text-3:    #6e6c66;
  --text-4:    #44433f;
  --purple-lt: #1e1c40;
  --green-lt:  #162608;
  --red-lt:    #2a0e0e;
  --amber-lt:  #271600;
  --blue-lt:   #0a1828;
}

/* ── Scrollbar ──────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-md); border-radius: 3px; }

/* ── Typography ─────────────────────────────────────────────────────────────── */
body { color: var(--text); background: var(--bg); }
h1 { font-size: 22px; font-weight: 500; }
h2 { font-size: 17px; font-weight: 500; }
h3 { font-size: 14px; font-weight: 500; }
a  { color: var(--purple); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: 'SF Mono', 'Fira Code', monospace; font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* ── App shell ──────────────────────────────────────────────────────────────── */
#app { display: flex; height: 100vh; overflow: hidden; }

/* ── Sidebar ────────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 0.5px solid var(--border-md);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: hidden;
}
.sidebar-logo {
  padding: 0 16px;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -.3px;
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
}
.sidebar-logo span { color: var(--purple); }

.sidebar-section {
  padding: 14px 16px 4px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-3);
}
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 12px;
  margin: 1px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-2);
  transition: background var(--t), color var(--t);
  user-select: none;
}
.sidebar-item:hover  { background: var(--surface-2); color: var(--text); }
.sidebar-item.active { background: var(--purple-lt); color: var(--purple-dk); font-weight: 500; }
[data-theme="dark"] .sidebar-item.active { color: var(--purple); }

.dash-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.sidebar-item .badge {
  margin-left: auto;
  font-size: 10px;
  background: var(--surface-3);
  color: var(--text-3);
  padding: 1px 6px;
  border-radius: 10px;
}
.sidebar-item.active .badge { background: rgba(127,119,221,.2); color: var(--purple); }

.sidebar-add-btn {
  margin: 4px 8px;
  padding: 7px 10px;
  border: 0.5px dashed var(--border-dk);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-3);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  width: calc(100% - 16px);
  transition: background var(--t), color var(--t);
}
.sidebar-add-btn:hover { background: var(--surface-2); color: var(--text); }

.sidebar-footer {
  margin-top: auto;
  border-top: 0.5px solid var(--border);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 9px;
  flex-shrink: 0;
}
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600;
  flex-shrink: 0;
  color: #fff;
}
.sidebar-user-name  { font-size: 12px; font-weight: 500; }
.sidebar-user-role  { font-size: 10px; color: var(--text-3); }
.sidebar-user-info  { flex: 1; min-width: 0; }
.icon-btn {
  width: 28px; height: 28px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-3);
  font-size: 16px;
  flex-shrink: 0;
  transition: background var(--t), color var(--t);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ── Main content ───────────────────────────────────────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

/* ── Topbar ─────────────────────────────────────────────────────────────────── */
#topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 0.5px solid var(--border-md);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  flex-shrink: 0;
}
#topbar-title {
  font-size: 15px;
  font-weight: 500;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: 4px;
  transition: background var(--t);
}
#topbar-title[contenteditable="true"] {
  white-space: nowrap;
  overflow: visible;
}
/* Hint om at titlen kan redigeres */
#topbar-title.renameable:hover {
  background: var(--surface-2);
  cursor: text;
}
.topbar-url {
  font-family: monospace;
  font-size: 11px;
  padding: 3px 10px;
  background: var(--surface-2);
  border: 0.5px solid var(--border-md);
  border-radius: 20px;
  color: var(--text-3);
  cursor: pointer;
  white-space: nowrap;
}
.topbar-url:hover { color: var(--purple); border-color: var(--purple); }

/* ── Buttons ────────────────────────────────────────────────────────────────── */
.btn {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: 0.5px solid var(--border-md);
  background: transparent;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--t);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--purple); border-color: var(--purple); color: #fff; }
.btn-primary:hover { background: var(--purple-dk); }
.btn-danger  { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover  { background: #c23a39; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px; width: 32px; height: 32px; justify-content: center; }

/* ── Forms ──────────────────────────────────────────────────────────────────── */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 5px; font-weight: 500; }
.field-hint   { font-size: 11px; color: var(--text-3); margin-top: 4px; }

input[type=text],
input[type=email],
input[type=password],
input[type=number],
input[type=search],
input[type=url],
input[type=color],
select,
textarea {
  width: 100%;
  padding: 8px 10px;
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--t);
}
input:focus, select:focus, textarea:focus { border-color: var(--purple); box-shadow: 0 0 0 3px rgba(127,119,221,.12); }
input[type=color] { padding: 2px; height: 34px; cursor: pointer; }
.input-row { display: flex; gap: 8px; }
.input-row > * { flex: 1; }
.form-error { font-size: 12px; color: var(--red); margin-top: 6px; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; }
.toggle-row label { font-size: 13px; }
.toggle {
  width: 36px; height: 20px;
  border-radius: 10px;
  background: var(--border-dk);
  border: none;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
  transition: background var(--t);
}
.toggle::after {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  top: 3px; left: 3px;
  transition: transform var(--t);
}
.toggle.on { background: var(--purple); }
.toggle.on::after { transform: translateX(16px); }

/* ── Dashboard canvas ───────────────────────────────────────────────────────── */
#canvas-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg);
  padding: 12px;
  position: relative;
}
#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-3);
  gap: 10px;
  text-align: center;
}
.empty-icon  { font-size: 44px; }
.empty-title { font-size: 17px; font-weight: 500; color: var(--text-2); }
.empty-sub   { font-size: 13px; }

/* ── GridStack overrides ────────────────────────────────────────────────────── */
.grid-stack { min-height: 200px; }

.grid-stack-item-content {
  background: transparent !important;
  border: none !important;
  border-radius: 0 !important;
  overflow: visible !important;
  box-sizing: border-box !important;
}

.widget-card {
  /* position:absolute + inset giver padding mellem widgets */
  position: absolute;
  inset: 6px;
  background: var(--surface);
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: border-color var(--t), box-shadow var(--t);
  box-sizing: border-box;
}

/* Padding varianter */
.widget-card.pad-0 .widget-body { padding: 0; }
.widget-card.pad-1 .widget-body { padding: 10px 12px; }
.widget-card.pad-2 .widget-body { padding: 16px 18px; }

/* Border-radius varianter */
.widget-card.radius-0 { border-radius: 0; }
.widget-card.radius-1 { border-radius: 6px; }
.widget-card.radius-2 { border-radius: var(--radius-md); }
.widget-card.radius-3 { border-radius: var(--radius-xl); }

/* Skygge */
.widget-card.shadow { box-shadow: 0 2px 12px rgba(0,0,0,.08); }
[data-theme="dark"] .widget-card.shadow { box-shadow: 0 2px 12px rgba(0,0,0,.4); }

/* Ingen border */
.widget-card.no-border { border-color: transparent; }

/* GridStack placeholder */
.grid-stack-placeholder > .placeholder-content {
  background: var(--purple-lt) !important;
  border: 2px dashed var(--purple) !important;
  border-radius: var(--radius-md) !important;
  margin: 4px !important;
  box-sizing: border-box !important;
}

/* Widget header */
.widget-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
  min-height: 36px;
}
.widget-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-3);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.widget-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--t);
}
.grid-stack-item:hover .widget-actions,
body.editing .widget-actions { opacity: 1; }

.widget-action {
  width: 22px; height: 22px;
  background: transparent; border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-3);
  font-size: 13px;
  transition: background var(--t), color var(--t);
}
.widget-action:hover { background: var(--surface-2); color: var(--text); }
.widget-action.danger:hover { background: var(--red-lt); color: var(--red); }

.widget-body { flex: 1; overflow: auto; padding: 10px 12px; }

/* Edit mode indicator */
body.editing .widget-card {
  border-color: rgba(127,119,221,.4);
}

/* GridStack resize handle — større og synlig i edit mode */
.ui-resizable-handle {
  opacity: 0;
  transition: opacity 0.15s;
}
body.editing .ui-resizable-handle {
  opacity: 1;
}
.ui-resizable-se {
  width: 24px !important;
  height: 24px !important;
  bottom: 4px !important;
  right: 4px !important;
  cursor: se-resize !important;
  background-image: none !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}
.ui-resizable-se::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--purple);
  border-bottom: 2px solid var(--purple);
  border-radius: 0 0 3px 0;
}

/* Drag-håndtag cursor i edit mode */
body.editing .grid-stack-item > .grid-stack-item-content {
  cursor: grab;
}
body.editing .grid-stack-item.ui-draggable-dragging > .grid-stack-item-content {
  cursor: grabbing;
  opacity: 0.85;
}

/* Edit mode: tydelig border på alle widgets */
body.editing .widget-card {
  border-color: rgba(127, 119, 221, 0.5) !important;
  border-style: dashed !important;
}

/* Edit mode topbar indikator */
#topbar.edit-active {
  background: var(--purple-lt);
  border-bottom-color: var(--purple);
}
[data-theme="dark"] #topbar.edit-active {
  background: rgba(127, 119, 221, 0.15);
}

/* ── Alert banner ───────────────────────────────────────────────────────────── */
#alert-banner { flex-shrink: 0; }
.alert-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  margin: 8px 12px 0;
  border-radius: var(--radius-md);
  border: 1.5px solid transparent;
}
.alert-critical { background: #A32D2D; border-color: #791F1F; animation: blink 1s ease-in-out infinite; }
.alert-warning  { background: #854F0B; border-color: #633806; }
.alert-info     { background: #185FA5; border-color: #0C447C; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.75} }
.alert-body   { flex: 1; }
.alert-level  { font-size: 10px; color: rgba(255,255,255,.7); text-transform: uppercase; letter-spacing:.07em; }
.alert-title  { font-size: 13px; font-weight: 500; color: #fff; }
.alert-msg    { font-size: 12px; color: rgba(255,255,255,.8); margin-top: 2px; }
.alert-close  {
  background: rgba(255,255,255,.15); border: none;
  color: rgba(255,255,255,.8); border-radius: 4px;
  width: 22px; height: 22px; cursor: pointer; flex-shrink: 0;
  font-size: 15px; display: flex; align-items: center; justify-content: center;
}
.alert-close:hover { background: rgba(255,255,255,.3); }

/* ── Modal ──────────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-lg);
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-sm { max-width: 400px; }
.modal-md { max-width: 520px; }
.modal-lg { max-width: 700px; }

.modal-header {
  display: flex; align-items: center;
  padding: 16px 20px;
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
}
.modal-title { font-size: 15px; font-weight: 500; flex: 1; }
.modal-close {
  width: 28px; height: 28px;
  background: transparent; border: none;
  font-size: 20px; cursor: pointer;
  color: var(--text-3); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { background: var(--surface-2); color: var(--text); }

.modal-body   { padding: 20px; overflow-y: auto; flex: 1; }
.modal-footer {
  padding: 14px 20px;
  border-top: 0.5px solid var(--border);
  display: flex; gap: 8px; justify-content: flex-end;
  flex-shrink: 0;
}

/* ── Tabs ───────────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: 4px; margin-bottom: 16px; }
.tab-btn {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-3);
  font-size: 13px;
  cursor: pointer;
  transition: background var(--t), color var(--t);
}
.tab-btn:hover  { background: var(--surface-2); color: var(--text); }
.tab-btn.active { background: var(--purple-lt); color: var(--purple); font-weight: 500; }
[data-theme="dark"] .tab-btn.active { color: var(--purple); }

/* ── Widget style panel ─────────────────────────────────────────────────────── */
.style-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.style-section { padding: 12px 0; border-bottom: 0.5px solid var(--border); }
.style-section:last-child { border: none; }
.style-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin-bottom: 8px; }

.radius-picker { display: flex; gap: 6px; }
.radius-opt {
  flex: 1; padding: 6px; border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm); cursor: pointer; text-align: center;
  font-size: 11px; color: var(--text-2); background: var(--surface);
  transition: border-color var(--t), background var(--t);
}
.radius-opt.selected { border-color: var(--purple); background: var(--purple-lt); color: var(--purple); }

.padding-picker { display: flex; gap: 6px; }
.padding-opt {
  flex: 1; padding: 6px; border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm); cursor: pointer; text-align: center;
  font-size: 11px; color: var(--text-2); background: var(--surface);
  transition: border-color var(--t), background var(--t);
}
.padding-opt.selected { border-color: var(--purple); background: var(--purple-lt); color: var(--purple); }

/* ── Login screen ───────────────────────────────────────────────────────────── */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 20px;
}
.login-box {
  background: var(--surface);
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-xl);
  padding: 36px;
  width: 100%;
  max-width: 380px;
}
.login-logo {
  text-align: center;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -.5px;
  margin-bottom: 28px;
  color: var(--text);
}
.login-logo span { color: var(--purple); }

/* ── Dropdown ───────────────────────────────────────────────────────────────── */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 160px;
  background: var(--surface);
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-md);
  overflow: hidden;
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0,0,0,.1);
}
.dropdown-item {
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text-2);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background var(--t);
}
.dropdown-item:hover { background: var(--surface-2); color: var(--text); }
.dropdown-item.danger { color: var(--red); }
.dropdown-item.danger:hover { background: var(--red-lt); }
.dropdown-sep { height: 0.5px; background: var(--border); margin: 4px 0; }

/* ── Toast ──────────────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 20px; left: 20px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--text);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: var(--radius-md);
  font-size: 13px;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s, transform .2s;
  pointer-events: auto;
  max-width: 320px;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* ── Utility ────────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }
.flex    { display: flex; }
.flex-1  { flex: 1; min-width: 0; }
.gap-8   { gap: 8px; }
.gap-12  { gap: 12px; }
.mt-8    { margin-top: 8px; }
.mt-16   { margin-top: 16px; }
.divider { height: 0.5px; background: var(--border); margin: 12px 0; }
.text-3  { color: var(--text-3); }
.text-sm { font-size: 12px; }
.mono    { font-family: monospace; }

@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* ── Widget actions overlay (synlig ved hover, selv uden titel/border) ──────── */
.widget-actions-overlay {
  position: absolute;
  top: 4px;
  right: 4px;
  z-index: 10;
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--t);
  background: var(--surface);
  border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm);
  padding: 2px;
  pointer-events: none;
}
.widget-card:hover .widget-actions-overlay,
body.editing .widget-actions-overlay {
  opacity: 1;
  pointer-events: auto;
}

/* Todo drag-sort */
.todo-drag-ghost { opacity: .3; background: var(--surface-2) !important; }
.todo-drag-item  { background: var(--surface) !important; box-shadow: 0 4px 12px rgba(0,0,0,.15); border-radius: var(--radius-sm); opacity: 1 !important; }

/* Dashboard drag-sort */
.dash-drag-handle {
  cursor: grab;
  color: var(--text-4);
  font-size: 13px;
  flex-shrink: 0;
  padding: 0 4px 0 0;
  opacity: 0;
  transition: opacity var(--t);
  user-select: none;
}
.sidebar-item:hover .dash-drag-handle { opacity: 1; }
.dash-drag-ghost { opacity: .3; background: var(--surface-2) !important; }
