/* ==========================================================================
   KPG Incident Tracker — Design System
   Warm, calm, trustworthy. Inspired by kaleidoscopeplus.org.uk (blue + gold).
   No build step — plain CSS with custom properties.
   ========================================================================== */

:root {
  /* Surfaces */
  --color-bg: #F7F3EA;
  --color-bg-alt: #EFE7D6;
  --color-surface: #FFFFFF;
  --color-surface-alt: #FCFAF4;
  --color-border: #E3D9C3;
  --color-border-strong: #D2C4A5;

  /* Text */
  --color-text: #241F19;
  --color-text-muted: #6B6152;
  --color-text-faint: #96897420;

  /* Brand */
  --color-primary: #1E3A5F;
  --color-primary-dark: #142943;
  --color-primary-light: #E8EFF6;
  --color-accent: #E7A335;
  --color-accent-dark: #C6842078;
  --color-accent-dark: #C68420;
  --color-accent-light: #FBEACB;

  /* Semantic */
  --color-danger: #B24A2C;
  --color-danger-light: #F6E1D6;
  --color-danger-dark: #8A3821;
  --color-success: #4F7A56;
  --color-success-light: #E4EEE1;
  --color-info: #33698C;
  --color-info-light: #E1EEF4;

  /* Chart / categorical palette (harmonious, muted) */
  --chart-1: #1E3A5F;
  --chart-2: #E7A335;
  --chart-3: #B24A2C;
  --chart-4: #4F7A56;
  --chart-5: #6E4C7E;
  --chart-6: #33698C;
  --chart-7: #A6762C;
  --chart-8: #7A5340;

  /* Radii & shadow */
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 24px;
  --shadow-sm: 0 1px 2px rgba(36, 31, 25, 0.07);
  --shadow-md: 0 10px 28px -12px rgba(30, 58, 95, 0.22);
  --shadow-lg: 0 24px 56px -20px rgba(30, 58, 95, 0.30);

  /* Type */
  --font-display: 'Fraunces', Georgia, 'Times New Roman', serif;
  --font-body: 'Public Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* -------------------------------------------------------------------- */
/* Reset                                                                 */
/* -------------------------------------------------------------------- */

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

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

body {
  margin: 0;
  min-height: 100vh;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.15;
  margin: 0 0 0.5em;
  color: var(--color-primary-dark);
  letter-spacing: -0.01em;
}

h1 { font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.5rem); }
h2 { font-size: clamp(1.4rem, 1.2rem + 0.8vw, 1.85rem); }
h3 { font-size: 1.25rem; }

p { margin: 0 0 1em; color: var(--color-text); }
a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

img, svg { max-width: 100%; display: block; }

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

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 5rem;
}

/* -------------------------------------------------------------------- */
/* Decorative bubbles (brand motif — used sparingly)                    */
/* -------------------------------------------------------------------- */

.bubble-field {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.bubble {
  position: absolute;
  border-radius: 50%;
  filter: blur(0px);
  opacity: 0.9;
}

.bubble--blue { background: var(--color-primary); }
.bubble--gold { background: var(--color-accent); }

/* -------------------------------------------------------------------- */
/* Header / Nav                                                         */
/* -------------------------------------------------------------------- */

.site-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.site-header__inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  color: var(--color-primary-dark);
}
.brand:hover { text-decoration: none; }

.brand__mark {
  width: 34px;
  height: 34px;
  flex: none;
  object-fit: contain;
}

.brand__text { display: flex; flex-direction: column; line-height: 1.1; }
.brand__name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
}
.brand__sub {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  margin-left: 0.5rem;
}
.main-nav a:not(.button) {
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 0.94rem;
}
.main-nav a:not(.button):hover { color: var(--color-primary); text-decoration: none; }

.nav-user {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.9rem;
}
.nav-user__name {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--color-text-muted);
}
.nav-user__logout button {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0;
}
.nav-user__logout button:hover { color: var(--color-danger); text-decoration: underline; }

/* -------------------------------------------------------------------- */
/* Buttons                                                               */
/* -------------------------------------------------------------------- */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 1.3rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.95rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
}
.button:hover { text-decoration: none; }
.button:active { transform: translateY(1px); }

.button--primary {
  background: var(--color-primary);
  color: #fff;
  box-shadow: var(--shadow-sm);
}
.button--primary:hover { background: var(--color-primary-dark); box-shadow: var(--shadow-md); }

.button--accent {
  background: var(--color-accent);
  color: var(--color-primary-dark);
}
.button--accent:hover { background: var(--color-accent-dark); }

.button--ghost {
  background: transparent;
  color: var(--color-primary);
  border-color: var(--color-border-strong);
}
.button--ghost:hover { background: var(--color-primary-light); }

.button--danger {
  background: var(--color-danger);
  color: #fff;
}
.button--danger:hover { background: var(--color-danger-dark); }

.button--block { width: 100%; }
.button--sm { padding: 0.45rem 0.9rem; font-size: 0.85rem; }

/* -------------------------------------------------------------------- */
/* Cards                                                                 */
/* -------------------------------------------------------------------- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.75rem;
}

.card--elevated { box-shadow: var(--shadow-md); }

.card-grid {
  display: grid;
  gap: 1.25rem;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

/* -------------------------------------------------------------------- */
/* Forms                                                                 */
/* -------------------------------------------------------------------- */

.field { margin-bottom: 1.4rem; }
.field label,
.field legend {
  display: block;
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--color-primary-dark);
  margin-bottom: 0.4rem;
  padding: 0;
}
.field .hint {
  font-size: 0.82rem;
  color: var(--color-text-muted);
  margin: -0.15rem 0 0.5rem;
}

.input, .select, textarea.input {
  width: 100%;
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--color-border-strong);
  background: var(--color-surface-alt);
  font-size: 0.98rem;
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input:focus, .select:focus, textarea.input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

textarea.input { resize: vertical; min-height: 5rem; }

fieldset { border: none; padding: 0; margin: 0 0 1.4rem; }

.option-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

/* Checkbox / radio rendered as friendly pill selectors */
.option-pill {
  position: relative;
}
.option-pill input {
  position: absolute;
  opacity: 0;
  width: 1px; height: 1px;
}
.option-pill span {
  display: inline-flex;
  align-items: center;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 1.5px solid var(--color-border-strong);
  background: var(--color-surface-alt);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.15s ease;
}
.option-pill input:checked + span {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
.option-pill input:focus-visible + span {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}
.option-pill:has(input:checked) span { box-shadow: var(--shadow-sm); }

.form-section {
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 1.75rem;
  margin-bottom: 1.75rem;
}
.form-section:last-of-type { border-bottom: none; }
.field-error {
  color: var(--color-danger-dark);
  font-size: 0.82rem;
  font-weight: 600;
  margin: 0.35rem 0 0;
}
.field--error .input,
.field--error .select,
.field--error textarea.input {
  border-color: var(--color-danger);
}
.field--error .option-pill span { border-color: var(--color-danger); }

.form-section__eyebrow {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-accent-dark);
  margin-bottom: 0.3rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.1rem 1.5rem;
  margin: 0;
}
.detail-grid dt {
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 700;
  color: var(--color-text-muted);
  margin-bottom: 0.2rem;
}
.detail-grid dd {
  margin: 0;
  font-weight: 600;
  color: var(--color-text);
}

/* -------------------------------------------------------------------- */
/* Flash messages                                                       */
/* -------------------------------------------------------------------- */

.flash-stack {
  max-width: 1080px;
  margin: 1.25rem auto 0;
  padding: 0 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.flash {
  padding: 0.85rem 1.1rem;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.92rem;
  border: 1px solid transparent;
}
.flash--success { background: var(--color-success-light); color: var(--color-success); border-color: #cfe0cb; }
.flash--error { background: var(--color-danger-light); color: var(--color-danger-dark); border-color: #ecc7b4; }
.flash--info { background: var(--color-info-light); color: var(--color-info); border-color: #c6dfe9; }

/* -------------------------------------------------------------------- */
/* Badges (status / outcome / escalation)                               */
/* -------------------------------------------------------------------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
}
.badge--neutral { background: var(--color-bg-alt); color: var(--color-text-muted); }
.badge--accent { background: var(--color-accent-light); color: var(--color-accent-dark); }
.badge--danger { background: var(--color-danger-light); color: var(--color-danger-dark); }
.badge--success { background: var(--color-success-light); color: var(--color-success); }
.badge--info { background: var(--color-info-light); color: var(--color-info); }

/* -------------------------------------------------------------------- */
/* Tables                                                                */
/* -------------------------------------------------------------------- */

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  /* Scroll-shadow hint: fades in from the edges only once there's more
     content to reveal in that direction (the two "local" layers scroll
     with the content, the two "scroll" layers stay fixed to the viewport).
     Relies on table cells not painting their own opaque background —
     true here, but NOT for the heatmap, whose cells are always opaque;
     that gets an explicit text hint instead (see .heatmap-hint). */
  background-image:
    linear-gradient(to right, var(--color-surface) 0%, rgba(255,255,255,0) 8%),
    linear-gradient(to left, var(--color-surface) 0%, rgba(255,255,255,0) 8%),
    linear-gradient(to right, rgba(36,31,25,0.14), rgba(36,31,25,0)),
    linear-gradient(to left, rgba(36,31,25,0.14), rgba(36,31,25,0));
  background-repeat: no-repeat;
  background-color: var(--color-surface);
  background-size: 48px 100%, 48px 100%, 16px 100%, 16px 100%;
  background-position: 0 0, 100% 0, 0 0, 100% 0;
  background-attachment: local, local, scroll, scroll;
}

.heatmap-scroll {
  overflow-x: auto;
  border-radius: var(--radius-md);
}
.heatmap-hint {
  display: block;
  font-size: 0.78rem;
  color: var(--color-text-muted);
  margin: 0 0 0.6rem;
}
table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}
.data-table th, .data-table td {
  text-align: left;
  padding: 0.55rem 0.65rem;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}
.data-table th {
  background: var(--color-bg-alt);
  color: var(--color-text-muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-weight: 700;
}
.data-table tbody tr:hover { background: var(--color-surface-alt); }
.data-table tbody tr:last-child td { border-bottom: none; }

/* Columns that help on desktop but aren't essential for a quick scan —
   drop them below ~700px so the columns that matter (and any row
   actions) stay in view instead of scrolling off with no visible hint. */
@media (max-width: 700px) {
  .data-table .col-optional { display: none; }
}

/* -------------------------------------------------------------------- */
/* Charts / stats                                                        */
/* -------------------------------------------------------------------- */

:root {
  --chart-blue-100: #E4E7EC;
  --chart-blue-300: #B7C0CC;
  --chart-blue-500: #8393A7;
  --chart-blue-700: #4B617F;
  --chart-blue-900: #1E3A5F;
  --chart-critical: #B24A2C;
  --chart-neutral: #8C7A5C;
  --chart-grid: #E3D9C3;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 1.5rem;
  align-items: start;
}

.chart-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
}
.chart-card h3 { margin: 0 0 0.2rem; }
.chart-card .hint { margin-top: 0; margin-bottom: 1rem; }
.chart-card canvas { max-width: 100%; }
.chart-card--wide { grid-column: 1 / -1; }

details.table-toggle { margin-top: 1rem; }
details.table-toggle summary {
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--color-primary);
  list-style: none;
}
details.table-toggle summary::-webkit-details-marker { display: none; }
details.table-toggle summary::before { content: "▸ "; }
details.table-toggle[open] summary::before { content: "▾ "; }
details.table-toggle table { margin-top: 0.75rem; font-size: 0.85rem; }

table.heatmap-table {
  border-collapse: collapse;
  font-size: 0.8rem;
  min-width: 100%;
}
.heatmap-table th, .heatmap-table td {
  padding: 0.5rem 0.65rem;
  text-align: center;
  border: 1px solid var(--color-surface);
  white-space: nowrap;
}
.heatmap-table thead th {
  background: var(--color-bg-alt);
  color: var(--color-text-muted);
  font-weight: 700;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  vertical-align: bottom;
}
.heatmap-table th.heatmap-row-label {
  text-align: left;
  background: var(--color-bg-alt);
  color: var(--color-text);
  font-weight: 700;
  font-size: 0.82rem;
  white-space: normal;
}
.heatmap-table td.heatmap-cell { font-weight: 700; color: var(--color-primary-dark); }
.heatmap-table td.heatmap-cell--zero { color: var(--color-text-faint); font-weight: 400; }

/* -------------------------------------------------------------------- */
/* Admin                                                                 */
/* -------------------------------------------------------------------- */

.admin-nav {
  display: flex;
  gap: 1.75rem;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 1.75rem;
  padding-bottom: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* Same scroll-shadow technique as .table-wrap: fades in only once
     there's more nav to reveal in that direction, so a narrow screen
     (5 tabs don't fit under ~420px) doesn't just silently truncate
     "Organisations"/"Users" with no hint they're still reachable. */
  background-image:
    linear-gradient(to right, var(--color-bg) 0%, rgba(247,243,234,0) 8%),
    linear-gradient(to left, var(--color-bg) 0%, rgba(247,243,234,0) 8%),
    linear-gradient(to right, rgba(36,31,25,0.14), rgba(36,31,25,0)),
    linear-gradient(to left, rgba(36,31,25,0.14), rgba(36,31,25,0));
  background-repeat: no-repeat;
  background-color: var(--color-bg);
  background-size: 32px 100%, 32px 100%, 12px 100%, 12px 100%;
  background-position: 0 0, 100% 0, 0 0, 100% 0;
  background-attachment: local, local, scroll, scroll;
}
.admin-nav a {
  display: inline-block;
  flex: none;
  white-space: nowrap;
  padding: 0 0 0.85rem;
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--color-text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.admin-nav a:hover { color: var(--color-primary); text-decoration: none; }
.admin-nav a.is-active { color: var(--color-primary); border-bottom-color: var(--color-accent); }

.stat-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 1.25rem 1.4rem;
}
.stat-tile__value {
  font-family: var(--font-display);
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-primary-dark);
  line-height: 1;
}
.stat-tile__label {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  margin-top: 0.4rem;
}
.stat-tile--danger .stat-tile__value { color: var(--color-danger); }

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding: 1.1rem 1.25rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}
.filter-bar .field { margin-bottom: 0; min-width: 160px; }
.filter-bar .field label { margin-bottom: 0.3rem; }

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}
.checkbox-field input { width: 17px; height: 17px; accent-color: var(--color-primary); }
.checkbox-field label { margin: 0; font-weight: 600; }

.table-actions { display: flex; gap: 0.6rem; }

.case-note {
  padding: 1rem 0;
  border-bottom: 1px solid var(--color-border);
}
.case-note:last-child { border-bottom: none; }
.case-note__meta {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  font-weight: 700;
  margin-bottom: 0.3rem;
}

/* -------------------------------------------------------------------- */
/* Auth screens                                                         */
/* -------------------------------------------------------------------- */

.auth-shell {
  position: relative;
  min-height: calc(100vh - 130px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem 1.5rem;
  overflow: hidden;
}

.auth-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 2.5rem 2.25rem;
  border: 1px solid var(--color-border);
}

.auth-card h1 { margin-bottom: 0.25rem; }
.auth-card .lede {
  color: var(--color-text-muted);
  font-size: 0.95rem;
  margin-bottom: 1.75rem;
}

/* -------------------------------------------------------------------- */
/* Footer                                                                */
/* -------------------------------------------------------------------- */

.site-footer {
  border-top: 1px solid var(--color-border);
  margin-top: 3rem;
}
.site-footer__inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--color-text-muted);
}

/* -------------------------------------------------------------------- */
/* Responsive                                                            */
/* -------------------------------------------------------------------- */

@media (max-width: 720px) {
  .site-header__inner { flex-wrap: wrap; }
  .main-nav { order: 3; width: 100%; gap: 1rem; }
  .nav-user { margin-left: auto; }
}
