:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #ebecf0;
  --text: #172b4d;
  --muted: #5e6c84;
  --border: #dfe1e6;
  --primary: #2f6fde;
  --primary-hover: #2459b8;
  --danger: #c9372c;
  --danger-hover: #a52b22;
  --shadow: 0 1px 2px rgba(9, 30, 66, .2);
  --radius: 8px;
  --list-width: 280px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161a1f;
    --surface: #22272b;
    --surface-2: #1d2125;
    --text: #dee4ea;
    --muted: #9fadbc;
    --border: #38414a;
    --primary: #579dff;
    --primary-hover: #85b8ff;
    --danger: #f87168;
    --danger-hover: #fd9891;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}
a { color: var(--primary); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.nowrap { white-space: nowrap; }
.grow { flex: 1; min-width: 0; }
.empty { color: var(--muted); padding: 2rem 0; text-align: center; }
.error-text, .form-error { color: var(--danger); }
.form-error { margin: 0; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  padding: .5rem .9rem; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger:hover { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.block { width: 100%; justify-content: flex-start; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  padding: .25rem .4rem; border-radius: 4px; font-size: .95rem; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

input[type="text"], input[type="password"], input:not([type]), textarea {
  width: 100%; padding: .55rem .65rem; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
}
textarea { resize: vertical; min-height: 6rem; }
input:focus, textarea:focus, .btn:focus-visible, .card:focus-visible, .dropzone:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .9rem; }
.stack { display: flex; flex-direction: column; gap: 1rem; }

.badge {
  display: inline-block; min-width: 1.6rem; padding: .05rem .45rem; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: .8rem; font-weight: 600; text-align: center;
}

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; }
.topbar-right { display: flex; align-items: center; gap: .75rem; }
main { padding: 1.25rem 1rem 3rem; max-width: 1400px; margin: 0 auto; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.description { white-space: pre-wrap; margin: .5rem 0 0; color: var(--muted); max-width: 70ch; }
.crumbs { margin-bottom: .75rem; }
.crumbs a { text-decoration: none; }

/* ---------- dashboard ---------- */
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.project-tile {
  display: flex; flex-direction: column; gap: .6rem; padding: 1rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); color: var(--text); text-decoration: none;
  transition: transform .1s, border-color .1s;
}
.tile-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .5rem; }
.project-handle { cursor: grab; color: var(--muted); padding: 0 .2rem; user-select: none; touch-action: none; font-size: 1.1rem; }
.project-tile:hover { border-color: var(--primary); transform: translateY(-1px); }
.list-summary { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .3rem; }
.list-summary li { display: flex; justify-content: space-between; gap: .5rem; font-size: .9rem; }

/* ---------- board ---------- */
.board {
  display: flex; align-items: flex-start; gap: .75rem;
  overflow-x: auto; padding-bottom: 1rem; margin-bottom: 2rem;
  scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
}
.list, .add-list { flex: 0 0 var(--list-width); scroll-snap-align: start; }
.list {
  display: flex; flex-direction: column; max-height: 75vh;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
}
.list-head { display: flex; align-items: center; gap: .35rem; padding: .6rem .5rem .4rem .4rem; }
.list-handle { cursor: grab; color: var(--muted); padding: 0 .2rem; user-select: none; touch-action: none; font-size: 1.1rem; }
.list-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .95rem; }
.cards {
  list-style: none; margin: 0; padding: .25rem .5rem; min-height: 2.5rem;
  display: flex; flex-direction: column; gap: .45rem; overflow-y: auto;
}
.card {
  padding: .55rem .7rem; background: var(--surface); border-radius: 6px; box-shadow: var(--shadow);
  cursor: pointer; overflow-wrap: anywhere; user-select: none; -webkit-user-select: none;
}
.card:hover { outline: 1px solid var(--primary); }
.drag-ghost { opacity: .4; }
.add-card-wrap { padding: .4rem .5rem .5rem; }
.add-card { display: flex; flex-direction: column; gap: .4rem; }

/* ---------- files ---------- */
.files { display: flex; flex-direction: column; gap: .75rem; }
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 1.25rem;
  text-align: center; cursor: pointer; background: var(--surface);
}
.dropzone.over { border-color: var(--primary); background: var(--surface-2); }
.dropzone .link { color: var(--primary); text-decoration: underline; }
.uploads { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.uploads li {
  display: grid; grid-template-columns: minmax(0, 1fr) 140px auto auto; align-items: center; gap: .6rem;
  padding: .45rem .7rem; background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
}
.uploads li.failed { border-color: var(--danger); grid-template-columns: minmax(0, 1fr) auto auto; }
.uploads progress { width: 100%; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .55rem .75rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
td.file-name { overflow-wrap: anywhere; }
td.actions { text-align: right; white-space: nowrap; }
td.actions .btn { margin-left: .3rem; }

/* ---------- modal ---------- */
.modal {
  width: min(520px, calc(100vw - 1.5rem)); max-height: calc(100vh - 2rem); max-height: calc(100dvh - 2rem);
  margin: auto; overflow: hidden;
  padding: 0; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
}
.modal[open] { display: flex; flex-direction: column; }
.modal-head, .modal-actions { flex: 0 0 auto; }
.modal-body { flex: 1 1 auto; min-height: 0; }
.modal.wide { width: min(720px, calc(100vw - 1.5rem)); }
.modal::backdrop { background: rgba(9, 30, 66, .5); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem .5rem; }
.modal-body { padding: .5rem 1.25rem 1rem; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; padding: .75rem 1.25rem 1rem; flex-wrap: wrap; }
.modal-actions.inline { padding: 0; }
.modal-actions .left { margin-right: auto; }
.fields-head { display: flex; align-items: center; justify-content: space-between; }
.fields { display: flex; flex-direction: column; gap: .5rem; }
.field-row { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr) auto; gap: .5rem; align-items: center; }

/* ---------- toasts ---------- */
#toasts { position: fixed; bottom: 1rem; right: 1rem; left: 1rem; display: flex; flex-direction: column; align-items: flex-end; gap: .5rem; z-index: 1000; pointer-events: none; }
.toast { padding: .65rem 1rem; border-radius: 6px; background: var(--text); color: var(--surface); box-shadow: var(--shadow); max-width: 420px; pointer-events: auto; }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- login ---------- */
.login-body { display: flex; align-items: center; justify-content: center; padding: 1rem; }
.login-box {
  width: min(380px, 100%); padding: 2rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
}
.login-box h1 { margin-bottom: 1.25rem; text-align: center; }
.login-box form { display: flex; flex-direction: column; gap: 1rem; }
.alert { padding: .6rem .8rem; border-radius: 6px; background: rgba(201, 55, 44, .12); color: var(--danger); margin: 0 0 1rem; }

/* ---------- mobile ---------- */
@media (max-width: 640px) {
  :root { --list-width: 82vw; }
  main { padding: 1rem .75rem 3rem; }
  h1 { font-size: 1.35rem; }
  .hide-sm { display: none; }
  .field-row { grid-template-columns: minmax(0, 1fr) auto; }
  .field-row input:nth-child(2) { grid-column: 1; }
  .field-row button { grid-row: 1 / span 2; grid-column: 2; }
  .uploads li { grid-template-columns: minmax(0, 1fr) auto; }
  .uploads li progress { grid-column: 1 / -1; grid-row: 2; }
  td.actions .btn:first-child { display: none; } /* file name is already a download link */
  #whoami { display: none; }
}
