/*
 * Hughie app kit: one look for every kit app. Phone first.
 * Rules it keeps: every tap target is 48px or taller, typing boxes are 16px
 * (so iPhones do not zoom), nothing scrolls sideways at 360px, the bottom bar
 * stays above the home-indicator and the keyboard.
 * Classes all start with k- so an app's own css never collides.
 */
:root {
  --k-brand: #1f4e79;
  --k-ink: #16202b;
  --k-soft: #5b6878;
  --k-line: #d9dfe7;
  --k-bg: #f2f4f7;
  --k-card: #ffffff;
  --k-done: #1e7d46;
  --k-done-bg: #e5f4ea;
  --k-wait: #9a6200;
  --k-wait-bg: #fff3d6;
  --k-bad: #b3261e;
  --k-bad-bg: #fde9e7;
  --k-picked-bg: #e8eff7;
  --k-radius: 14px;
  --k-tap: 48px;
  --k-vh: 100dvh;
  --k-top: env(safe-area-inset-top, 0px);
  --k-bottom: env(safe-area-inset-bottom, 0px);
}

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

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

body.k-body {
  margin: 0;
  background: var(--k-bg);
  color: var(--k-ink);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

.k-app {
  display: flex;
  flex-direction: column;
  min-height: var(--k-vh);
  max-width: 720px;
  margin: 0 auto;
}

/* ── Top bar ─────────────────────────────────────────────── */
.k-top {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: calc(56px + var(--k-top));
  padding: var(--k-top) 6px 0;
  background: var(--k-brand);
  color: #fff;
}
.k-top h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 0 6px;
  font-size: 1.12rem;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.k-top-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: var(--k-tap);
  height: var(--k-tap);
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.k-top-btn:active {
  background: rgba(255, 255, 255, 0.18);
}
.k-top-btn[hidden] {
  display: none;
}
.k-top-btn svg {
  width: 24px;
  height: 24px;
}
.k-who {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.2);
  font-size: 0.86rem;
  font-weight: 700;
}

/* ── Banner under the top bar (no signal, waiting to send) ─ */
.k-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 8px 14px;
  border: 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  background: var(--k-wait-bg);
  color: var(--k-wait);
  font: inherit;
  font-size: 0.94rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}
.k-banner[hidden] {
  display: none;
}
.k-banner.is-bad {
  background: var(--k-bad-bg);
  color: var(--k-bad);
}
.k-banner span:last-child {
  margin-left: auto;
  font-weight: 500;
  text-decoration: underline;
}

/* ── Page ────────────────────────────────────────────────── */
.k-main {
  flex: 1;
  padding: 14px 14px 24px;
}
.k-main > * + * {
  margin-top: 14px;
}
.k-section > h2 {
  margin: 4px 2px 8px;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--k-soft);
}
.k-section > * + * {
  margin-top: 10px;
}
.k-card {
  padding: 14px;
  border: 1px solid var(--k-line);
  border-radius: var(--k-radius);
  background: var(--k-card);
}
.k-card > * + * {
  margin-top: 10px;
}
.k-text {
  margin: 0;
}
.k-muted {
  color: var(--k-soft);
  font-size: 0.92rem;
}

/* ── Buttons ─────────────────────────────────────────────── */
.k-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--k-tap);
  padding: 10px 18px;
  border: 1.5px solid var(--k-line);
  border-radius: 12px;
  background: var(--k-card);
  color: var(--k-ink);
  font: inherit;
  font-weight: 650;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.k-btn:active {
  transform: translateY(1px);
  filter: brightness(0.96);
}
.k-btn.is-primary {
  border-color: var(--k-brand);
  background: var(--k-brand);
  color: #fff;
}
.k-btn.is-danger {
  border-color: var(--k-bad);
  color: var(--k-bad);
}
.k-btn.is-big {
  min-height: 56px;
  font-size: 1.06rem;
}
.k-btn.is-wide {
  display: flex;
  width: 100%;
}
.k-btn[disabled] {
  opacity: 0.55;
  cursor: default;
}
.k-btn.is-busy::before {
  content: '';
  width: 16px;
  height: 16px;
  border: 2.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: k-spin 0.7s linear infinite;
}
@keyframes k-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── Lists ───────────────────────────────────────────────── */
.k-list {
  border: 1px solid var(--k-line);
  border-radius: var(--k-radius);
  background: var(--k-card);
  overflow: hidden;
}
.k-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 60px;
  padding: 10px 14px;
  border: 0;
  border-top: 1px solid var(--k-line);
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
}
.k-row:first-child {
  border-top: 0;
}
button.k-row {
  cursor: pointer;
}
button.k-row:active {
  background: var(--k-picked-bg);
}
.k-row-body {
  flex: 1;
  min-width: 0;
}
.k-row-title {
  display: block;
  font-weight: 650;
  overflow-wrap: anywhere;
}
.k-row-sub {
  display: block;
  margin-top: 2px;
  color: var(--k-soft);
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}
.k-row-go {
  flex: none;
  width: 18px;
  height: 18px;
  color: var(--k-soft);
}
.k-thumb {
  flex: none;
  width: 48px;
  height: 48px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--k-bg);
}

.k-pill {
  flex: none;
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--k-picked-bg);
  color: var(--k-brand);
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}
.k-pill.is-done {
  background: var(--k-done-bg);
  color: var(--k-done);
}
.k-pill.is-wait {
  background: var(--k-wait-bg);
  color: var(--k-wait);
}
.k-pill.is-bad {
  background: var(--k-bad-bg);
  color: var(--k-bad);
}

.k-empty {
  padding: 34px 18px;
  border: 1.5px dashed var(--k-line);
  border-radius: var(--k-radius);
  text-align: center;
  color: var(--k-soft);
}
.k-empty strong {
  display: block;
  margin-bottom: 4px;
  color: var(--k-ink);
  font-size: 1.04rem;
}

.k-note {
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--k-picked-bg);
  color: var(--k-brand);
  font-size: 0.95rem;
}
.k-note.is-done {
  background: var(--k-done-bg);
  color: var(--k-done);
}
.k-note.is-wait {
  background: var(--k-wait-bg);
  color: var(--k-wait);
}
.k-note.is-bad {
  background: var(--k-bad-bg);
  color: var(--k-bad);
}

/* ── Form fields ─────────────────────────────────────────── */
.k-field {
  display: block;
}
.k-label {
  display: block;
  margin: 0 2px 6px;
  font-weight: 650;
}
.k-label small {
  margin-left: 6px;
  color: var(--k-soft);
  font-weight: 500;
}
.k-field.is-missing .k-label {
  color: var(--k-bad);
}
.k-field.is-missing .k-input,
.k-field.is-missing .k-choices {
  outline: 2px solid var(--k-bad);
  outline-offset: 2px;
  border-radius: 12px;
}
.k-input {
  display: block;
  width: 100%;
  min-height: var(--k-tap);
  padding: 11px 14px;
  border: 1.5px solid var(--k-line);
  border-radius: 12px;
  background: var(--k-card);
  color: var(--k-ink);
  font: inherit;
  font-size: 16px;
}
textarea.k-input {
  min-height: 96px;
  resize: vertical;
}
.k-input:focus {
  outline: 2px solid var(--k-brand);
  outline-offset: 1px;
  border-color: var(--k-brand);
}

.k-choices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}
.k-choices.is-tight {
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
}
.k-choice {
  min-height: 52px;
  padding: 8px 12px;
  border: 1.5px solid var(--k-line);
  border-radius: 12px;
  background: var(--k-card);
  color: var(--k-ink);
  font: inherit;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
  overflow-wrap: anywhere;
}
.k-choice small {
  display: block;
  color: var(--k-soft);
  font-weight: 500;
}
.k-choice.is-on {
  border-color: var(--k-brand);
  background: var(--k-brand);
  color: #fff;
}
.k-choice.is-on small {
  color: rgba(255, 255, 255, 0.85);
}

.k-stepper {
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.k-stepper .k-btn {
  flex: none;
  width: 56px;
  padding: 0;
  font-size: 1.5rem;
}
.k-stepper .k-input {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 1.2rem;
  font-weight: 700;
}
.k-quick {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.k-quick .k-btn {
  flex: 1;
  min-width: 64px;
  padding: 8px;
}

.k-photos {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 8px;
}
.k-photo {
  position: relative;
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  background: var(--k-bg);
}
.k-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.k-photo button {
  position: absolute;
  top: 0;
  right: 0;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 0 0 0 12px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
}
.k-photo-add {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border: 1.5px dashed var(--k-brand);
  border-radius: 12px;
  background: var(--k-picked-bg);
  color: var(--k-brand);
  font-weight: 650;
  text-align: center;
  cursor: pointer;
}
.k-photo-add input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}
.k-photo-add:focus-within {
  outline: 2px solid var(--k-brand);
}

/* ── Bottom action bar ───────────────────────────────────── */
.k-bar {
  position: sticky;
  bottom: 0;
  z-index: 15;
  display: flex;
  gap: 10px;
  padding: 10px 14px calc(10px + var(--k-bottom));
  border-top: 1px solid var(--k-line);
  background: rgba(255, 255, 255, 0.96);
}
.k-bar:empty,
.k-bar[hidden] {
  display: none;
}
.k-bar .k-btn {
  flex: 1;
  min-height: 54px;
}

/* ── Sheet (slides up from the bottom) ───────────────────── */
.k-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(10, 20, 30, 0.45);
}
.k-sheet {
  width: 100%;
  max-width: 720px;
  max-height: calc(var(--k-vh) - 40px);
  overflow-y: auto;
  padding: 8px 16px calc(16px + var(--k-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--k-card);
  animation: k-up 0.18s ease-out;
}
.k-sheet::before {
  content: '';
  display: block;
  width: 40px;
  height: 4px;
  margin: 2px auto 12px;
  border-radius: 2px;
  background: var(--k-line);
}
.k-sheet h2 {
  margin: 0 0 8px;
  font-size: 1.12rem;
}
.k-sheet > * + * {
  margin-top: 12px;
}
.k-sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
@keyframes k-up {
  from {
    transform: translateY(24px);
    opacity: 0.4;
  }
}

/* ── Toast ───────────────────────────────────────────────── */
.k-toasts {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(86px + var(--k-bottom));
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  pointer-events: none;
}
.k-toast {
  max-width: 100%;
  padding: 12px 18px;
  border-radius: 12px;
  background: #16202b;
  color: #fff;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.k-toast.is-done {
  background: var(--k-done);
}
.k-toast.is-bad {
  background: var(--k-bad);
}

/* ── Waiting / whole-page messages ───────────────────────── */
.k-wait {
  display: grid;
  place-items: center;
  min-height: 40vh;
  color: var(--k-soft);
}
.k-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--k-line);
  border-top-color: var(--k-brand);
  border-radius: 50%;
  animation: k-spin 0.8s linear infinite;
}

/* ── Sign-in and message pages ───────────────────────────── */
.k-center {
  min-height: var(--k-vh);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
  max-width: 420px;
  margin: 0 auto;
  padding: calc(20px + var(--k-top)) 20px calc(20px + var(--k-bottom));
}
.k-center h1 {
  margin: 0;
  font-size: 1.5rem;
}
.k-center form > * + * {
  margin-top: 14px;
}

@media (min-width: 720px) {
  .k-main {
    padding: 22px 20px 32px;
  }
  .k-backdrop {
    align-items: center;
  }
  .k-sheet {
    max-width: 480px;
    border-radius: 18px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
  }
}

@media print {
  .k-top,
  .k-bar,
  .k-banner,
  .k-toasts {
    display: none !important;
  }
  body.k-body {
    background: #fff;
  }
}
