/* v19.5 — final semantic surface contract.
   This file is intentionally loaded last so legacy fixed white/grey styles
   cannot escape the selected Light, Dark or Qorgon theme. */

body {
  --ui-layer: color-mix(in srgb, var(--surface) 90%, transparent);
  --ui-layer-raised: color-mix(in srgb, var(--surface-2) 88%, var(--surface));
  --ui-layer-soft: color-mix(in srgb, var(--surface-2) 72%, transparent);
  --ui-control: color-mix(in srgb, var(--surface-2) 86%, var(--text) 4%);
  --ui-control-hover: color-mix(in srgb, var(--blue) 10%, var(--surface-2));
  --ui-stroke: color-mix(in srgb, var(--line) 86%, var(--text) 8%);
  --ui-stroke-soft: color-mix(in srgb, var(--line) 68%, transparent);
  --ui-danger-soft: color-mix(in srgb, var(--red) 10%, var(--surface-2));
  --ui-focus-ring: color-mix(in srgb, var(--blue) 22%, transparent);
  --telegram-control-bg: var(--ui-control);
  --telegram-control-border: var(--ui-stroke);
  --telegram-rail-bg: color-mix(in srgb, var(--surface) 86%, var(--bg));
  --telegram-rail-active: color-mix(in srgb, var(--telegram-accent) 14%, var(--surface));
  --telegram-rail-muted: color-mix(in srgb, var(--text) 62%, var(--muted));
}

/* Navigation geometry is identical in all themes and workspaces. */
.rail,
.app-shell.media-active .rail,
.app-shell.portal-active .rail,
.app-shell.shop-active .rail {
  gap: 4px;
  padding: 68px 6px 8px;
  background: var(--telegram-rail-bg) !important;
}

.rail-item,
body[data-theme="qorgon"] .rail-item,
.app-shell.media-active .rail-item,
.app-shell.portal-active .rail-item,
.app-shell.shop-active .rail-item {
  width: 100%;
  min-height: 58px;
  margin: 0;
  padding: 6px 3px 5px;
  overflow: visible;
  border: 1px solid transparent !important;
  border-radius: 14px !important;
  background: transparent !important;
  color: var(--telegram-rail-muted) !important;
}

.rail-item:hover,
.rail-item.active,
body[data-theme="qorgon"] .rail-item.active,
body[data-theme="qorgon"] .app-shell.media-active .rail-item.active,
.app-shell.media-active .rail-item.active,
.app-shell.portal-active .rail-item.active,
.app-shell.shop-active .rail-item.active {
  border-color: color-mix(in srgb, var(--telegram-accent) 26%, var(--ui-stroke)) !important;
  border-radius: 14px !important;
  background: var(--telegram-rail-active) !important;
  color: var(--telegram-accent) !important;
  box-shadow: inset 0 1px 0 color-mix(in srgb, var(--surface) 72%, transparent),
    0 5px 14px color-mix(in srgb, var(--text) 7%, transparent) !important;
}

.rail-icon-wrap {
  width: 32px;
  height: 29px;
}

.rail-badge {
  top: -7px;
  right: -10px;
  left: auto;
  min-width: 19px;
  height: 19px;
  padding: 0 4px;
  border: 2px solid var(--telegram-rail-bg);
  background: var(--telegram-accent);
  font-size: 9px;
}

.rail-item.active .rail-badge {
  border-color: var(--telegram-rail-active);
}

.rail-label {
  color: currentColor !important;
  font-weight: 720;
}

.version-card {
  border-color: color-mix(in srgb, var(--blue) 34%, var(--ui-stroke)) !important;
  background: linear-gradient(
    145deg,
    color-mix(in srgb, var(--blue) 18%, var(--ui-layer-raised)),
    color-mix(in srgb, var(--teal) 12%, var(--ui-layer-raised))
  ) !important;
  color: var(--text) !important;
}

.switch-input:not(:checked) {
  border-color: var(--ui-stroke) !important;
  background: color-mix(in srgb, var(--muted) 24%, var(--surface-2)) !important;
}

.switch-input::after {
  background: var(--surface) !important;
}

/* Dialog shells, drawers and floating surfaces. */
:is(
  .settings-dialog,
  .space-settings-dialog,
  .space-dialog,
  .forward-dialog,
  .source-dialog,
  .comments-dialog,
  .prototype-dialog,
  .tool-manager-window,
  .account-drawer,
  .space-info-popover,
  .message-menu
) {
  border-color: var(--ui-stroke) !important;
  background: var(--ui-layer) !important;
  color: var(--text) !important;
}

:is(
  .settings-window .settings-head,
  .settings-footer,
  .settings-nav-list,
  .space-settings-head,
  .space-settings-footer,
  .forward-head,
  .comments-dialog .forward-head,
  .tool-manager-head
) {
  border-color: var(--ui-stroke-soft) !important;
  background: color-mix(in srgb, var(--ui-layer-raised) 90%, transparent) !important;
  color: var(--text) !important;
}

.settings-content,
.space-settings-body,
.forward-targets,
.comments-list,
.tool-manager-body {
  color: var(--text);
}

/* Reusable cards and rows. */
:is(
  .settings-row,
  .range-setting,
  .settings-note,
  .settings-backdrop-upload,
  .settings-advanced-appearance,
  .sync-status-card,
  .device-card,
  .telegram-account-card,
  .dual-username-preview,
  .space-settings-profile,
  .space-settings-section,
  .source-preview,
  .comment-post-preview,
  .tool-block,
  .tool-profile
) {
  border-color: var(--ui-stroke) !important;
  background: var(--ui-layer-soft) !important;
  color: var(--text) !important;
  box-shadow: inset 0 1px 0 color-mix(in srgb, var(--surface) 68%, transparent) !important;
}

.space-settings-member,
.member-line,
.tool-catalog-row {
  border-color: var(--ui-stroke-soft) !important;
  color: var(--text) !important;
}

/* Fields and search controls must never inherit a fixed grey fill. */
:is(
  .settings-panel,
  .space-settings-section,
  .space-dialog,
  .forward-dialog,
  .source-dialog,
  .tool-manager-window
) :is(input:not(.switch-input), select, textarea),
:is(
  .space-search,
  .media-searchbar,
  .conversation-search-bar,
  .forward-search,
  .tool-search,
  .tool-manager-search,
  .composer-row
) {
  border-color: var(--ui-stroke) !important;
  background: var(--ui-control) !important;
  color: var(--text) !important;
  box-shadow: inset 0 1px 0 color-mix(in srgb, var(--surface) 68%, transparent) !important;
}

:is(input, select, textarea)::placeholder {
  color: color-mix(in srgb, var(--muted) 86%, transparent) !important;
  opacity: 1;
}

select,
select option {
  color: var(--text);
  background-color: var(--surface);
}

:is(input, select, textarea):focus,
:is(.space-search, .media-searchbar, .conversation-search-bar, .forward-search, .tool-search):focus-within {
  border-color: color-mix(in srgb, var(--blue) 58%, var(--ui-stroke)) !important;
  outline: 0;
  box-shadow: 0 0 0 3px var(--ui-focus-ring) !important;
}

/* Neutral actions use theme surfaces; destructive actions stay semantic. */
:is(
  .space-settings-action,
  .secondary-button,
  .drawer-menu button,
  .drawer-account,
  .member-actions button,
  .tool-action,
  .tool-manager-body > aside button,
  .tool-search-actions button
) {
  border-color: var(--ui-stroke) !important;
  background: var(--ui-control) !important;
  color: var(--text) !important;
}

:is(
  .space-settings-action,
  .secondary-button,
  .drawer-menu button,
  .drawer-account,
  .member-actions button,
  .tool-action,
  .tool-manager-body > aside button,
  .tool-search-actions button
):hover {
  background: var(--ui-control-hover) !important;
}

:is(.space-settings-action.danger, .tool-action.danger, .member-actions button.danger) {
  border-color: color-mix(in srgb, var(--red) 30%, var(--ui-stroke)) !important;
  background: var(--ui-danger-soft) !important;
  color: var(--red) !important;
}

.forward-target:hover,
.forward-target.selected,
.drawer-account.active,
.tool-catalog-row:hover,
.tool-catalog-row.active {
  background: var(--ui-control-hover) !important;
}

.shop-maker-story > div:first-child button {
  border-color: var(--shop-line, var(--ui-stroke)) !important;
  background: var(--shop-surface-soft, var(--ui-control)) !important;
  color: var(--shop-text, var(--text)) !important;
}

.settings-row small,
.settings-panel-head p,
.space-settings-head p,
.space-settings-member small,
.space-settings-profile small,
.telegram-account-card small,
.device-card small,
.sync-status-card small {
  color: var(--muted) !important;
}

/* Telegram's active player belongs to the conversation chrome.  The legacy
   application topbar is intentionally collapsed by the current layout, so a
   player mounted there would be alive but invisible. */
.conversation-header .qorgon-global-audio {
  display: grid;
  flex: 1 1 520px;
  width: auto !important;
  max-width: 760px;
  min-width: 260px;
  border: 1px solid var(--ui-stroke);
  background: var(--ui-layer-soft);
  color: var(--text);
  box-shadow: inset 0 1px 0 color-mix(in srgb, var(--surface) 68%, transparent);
}

.conversation-header .qorgon-global-audio[hidden] {
  display: none;
}

.conversation-header:has(.qorgon-global-audio:not([hidden])) .conversation-info-button {
  flex: 0 1 260px;
  min-width: 130px;
}

/* Telegram bot keyboards often consist only of emoji.  Keep a native colour
   emoji fallback and a visible semantic surface in every selected theme. */
.telegram-bot-keyboard .telegram-inline-button {
  border: 1px solid color-mix(in srgb, var(--blue) 22%, var(--ui-stroke)) !important;
  background: color-mix(in srgb, var(--blue) 8%, var(--ui-control)) !important;
  color: var(--blue) !important;
  opacity: 1 !important;
}

.telegram-bot-keyboard .telegram-inline-button > span,
.telegram-custom-emoji,
.emoji-grid button {
  font-family: Inter, "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", ui-sans-serif, system-ui, sans-serif;
  font-variant-emoji: emoji;
}

.telegram-bot-keyboard .telegram-inline-button:disabled {
  color: var(--muted) !important;
  opacity: .72 !important;
}

@media (max-width: 1100px) {
  .conversation-header .qorgon-global-audio {
    flex-basis: 360px;
    min-width: 220px;
  }

  .conversation-header:has(.qorgon-global-audio:not([hidden])) .conversation-info-button {
    max-width: 190px;
  }
}

@media (max-width: 760px) {
  .rail,
  .app-shell.media-active .rail,
  .app-shell.portal-active .rail,
  .app-shell.shop-active .rail {
    padding: 56px 4px 6px;
  }

  .rail-item,
  body[data-theme="qorgon"] .rail-item,
  .app-shell.media-active .rail-item {
    min-height: 55px;
    border-radius: 12px !important;
  }

  .conversation-header:has(.qorgon-global-audio:not([hidden])) .conversation-info-button {
    display: none;
  }

  .conversation-header .qorgon-global-audio {
    flex: 1 1 auto;
    min-width: 0;
  }
}
