:root {
  --ink: #30213d;
  --muted: #756e7f;
  --purple: #792785;
  --purple-deep: #672076;
  --line: #e6dbea;
  --panel: #ffffff;
  --bg: #f6f3f9;
  --radius: 16px;
  --shadow: 0 10px 32px rgba(63, 23, 90, 0.07);
  --safe: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.topbar {
  background: var(--panel);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: calc(12px + var(--safe-t)) max(16px, calc((100vw - 1300px) / 2 + var(--safe-r))) 12px max(16px, calc((100vw - 1300px) / 2 + var(--safe)));
  border-bottom: 1px solid var(--line);
}
.brand-logo {
  width: min(168px, 42vw);
  height: auto;
  max-height: 58px;
  object-fit: contain;
  display: block;
}
.brand-text strong {
  font-size: clamp(16px, 2.4vw, 20px);
  color: var(--purple);
  display: block;
  line-height: 1.25;
}
.brand-text small {
  display: block;
  color: var(--muted);
  margin-top: 2px;
  font-size: 12px;
}
.grow { flex: 1; }
.session {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
#who { font-size: 12px; color: var(--muted); }

main {
  flex: 1;
  width: 100%;
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px max(16px, var(--safe-r)) 32px max(16px, var(--safe));
}
[hidden] { display: none !important; }

nav, .controls, .row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}
nav {
  margin-bottom: 20px;
  overflow-x: auto;
  flex-wrap: nowrap;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  padding-bottom: 2px;
}
nav button { flex: 0 0 auto; white-space: nowrap; }

button {
  font: inherit;
  border: 0;
  border-radius: 10px;
  background: #eae1ee;
  color: var(--purple-deep);
  padding: 11px 16px;
  min-height: 44px;
  font-weight: 700;
  cursor: pointer;
}
button.primary, nav button.active {
  background: var(--purple);
  color: #fff;
}
button.primary.wide { width: 100%; }
button.blue { background: #24a6d8; color: #fff; }
button.danger { background: #ffe4e6; color: #982d39; }
button:disabled { opacity: .5; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--purple);
  outline-offset: 2px;
}

input, select, textarea {
  font: inherit;
  width: 100%;
  min-height: 44px;
  border-radius: 10px;
  padding: 10px 12px;
  border: 1px solid #ded3e2;
  background: #fff;
  max-width: 100%;
}
label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 650;
}
textarea { min-height: 110px; resize: vertical; }

.panel, .card, .login-card {
  background: var(--panel);
  border: 1px solid #ebe2ed;
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 17px;
  box-shadow: var(--shadow);
}
.card { margin: 0; }
.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 13px;
  margin: 14px 0 24px;
}
.cards-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.metric { font-size: clamp(24px, 4vw, 30px); font-weight: 800; color: var(--purple); }
h2 { margin: 0 0 8px; color: #70267d; font-size: clamp(20px, 3vw, 26px); }
h3 { margin: 15px 0 8px; }
.muted { color: var(--muted); font-size: 13px; line-height: 1.45; }
.controls { margin: 15px 0; }
.controls label { flex: 1 1 160px; min-width: min(100%, 160px); }

.scroll {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
table {
  border-collapse: collapse;
  width: 100%;
  min-width: 760px;
}
th, td {
  border-bottom: 1px solid #eee7f2;
  text-align: left;
  padding: 12px;
  vertical-align: middle;
}
th { background: #faf8fb; color: #665c70; font-size: 12px; }
tr:hover td { background: #faf7fc; }
.badge {
  border-radius: 30px;
  background: #ebe1f1;
  padding: 5px 10px;
  white-space: nowrap;
  font-size: 12px;
}
.system { background: #ebe1f1; color: var(--purple); font-weight: 700; }
.done { background: #ddf3e6; color: #176b45; }
.late { background: #ffe4e6; color: #982d39; }
.soon { background: #fff0cd; color: #8b6208; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.grid .primary { justify-self: start; }
.group {
  padding: 12px;
  background: #f0e3f5;
  color: var(--purple);
  font-weight: 800;
  border-radius: 8px;
  margin-top: 20px;
}
.criterion { padding: 14px 0; border-bottom: 1px solid #eee; }
.criterion h4 { margin: 0 0 5px; }
.options { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.options label {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px 12px;
  min-height: 44px;
  font-weight: 400;
  cursor: pointer;
}
.options label:has(input:checked) { background: #f1e0f4; border-color: var(--purple); }
.warning { border-left: 4px solid #d78542; background: #fff3df; padding: 10px; margin: 7px 0; }
#messages:not(:empty) {
  background: #fff3df;
  padding: 12px 14px;
  margin: 0 0 16px;
  border-radius: 10px;
}
footer {
  text-align: center;
  padding: 20px 16px calc(20px + var(--safe-b));
  color: var(--muted);
  font-size: 12px;
}
code.secret {
  display: block;
  margin: 10px 0;
  padding: 12px;
  background: #faf8fb;
  border: 1px dashed #d7c6de;
  border-radius: 8px;
  font-size: 18px;
  letter-spacing: .04em;
  word-break: break-all;
}
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.note-block { margin: 12px 0; }

body.guest main {
  max-width: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}
body.guest #messages { width: min(440px, 100%); }
.login-card {
  width: min(440px, 100%);
  margin: 0 auto;
  padding: 36px 32px;
  border-radius: 20px;
}
.login-card h2 { font-size: 26px; }
.login-card form { display: grid; gap: 16px; margin-top: 8px; }
.login-card label { margin: 0; }

@media (max-width: 960px) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .session { width: 100%; justify-content: space-between; }
  body.guest:not(.signed) .session { display: none; }
  .cards, .cards-2, .grid { grid-template-columns: 1fr; }
  .grid .primary { width: 100%; justify-self: stretch; }
  .controls { flex-direction: column; align-items: stretch; }
  .controls label { width: 100%; }
  .panel, .login-card { padding: 20px 16px; }
  .login-card { padding: 28px 20px; }
  table { min-width: 0; }
  .stack-table thead { display: none; }
  .stack-table tr {
    display: block;
    background: #fff;
    border: 1px solid #ebe2ed;
    border-radius: 12px;
    margin-bottom: 12px;
    padding: 6px 10px;
    box-shadow: none;
  }
  .stack-table td {
    display: block;
    border: 0;
    border-bottom: 1px solid #f3eef6;
    padding: 10px 2px;
  }
  .stack-table tr:hover td { background: transparent; }
  .stack-table td:last-child { border-bottom: 0; }
  .stack-table td::before {
    content: attr(data-label);
    display: block;
    font-weight: 650;
    color: #665c70;
    font-size: 12px;
    margin-bottom: 4px;
  }
}

@media (max-width: 420px) {
  .brand-text small { display: none; }
  nav button { padding: 10px 12px; }
}

@media print {
  header, nav, button, .controls, footer { display: none !important; }
  main { max-width: none; }
  .panel { box-shadow: none; break-inside: avoid; }
}
