/**
 * Rijksmuseum Artwork Viewer Styles
 *
 * Uses host CSS variables for theme integration.
 * Variables are injected by applyHostStyleVariables() from ext-apps SDK.
 */

:root {
  --color-background-primary: #fafafa;
  --color-background-secondary: #e8e7e3;
  --color-background-tertiary: #f5f4f1;

  --color-text-primary: #333;
  --color-text-secondary: #666;
  --color-text-tertiary: #999;

  --color-border-primary: #ccc;
  --color-border-secondary: #ccc;

  --color-accent-primary: #c13832;
  --color-accent-secondary: #a02e29;

  /* Surface for the header/footer chrome bands. Not a host-injected variable
     (unlike --color-background-secondary, which hosts override at runtime), so
     this value always wins regardless of the host's theme. */
  --color-header-surface: #efeeeb;

  --border-radius-sm: 4px;
  --border-radius-md: 8px;
  --border-radius-lg: 12px;

  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Monaco, 'Cascadia Code', monospace;
}

[data-theme="dark"] {
  --color-background-primary: #1a1a1a;
  --color-background-secondary: #222;
  --color-background-tertiary: #333;
  --color-text-primary: #e5e5e5;
  --color-text-secondary: #b0b0b0;
  --color-text-tertiary: #808080;
  --color-border-primary: #333;
  --color-border-secondary: #444;
  --color-header-surface: #222;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  background: var(--color-background-primary);
  color: var(--color-text-primary);
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
}

/* Inline default: no fixed height — the host (autoResize) sizes the iframe to
   our content, so .main flexes to header + content + footer. .content carries
   the intrinsic size via aspect-ratio (see below), which scales with the
   host-set iframe width instead of dropping a constant 700px slab into narrow
   layouts (e.g. ChatGPT mobile). The :fullscreen rule below restores a
   definite height. */
.main {
  display: flex;
  flex-direction: column;
  border-radius: var(--border-radius-lg);
  overflow: hidden;
  background: var(--color-background-primary);
}

/* Loading state */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  gap: 1rem;
  color: var(--color-text-secondary);
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-border-primary);
  border-top-color: var(--color-accent-primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading p {
  font-size: 1rem;
  text-align: center;
  max-width: 300px;
}

/* Header */
.header {
  background: var(--color-header-surface);
  color: var(--color-text-primary);
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex-shrink: 0;
  border-bottom: 1px solid var(--color-border-primary);
}

.header-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

.header h1 {
  font-size: 1rem;
  font-weight: 600;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
}

/* Outer span holds the click-to-copy + tooltip; sized to its truncated text
   content so the hover/click area matches what the user sees. Crucially does
   NOT have overflow:hidden — that would clip the :hover::after tooltip. */
.header h1 > .copyable {
  display: flex;
  min-width: 0;
  max-width: 100%;
  align-items: center;
}

/* Inner span carries the truncation; its overflow:hidden is what makes
   text-overflow:ellipsis kick in for long titles. */
.header h1 .title-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.metadata {
  display: flex;
  gap: 1rem;
  align-items: center;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  flex-wrap: wrap;
}

.metadata span {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  white-space: nowrap;
}

/* Click-to-copy with custom tooltip */
.copyable {
  cursor: pointer;
  position: relative;
}

.copyable[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.25rem 0.5rem;
  background: rgba(0, 0, 0, 0.85);
  color: #e5e5e5;
  font-size: 0.75rem;
  font-weight: 400;
  white-space: nowrap;
  border-radius: var(--border-radius-sm);
  pointer-events: none;
  z-index: 30;
  margin-bottom: 4px;
}

/* Header tooltips display below — no room above the title */
.header [data-tooltip]:hover::after {
  bottom: auto;
  top: 100%;
  margin-bottom: 0;
  margin-top: 4px;
}

.external-links {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.external-links a {
  color: var(--color-text-primary);
  text-decoration: none;
  padding: 0.25rem 0.6rem;
  border-radius: var(--border-radius-sm);
  background: var(--color-background-tertiary);
  border: 1px solid var(--color-border-primary);
  font-size: 0.8rem;
  transition: background 0.2s, border-color 0.2s;
  white-space: nowrap;
}

.external-links a:hover {
  background: rgb(204, 76, 39);
  border-color: rgb(204, 76, 39);
  color: white;
}

/* Viewport wrapper — holds the image box (.content) plus the controls.
   Takes over the flex-item role so the image still sizes correctly, and is the
   positioning + container-query context for .image-controls. On wide widths the
   controls are absolutely positioned, so this box collapses to the image height
   and bottom:1rem floats them over the image exactly as before. On narrow
   widths the container query makes the controls in-flow, so this box grows to
   image + control-band height. container-type: inline-size keys the query off
   the *rendered content width* — the one dimension every MCP host honors
   identically — not a vw/device proxy. Inline-only containment leaves the block
   axis free, so .content's aspect-ratio still derives height from width. */
.viewport-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  container-type: inline-size;
}

/* Content area — full-width image viewer.
   flex-basis: auto (not the `flex:1` shorthand's 0%) so the aspect-ratio box
   has a real automatic height when .main is content-sized (inline mode); the
   flex-grow:1 still lets it fill the extra space when .main has a definite
   height (:fullscreen). aspect-ratio derives the inline height from the iframe
   width — ~4:3 keeps the desktop card close to its old ~700px while shrinking
   gracefully on phones. */
.content {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
  overflow: hidden;
  aspect-ratio: 4 / 3;
}

#openseadragon-viewer {
  width: 100%;
  height: 100%;
}

/* Image error state */
.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--color-text-secondary);
  text-align: center;
  padding: 1rem;
}

.image-error a {
  color: var(--color-accent-primary);
  margin-top: 0.5rem;
}

/* Image controls — WIDE default: a floating translucent pill over the bottom
   of the image, positioned against .viewport-wrap (which collapses to the image
   box here, so bottom:1rem floats over the image bottom exactly as before). The
   @container rule below converts this into an in-flow band only on narrow
   widths, so desktop is unchanged. The max-width + flex-wrap are a degradation
   safety net: on an engine too old for @container (none of the MCP hosts we
   target), the band never activates and this wraps instead of clipping. */
.image-controls {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.5rem;
  background: rgba(0, 0, 0, 0.7);
  padding: 0.5rem;
  border-radius: var(--border-radius-md);
  z-index: 10;
  max-width: calc(100% - 1rem);
  flex-wrap: wrap;
  justify-content: center;
}

.image-controls button {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  padding: 0.5rem 0.75rem;
  border-radius: var(--border-radius-sm);
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.2s;
}

.image-controls button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
}

.image-controls button:disabled {
  opacity: 0.35;
  cursor: default;
}

.image-controls button.active {
  background: rgba(59, 130, 246, 0.4);
}

/* Uniform background width for all icon buttons (everything except Reset,
   which is a text label and naturally wider). The select-region button keeps
   a larger glyph for prominence but its background matches the others. */
.image-controls #show-shortcuts,
.image-controls #zoom-in,
.image-controls #zoom-out,
.image-controls #prev-related,
.image-controls #next-related,
.image-controls #select-mode {
  width: 2.4rem;
  padding: 0.4rem 0;
  font-size: 0.9rem;
  text-align: center;
  box-sizing: border-box;
}

.image-controls #zoom-in,
.image-controls #zoom-out {
  font-size: 1.05rem;
}

.image-controls #prev-related,
.image-controls #next-related {
  font-size: 0.75rem;
}

.image-controls #select-mode {
  font-size: 1.1rem;
}

/* NARROW: when the viewport-wrap is too narrow for the floating pill to fit one
   row, drop the controls out of the image into an in-flow band below it. Keyed
   off .viewport-wrap's rendered inline width (its container-type), not a
   vw/device proxy, so it triggers on the same real estate in every MCP host.
   The band can never clip or cover the artwork; it wraps tidily if even it runs
   out of room. Dark surface keeps the existing white-on-translucent buttons
   legible regardless of host theme (matching the over-image pill they replace). */
@container (max-width: 380px) {
  .image-controls {
    position: static;
    transform: none;
    max-width: none;
    border-radius: 0;
    gap: 0.3rem;
    padding: 0.4rem;
    background: #1c1c1c;
    border-top: 1px solid var(--color-border-primary);
  }

  /* Shrink the controls so all seven fit one row in the band on a typical
     phone. The host upscales the narrow iframe, so these reduced CSS sizes
     still render as comfortably tappable targets on screen; if a host's width
     is narrower still, the band simply wraps tidily rather than clipping. */
  .image-controls #show-shortcuts,
  .image-controls #zoom-in,
  .image-controls #zoom-out,
  .image-controls #prev-related,
  .image-controls #next-related,
  .image-controls #select-mode {
    width: 1.9rem;
  }

  .image-controls #reset-view {
    padding: 0.4rem 0.5rem;
    font-size: 0.85rem;
  }
}

/* Suppress the browser's default focus ring after a mouse click — keyboard
   users still get a clear indicator via :focus-visible below. */
.image-controls button:focus {
  outline: none;
}

.image-controls button:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.6);
  outline-offset: 1px;
}

.image-controls .control-separator {
  width: 1px;
  height: 20px;
  background: rgba(255, 255, 255, 0.3);
  margin: 0 0.25rem;
  align-self: center;
}

/* Footer */
.footer {
  background: var(--color-header-surface);
  color: var(--color-text-secondary);
  padding: 0.5rem 1rem;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1.5rem;
  flex-shrink: 0;
  border-top: 1px solid var(--color-border-primary);
  font-size: 0.8rem;
}

.footer .license {
  white-space: nowrap;
  flex-shrink: 0;
}

.footer .license a {
  color: var(--color-text-tertiary);
  text-decoration: none;
}

.footer .license a:hover {
  color: var(--color-text-primary);
}

.footer .dimensions {
  color: var(--color-text-tertiary);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-align: right;
}

/* Error state */
.error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 2rem;
  text-align: center;
}

.error h2 {
  color: #c0392b;
  margin-bottom: 1rem;
}

.error p {
  color: var(--color-text-secondary);
  max-width: 400px;
}

.error button {
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  background: var(--color-accent-primary);
  color: white;
  border: none;
  border-radius: var(--border-radius-sm);
  cursor: pointer;
}

.error button:hover {
  background: var(--color-accent-secondary);
}

/* Shortcuts overlay */
.shortcuts-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  z-index: 20;
}

.shortcuts-overlay.hidden {
  display: none;
}

.shortcuts-content {
  background: rgba(0, 0, 0, 0.85);
  color: #e5e5e5;
  padding: 1.25rem 1.5rem;
  border-radius: var(--border-radius-md);
  min-width: 240px;
}

.shortcuts-header {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  color: white;
}

.shortcuts-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.shortcut-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1.5rem;
  font-size: 0.85rem;
}

.shortcut-row kbd {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  background: rgba(255, 255, 255, 0.12);
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  min-width: 1.5em;
  text-align: center;
}

.shortcut-row span {
  color: #b0b0b0;
}

/* Selection preview (select mode) */
.selection-preview {
  pointer-events: none;
  box-sizing: border-box;
}

/* Region overlays (navigate_viewer) */
.region-overlay {
  pointer-events: none;
  box-sizing: border-box;
}

.region-label {
  position: absolute;
  bottom: -20px;
  left: 0;
  background: rgba(0, 0, 0, 0.75);
  color: white;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 3px;
  white-space: nowrap;
  font-family: var(--font-sans);
}

/* Fullscreen */
.main:fullscreen {
  height: 100vh;
  border-radius: 0;
}

/* Responsive */
@media (max-width: 600px) {
  .header {
    padding: 0.5rem 0.75rem;
  }

  .header-title-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .metadata {
    gap: 0.5rem;
  }
}
