@tailwind base;
@tailwind components;
@tailwind utilities;

/* =====================================================================
   HERMES design system

   Tokens are declared on :root for light and redefined under both
   prefers-color-scheme and [data-theme], so the platform follows the
   operating system by default and still honours an explicit choice.

   Colour is never the only carrier of meaning: every status badge pairs
   its colour with a text label and a shaped dot, so the interface remains
   readable to colour-blind users and in a printed briefing pack.
   ===================================================================== */

@layer base {
  :root {
    --bg:            #f4f6f9;
    --bg-elevated:   #ffffff;
    --bg-sunken:     #eceff4;
    --border:        #dde3ec;
    --border-strong: #c3ccdb;
    --text:          #14213d;
    --text-muted:    #5b6b85;
    --text-subtle:   #8494ab;
    --brand:         #2c4262;
    --brand-contrast:#ffffff;
    --accent:        #a8802f;
    --focus:         #456691;
    --shadow:        0 1px 3px rgb(16 24 40 / 0.08);
  }

  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg:            #0f1523;
      --bg-elevated:   #161d2e;
      --bg-sunken:     #0b1120;
      --border:        #26304a;
      --border-strong: #38445f;
      --text:          #e6ebf4;
      --text-muted:    #a3b0c7;
      --text-subtle:   #75839c;
      --brand:         #9db2cd;
      --brand-contrast:#111827;
      --accent:        #dcc079;
      --focus:         #9db2cd;
      --shadow:        0 1px 3px rgb(0 0 0 / 0.5);
    }
  }

  :root[data-theme="dark"] {
    --bg:            #0f1523;
    --bg-elevated:   #161d2e;
    --bg-sunken:     #0b1120;
    --border:        #26304a;
    --border-strong: #38445f;
    --text:          #e6ebf4;
    --text-muted:    #a3b0c7;
    --text-subtle:   #75839c;
    --brand:         #9db2cd;
    --brand-contrast:#111827;
    --accent:        #dcc079;
    --focus:         #9db2cd;
    --shadow:        0 1px 3px rgb(0 0 0 / 0.5);
  }

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

  body {
    background: var(--bg);
    color: var(--text);
    @apply font-sans text-[15px] leading-relaxed antialiased;
  }

  /* A visible, high-contrast focus ring on everything keyboard-reachable.
     Removing outlines without replacing them is the single most common way
     an otherwise good admin interface becomes unusable without a mouse. */
  :where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
    border-radius: 3px;
  }

  ::placeholder { color: var(--text-subtle); opacity: 1; }

  h1, h2, h3, h4 { @apply font-semibold tracking-tight; color: var(--text); }
  h1 { @apply text-xl sm:text-2xl; }
  h2 { @apply text-lg; }
  h3 { @apply text-base; }

  a { color: inherit; }

  hr { border-color: var(--border); }

  /* Respect a reduced-motion preference rather than overriding it. */
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
      scroll-behavior: auto !important;
    }
  }

  @media print {
    .no-print { display: none !important; }
    body { background: #fff; color: #000; }
    .card { border: 1px solid #ccc !important; box-shadow: none !important; }
  }
}

@layer components {

  /* --- Layout ------------------------------------------------------- */
  .app-shell   { @apply min-h-screen flex; }
  .app-main    { @apply flex-1 min-w-0 flex flex-col; }
  .page        { @apply px-4 py-5 sm:px-6 lg:px-8 max-w-[1600px] w-full mx-auto; }
  .page-header { @apply flex flex-wrap items-start justify-between gap-3 mb-5; }
  .page-title  { @apply text-xl sm:text-2xl font-semibold tracking-tight; }
  .page-sub    { @apply text-sm mt-0.5; color: var(--text-muted); }

  /* --- Surfaces ----------------------------------------------------- */
  .card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    @apply rounded-xl;
  }
  .card-header { @apply flex items-center justify-between gap-3 px-4 py-3 sm:px-5;
                 border-bottom: 1px solid var(--border); }
  .card-title  { @apply text-sm font-semibold uppercase tracking-wide; color: var(--text-muted); }
  .card-body   { @apply p-4 sm:p-5; }
  .card-footer { @apply px-4 py-3 sm:px-5; border-top: 1px solid var(--border); background: var(--bg-sunken); }

  /* --- Buttons ------------------------------------------------------ */
  .btn {
    @apply inline-flex items-center justify-center gap-2 rounded-lg px-3.5 py-2
           text-sm font-medium transition-colors select-none;
    /* 40px minimum target: officers use this on a phone, one-handed,
       often in a hurry. */
    min-height: 2.5rem;
    border: 1px solid transparent;
  }
  .btn:disabled, .btn[aria-disabled="true"] { @apply opacity-50 cursor-not-allowed; }

  .btn-primary   { background: var(--brand); color: var(--brand-contrast); }
  .btn-primary:hover:not(:disabled) { filter: brightness(1.12); }

  .btn-secondary { background: var(--bg-elevated); color: var(--text); border-color: var(--border-strong); }
  .btn-secondary:hover:not(:disabled) { background: var(--bg-sunken); }

  .btn-ghost     { background: transparent; color: var(--text-muted); }
  .btn-ghost:hover:not(:disabled) { background: var(--bg-sunken); color: var(--text); }

  .btn-danger    { @apply bg-rose-600 text-white; }
  .btn-danger:hover:not(:disabled) { @apply bg-rose-700; }

  .btn-sm { @apply px-2.5 py-1.5 text-xs; min-height: 2rem; }
  .btn-lg { @apply px-5 py-2.5 text-base; min-height: 2.875rem; }
  .btn-block { @apply w-full; }

  /* --- Forms -------------------------------------------------------- */
  .field       { @apply mb-4; }
  .label       { @apply block text-sm font-medium mb-1.5; color: var(--text); }
  .label-hint  { @apply block text-xs font-normal mt-0.5; color: var(--text-subtle); }
  .required    { @apply text-rose-600; }

  .input, .select, .textarea {
    @apply w-full rounded-lg px-3 py-2 text-sm transition-colors;
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    color: var(--text);
    min-height: 2.5rem;
  }
  .input:focus, .select:focus, .textarea:focus {
    border-color: var(--focus);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 18%, transparent);
    outline: none;
  }
  .textarea { @apply py-2 leading-relaxed; min-height: 6rem; }
  .select   { @apply pr-8; }
  .input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] {
    @apply border-rose-500;
  }
  .input:disabled, .select:disabled, .textarea:disabled {
    background: var(--bg-sunken); @apply cursor-not-allowed opacity-70;
  }

  .field-error { @apply mt-1.5 text-xs text-rose-600 flex items-start gap-1; }
  .checkbox, .radio {
    @apply h-4 w-4 rounded shrink-0;
    border: 1px solid var(--border-strong);
    accent-color: var(--brand);
  }
  .radio { @apply rounded-full; }
  .check-row { @apply flex items-start gap-2.5 py-1.5; }

  .form-grid { @apply grid grid-cols-1 sm:grid-cols-2 gap-x-5 gap-y-1; }
  .form-grid .col-span-full { @apply sm:col-span-2; }

  /* --- Tables ------------------------------------------------------- */
  .table-wrap { @apply overflow-x-auto -mx-4 sm:mx-0; }
  .table      { @apply w-full text-sm border-collapse; min-width: 40rem; }
  .table thead th {
    @apply px-3 py-2.5 text-left text-xs font-semibold uppercase tracking-wide whitespace-nowrap;
    color: var(--text-muted);
    background: var(--bg-sunken);
    border-bottom: 1px solid var(--border);
  }
  .table tbody td { @apply px-3 py-2.5 align-middle; border-bottom: 1px solid var(--border); }
  .table tbody tr:hover td { background: var(--bg-sunken); }
  .table tbody tr:last-child td { border-bottom: 0; }
  .table .num { @apply text-right tabular-nums; }
  .th-sort { @apply inline-flex items-center gap-1 hover:underline; }
  .th-sort.is-sorted { color: var(--text); }

  /* Cards instead of rows below the table's minimum width — a 12-column
     register is unreadable on a phone, and officers file from phones. */
  .row-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    @apply rounded-lg p-3.5 mb-2.5;
  }

  /* --- Badges ------------------------------------------------------- */
  .badge {
    @apply inline-flex items-center gap-1.5 rounded-full px-2.5 py-0.5
           text-xs font-medium whitespace-nowrap;
    border: 1px solid transparent;
  }
  .badge::before {
    content: '';
    @apply inline-block h-1.5 w-1.5 rounded-full shrink-0;
    background: currentColor;
  }
  /* Square marker for terminal states, round for in-flight: the shape
     carries the distinction even in greyscale. */
  .badge-complete::before, .badge-published::before, .badge-approved::before,
  .badge-archived::before, .badge-cancelled::before { @apply rounded-none; }

  .badge-draft        { @apply bg-slate-100  text-slate-700  border-slate-200; }
  .badge-issued       { @apply bg-sky-100    text-sky-800    border-sky-200; }
  .badge-acknowledged { @apply bg-teal-100   text-teal-800   border-teal-200; }
  .badge-progress     { @apply bg-violet-100 text-violet-800 border-violet-200; }
  .badge-review       { @apply bg-amber-100  text-amber-800  border-amber-200; }
  .badge-approved     { @apply bg-emerald-100 text-emerald-800 border-emerald-200; }
  .badge-published    { @apply bg-emerald-100 text-emerald-800 border-emerald-200; }
  .badge-complete     { @apply bg-emerald-100 text-emerald-800 border-emerald-200; }
  .badge-archived     { @apply bg-zinc-100   text-zinc-600   border-zinc-200; }
  .badge-cancelled    { @apply bg-zinc-100   text-zinc-500   border-zinc-200; }
  .badge-overdue      { @apply bg-rose-100   text-rose-800   border-rose-200; }
  .badge-urgent       { @apply bg-rose-100   text-rose-800   border-rose-200; }
  .badge-high         { @apply bg-orange-100 text-orange-800 border-orange-200; }
  .badge-normal       { @apply bg-slate-100  text-slate-700  border-slate-200; }
  .badge-low          { @apply bg-slate-50   text-slate-500  border-slate-200; }

  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .badge { @apply bg-opacity-15 border-opacity-30; }
  }

  /* --- Alerts ------------------------------------------------------- */
  .alert { @apply flex items-start gap-3 rounded-lg px-4 py-3 text-sm mb-4; border: 1px solid transparent; }
  .alert-success { @apply bg-emerald-50 text-emerald-900 border-emerald-200; }
  .alert-error   { @apply bg-rose-50    text-rose-900    border-rose-200; }
  .alert-warning { @apply bg-amber-50   text-amber-900   border-amber-200; }
  .alert-info    { @apply bg-sky-50     text-sky-900     border-sky-200; }

  /* --- Navigation --------------------------------------------------- */
  .sidebar {
    @apply w-64 shrink-0 flex-col hidden lg:flex;
    background: var(--bg-elevated);
    border-right: 1px solid var(--border);
  }
  .nav-section { @apply px-3 pt-4 pb-1 text-2xs font-semibold uppercase tracking-widest;
                 color: var(--text-subtle); }
  .nav-item {
    @apply flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors;
    color: var(--text-muted);
    min-height: 2.5rem;
  }
  .nav-item:hover { background: var(--bg-sunken); color: var(--text); }
  .nav-item.is-active { background: var(--brand); color: var(--brand-contrast); }
  .nav-item .nav-icon { @apply h-[18px] w-[18px] shrink-0; }
  .nav-count { @apply ml-auto text-2xs font-semibold rounded-full px-1.5 py-0.5;
               background: var(--bg-sunken); color: var(--text-muted); }
  .nav-item.is-active .nav-count { background: rgb(255 255 255 / 0.2); color: inherit; }

  /* Bottom tab bar, phone only. */
  .tabbar {
    @apply fixed inset-x-0 bottom-0 z-40 flex lg:hidden;
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar-item { @apply flex-1 flex flex-col items-center justify-center gap-0.5 py-2 text-2xs;
                 color: var(--text-subtle); min-height: 3.25rem; }
  .tabbar-item.is-active { color: var(--brand); }

  .topbar {
    @apply sticky top-0 z-30 flex items-center gap-3 px-4 py-2.5 sm:px-6;
    background: color-mix(in srgb, var(--bg-elevated) 92%, transparent);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(8px);
  }

  /* --- Stat tiles --------------------------------------------------- */
  .stat { @apply card p-4 flex flex-col gap-1; }
  .stat-label { @apply text-xs font-medium uppercase tracking-wide; color: var(--text-muted); }
  .stat-value { @apply text-2xl font-semibold tabular-nums tracking-tight; }
  .stat-meta  { @apply text-xs; color: var(--text-subtle); }

  /* --- Misc --------------------------------------------------------- */
  .empty { @apply text-center py-12 px-4; }
  .empty-title { @apply text-sm font-medium; color: var(--text); }
  .empty-text  { @apply text-sm mt-1 mx-auto max-w-sm; color: var(--text-muted); }

  .divider { @apply my-4; border-top: 1px solid var(--border); }
  .kbd { @apply inline-block rounded px-1.5 py-0.5 text-2xs font-mono;
         background: var(--bg-sunken); border: 1px solid var(--border-strong); }
  .hl { @apply bg-accent-200 text-ink-900 rounded-sm px-0.5; }

  /* Search typeahead. Keyboard highlight and mouse hover deliberately share
     one appearance, so arrowing down looks exactly like pointing at it. */
  .suggest-item { color: var(--text); }
  .suggest-item:hover,
  .suggest-item.is-active { background: var(--bg-sunken); }

  /* The row an inline edit panel is currently working on. Marked with a bar
     as well as a tint, so it reads without relying on colour. */
  tr.is-selected > td { background: var(--bg-sunken); }
  tr.is-selected > td:first-child { box-shadow: inset 3px 0 0 var(--brand); }

  /* The two-factor enrolment QR. Rendered at a generous module size for
     the camera, then allowed to scale down — never up — so it fits a phone
     without ever being enlarged past its natural crispness. */
  .qr-frame > svg { max-width: 100%; height: auto; display: block; }

  /* Export format menu. Built on <details>, so the default disclosure
     triangle has to be suppressed in both WebKit and everything else. */
  .export-menu > summary::-webkit-details-marker { display: none; }
  .export-menu > summary::marker { content: ''; }
  .export-menu[open] > summary { background: var(--bg-sunken); }

  .avatar { @apply inline-flex items-center justify-center rounded-full font-semibold shrink-0
                   text-xs uppercase; background: var(--bg-sunken); color: var(--text-muted); }

  .chip { @apply inline-flex items-center gap-1 rounded-md px-2 py-0.5 text-xs;
          background: var(--bg-sunken); color: var(--text-muted); border: 1px solid var(--border); }

  /* --- Rich text from the repository --------------------------------
     Styling for the sanitised HTML of a press statement or briefing note.
     Scoped to .prose-hermes so it cannot leak into the rest of the UI, and
     limited to the tags App\Core\Sanitiser actually permits. */
  .prose-hermes > * + * { margin-top: 0.85em; }
  .prose-hermes h2 { @apply text-base font-semibold mt-6 mb-2; }
  .prose-hermes h3 { @apply text-sm font-semibold mt-5 mb-1.5; }
  .prose-hermes h4 { @apply text-sm font-semibold mt-4 mb-1; }
  .prose-hermes p  { @apply leading-relaxed; }
  .prose-hermes ul { @apply list-disc pl-5 space-y-1; }
  .prose-hermes ol { @apply list-decimal pl-5 space-y-1; }
  .prose-hermes li { @apply leading-relaxed; }
  .prose-hermes a  { color: var(--brand); @apply underline underline-offset-2; }
  .prose-hermes strong { @apply font-semibold; }
  .prose-hermes blockquote {
    @apply pl-4 italic;
    border-left: 3px solid var(--border-strong);
    color: var(--text-muted);
  }
  .prose-hermes hr { @apply my-6; border-color: var(--border); }
  .prose-hermes code { @apply text-xs font-mono rounded px-1 py-0.5; background: var(--bg-sunken); }
  .prose-hermes pre  { @apply text-xs font-mono rounded-lg p-3 overflow-x-auto; background: var(--bg-sunken); }
  /* A pasted table must scroll inside itself rather than pushing the page wide. */
  .prose-hermes table { @apply w-full text-sm my-4 block overflow-x-auto; border-collapse: collapse; }
  .prose-hermes th, .prose-hermes td { @apply px-3 py-2 text-left align-top; border: 1px solid var(--border); }
  .prose-hermes th { @apply font-semibold; background: var(--bg-sunken); }

  .skip-link {
    @apply absolute left-2 top-2 z-50 -translate-y-20 rounded-lg px-3 py-2 text-sm font-medium
           focus:translate-y-0 transition-transform;
    background: var(--brand); color: var(--brand-contrast);
  }
}

@layer utilities {
  .sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border-width: 0;
  }
  .tabular { font-variant-numeric: tabular-nums; }
  .scrollbar-thin { scrollbar-width: thin; }
}
