:root{
  --bg: #F3F5F7;
  --surface: #FFFFFF;
  --surface-2: #FAFBFC;
  --border: #E3E7EC;
  --border-strong: #D3D9E0;
  --text: #1B1F24;
  --text-soft: #66707B;
  --text-faint: #98A1AA;
  --accent: #C2483A;
  --accent-ink: #FFFFFF;
  --accent-soft: rgba(194,72,58,0.10);
  --accent-soft-strong: rgba(194,72,58,0.22);
  --danger: #B0463E;
  --good: #2E8B57;
  --shadow-sm: 0 1px 2px rgba(20,24,28,0.04);
  --shadow-md: 0 4px 16px rgba(20,24,28,0.08), 0 1px 3px rgba(20,24,28,0.06);
  --radius: 12px;
  --radius-sm: 8px;
}
@media (prefers-color-scheme: dark){
  :root{
    --bg: #16191D; --surface: #1D2126; --surface-2: #22262C; --border: #2B3036; --border-strong: #3A4048;
    --text: #E8EAED; --text-soft: #A0A8B1; --text-faint: #6C7580;
    --accent: #E0685A; --accent-ink: #0E1512; --accent-soft: rgba(224,104,90,0.14); --accent-soft-strong: rgba(224,104,90,0.28);
    --danger: #D97570; --good: #4FAE78;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3); --shadow-md: 0 8px 24px rgba(0,0,0,0.35), 0 1px 3px rgba(0,0,0,0.3);
  }
}

*{ box-sizing: border-box; }
[hidden]{ display: none !important; }
html, body{ margin: 0; padding: 0; height: 100%; }
body{
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: grid; grid-template-columns: 264px 1fr; min-height: 100vh;
}
button{ font-family: inherit; cursor: pointer; }
input{ font-family: inherit; }
*{ scrollbar-color: var(--border-strong) transparent; scrollbar-width: thin; }

/* ---------------- Sidebar ---------------- */
.sidebar{
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; height: 100vh; position: sticky; top: 0;
}
.sidebar-head{ padding: 20px 18px 8px; }
.brand{ font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.sidebar-section-label{ padding: 14px 18px 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); }
.project-list{ flex: 1; overflow-y: auto; padding: 4px 10px; display: flex; flex-direction: column; gap: 2px; }
.project-item{
  display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-radius: var(--radius-sm);
  cursor: pointer; border: 1px solid transparent; background: none; text-align: left; width: 100%; color: inherit; font: inherit;
}
.project-item:hover{ background: var(--surface-2); }
.project-item.active{ background: var(--accent-soft); border-color: var(--accent-soft-strong); }
.project-item-text{ min-width: 0; flex: 1; }
.project-item-name{ font-size: 13.8px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-item-sub{ font-size: 11.5px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.new-project-btn{
  margin: 10px 14px; padding: 10px; border: 1px dashed var(--border-strong); border-radius: var(--radius-sm);
  background: transparent; color: var(--text-soft); font-weight: 600; font-size: 13px;
}
.new-project-btn:hover{ background: var(--surface-2); color: var(--text); border-color: var(--accent); }
.sidebar-logout-btn{
  margin: 0 14px 16px; padding: 9px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: transparent; color: var(--text-soft); font-size: 13px; font-weight: 600;
}
.sidebar-logout-btn:hover{ background: var(--surface-2); color: var(--text); }

.sidebar-toggle{
  display: none; position: fixed; top: 14px; left: 14px; z-index: 20;
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow-sm);
}
.sidebar-scrim{ display: none; }

/* ---------------- Hauptbereich ---------------- */
.main{ min-height: 100vh; overflow-y: auto; }
.empty-state{ display: flex; align-items: center; justify-content: center; height: 60vh; color: var(--text-faint); text-align: center; padding: 0 20px; }

.project-view{ padding: 24px 28px 60px; max-width: 900px; margin: 0 auto; }

.topbar{
  display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 20px;
}
.project-title{
  font-size: 22px; font-weight: 700; margin: 0; border-radius: 6px; padding: 2px 4px; margin-left: -4px;
  min-width: 0; overflow: hidden; text-overflow: ellipsis;
}
.project-title:focus{ outline: none; background: var(--surface-2); }
.topbar-actions{ display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.btn-primary{
  border: none; background: var(--accent); color: var(--accent-ink); padding: 9px 14px;
  border-radius: var(--radius-sm); font-weight: 600; font-size: 13.5px;
}
.btn-ghost{
  border: 1px solid var(--border-strong); background: transparent; color: var(--text);
  padding: 8px 13px; border-radius: var(--radius-sm); font-size: 13.5px;
}
.btn-icon{
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-soft);
  width: 34px; height: 34px; border-radius: 50%; font-size: 16px; line-height: 1;
}
.btn-icon:hover{ background: var(--surface-2); color: var(--text); }

.gallery{
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px;
}
.gallery-item{
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow-sm); text-align: left; padding: 0;
}
.gallery-item img{ display: block; width: 100%; aspect-ratio: 4/3; object-fit: cover; background: var(--surface-2); }
.gallery-item-info{ padding: 8px 10px; }
.gallery-item-title{ font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gallery-item-meta{ font-size: 11px; color: var(--text-faint); margin-top: 2px; }

/* ---------------- Editor ---------------- */
.editor{
  position: fixed; inset: 0; background: var(--bg); z-index: 50;
  display: none; flex-direction: column;
}
.editor:not([hidden]){ display: flex; }
.editor-topbar{
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.editor-title{
  flex: 1; font-size: 15px; font-weight: 600; border: none; background: transparent; color: var(--text);
  padding: 4px 6px; border-radius: 6px; min-width: 0;
}
.editor-title:focus{ outline: none; background: var(--surface-2); }
.editor-hint{
  padding: 8px 12px; font-size: 12.5px; color: var(--text-soft); background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.editor-stage{
  flex: 1; overflow: auto; display: flex; align-items: flex-start; justify-content: center;
  background: #111; padding: 0;
}
.editor-image-wrap{ position: relative; max-width: 100%; touch-action: manipulation; }
.editor-image-wrap img{ display: block; max-width: 100%; height: auto; user-select: none; -webkit-user-drag: none; }
.editor-svg{ position: absolute; inset: 0; width: 100%; height: 100%; }
.m-line{ stroke: #FFD43B; stroke-width: 3; }
.m-line-draft{ stroke-dasharray: 6 4; }
.m-tick{ stroke: #FFD43B; stroke-width: 3; }
.m-point{ fill: #FFD43B; stroke: #1B1F24; stroke-width: 1.5; }
.m-label-bg{ fill: rgba(20,20,20,0.8); }
.m-label{ fill: #fff; font-size: 14px; font-weight: 600; }
.m-hitarea{ stroke: transparent; stroke-width: 26; cursor: pointer; touch-action: none; }
.m-endpoint-handle{ fill: transparent; cursor: grab; touch-action: none; }

.confirm-position-btn{
  position: absolute; transform: translate(-50%, 0);
  background: var(--accent); color: var(--accent-ink); border: none; border-radius: 999px;
  padding: 8px 16px; font-size: 13px; font-weight: 600; box-shadow: var(--shadow-md); z-index: 6;
}
.magnifier{
  position: absolute; width: 120px; height: 120px; border-radius: 50%; overflow: hidden;
  border: 3px solid #fff; box-shadow: var(--shadow-md); pointer-events: none; z-index: 10;
  transform: translate(-50%, 0); background: #111;
}
.magnifier canvas{ display: block; width: 100%; height: 100%; }

.length-form{
  position: absolute; transform: translate(-50%, -50%);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md); padding: 8px; display: flex; gap: 6px; z-index: 5;
}
.length-form input{
  width: 110px; padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface-2); color: var(--text); font-size: 14px;
}
.length-form input:focus{ outline: none; border-color: var(--accent); }
.length-form button{
  border: none; border-radius: 6px; padding: 7px 10px; font-size: 13px; font-weight: 600;
}
.length-form .len-ok{ background: var(--accent); color: var(--accent-ink); }
.length-form .len-cancel{ background: var(--surface-2); color: var(--text-soft); border: 1px solid var(--border); }

.editor-footer{
  padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--border);
  font-size: 12.5px; color: var(--text-faint); text-align: center;
}

.toast{
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 100;
  max-width: calc(100% - 32px); background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow-md);
  padding: 12px 16px; font-size: 13.5px;
}
.toast.toast-error{ border-color: var(--danger); color: var(--danger); }

/* ---------------- Modal (Neues Projekt) ---------------- */
.modal-scrim{
  position: fixed; inset: 0; background: rgba(15,17,20,0.45);
  display: flex; align-items: center; justify-content: center; z-index: 60;
}
.modal{
  background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border);
  box-shadow: var(--shadow-md); padding: 22px; width: 320px; max-width: calc(100% - 32px);
}
.modal h3{ margin: 0 0 14px; font-size: 16px; }
.modal input{
  width: 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 9px 11px; background: var(--surface-2); color: var(--text);
}
.modal input:focus{ outline: none; border-color: var(--accent); background: var(--surface); }
.modal-actions{ display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ---------------- Login ---------------- */
.login-wrap{ min-height: 100vh; display: flex; align-items: center; justify-content: center; grid-column: 1/-1; }
.login-card{ width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md); padding: 32px; }
.login-card h1{ font-size: 18px; margin: 0 0 20px; }
.login-card input{ width: 100%; padding: 10px 12px; margin-bottom: 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text); font-size: 14px; }
.login-card button{ width: 100%; padding: 10px; border: none; border-radius: var(--radius-sm); background: var(--accent); color: var(--accent-ink); font-size: 14px; }
.login-error{ color: var(--danger); font-size: 13px; margin: -6px 0 14px; min-height: 16px; }

/* ---------------- Responsive ---------------- */
@media (max-width: 860px){
  body{ grid-template-columns: 1fr; }
  .sidebar{
    position: fixed; left: 0; top: 0; bottom: 0; width: 280px; z-index: 30;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow-md);
  }
  .sidebar.open{ transform: translateX(0); }
  .sidebar-toggle{ display: flex; align-items: center; justify-content: center; }
  .sidebar-scrim.open{ display: block; position: fixed; inset: 0; background: rgba(15,17,20,0.4); z-index: 25; }
  .project-view{ padding: 70px 18px 50px; }
}
