/* ==========================================================================
   Light-mode overrides (TUI theme: dark is the default)
   --------------------------------------------------------------------------
   The terminal look is dark by default (:root in styles.css). The darkmode
   module still sets data-dark-mode="dark|light" on <html>; here the "light"
   preference maps to a light-terminal scheme (think white-background terminal
   profile). Only design tokens are redefined, so every element that references
   the tokens picks up the scheme automatically.

   The light palette values live in one place (--lightmode-* below) so the two
   activation paths (explicit marker + media-query fallback) can share them
   without the values drifting.
   ========================================================================== */

:root {
  --lightmode-text-primary: #343b59;
  --lightmode-text-secondary: #51577d;
  --lightmode-text-muted: #8990b3;
  --lightmode-text-inverse: #f8f8f4;
  --lightmode-bg-primary: #f8f8f4;
  --lightmode-bg-secondary: #ecece6;
  --lightmode-bg-tertiary: #e0e0d8;
  --lightmode-bg-chrome: #e9e8e0;
  --lightmode-border-light: #d8d8d0;
  --lightmode-border-medium: #b8b8ae;
  --lightmode-primary: #2e7de9;
  --lightmode-primary-hover: #1a6ce0;
  --lightmode-link: #007197;
  --lightmode-link-hover: #005a7a;

  /* Status colors + tinted backgrounds tuned for light surfaces. */
  --lightmode-success: #587539;
  --lightmode-warning: #8f5e15;
  --lightmode-error: #c6245c;
  --lightmode-status-bg: #e9f0e0;
  --lightmode-warning-bg: #f3ead8;
  --lightmode-error-bg: #f6dee4;
}

/* The token mapping. It has to appear in both the explicit-marker selector and
   the media-query fallback selector because CSS cannot reuse a declaration
   block across a bare selector and a media-gated one; the values themselves are
   single-sourced above. */
html[data-dark-mode="light"] {
  color-scheme: light;
  --color-text-primary: var(--lightmode-text-primary);
  --color-text-secondary: var(--lightmode-text-secondary);
  --color-text-muted: var(--lightmode-text-muted);
  --color-text-inverse: var(--lightmode-text-inverse);
  --color-bg-primary: var(--lightmode-bg-primary);
  --color-bg-secondary: var(--lightmode-bg-secondary);
  --color-bg-tertiary: var(--lightmode-bg-tertiary);
  --color-bg-chrome: var(--lightmode-bg-chrome);
  --color-border-light: var(--lightmode-border-light);
  --color-border-medium: var(--lightmode-border-medium);
  --color-primary: var(--lightmode-primary);
  --color-primary-hover: var(--lightmode-primary-hover);
  --color-link: var(--lightmode-link);
  --color-link-hover: var(--lightmode-link-hover);
  /* The "paper" surface becomes real white on light terminals. */
  --color-white: #ffffff;
  /* Status messages. */
  --color-success: var(--lightmode-success);
  --color-warning: var(--lightmode-warning);
  --color-error: var(--lightmode-error);
  --color-status-bg: var(--lightmode-status-bg);
  --color-warning-bg: var(--lightmode-warning-bg);
  --color-error-bg: var(--lightmode-error-bg);
}

/* Fallback when the module has not applied a marker (e.g. JavaScript
   disabled): respect an OS light preference, but never override an explicit
   marker. */
@media (prefers-color-scheme: light) {
  html:not([data-dark-mode]) {
    color-scheme: light;
    --color-text-primary: var(--lightmode-text-primary);
    --color-text-secondary: var(--lightmode-text-secondary);
    --color-text-muted: var(--lightmode-text-muted);
    --color-text-inverse: var(--lightmode-text-inverse);
    --color-bg-primary: var(--lightmode-bg-primary);
    --color-bg-secondary: var(--lightmode-bg-secondary);
    --color-bg-tertiary: var(--lightmode-bg-tertiary);
    --color-bg-chrome: var(--lightmode-bg-chrome);
    --color-border-light: var(--lightmode-border-light);
    --color-border-medium: var(--lightmode-border-medium);
    --color-primary: var(--lightmode-primary);
    --color-primary-hover: var(--lightmode-primary-hover);
    --color-link: var(--lightmode-link);
    --color-link-hover: var(--lightmode-link-hover);
    --color-white: #ffffff;
    --color-success: var(--lightmode-success);
    --color-warning: var(--lightmode-warning);
    --color-error: var(--lightmode-error);
    --color-status-bg: var(--lightmode-status-bg);
    --color-warning-bg: var(--lightmode-warning-bg);
    --color-error-bg: var(--lightmode-error-bg);
  }
}

/* ---------------------------------------------------------------------------
   CKEditor 5 — map its --ck-color-* system onto the theme tokens. Because the
   tokens themselves flip between the dark and light terminal schemes, one
   always-on mapping covers both modes. html:root outweighs CKEditor's own
   :root definitions regardless of load order.
   --------------------------------------------------------------------------- */
html:root {
  --ck-color-base-background: var(--color-bg-primary);
  --ck-color-base-foreground: var(--color-bg-secondary);
  --ck-color-base-border: var(--color-border-medium);
  --ck-color-text: var(--color-text-primary);
  --ck-color-focus-border: var(--color-primary);
  --ck-color-shadow-drop: rgba(0, 0, 0, 0.3);
  --ck-color-shadow-inner: rgba(0, 0, 0, 0.15);

  --ck-color-toolbar-background: var(--color-bg-secondary);
  --ck-color-toolbar-border: var(--color-border-medium);

  --ck-color-button-default-background: transparent;
  --ck-color-button-default-hover-background: var(--color-bg-tertiary);
  --ck-color-button-default-active-background: var(--color-border-medium);
  --ck-color-button-default-disabled-background: transparent;
  --ck-color-button-on-background: var(--color-bg-tertiary);
  --ck-color-button-on-hover-background: var(--color-border-medium);
  --ck-color-button-on-active-background: var(--color-border-medium);
  --ck-color-button-on-color: var(--color-primary);

  --ck-color-dropdown-panel-background: var(--color-bg-secondary);
  --ck-color-dropdown-panel-border: var(--color-border-medium);
  --ck-color-panel-background: var(--color-bg-secondary);
  --ck-color-panel-border: var(--color-border-medium);
  --ck-color-list-background: var(--color-bg-secondary);
  --ck-color-list-button-hover-background: var(--color-bg-tertiary);
  --ck-color-list-button-on-background: var(--color-primary);

  --ck-color-input-background: var(--color-bg-primary);
  --ck-color-input-border: var(--color-border-medium);
  --ck-color-input-text: var(--color-text-primary);

  --ck-color-tooltip-background: var(--color-bg-tertiary);
  --ck-color-tooltip-text: var(--color-text-primary);
}
