/* ---------------------------------------------------------------------------
   Lexicarta — base stylesheet
   ---------------------------------------------------------------------------
   INVARIANT 12 — RTL IS BUILT IN STAGE 1, before any RTL language is enabled.
   Every directional property here is LOGICAL (margin-inline-start, not
   margin-left; padding-block, not padding-top). Retrofitting RTL into a
   force-directed graph is painful; writing it this way from the start is free.

   INVARIANT 10 — three edge hues, no more. The palette below is BUILD-SPEC
   § 10's validated set, which passes colour-vision-deficiency separation for
   all-pairs viewing. A fourth hue fails that validation. Never hardcode a hex
   in a component — use the tokens.
--------------------------------------------------------------------------- */

:root {
  color-scheme: light;

  --surface-1:      #fcfcfb;
  --surface-2:      #f4f4f1;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --gridline:       #e1e0d9;

  --edge-taxonomy:    #2a78d6;  /* blue   */
  --edge-composition: #eb6834;  /* orange */
  --edge-association: #1baf7a;  /* aqua   */
  --edge-translation: #898781;  /* neutral, dashed — translations take no hue */

  --focus: #2a78d6;

  --measure: 68ch;
  --measure-wide: 1400px;
  /* The canvas is a panel on the page, not the page. Capped narrower than
     --measure-wide so a wide monitor keeps gutters either side: they are
     where the wheel scrolls the page, and over the canvas the wheel zooms. */
  --measure-canvas: 1120px;
  --step-0: 1rem;
  --step-1: 1.25rem;
  --step-2: 1.65rem;
  --step-3: 2.2rem;
}

/* Dark mode is a SELECTED palette, not a filter or an opacity trick. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --gridline:       #2c2c2a;

    --edge-taxonomy:    #3987e5;
    --edge-composition: #d95926;
    --edge-association: #199e70;
    --edge-translation: #898781;

    --focus: #3987e5;
  }
}

/* Repeated so an explicit toggle wins over the OS preference. */
:root[data-theme="dark"] {
  color-scheme: dark;

  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --gridline:       #2c2c2a;

  --edge-taxonomy:    #3987e5;
  --edge-composition: #d95926;
  --edge-association: #199e70;
  --edge-translation: #898781;

  --focus: #3987e5;
}

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

body {
  margin: 0;
  background: var(--surface-1);
  color: var(--text-primary);
  font: var(--step-0)/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4 { line-height: 1.25; text-wrap: balance; }
h1 { font-size: var(--step-3); margin-block: 0 0.5rem; }
h2 { font-size: var(--step-2); }
h3 { font-size: var(--step-1); }

a { color: var(--edge-taxonomy); text-underline-offset: 0.15em; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 2px;
}

.muted { color: var(--text-muted); }

.visually-hidden {
  position: absolute;
  inline-size: 1px; block-size: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}

.skip-link {
  position: absolute;
  inset-block-start: -3rem;
  inset-inline-start: 1rem;
  background: var(--surface-2);
  padding: 0.5rem 1rem;
  z-index: 10;
  transition: inset-block-start 120ms ease;
}
.skip-link:focus { inset-block-start: 0.5rem; }

/* --- top bar ------------------------------------------------------------ */

.topbar {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;            /* 16px side gutter, per the design rules */
  border-block-end: 1px solid var(--gridline);
  background: var(--surface-1);
}

/* Logo top-left: conventional, and where users look for the home link. */
.brand {
  font-weight: 700;
  font-size: var(--step-1);
  color: var(--text-primary);
  text-decoration: none;
}

/* Capped: no word or phrase needs a field that spans the viewport, and a
   full-width input made the bar look like the page's main content. */
.search { display: flex; gap: 0.5rem; flex: 1 1 16rem; max-inline-size: 30rem; }
.langs { margin-inline-start: auto; }
.search input {
  flex: 1;
  min-inline-size: 0;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--gridline);
  border-radius: 6px;
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
}
.search button {
  padding: 0.6rem 1rem;
  border: 1px solid var(--gridline);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-primary);
  font: inherit;
  cursor: pointer;
  min-block-size: 44px;             /* 44x44 minimum touch target */
  min-inline-size: 44px;
}

.langs { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.langs a { text-decoration: none; }
.langs a[aria-current] { font-weight: 700; text-decoration: underline; }

/* --- content ------------------------------------------------------------ */

main { padding: 1.5rem 0; }

/* Text stays at a readable measure; the canvas does not. Each block opts in
   rather than the page imposing one width on everything. */
.measure {
  max-inline-size: var(--measure);
  margin-inline: auto;
  padding-inline: 1rem;
}
.measure-wide {
  max-inline-size: var(--measure-wide);
  margin-inline: auto;
}

.hero, .prose { max-inline-size: var(--measure); margin-inline: auto; padding-inline: 1rem; }
.lede { font-size: var(--step-1); color: var(--text-secondary); }

.word-head .pos { margin-block-start: 0; }

/* The word rides on the canvas, top-inline-start, on a plate that lets the
   graph show through. A full-width masthead cost a fifth of a large screen
   before the graph started. Still the page's <h1> — only its placement moved. */
.word-head--onstage {
  position: absolute;
  z-index: 2;
  inset-block-start: 3.6rem;
  inset-inline-start: 0.9rem;
  padding: 0.3rem 0.8rem 0.4rem;
  border-radius: 9px;
  background: color-mix(in srgb, var(--surface-1) 78%, transparent);
  /* Never eat a click meant for a node underneath it. */
  pointer-events: none;
}
.word-head--onstage h1 {
  margin-block: 0;
  font-size: var(--step-2);
  line-height: 1.1;
}
.word-head--onstage .pos {
  margin-block: 0.1rem 0;
  font-size: 0.82rem;
}
.sense { padding-block: 1rem; border-block-start: 1px solid var(--gridline); }
.definition { color: var(--text-secondary); }
.example q { color: var(--text-muted); font-style: italic; }

.relgroup { margin-block-start: 1rem; }
.relgroup h4 {
  margin-block-end: 0.5rem;
  padding-inline-start: 0.6rem;
  /* Colour never carries meaning alone — the group is also named in the
     heading text. This border is reinforcement, not the signal. */
  border-inline-start: 4px solid var(--group-hue, var(--text-muted));
}
.relgroup--taxonomy    { --group-hue: var(--edge-taxonomy); }
.relgroup--composition { --group-hue: var(--edge-composition); }
.relgroup--association { --group-hue: var(--edge-association); }

.neighbours { list-style: none; padding-inline-start: 0; margin: 0; display: grid; gap: 0.35rem; }
.neighbours li { display: flex; gap: 0.5rem; align-items: baseline; flex-wrap: wrap; }
.reltype { font-size: 0.85em; }

/* Gap nodes: visibly secondary, never illegible. */
.neighbours li.is-gap { opacity: 0.72; }
.gap-label {
  border-block-end: 1px dashed var(--text-muted);
  color: var(--text-secondary);
}
.chip {
  font-size: 0.75em;
  border: 1px solid var(--gridline);
  border-radius: 999px;
  padding-inline: 0.45em;
}

.more { font-size: 0.9em; margin-block-start: 0.4rem; }

.table-view { margin-block-start: 2rem; }
.table-view table { inline-size: 100%; border-collapse: collapse; font-size: 0.92em; }
.table-view th, .table-view td {
  text-align: start;                 /* logical: flips under RTL automatically */
  padding: 0.4rem 0.5rem;
  border-block-end: 1px solid var(--gridline);
}

.footer {
  margin-block-start: 3rem;
  padding: 1.5rem 1rem;
  border-block-start: 1px solid var(--gridline);
  color: var(--text-muted);
}
.footer nav { display: flex; gap: 1rem; flex-wrap: wrap; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

@media (forced-colors: active) {
  .relgroup h4 { border-inline-start-color: CanvasText; }
  .gap-label { border-block-end-color: CanvasText; }
}

/* ---------------------------------------------------------------------------
   Graph canvas
   ---------------------------------------------------------------------------
   Node tokens live here rather than in the JS so light mode, dark mode and
   forced-colors are all defined in ONE place. graph.js reads them back out
   with getComputedStyle.

   Invariant 10 still holds: the three edge hues above are the whole budget.
   Part of speech is encoded by node SHAPE, and relation type by LINE STYLE
   and arrowhead — never by a fourth hue.
--------------------------------------------------------------------------- */

:root {
  --node-fill:        #e9e9e3;
  --node-stroke:      #c7c6bd;
  --node-center-fill: #1f6fb2;
  --node-center-text: #ffffff;
  --canvas-bg:        #fbfbf9;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --node-fill:        #35352f;
    --node-stroke:      #4e4e46;
    --node-center-fill: #4a9ae8;
    --node-center-text: #0b0b0b;
    --canvas-bg:        #141413;
  }
}
:root[data-theme="dark"] {
  --node-fill:        #35352f;
  --node-stroke:      #4e4e46;
  --node-center-fill: #4a9ae8;
  --node-center-text: #0b0b0b;
  --canvas-bg:        #141413;
}

.graph-shell {
  position: relative;
  max-inline-size: var(--measure-canvas);
  margin: 1rem auto 0.5rem;
  border: 1px solid var(--gridline);
  border-radius: 10px;
  background: var(--canvas-bg);
  overflow: hidden;
}
.graph-shell:fullscreen { border-radius: 0; max-inline-size: none; margin: 0; }

.graph-canvas {
  /* Tall enough for a roughly circular graph — it is height, not width, that
     bounds the zoom — but short enough that the senses below start on screen:
     a canvas that fills the viewport reads as the end of the page. Moving the
     word onto the canvas is what paid for this height. */
  block-size: min(70vh, 720px);
  inline-size: 100%;
}
.graph-shell:fullscreen .graph-canvas { block-size: calc(100vh - 3.25rem); }

/* Toolbar ABOVE the canvas. The floating cluster this replaces sat over the
   bottom of the graph, which is where a radial layout puts a third of its
   nodes. */
.graph-toolbar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
  padding: 0.45rem 0.6rem;
  border-block-end: 1px solid var(--gridline);
  background: var(--surface-1);
}
.graph-filters, .graph-tools { display: flex; gap: 0.35rem; flex-wrap: wrap; align-items: center; }
.graph-tools { margin-inline-start: auto; }

.graph-toolbar button {
  min-block-size: 34px;
  padding-inline: 0.65rem;
  border: 1px solid var(--gridline);
  border-radius: 7px;
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.84rem;
  cursor: pointer;
}
.graph-toolbar button:hover { background: var(--surface-2); }
.graph-tools button { min-inline-size: 38px; }

.graph-canvas.is-loading::after {
  content: "";
  position: absolute;
  inset-block-start: 0;
  inset-inline: 0;
  block-size: 2px;
  background: var(--edge-taxonomy);
  animation: graph-progress 900ms ease-in-out infinite;
}
@keyframes graph-progress {
  0%   { transform: scaleX(0);   transform-origin: left; }
  50%  { transform: scaleX(1);   transform-origin: left; }
  100% { transform: scaleX(0);   transform-origin: right; }
}

/* Relation-group filter chips. The colour is a swatch, and the LABEL carries
   the meaning — colour never carries it alone. */
.group-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.group-chip::before {
  content: "";
  inline-size: 12px;
  block-size: 3px;
  border-radius: 2px;
  background: var(--chip-hue, var(--text-muted));
}
.group-chip[data-group-token="t"] { --chip-hue: var(--edge-taxonomy); }
.group-chip[data-group-token="c"] { --chip-hue: var(--edge-composition); }
.group-chip[data-group-token="a"] { --chip-hue: var(--edge-association); }
.group-chip.is-off { opacity: 0.45; text-decoration: line-through; }

/* Node detail slides OVER the canvas rather than opening a modal. */
#node-detail {
  position: absolute;
  /* Top-inline-END: the word plate holds the opposite corner. */
  inset-block-start: 3.5rem;
  inset-inline-end: 0.75rem;
  inline-size: min(300px, calc(100% - 1.5rem));
  max-block-size: calc(100% - 5rem);
  overflow-y: auto;
  padding: 0.85rem 1rem;
  border: 1px solid var(--gridline);
  border-radius: 10px;
  background: var(--surface-1);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.10);
}
#node-detail h3 { margin-block: 0 0.35rem; font-size: 1.05rem; }
#node-detail p  { margin-block: 0 0.5rem; font-size: 0.9rem; }
#node-detail .panel-link { font-size: 0.9rem; font-weight: 600; }

/* --- the legend, deliberately small ------------------------------------ */
/* Collapsed to a single line by default: the graph is the point, and a
   reference table that fills half the viewport competes with it. Open it and
   the full key appears. */

.legend {
  /* Tracks the canvas width, not the text width — it is the canvas's key. */
  max-inline-size: var(--measure-canvas);
  margin-block-end: 1.5rem;
  margin-inline: auto;
  border: 1px solid var(--gridline);
  border-radius: 8px;
  background: var(--surface-1);
  font-size: 0.82rem;
}
.legend > summary {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  color: var(--text-secondary);
  list-style: none;
}
.legend > summary::-webkit-details-marker { display: none; }
.legend > summary::after {
  content: "▸";
  margin-inline-start: auto;
  color: var(--text-muted);
  transition: transform 120ms ease;
}
.legend[open] > summary::after { transform: rotate(90deg); }

.legend-key { display: inline-flex; align-items: center; gap: 0.35rem; white-space: nowrap; }
.legend-key .swatch { inline-size: 16px; block-size: 3px; border-radius: 2px; background: var(--key-hue, var(--text-muted)); }
.legend-key[data-group="taxonomy"]    { --key-hue: var(--edge-taxonomy); }
.legend-key[data-group="composition"] { --key-hue: var(--edge-composition); }
.legend-key[data-group="association"] { --key-hue: var(--edge-association); }

.legend-body {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.5rem 1rem;
  padding: 0 0.75rem 0.75rem;
  border-block-start: 1px solid var(--gridline);
  padding-block-start: 0.6rem;
  color: var(--text-secondary);
}
.legend-body dl { margin: 0; }
.legend-body dt { font-weight: 600; color: var(--text-primary); margin-block-end: 0.25rem; }
.legend-body dd { margin-inline-start: 0; display: flex; align-items: center; gap: 0.4rem; }

.legend-line { inline-size: 22px; block-size: 0; border-block-start: 2px solid var(--text-secondary); }
.legend-line.dashed { border-block-start-style: dashed; }
.legend-line.dotted { border-block-start-style: dotted; }
.legend-shape { inline-size: 11px; block-size: 11px; background: var(--node-fill); border: 1.5px solid var(--node-stroke); }
.legend-shape.round  { border-radius: 50%; }
.legend-shape.square { border-radius: 3px; }
.legend-shape.diamond{ transform: rotate(45deg); }
.legend-shape.hex    { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); }

/* The server-rendered lists stay in the DOM under the canvas: they are the
   crawlable layer (invariant 8) and the keyboard/no-JS path. */
.word-text { margin-block-start: 1.5rem; }

@media (max-width: 640px) {
  .graph-canvas { block-size: 58vh; }
  /* Bottom sheet on small screens: at the top it would cover the word plate,
     and there is no room for both. */
  #node-detail {
    inline-size: calc(100% - 1.5rem);
    inset-inline: 0.75rem;
    inset-block-start: auto;
    inset-block-end: 0.75rem;
    max-block-size: 45%;
  }
  .graph-tools { margin-inline-start: 0; }
  /* 44x44 minimum touch targets once there is no hover to fall back on. */
  .graph-toolbar button { min-block-size: 44px; }
}

@media (min-width: 1100px) {
  .graph-shell, .legend { inline-size: calc(100% - 2rem); }
}

@media (forced-colors: active) {
  .group-chip::before, .legend-key .swatch { background: CanvasText; }
}
