/*
 * Bausteine für Produkt-Oberflächen (auch interne Werkzeuge).
 *
 * Setzt tokens.css und base.css voraus. Die Klasse .tnw-app und gegebenenfalls
 * data-theme stehen am html-Element. Das Thema folgt der Systemeinstellung;
 * data-theme="light" bzw. "dark" erzwingt eines (Umschalter im Kopf: hell / dunkel / System).
 */

/* Thema: hell */
.tnw-app {
  --ui-bg: var(--tnw-white);
  --ui-surface: var(--tnw-white);
  --ui-hover: var(--tnw-wash);
  --ui-hover-raised: var(--tnw-wash);
  --ui-wash: var(--tnw-wash);
  --ui-canvas: var(--tnw-wash);
  --ui-heading: var(--tnw-ink);
  --ui-text: var(--tnw-text);
  --ui-text-secondary: var(--tnw-text-secondary);
  --ui-deco: var(--tnw-deco);
  --ui-line: var(--tnw-line);
  --ui-field-bg: var(--tnw-wash);
  --ui-field-placeholder: var(--tnw-text-secondary);
  --ui-field-line: var(--tnw-field-line);
  --ui-button-fill: var(--tnw-ink);
  --ui-button-fill-hover: var(--tnw-fill-hover);
  --ui-toast: var(--tnw-dark-raised);
  --ui-backdrop: rgba(0, 0, 0, 0.4);
  --ui-skeleton: var(--tnw-line);
  --ui-label: var(--accent-label);
  --ui-syntax-keyword: var(--tnw-syntax-keyword);
  --ui-syntax-string: var(--tnw-syntax-string);
  --ui-syntax-number: var(--tnw-syntax-number);
  --ui-syntax-property: var(--tnw-syntax-property);
  --ui-syntax-function: var(--tnw-syntax-function);
  --ui-syntax-comment: var(--tnw-syntax-comment);
  color-scheme: light;
  background: var(--ui-bg);
  color: var(--ui-text);
  font-size: var(--tnw-size-control);
}

/* Thema: dunkel – nach System oder erzwungen. */
@media (prefers-color-scheme: dark) {
  .tnw-app:not([data-theme="light"]) {
    --ui-bg: var(--tnw-dark);
    --ui-surface: var(--tnw-dark-raised);
    --ui-hover: var(--tnw-dark-raised);
    --ui-hover-raised: var(--tnw-dark-line);
    --ui-wash: var(--tnw-dark-raised);
    --ui-canvas: var(--tnw-dark);
    --ui-heading: var(--tnw-on-dark-heading);
    --ui-text: var(--tnw-on-dark-text);
    --ui-text-secondary: var(--tnw-on-dark-secondary);
    --ui-deco: var(--tnw-dark-deco);
    --ui-line: var(--tnw-dark-line);
    --ui-field-bg: var(--tnw-dark-line);
    --ui-field-placeholder: var(--tnw-on-dark-text);
    --ui-field-line: var(--tnw-field-line-dark);
    --ui-button-fill: var(--tnw-dark-line);
    --ui-button-fill-hover: var(--tnw-dark-control-hover);
    --ui-toast: var(--tnw-dark-line);
    --ui-backdrop: rgba(0, 0, 0, 0.6);
    --ui-skeleton: var(--tnw-dark-line);
    --ui-label: var(--accent-label-on-dark);
    --ui-syntax-keyword: var(--tnw-syntax-keyword-dark);
    --ui-syntax-string: var(--tnw-syntax-string-dark);
    --ui-syntax-number: var(--tnw-syntax-number-dark);
    --ui-syntax-property: var(--tnw-syntax-property-dark);
    --ui-syntax-function: var(--tnw-syntax-function-dark);
    --ui-syntax-comment: var(--tnw-syntax-comment-dark);
    color-scheme: dark;
  }
}
.tnw-app[data-theme="dark"] {
  --ui-bg: var(--tnw-dark);
  --ui-surface: var(--tnw-dark-raised);
  --ui-hover: var(--tnw-dark-raised);
  --ui-hover-raised: var(--tnw-dark-line);
  --ui-wash: var(--tnw-dark-raised);
  --ui-canvas: var(--tnw-dark);
  --ui-heading: var(--tnw-on-dark-heading);
  --ui-text: var(--tnw-on-dark-text);
  --ui-text-secondary: var(--tnw-on-dark-secondary);
  --ui-deco: var(--tnw-dark-deco);
  --ui-line: var(--tnw-dark-line);
  --ui-field-bg: var(--tnw-dark-line);
  --ui-field-placeholder: var(--tnw-on-dark-text);
  --ui-field-line: var(--tnw-field-line-dark);
  --ui-button-fill: var(--tnw-dark-line);
  --ui-button-fill-hover: var(--tnw-dark-control-hover);
  --ui-toast: var(--tnw-dark-line);
  --ui-backdrop: rgba(0, 0, 0, 0.6);
  --ui-skeleton: var(--tnw-dark-line);
  --ui-label: var(--accent-label-on-dark);
  --ui-syntax-keyword: var(--tnw-syntax-keyword-dark);
  --ui-syntax-string: var(--tnw-syntax-string-dark);
  --ui-syntax-number: var(--tnw-syntax-number-dark);
  --ui-syntax-property: var(--tnw-syntax-property-dark);
  --ui-syntax-function: var(--tnw-syntax-function-dark);
  --ui-syntax-comment: var(--tnw-syntax-comment-dark);
  color-scheme: dark;
}

/* Grundbausteine aus base.css an das Thema binden. */
.tnw-app body { background: var(--ui-bg); color: var(--ui-text); }
.tnw-app :is(.tnw-hero, .tnw-stage, .tnw-page-title, .tnw-section-title, .tnw-sub-title, .tnw-small-title) { color: var(--ui-heading); }
.tnw-app .tnw-label { color: var(--ui-label); }
.tnw-app .tnw-secondary { color: var(--ui-text-secondary); }
.tnw-app .tnw-sep { color: var(--ui-deco); }
.tnw-app .tnw-link { color: var(--ui-heading); }
.tnw-app .tnw-link:hover { --link-line: var(--ui-heading); }

/* Kein Zoom beim Doppeltipp: Ein zweiter Tipp, der etwas zu schnell kommt, ist in einer
   Anwendung nie als Zoom gemeint. Mit zwei Fingern zoomen bleibt. */
.tnw-app { touch-action: manipulation; }

/* App-Kopf: immer dunkel, Logo und Titel der Anwendung links, Bereiche, rechts Suche, Thema, Person. */
.tnw-app-bar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: var(--tnw-space-24); height: var(--tnw-app-bar-height); padding-inline: var(--tnw-space-24); background: var(--tnw-dark); color: var(--tnw-on-dark-heading); }
.tnw-app-brand { display: inline-flex; align-items: center; gap: var(--tnw-space-12); flex: none; color: var(--tnw-on-dark-heading); font-family: var(--tnw-font-title); font-size: var(--tnw-size-body); font-weight: 500; text-decoration: none; }
.tnw-app-brand img, .tnw-app-brand svg { width: 24px; height: 24px; flex: none; }
.tnw-app-areas { display: flex; align-items: center; height: 100%; }
.tnw-app-areas a { position: relative; display: inline-flex; align-items: center; gap: var(--tnw-space-8); height: 100%; padding: 0 var(--tnw-space-16); color: var(--tnw-on-dark-text); font-size: var(--tnw-size-control); text-decoration: none; transition: color var(--tnw-duration-hover) ease-out, background-color var(--tnw-duration-hover) ease-out; }
/* Aktiver Bereich als helleres Feld über die ganze Höhe, nicht als Linie (sie bisse sich mit der Rule darunter). */
.tnw-app-areas a:hover { background: var(--tnw-dark-raised); color: #ffffff; }
.tnw-app-areas a[aria-current="page"] { background: var(--tnw-dark-line); color: #ffffff; font-weight: 600; }
.tnw-app-tools { display: flex; align-items: center; gap: var(--tnw-space-16); margin-left: auto; color: var(--tnw-on-dark-text); font-size: var(--tnw-size-secondary); }
.tnw-app-search { display: flex; align-items: center; justify-content: space-between; gap: var(--tnw-space-12); width: 360px; height: var(--tnw-control-height); padding-inline: var(--tnw-space-12); border: 1px solid var(--tnw-dark-line); background: var(--tnw-dark-raised); color: var(--tnw-on-dark-secondary); font-size: var(--tnw-size-secondary); }
/* Schmale Form der Suche: nur die Lupe, sie bleibt in der Leiste. Ab welcher Breite sie gilt
   und ab welcher die Bereiche hinter den Button gehen, misst jede Anwendung an ihrem eigenen
   Kopf; eine feste Zahl gibt es hier deshalb nicht. */
.tnw-app-search--symbol { justify-content: center; width: var(--tnw-control-height); padding: 0; border-color: transparent; background: none; color: var(--tnw-on-dark-text); }
.tnw-app-search--symbol > :not(.tnw-icon) { display: none; }
.tnw-app-main { padding: var(--tnw-space-32); }
@media (max-width: 899px) { .tnw-app-main { padding: var(--tnw-space-24) var(--tnw-gutter); } }

/* Kompakte Größe: Buttons in Produkten durchgehend 34 px. */
.tnw-app .tnw-button { --button-fill: var(--ui-button-fill); --button-fill-hover: var(--ui-button-fill-hover); min-height: var(--tnw-control-height); padding: 8px 16px; }
.tnw-app .tnw-button:focus-visible { outline-color: var(--ui-heading); }

/* Felder, Auswahl, Textbereiche */
.tnw-field { display: flex; flex-direction: column; gap: var(--tnw-space-8); }
.tnw-field > .tnw-label small { color: var(--ui-text-secondary); font-size: var(--tnw-size-small); font-weight: 400; letter-spacing: 0; text-transform: none; }
/* Felder: getönte Fläche mit Linie unten, kein Rahmen. */
.tnw-input { box-sizing: border-box; width: 100%; height: var(--tnw-control-height); padding: 0 var(--tnw-space-12); border: 0; border-bottom: 1px solid var(--ui-field-line); background: var(--ui-field-bg); color: var(--ui-heading); font: inherit; font-size: var(--tnw-size-control); }
textarea.tnw-input { height: auto; min-height: 72px; padding-block: var(--tnw-space-8); resize: vertical; }
.tnw-input::placeholder { color: var(--ui-field-placeholder); }
.tnw-input:focus-visible { outline: 2px solid var(--ui-heading); outline-offset: 0; }
.tnw-input[aria-invalid="true"] { border-bottom: 2px solid var(--tnw-error); }
.tnw-field-message { display: flex; align-items: center; gap: var(--tnw-space-8); color: var(--ui-heading); font-size: var(--tnw-size-small); }
.tnw-field-message .tnw-icon { width: 14px; height: 14px; color: var(--tnw-error); }
.tnw-field-hint { color: var(--ui-text-secondary); font-size: var(--tnw-size-small); }

/* Umschalter aus Segmenten */
.tnw-segmented { display: inline-flex; height: var(--tnw-control-height); border: 1px solid var(--ui-field-line); }
.tnw-segmented button { padding: 0 var(--tnw-space-16); border: 0; background: transparent; color: var(--ui-text); cursor: pointer; font: inherit; font-size: var(--tnw-size-control); transition: background-color var(--tnw-duration-hover) ease-out; }
.tnw-segmented button + button { border-left: 1px solid var(--ui-field-line); }
.tnw-segmented button:hover { background: var(--ui-hover); }
.tnw-segmented button[aria-pressed="true"] { background: var(--ui-heading); color: var(--ui-bg); font-weight: 600; }
.tnw-segmented button:focus-visible { outline: 2px solid var(--ui-heading); outline-offset: 2px; }

/* Checkbox: native Eingabe, eckig, gefüllt im Textton. */
.tnw-check { display: inline-flex; align-items: center; gap: var(--tnw-space-8); color: var(--ui-heading); cursor: pointer; }
.tnw-check input { appearance: none; display: inline-grid; place-content: center; width: 18px; height: 18px; margin: 0; border: 1px solid var(--ui-field-line); background: var(--ui-field-bg); cursor: pointer; }
.tnw-check input:checked { border-color: var(--ui-heading); background: var(--ui-heading); }
.tnw-check input:checked::after { content: ""; width: 10px; height: 6px; border-bottom: 2px solid var(--ui-bg); border-left: 2px solid var(--ui-bg); transform: translateY(-1px) rotate(-45deg); }
.tnw-check input:focus-visible { outline: 2px solid var(--ui-heading); outline-offset: 2px; }

/* Werkzeugleiste */
.tnw-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--tnw-space-12); }
.tnw-toolbar-spacer { flex: 1; }

/* Listen und Tabellen: 36 px einzeilig, 56 px zweizeilig, Kopf als Label mit dunkler Linie. */
.tnw-rows { width: 100%; border-collapse: collapse; font-size: var(--tnw-size-secondary); font-variant-numeric: tabular-nums; }
.tnw-rows th { position: sticky; top: var(--tnw-app-bar-height); z-index: 1; padding: 8px 12px 8px 0; border-bottom: 1px solid var(--ui-heading); background: var(--ui-bg); color: var(--ui-heading); font-size: var(--tnw-size-label); font-weight: 600; letter-spacing: 1.2px; text-align: left; text-transform: uppercase; }
.tnw-rows td { padding: 8px 12px 8px 0; border-bottom: 1px solid var(--ui-line); vertical-align: middle; }
.tnw-rows td.is-number, .tnw-rows th.is-number { text-align: right; }
.tnw-rows tr:hover td { background: var(--ui-hover); }
/* Auf angehobenen Flächen (Panel, Dialog) braucht Hover eine eigene Stufe; Nebentext wird dort zu Textfarbe (4,5 : 1). */
:is(.tnw-panel, .tnw-dialog) .tnw-rows tr:hover td { background: var(--ui-hover-raised); }
:is(.tnw-panel, .tnw-dialog) .tnw-rows tr:hover .tnw-secondary { color: var(--ui-text); }
.tnw-rows tr[aria-selected="true"] td:first-child { box-shadow: inset 2px 0 0 var(--accent); }
.tnw-rows .tnw-primary { display: block; color: var(--ui-heading); font-size: var(--tnw-size-control); font-weight: 600; }
.tnw-rows .tnw-secondary { display: block; }
.tnw-mono { font-family: var(--tnw-font-code); font-variant-numeric: tabular-nums; }

/* Zustand: Sechseck in der Zustandsfarbe, Wort neutral. */
.tnw-state { display: inline-flex; align-items: center; gap: var(--tnw-space-8); color: var(--ui-heading); }
.tnw-state::before { content: ""; flex: none; width: 9px; height: 10px; background: var(--state, var(--ui-deco)); clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%); }
.tnw-state--error { --state: var(--tnw-error); }
.tnw-state--warning { --state: var(--tnw-warning); }
.tnw-state--success { --state: var(--tnw-success); }

/* Seitenpanel von rechts: Anlegen, Bearbeiten, Details. */
/* Panel über die volle Höhe; dahinter wird die Seite abgedunkelt und unscharf (wie in Makani), ein Klick darauf schließt. */
.tnw-scrim { position: fixed; inset: 0; z-index: 20; width: 100%; padding: 0; border: 0; background: var(--ui-backdrop); backdrop-filter: blur(6px); cursor: default; }
.tnw-panel { position: fixed; top: 0; right: 0; bottom: 0; z-index: 21; display: flex; flex-direction: column; width: min(460px, 100vw); border-left: 1px solid var(--ui-line); background: var(--ui-surface); box-shadow: -12px 0 32px rgba(0, 0, 0, 0.12); }
.tnw-panel--wide { width: min(1080px, 100vw); }
.tnw-panel-head { display: flex; align-items: center; justify-content: space-between; gap: var(--tnw-space-12); padding: var(--tnw-space-16) var(--tnw-space-24); border-bottom: 1px solid var(--ui-line); }
.tnw-panel-body { flex: 1; display: flex; flex-direction: column; gap: var(--tnw-space-16); overflow-y: auto; padding: var(--tnw-space-24); }
.tnw-panel-foot { display: flex; justify-content: flex-end; gap: var(--tnw-space-12); padding: var(--tnw-space-12) var(--tnw-space-24); border-top: 1px solid var(--ui-line); }
@media (max-width: 599px) { .tnw-panel, .tnw-panel--wide { top: 0; width: 100vw; } }

/* Dialog: nur kurze Bestätigungen, genau zwei Buttons. */
.tnw-dialog { width: min(440px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--ui-line); background: var(--ui-surface); color: var(--ui-text); box-shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.35); }
.tnw-dialog::backdrop { background: var(--ui-backdrop); backdrop-filter: blur(6px); }
.tnw-dialog > div { padding: var(--tnw-space-24); }
.tnw-dialog footer { display: flex; justify-content: flex-end; gap: var(--tnw-space-12); padding: var(--tnw-space-12) var(--tnw-space-24); border-top: 1px solid var(--ui-line); }

/* Meldung unten rechts: Erfolg verschwindet nach etwa 4 s, Fehler bleibt bis zum Schließen. */
.tnw-toasts { position: fixed; right: var(--tnw-space-24); bottom: var(--tnw-space-24); z-index: 30; display: flex; flex-direction: column; gap: var(--tnw-space-8); }
.tnw-toast { display: flex; align-items: center; gap: var(--tnw-space-12); max-width: 360px; padding: 12px 16px; border-left: 4px solid var(--state, var(--accent)); background: var(--ui-toast); color: var(--tnw-on-dark-heading); font-size: var(--tnw-size-control); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3); }
.tnw-toast .tnw-icon { color: var(--state, var(--accent)); }

/* Laden: erst nach 0,4 s, ruhendes Gerüst in der Form des Inhalts, ohne Schimmer. */
.tnw-skeleton { display: block; height: 12px; background: var(--ui-skeleton); animation: tnw-skeleton-in 0s 0.4s both; }
@keyframes tnw-skeleton-in { from { visibility: hidden; } to { visibility: visible; } }

/* Tastenzeichen */
kbd, .tnw-kbd { padding: 0 var(--tnw-space-4); border: 1px solid var(--ui-field-line); color: var(--ui-text-secondary); font-family: var(--tnw-font-code); font-size: 12px; }

/* Code im Produkt folgt dem Thema. */
.tnw-app .tnw-code { background: var(--ui-surface); color: var(--ui-heading); border: 1px solid var(--ui-line); border-top: 3px solid var(--accent); }
.tnw-app .tnw-code .tok-keyword { color: var(--ui-syntax-keyword); }
.tnw-app .tnw-code .tok-string { color: var(--ui-syntax-string); }
.tnw-app .tnw-code .tok-number { color: var(--ui-syntax-number); }
.tnw-app .tnw-code .tok-property { color: var(--ui-syntax-property); }
.tnw-app .tnw-code .tok-function { color: var(--ui-syntax-function); }
.tnw-app .tnw-code .tok-comment { color: var(--ui-syntax-comment); }

/* Mit grobem Zeiger wird alles, was ein Finger trifft, mindestens 44 px hoch -
   Menüeinträge eingeschlossen. Ohne sie blieben die Menüs zweier Anwendungen
   bei 35 px, weil die Regel sie schlicht nicht aufzählte. */
@media (pointer: coarse) {
  .tnw-button, .tnw-icon-button, .tnw-input, .tnw-segmented button,
  [role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"] { min-height: 44px; }
  [role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"] { display: flex; align-items: center; }
  .tnw-app-search--symbol { width: 44px; height: 44px; }
  /* Unter 16 px zoomt Safari auf dem iPhone beim Antippen eines Feldes heran
     und bleibt danach gezoomt. */
  .tnw-input { font-size: var(--tnw-size-field-touch); }
}
