/* tokens */
:root {
  color-scheme: light dark;

  --bg: #f4f6f4;
  --surface: #ffffff;
  --raised: #eaefec;
  --selected: #ffffff;
  --text: #16201d;
  --muted: #52605b;
  /* decoration only; borders that carry meaning use --field */
  --line: #dbe2de;
  --field: #7b8a84;
  --accent: #0b6655;
  --accent-hover: #084f42;
  --on-accent: #ffffff;
  --accent-soft: #dff0ea;
  --danger: #b42318;
  --danger-hover: #8f1b12;
  --on-danger: #ffffff;
  --danger-soft: #fdecea;
  --warn: #7a4b00;
  --warn-soft: #fbefd5;
  --shadow: 0 1px 2px rgb(22 32 29 / 6%), 0 4px 16px rgb(22 32 29 / 4%);

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --r-s: 0.5rem;
  --r-m: 0.75rem;
  --r-l: 1rem;

  /* smallest touch target */
  --touch: 2.75rem;
  --pad: 1.25rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1513;
    --surface: #18201d;
    --raised: #222c28;
    --selected: #34423c;
    --text: #e8efec;
    --muted: #a0afa9;
    --line: #2d3834;
    --field: #6c7d76;
    --accent: #6fd3b8;
    --accent-hover: #93e2cc;
    --on-accent: #04231b;
    --accent-soft: #163a31;
    --danger: #ff9387;
    --danger-hover: #ffb1a8;
    --on-danger: #3a0804;
    --danger-soft: #3c1b18;
    --warn: #f0c069;
    --warn-soft: #382a10;
    --shadow: 0 0 transparent;
  }
}

@media (min-width: 40rem) {
  :root {
    --pad: 1.5rem;
  }
}

/* base */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem / 1.5 var(--sans);
  overflow-wrap: anywhere;
}

main {
  max-width: 46rem;
  margin: 0 auto;
  padding: 1.25rem max(1rem, env(safe-area-inset-right)) calc(2.5rem + env(safe-area-inset-bottom))
    max(1rem, env(safe-area-inset-left));
}

/* login, password, confirmation */
main:has(> form[method="post"]) {
  max-width: 28rem;
}

/* no header without a session: the notch inset is ours */
body > main:first-child {
  padding-top: max(12vh, calc(1.5rem + env(safe-area-inset-top)));
}

/* names come from IPA: keep bidi control characters from leaking out */
h1,
h2,
h3,
strong,
td,
dd,
code,
a,
.muted,
.login {
  unicode-bidi: isolate;
}

h1,
h2,
h3,
p,
ul,
ol,
dl,
table,
form {
  margin: 0 0 1rem;
}

h1,
h2,
h3 {
  font-weight: 600;
}

h1 {
  font-size: 1.625rem;
  line-height: 1.2;
  letter-spacing: -0.015em;
  text-wrap: balance;
}

h2 {
  font-size: 1.125rem;
  line-height: 1.3;
}

h3 {
  font-size: 0.9375rem;
}

main > h2 {
  margin: 2rem 0 0.75rem;
}

ol {
  padding-left: 1.4rem;
}

li + li {
  margin-top: 0.5rem;
}

strong {
  font-weight: 600;
}

a {
  color: var(--accent);
  text-underline-offset: 0.2em;
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
}

p code,
li code {
  padding: 0.1em 0.35em;
  border-radius: var(--r-s);
  background: var(--raised);
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

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

/* text links grow to the touch target without moving the lines around */
:where(dd a, ol a, p > a:not(.button)) {
  display: inline-flex;
  align-items: center;
  min-width: var(--touch);
  min-height: var(--touch);
  margin: -0.65rem 0;
}

/* forms: plain label + input + button must look right */
label {
  display: block;
  margin: 0 0 0.375rem;
  font-size: 0.9375rem;
  font-weight: 600;
}

input,
select,
textarea,
button {
  font: inherit;
}

input,
select,
textarea {
  display: block;
  width: 100%;
  min-height: var(--touch);
  margin: 0 0 1rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--field);
  border-radius: var(--r-s);
  background: var(--surface);
  color: var(--text);
  /* smaller makes Safari zoom in */
  font-size: 1rem;
}

input[hidden] {
  display: none;
}

input[type="search"] {
  -webkit-appearance: none;
  appearance: none;
}

select {
  padding-right: 2.25rem;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(-45deg, transparent 50%, var(--muted) 50%);
  background-position:
    right 19px center,
    right 13px center;
  background-repeat: no-repeat;
  background-size: 6px 6px;
  -webkit-appearance: none;
  appearance: none;
}

label > input,
label > select {
  margin: 0.375rem 0 1rem;
  font-weight: 400;
}

input[type="checkbox"] {
  flex: none;
  width: 1.4rem;
  height: 1.4rem;
  min-height: 0;
  margin: 0 0.625rem 0 0;
  padding: 0;
  accent-color: var(--accent);
}

label:has(> input[type="checkbox"]) {
  display: flex;
  align-items: center;
  min-height: var(--touch);
  margin-bottom: 0.75rem;
  font-size: 1rem;
  font-weight: 400;
}

button,
.button,
summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--touch);
  min-height: var(--touch);
  padding: 0.5rem 1.125rem;
  border: 1px solid transparent;
  border-radius: var(--r-s);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.secondary,
summary {
  border-color: var(--field);
  background: var(--surface);
  color: var(--text);
}

.danger {
  background: var(--danger);
  color: var(--on-danger);
}

/* surfaces */
.card,
.apps,
.access,
table,
main > form[method="post"] {
  border: 1px solid var(--line);
  border-radius: var(--r-l);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.card,
main > form[method="post"] {
  margin: 0 0 1rem;
  padding: var(--pad);
}

:where(main > form[method="post"]) > button {
  width: 100%;
}

/* messages: by role, so the account pages are covered too */
[role="alert"],
[role="status"] {
  padding: 0.75rem 1rem;
  border: 1px solid var(--danger);
  border-radius: var(--r-m);
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 600;
}

[role="status"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.muted {
  color: var(--muted);
  font-size: 0.9375rem;
}

.empty {
  padding: 2.5rem 1rem;
  border: 1px dashed var(--field);
  border-radius: var(--r-l);
  color: var(--muted);
  text-align: center;
}

.hint {
  color: var(--muted);
  font-size: 0.875rem;
  text-align: center;
  text-wrap: balance;
}

.brand {
  margin-bottom: 0.25rem;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.875rem;
}

.bundle-id {
  font-family: var(--mono);
  font-size: 0.8125rem;
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 99px;
  background: var(--raised);
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.4;
}

.badge.ok {
  background: var(--accent-soft);
  color: var(--accent);
}

.badge.warn {
  background: var(--warn-soft);
  color: var(--warn);
}

.badge.bad {
  background: var(--danger-soft);
  color: var(--danger);
}

/* tables */
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.875rem;
  overflow: hidden;
}

th,
td {
  padding: 0.75rem 0.5rem;
  overflow-wrap: break-word;
  text-align: left;
  vertical-align: top;
}

th {
  padding-block: 0.6rem;
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 600;
}

td {
  border-top: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}

th:first-child,
td:first-child {
  padding-left: 1rem;
}

th:last-child,
td:last-child {
  padding-right: 1rem;
}

td:first-child,
.journal td,
.bundles td:nth-child(2) {
  overflow-wrap: anywhere;
}

td a {
  display: inline-block;
  min-width: var(--touch);
  min-height: var(--touch);
  margin: -0.75rem 0;
  padding: 0.75rem 0;
  font-weight: 600;
  text-decoration: none;
}

.bundles td:nth-child(2) {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.8125rem;
}

@media (max-width: 24rem) {
  table {
    font-size: 0.8125rem;
  }

  th,
  td {
    padding-inline: 0.3rem;
  }

  th:first-child,
  td:first-child {
    padding-left: 0.75rem;
  }

  th:last-child,
  td:last-child {
    padding-right: 0.5rem;
  }
}

/* four columns of words do not fit the narrowest phones */
@media (max-width: 24rem) {
  .people,
  .groups,
  .people tbody,
  .groups tbody,
  .people tr,
  .groups tr {
    display: block;
  }

  .people thead,
  .groups thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  .people tr,
  .groups tr {
    padding: 0.75rem 1rem;
  }

  .people tr + tr,
  .groups tr + tr {
    border-top: 1px solid var(--line);
  }

  .people td,
  .groups td {
    display: inline;
    padding: 0;
    border: 0;
    color: var(--muted);
    font-size: 0.875rem;
  }

  .people td:first-child,
  .groups td:first-child {
    display: block;
    padding: 0;
  }

  .people td:nth-child(n + 3)::before {
    content: " · ";
  }

  .groups td:nth-child(2)::before {
    content: "Людей: ";
  }

  .groups td:nth-child(3)::before {
    content: " · Прил: ";
  }

  .groups td:nth-child(4)::before {
    content: " · Новые прилы: ";
  }
}

/* five columns do not fit any phone */
@media (max-width: 39.99rem) {
  .journal,
  .journal tbody,
  .journal tr,
  .journal td {
    display: block;
  }

  .journal thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  .journal tr {
    padding: 0.75rem 1rem;
  }

  .journal tr + tr {
    border-top: 1px solid var(--line);
  }

  .journal td {
    padding: 0;
    border: 0;
    color: var(--muted);
    font-size: 0.875rem;
  }

  .journal td:nth-child(2) {
    color: var(--text);
    font-size: 1rem;
    font-weight: 600;
  }

  .journal td:nth-child(3)::before {
    content: "Кто: ";
  }

  .journal td:nth-child(4)::before {
    content: "IP: ";
  }

  .journal td:empty {
    display: none;
  }
}

/* way up */
.back {
  margin: -0.75rem 0 0;
}

.back > a {
  gap: 0.5rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.9375rem;
  font-weight: 600;
  text-decoration: none;
}

.back > a::before,
.apps li > a::after,
summary::after {
  flex: none;
  width: 0.5rem;
  height: 0.5rem;
  border: solid currentColor;
  border-width: 0 2px 2px 0;
  content: "";
}

.back > a::before {
  margin-left: 0.2rem;
  transform: rotate(135deg);
}

/* header */
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0 1rem;
  padding: env(safe-area-inset-top) max(1rem, env(safe-area-inset-right)) 0
    max(1rem, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.top nav,
.top form {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: 0;
  margin: 0;
}

.top form {
  margin-left: auto;
}

.top a,
.top button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--touch);
  min-height: 3.25rem;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.top a {
  /* the top border keeps the text on the axis of the bar */
  border: solid transparent;
  border-width: 2px 0;
}

.top [aria-current] {
  border-bottom-color: var(--accent);
  color: var(--text);
}

/* shown in the settings; no room in one row on a phone */
.top .login {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 22.5rem) {
  .top {
    gap: 0 0.5rem;
  }

  .top nav {
    gap: 0.6rem;
  }

  .top a,
  .top button {
    font-size: 0.9375rem;
  }
}

/* admin tabs: no horizontal scroll, nothing would bring the current one into view */
.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.25rem;
  margin: 0 0 1.25rem;
  padding: 0.25rem;
  border-radius: var(--r-m);
  background: var(--raised);
}

.tabs a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  border: 1px solid transparent;
  border-radius: var(--r-s);
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.tabs [aria-current] {
  border-color: var(--field);
  background: var(--selected);
  box-shadow: var(--shadow);
  color: var(--text);
}

/* definitions */
dl {
  display: grid;
  grid-template-columns: fit-content(42%) minmax(0, 1fr);
  gap: 0.375rem 1rem;
  align-items: baseline;
  font-size: 0.9375rem;
}

dt {
  color: var(--muted);
  overflow-wrap: normal;
}

dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

dd code {
  font-size: 0.8125rem;
  -webkit-user-select: all;
  user-select: all;
}

/* lists of apps and grants */
.row {
  display: flex;
  gap: 0.5rem;
}

.row input {
  flex: 1;
  min-width: 0;
  margin: 0;
}

.icon {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1.375rem;
  font-weight: 600;
  object-fit: cover;
  overflow: hidden;
}

/* white icons would melt into the card */
img.icon {
  box-shadow: 0 0 0 1px var(--line);
}

.apps,
.access {
  padding: 0;
  list-style: none;
  overflow: hidden;
}

.apps li > a,
.access li {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  padding: 0.75rem 1rem;
}

.apps li > a {
  color: inherit;
  text-decoration: none;
}

.apps li > a::after {
  margin-right: 0.2rem;
  color: var(--muted);
  transform: rotate(-45deg);
}

.apps .muted,
.access .muted {
  font-size: 0.875rem;
}

.text {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.access li {
  min-height: 3.75rem;
  padding-block: 0.5rem;
}

.apps li + li,
.access li + li {
  margin: 0;
  border-top: 1px solid var(--line);
}

.access form {
  flex: none;
  margin: 0;
}

.access button {
  min-width: 6.5rem;
  padding-inline: 0.75rem;
}

.access .text a {
  position: relative;
  align-self: start;
  font-weight: 600;
  text-decoration: none;
}

.apps li > a:focus-visible {
  outline-offset: -3px;
  border-radius: var(--r-l);
}

/* touch target: the name and the line below it, without a gap */
.access .text a::after {
  position: absolute;
  inset: -0.75rem -0.5rem;
  min-width: var(--touch);
  content: "";
}

.text > :empty {
  display: none;
}

.mark {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  border: 2px solid var(--field);
  border-radius: 50%;
}

.mark.on {
  border-color: var(--accent);
  background: var(--accent);
}

.mark.on::after {
  width: 0.35rem;
  height: 0.6rem;
  margin-top: -0.15rem;
  border: solid var(--on-accent);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
  content: "";
}

/* app page */
.app-head {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.app-head .icon {
  align-self: flex-start;
  width: 72px;
  height: 72px;
  border-radius: 16px;
  font-size: 1.875rem;
}

.app-head h1 {
  margin: 0 0 0.125rem;
  font-size: 1.5rem;
}

.app-head p {
  margin: 0;
}

.version,
.build > h2 {
  margin-bottom: 0.25rem;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.version {
  font-size: 1rem;
}

.build > h3 {
  margin-bottom: 0.25rem;
  color: var(--muted);
  font-size: 0.875rem;
}

.actions + .muted {
  margin-top: 1rem;
}

.build > .muted,
.card > .version + .muted {
  margin-bottom: 0.5rem;
  font-variant-numeric: tabular-nums;
}

.build > .button {
  width: 100%;
  margin-bottom: 1rem;
}

.build > dl {
  padding-top: 0.875rem;
  border-top: 1px solid var(--line);
}

.latest {
  border-color: var(--accent);
}

.latest > h2 {
  font-size: 1.375rem;
}

.latest > .button {
  min-height: 3.5rem;
  margin-bottom: 0.75rem;
  border-radius: var(--r-m);
  font-size: 1.125rem;
}

.changelog {
  margin: 0 0 1rem;
  white-space: pre-wrap;
  unicode-bidi: plaintext;
}

.qr {
  text-align: center;
}

.qr-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 1rem 1.5rem;
}

.qr-row p {
  flex: 1 1 14rem;
  margin: 0;
}

.qr img,
.qr-row img {
  max-width: 100%;
  height: auto;
  border-radius: var(--r-m);
}

/* actions and confirmations */
.actions,
.pager {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.75rem;
}

.actions > form {
  margin-bottom: 0;
}

summary {
  gap: 0.6rem;
  color: var(--danger);
  list-style: none;
}

summary::-webkit-details-marker {
  display: none;
}

summary::after {
  margin-top: -0.25rem;
  transform: rotate(45deg);
}

.calm > summary {
  color: var(--text);
}

.confirm[open] {
  flex: 1 0 100%;
  padding: 0.75rem;
  border: 1px solid var(--danger);
  border-radius: var(--r-m);
  background: var(--danger-soft);
}

.calm[open] {
  border-color: var(--field);
  background: var(--raised);
}

.confirm[open] > summary::after {
  margin-top: 0.25rem;
  transform: rotate(-135deg);
}

.confirm > p {
  margin: 0.75rem 0;
}

.confirm > form {
  margin: 0;
}

.secret {
  display: block;
  padding: 0.875rem;
  border: 1px dashed var(--field);
  border-radius: var(--r-s);
  background: var(--raised);
  font-size: 1.0625rem;
  -webkit-user-select: all;
  user-select: all;
}

meter {
  display: block;
  width: 100%;
  height: 0.75rem;
  margin-bottom: 1rem;
  border: 0;
  background: none;
  -webkit-appearance: none;
  appearance: none;
}

meter::-webkit-meter-bar {
  grid-row: 1 / -1;
  height: 0.75rem;
  border: 0;
  border-radius: 99px;
  background: var(--raised);
}

meter::-webkit-meter-optimum-value {
  background: var(--accent);
}

meter::-webkit-meter-suboptimum-value {
  background: var(--warn);
}

meter::-moz-meter-bar {
  background: var(--accent);
}

button:active,
.button:active,
summary:active {
  transform: translateY(1px);
}

.card > :last-child {
  margin-bottom: 0;
}

/* touch screens keep :hover after a tap */
@media (hover: hover) {
  a:hover {
    color: var(--accent-hover);
  }

  button:hover,
  .button:hover {
    background: var(--accent-hover);
    color: var(--on-accent);
  }

  .tabs a:hover,
  .back > a:hover {
    color: var(--text);
  }

  .top a:hover,
  .top button:hover {
    background: none;
    color: var(--text);
  }

  .secondary:hover,
  summary:hover,
  .apps li > a:hover {
    background: var(--raised);
  }

  .secondary:hover,
  .apps li > a:hover,
  .calm > summary:hover {
    color: var(--text);
  }

  button.danger:hover {
    background: var(--danger-hover);
    color: var(--on-danger);
  }

  .confirm[open] > summary:hover {
    background: var(--surface);
  }
}

@media (prefers-reduced-motion: no-preference) {
  a,
  button,
  summary {
    transition:
      background-color 120ms,
      color 120ms;
  }
}

@media (max-width: 39.99rem) {
  .actions > *,
  .actions > form > button,
  .actions summary {
    width: 100%;
  }
}

@media (min-width: 40rem) {
  main {
    max-width: 52rem;
  }

  .top {
    padding-inline: max(1.5rem, env(safe-area-inset-left)) max(1.5rem, env(safe-area-inset-right));
  }

  .top nav,
  .top form {
    gap: 1.5rem;
  }

  .top .login {
    position: static;
    width: auto;
    max-width: 12rem;
    height: auto;
    color: var(--muted);
    font-size: 0.9375rem;
    text-overflow: ellipsis;
    clip-path: none;
  }

  .tabs {
    display: inline-flex;
    flex-wrap: wrap;
  }

  .tabs a {
    padding: 0 0.9rem;
    font-size: 0.9375rem;
  }

  table {
    font-size: 0.9375rem;
  }

  .journal {
    font-size: 0.875rem;
  }

  .journal th:first-child {
    width: 8.75rem;
  }

  .journal td:nth-child(3) {
    min-width: 8.5rem;
  }

  /* an IPv4 address in one line; IPv6 may wrap */
  .journal td:nth-child(4) {
    min-width: 8rem;
  }

  .build:not(.latest) > .button {
    width: auto;
  }
}
