:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --border: #e3e7ee;
  --primary: #2f6df6;
  --primary-ink: #ffffff;
  --danger: #d94141;
  --ok-bg: #e7f6ec; --ok-ink: #1c6b3a;
  --err-bg: #fdecec; --err-ink: #a02323;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --maxw: 1040px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #161d27;
    --text: #e7ecf3;
    --muted: #92a0b3;
    --border: #26303d;
    --primary: #5b8cff;
    --ok-bg: #12291c; --ok-ink: #7ddba0;
    --err-bg: #2c1618; --err-ink: #f2a3a3;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { scroll-padding-top: calc(64px + env(safe-area-inset-top, 0px)); }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Topbar ------------------------------------------------------------------ */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; flex-wrap: wrap; gap: .5rem;
  padding: .7rem 1rem;
  padding-top: calc(.7rem + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); font-size: 1.05rem; margin-right: auto; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; align-items: center; }
.topbar nav a {
  text-decoration: none; color: var(--text);
  padding: .4rem .7rem; border-radius: 8px; font-size: .95rem;
}
.topbar nav a:hover { background: var(--bg); }
.topbar nav a.logout { color: var(--muted); }

/* Menu hamburger (solo mobile) */
.navtoggle, .navbtn { display: none; }
@media (max-width: 640px) {
  .navbtn {
    display: inline-flex; cursor: pointer; font-size: 1.4rem;
    padding: .1rem .5rem; border-radius: 8px; user-select: none;
  }
  .topbar nav { flex-basis: 100%; flex-direction: column; align-items: stretch; display: none; }
  .navtoggle:checked ~ nav { display: flex; }
  .topbar nav a { padding: .6rem .5rem; border-radius: 8px; }
}

/* Layout ------------------------------------------------------------------ */
.container { max-width: var(--maxw); margin: 0 auto; padding: 1.2rem 1rem 3rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1.2rem;
}
.card.auth { max-width: 380px; margin: 8vh auto 0; }

h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0 0 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85rem; color: var(--muted); }

/* Form --------------------------------------------------------------------- */
.stack { display: flex; flex-direction: column; gap: .9rem; }
.stack label { display: flex; flex-direction: column; gap: .35rem; font-weight: 600; font-size: .9rem; }
label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 500; }

input[type=text], input[type=password], select, input[type=file] {
  width: 100%;
  padding: .6rem .7rem;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
input[type=checkbox] { width: auto; }

.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.row input[type=text] { flex: 1 1 220px; }

.grid-form {
  display: grid; gap: .8rem;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  align-items: end;
}
.grid-form label { display: flex; flex-direction: column; gap: .35rem; font-weight: 600; font-size: .9rem; }
.grid-form button { height: fit-content; }

/* Bottoni ------------------------------------------------------------------ */
.btn {
  display: inline-block; cursor: pointer;
  font: inherit; font-weight: 600;
  padding: .6rem 1rem; border: 1px solid var(--border);
  border-radius: 9px; background: var(--surface); color: var(--text);
}
.btn:hover { border-color: var(--primary); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.05); }
.btn-mini { padding: .35rem .6rem; font-size: .85rem; }
.btn-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger:hover { background: var(--err-bg); border-color: var(--danger); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* Flash -------------------------------------------------------------------- */
.flash { padding: .7rem .9rem; border-radius: 9px; margin-bottom: .8rem; font-size: .92rem; }
.flash-success { background: var(--ok-bg); color: var(--ok-ink); }
.flash-error   { background: var(--err-bg); color: var(--err-ink); }

/* Galleria ----------------------------------------------------------------- */
.gallery-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.gallery-head h2 { margin: 0; }
.count {
  display: inline-block; margin-left: .3rem; padding: 0 .5rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 999px;
  font-size: .8rem; color: var(--muted); vertical-align: middle;
}
.filter select { width: auto; }

.grid {
  display: grid; gap: 1rem; margin-top: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
}
.tile {
  margin: 0; border: 1px solid var(--border); border-radius: 10px;
  overflow: hidden; background: var(--bg); display: flex; flex-direction: column;
}
.tile .thumb { display: block; aspect-ratio: 1 / 1; background: repeating-conic-gradient(#0000 0% 25%, rgba(128,128,128,.08) 0% 50%) 50% / 20px 20px; }
.tile .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile figcaption { padding: .55rem .6rem; display: flex; flex-direction: column; gap: .3rem; }
.tile .fname { font-size: .82rem; font-weight: 600; word-break: break-all; }
.tile .meta { font-size: .75rem; color: var(--muted); }
.tile-actions { display: flex; gap: .4rem; margin-top: .3rem; flex-wrap: wrap; align-items: center; }
.tile-actions form { margin: 0; }

/* Tabelle ------------------------------------------------------------------ */
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { text-align: left; padding: .6rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.table .num { text-align: right; }
.inline { display: inline-flex; gap: .35rem; align-items: center; margin: 0; }
.inline input[type=text] { width: auto; min-width: 120px; }
.inline select { width: auto; }
.actions-cell { display: flex; gap: .8rem; flex-wrap: wrap; align-items: center; }

@media (max-width: 640px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { border: 1px solid var(--border); border-radius: 10px; margin-bottom: .7rem; padding: .3rem .5rem; }
  .table td { border: none; padding: .35rem .2rem; }
  .actions-cell { flex-direction: column; align-items: stretch; }
}

/* Footer ------------------------------------------------------------------- */
.footer { text-align: center; color: var(--muted); font-size: .82rem; padding: 1.5rem 1rem; }
