/* ============ Design-Tokens ============ */
:root {
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --ink: #1a2430;
  --ink-2: #4a5a6a;
  --ink-3: #6f7e8c;
  --line: #e2e8ef;
  --line-strong: #c9d4de;
  --accent: #0f766e;
  --accent-strong: #0b5f58;
  --accent-soft: #e8f6f4;
  --danger: #d64040;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow-1: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-2: 0 24px 70px rgba(16, 24, 40, .28);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ Kopfzeile ============ */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(255, 255, 255, .86);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.app-header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -.01em;
}

.brand .subtitle {
  margin: 2px 0 0;
  font-size: .86rem;
  color: var(--ink-2);
}

.header-actions { display: flex; align-items: center; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s, box-shadow .15s, transform .05s;
}

.btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.btn:active { transform: translateY(1px); }

.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }

/* ============ Inhaltsbereich ============ */
.app-main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px 28px 64px;
}

/* ============ Tabelle ============ */
.table-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  overflow-x: auto;
  width: 100%;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  table-layout: fixed;
  font-variant-numeric: tabular-nums;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--surface-2);
  color: var(--ink-2);
  font-weight: 600;
  font-size: .85rem;
  padding: 13px 14px;
  text-align: center;
  border-bottom: 2px solid var(--line-strong);
  border-right: 1px solid var(--line);
  white-space: nowrap;
  min-width: 108px;
}

th.row-head {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
  color: var(--ink-2);
  font-weight: 600;
  font-size: .85rem;
  text-align: left;
  padding: 13px 16px;
  border-right: 1px solid var(--line-strong);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  width: 170px;
  min-width: 170px;
}

th.corner {
  left: 0;
  top: 0;
  z-index: 4;
  background: var(--surface-2);
  color: var(--ink-3);
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  text-align: left;
  border-right: 1px solid var(--line-strong);
  border-bottom: 2px solid var(--line-strong);
  width: 170px;
  min-width: 170px;
}

td.cell {
  position: relative;
  background: var(--surface);
  padding: 0;
  min-width: 108px;
  width: 108px;
  height: 60px;
  text-align: center;
  cursor: pointer;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  transition: background .12s;
}

td.cell:hover { background: var(--accent-soft); }

td.cell .count {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--ink-3);
}

td.cell.has .count { color: var(--accent); font-weight: 700; font-size: 1.15rem; }

td.cell .plus {
  position: absolute;
  right: 8px;
  bottom: 6px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: .85rem;
  line-height: 1;
  opacity: 0;
  transform: scale(.6);
  transition: opacity .12s, transform .12s;
  pointer-events: none;
}

td.cell:hover .plus { opacity: 1; transform: scale(1); }

td.cell.drag-over {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 2px var(--accent);
}

td.cell.bump .count {
  animation: bump .35s ease;
}

@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); }
  100% { transform: scale(1); }
}

/* Drag-to-reorder: Grip-Hinweis + Einfüge-Indikator */
th[draggable] { cursor: grab; }
th[draggable]:active { cursor: grabbing; }

th[draggable] .grip {
  opacity: .35;
  margin-left: 8px;
  font-size: .7rem;
  letter-spacing: -2px;
  user-select: none;
}

th[draggable]:hover .grip { opacity: .7; }

th[draggable].dragging { opacity: .4; }

th.drop-before::after, th.drop-after::after {
  content: "";
  position: absolute;
  background: var(--accent);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .6);
  z-index: 5;
  pointer-events: none;
}

th.drop-before::after { left: -2px; top: 0; bottom: 0; width: 4px; border-radius: 2px; }
th.drop-after::after { right: -2px; top: 0; bottom: 0; width: 4px; border-radius: 2px; }

/* Nur bei horizontalem Überlauf wird die Karte zum Scroll-Container */
.table-card.scroll-x { overflow-x: auto; }

/* Zugriffe für die Dirty-Leiste (ungespeicherte Änderungen) */
.dirty-bar {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  background: #1a2430;
  color: #fff;
  padding: 10px 12px 10px 18px;
  border-radius: 14px;
  box-shadow: var(--shadow-2);
  z-index: 90;
  font-size: .88rem;
  font-weight: 500;
  max-width: calc(100vw - 32px);
}

.dirty-bar.hidden { display: none; }

.dirty-bar .dirty-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #f6c544;
  box-shadow: 0 0 0 3px rgba(246, 197, 68, .25);
  flex-shrink: 0;
}

.dirty-bar .dirty-spacer { flex: 1; }

.dirty-bar .btn { padding: 6px 12px; font-size: .83rem; }
.dirty-bar .btn.ghost { color: #cbd5e1; border-color: #3a4657; }
.dirty-bar .btn.ghost:hover { background: #2a3450; color: #fff; }

/* ============ Willkommens-Zustand (leeres Setup) ============ */
.welcome {
  padding: 72px 32px;
  text-align: center;
  display: grid;
  place-items: center;
  gap: 8px;
  background: var(--surface);
  color: var(--ink-2);
}

.welcome-icon { font-size: 4rem; line-height: 1; margin-bottom: 8px; }

.welcome h2 {
  margin: 0;
  color: var(--ink);
  font-size: 1.3rem;
}

.welcome p {
  margin: 0 0 18px;
  max-width: 460px;
  font-size: .95rem;
}

/* ============ Dialog(e) ============ */
dialog {
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  max-width: calc(100vw - 24px);
}

dialog::backdrop {
  background: rgba(15, 23, 42, .5);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.sheet {
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow-2);
  width: min(960px, 94vw);
  max-height: min(88vh, 860px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.sheet-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.sheet-title h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: .78rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

.icon-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-2);
  font-size: 1rem;
  cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
}

.icon-btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--line); }

/* Foto-Dialog */
.dropzone {
  position: relative;
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: var(--surface-2);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 14px;
  transition: opacity .15s;
}

.photo {
  position: relative;
  margin: 0;
  border-radius: 12px;
  overflow: hidden;
  background: #dfe6ee;
  aspect-ratio: 3 / 4;
  box-shadow: var(--shadow-1);
}

.photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  cursor: zoom-in;
  transition: transform .2s;
}

.photo:hover img { transform: scale(1.04); }

.photo .del {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: rgba(15, 23, 42, .62);
  color: #fff;
  font-size: .8rem;
  cursor: pointer;
  opacity: 0;
  transform: scale(.7);
  transition: opacity .12s, transform .12s, background .12s;
}

.photo:hover .del, .photo:focus-within .del { opacity: 1; transform: scale(1); }
.photo .del:hover { background: var(--danger); }

.empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 56px 20px;
  color: var(--ink-3);
}

/* Lade-Spinner während die Fotos geholt werden */
.grid-loading {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 56px 20px;
  color: var(--ink-3);
  font-size: .9rem;
}

.grid-loading .spinner {
  position: static;
  margin: 0;
  opacity: 1;
  pointer-events: none;
}

.empty-icon { font-size: 3rem; line-height: 1; margin-bottom: 12px; }
.empty-title { font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
.empty-sub { font-size: .87rem; }

.drop-overlay {
  position: absolute;
  inset: 10px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: rgba(15, 118, 110, .92);
  color: #fff;
  font-weight: 700;
  font-size: 1.2rem;
  letter-spacing: .02em;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s;
}

.dropzone.dragging .drop-overlay { opacity: 1; }

.spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 34px;
  height: 34px;
  margin: -17px 0 0 -17px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  opacity: 0;
  pointer-events: none;
}

.dropzone.uploading .grid { opacity: .35; pointer-events: none; }
.dropzone.uploading .spinner { opacity: 1; }

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

.sheet-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.foot-hint { color: var(--ink-2); font-size: .85rem; }

/* Einstellungen-Dialog */
.settings-sheet { width: min(680px, 94vw); }

.settings-body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 20px;
  overflow-y: auto;
}

@media (max-width: 640px) {
  .settings-body { grid-template-columns: 1fr; }
}

.settings-body h3 {
  margin: 0 0 10px;
  font-size: .85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.settings-body h3 .muted { color: var(--ink-3); text-transform: none; letter-spacing: 0; font-weight: 400; }

.edit-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }

.move-btns {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}

.move-btn {
  width: 26px;
  height: 17px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--ink-3);
  font-size: .6rem;
  line-height: 1;
  cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
  padding: 0;
}

.move-btn:hover { background: var(--surface-2); color: var(--accent); border-color: var(--line); }
.move-btn:disabled { opacity: .3; cursor: default; }

.edit-row input {
  flex: 1;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: .93rem;
  background: var(--surface);
  transition: border-color .12s, box-shadow .12s;
}

.edit-row input:hover { border-color: var(--line-strong); }
.edit-row input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(15, 118, 110, .15); }

.row-del {
  flex-shrink: 0;
  width: 36px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-3);
  font-size: .9rem;
  cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
}

.row-del:hover { background: #fdeaea; color: var(--danger); border-color: #f3caca; }

.add-row {
  width: 100%;
  padding: 8px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: .88rem;
  cursor: pointer;
  transition: border-color .12s, color .12s, background .12s;
}

.add-row:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.settings-note {
  margin: 0;
  padding: 0 20px 4px;
  font-size: .8rem;
  color: var(--ink-3);
  min-height: 1.2em;
}

.settings-note.dirty {
  color: #b45309;
  font-weight: 600;
}

.settings-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--line);
}

.form-error {
  flex: 1;
  color: var(--danger);
  font-size: .85rem;
  font-weight: 500;
}

/* ============ Toast ============ */
.toast {
  position: fixed;
  top: 96px;
  left: 50%;
  transform: translateX(-50%);
  background: #1a2430;
  color: #fff;
  padding: 11px 20px;
  border-radius: 999px;
  font-size: .9rem;
  font-weight: 500;
  box-shadow: var(--shadow-2);
  z-index: 100;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px);
  text-align: center;
}

.toast.hidden { opacity: 0; transform: translate(-50%, -12px); pointer-events: none; }

@media (max-width: 720px) {
  .brand .subtitle { display: none; }
  .app-header-inner { padding: 12px 16px; }
  .app-main { padding: 14px 12px 48px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .dropzone { padding: 12px; }
  .foot-hint { display: none; }
}