.fr {
  --fr-bg: #000;
  --fr-fg: #fff;
  --fr-muted: #8a8a8a;
  --fr-line: #2a2a2a;
  --fr-surface: #111;
  --fr-accent: #c00;
  --fr-axis: #555;
  --fr-font: Helvetica, "Helvetica Neue", Arial, sans-serif;

  display: grid;
  /* minmax(0, …) stops the tray's long row of covers from widening the
     column past the screen (an auto column grows to fit its content). */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "toolbar" "stage" "tray";
  height: 100vh;
  height: 100dvh;
  background: var(--fr-bg);
  color: var(--fr-fg);
  font-family: var(--fr-font);
  overflow: hidden;
}

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

.fr button,
.fr input { font: inherit; color: inherit; }

.fr button {
  min-height: 36px;
  padding: 0 12px;
  background: var(--fr-surface);
  border: 1px solid var(--fr-line);
  border-radius: 4px;
  cursor: pointer;
}
.fr button:disabled { opacity: 0.4; cursor: default; }
.fr button.fr-primary { background: var(--fr-accent); border-color: var(--fr-accent); }

/* Toolbar */
.fr-toolbar {
  grid-area: toolbar;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "home title actions" "mode mode mode" "options options options";
  align-items: center;
  gap: 8px 12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--fr-line);
}
.fr-home { grid-area: home; display: block; line-height: 0; }
.fr-home img { height: 24px; width: auto; }
.fr-title { grid-area: title; margin: 0; font-size: 16px; font-weight: bold; letter-spacing: 0.04em; text-transform: uppercase; }
.fr-actions { grid-area: actions; display: flex; gap: 8px; }

.fr-mode { grid-area: mode; display: inline-flex; justify-self: start; }
.fr-mode input { position: absolute; opacity: 0; pointer-events: none; }
.fr-mode label {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 12px;
  background: var(--fr-surface);
  border: 1px solid var(--fr-line);
  cursor: pointer;
  font-size: 14px;
}
.fr-mode label:first-of-type { border-radius: 4px 0 0 4px; }
.fr-mode label:last-of-type { border-radius: 0 4px 4px 0; border-left: 0; }
.fr-mode input:checked + label { background: var(--fr-accent); border-color: var(--fr-accent); }
.fr-mode input:focus-visible + label { outline: 2px solid var(--fr-fg); outline-offset: 1px; }
#fr-auto-layout[hidden] { display: none; }
.fr-options { grid-area: options; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; font-size: 14px; }
.fr-field { display: flex; align-items: center; gap: 6px; flex: 1 1 180px; }
.fr-field input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 0 8px;
  background: var(--fr-surface);
  border: 1px solid var(--fr-line);
  border-radius: 4px;
  font-size: 16px; /* prevents iOS zoom-on-focus */
}
.fr-check { display: flex; align-items: center; gap: 6px; }

/* Stage */
.fr-stage { grid-area: stage; position: relative; min-height: 0; }
#fr-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.fr-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 24px;
  text-align: center;
  color: var(--fr-muted);
  pointer-events: none;
}
.fr-hint[hidden] { display: none; }

.fr-panel {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  width: min(420px, calc(100% - 24px));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--fr-surface);
  border: 1px solid var(--fr-line);
  border-radius: 6px;
}
.fr-panel[hidden] { display: none; }

.fr-guide-hint {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 24px);
  margin: 0;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.75);
  border-radius: 4px;
  color: var(--fr-muted);
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
  pointer-events: none;
}
.fr-guide-hint strong { color: var(--fr-fg); font-weight: bold; }
.fr-guide-hint[hidden] { display: none; }

.fr-toast {
  position: absolute;
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 8px 14px;
  background: var(--fr-fg);
  color: var(--fr-bg);
  border-radius: 4px;
  font-size: 14px;
  pointer-events: none;
}
.fr-toast[hidden] { display: none; }

/* Graph */
#fr-canvas .fr-edge { stroke: #bbb; stroke-width: 2; }
#fr-canvas .fr-edge.is-selected { stroke: var(--fr-accent); stroke-width: 3; }
#fr-canvas .fr-arrowhead { fill: #bbb; }
#fr-canvas .fr-arrowhead.is-selected { fill: var(--fr-accent); }
#fr-canvas .fr-edge-hit { stroke: transparent; stroke-width: 18; cursor: pointer; }
#fr-canvas .fr-label { cursor: pointer; }
#fr-canvas .fr-label rect { fill: var(--fr-accent); }
#fr-canvas .fr-label.is-selected rect { fill: var(--fr-fg); }
#fr-canvas .fr-label text { fill: var(--fr-fg); font-size: 12px; font-family: var(--fr-font); }
#fr-canvas .fr-label.is-selected text { fill: var(--fr-bg); }
#fr-canvas .fr-node { cursor: grab; }
#fr-canvas .fr-node image { pointer-events: none; }
#fr-canvas .fr-node-frame { fill: transparent; stroke: #444; stroke-width: 1; }
#fr-canvas .fr-node.is-root .fr-node-frame { stroke: var(--fr-fg); stroke-width: 2; }
#fr-canvas .fr-node.is-selected .fr-node-frame { stroke: var(--fr-accent); stroke-width: 5; }
#fr-canvas .fr-node-caption { pointer-events: none; }
#fr-canvas .fr-node-caption-bg { fill: var(--fr-bg); }
#fr-canvas .fr-node-title,
#fr-canvas .fr-node-year { font-size: 13px; font-family: var(--fr-font); pointer-events: none; }
#fr-canvas .fr-node-title { fill: var(--fr-fg); }
#fr-canvas .fr-node-year { fill: var(--fr-muted); }

/* Compass */
#fr-canvas .fr-axis { stroke: var(--fr-axis); stroke-width: 1.5; }
#fr-canvas .fr-axis-label { fill: var(--fr-fg); font-family: var(--fr-font); font-weight: bold; pointer-events: none; }
#fr-canvas .fr-axis-label.is-placeholder { fill: var(--fr-muted); font-weight: normal; }
#fr-canvas .fr-axis-label.is-selected { fill: var(--fr-accent); }
#fr-canvas .fr-axis-hit { fill: transparent; cursor: pointer; }

.fr-panel input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 0 8px;
  background: var(--fr-bg);
  border: 1px solid var(--fr-line);
  border-radius: 4px;
  font-size: 16px;
}
.fr-panel-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.fr button.fr-danger { border-color: var(--fr-accent); }

/* Tray */
.fr-tray-wrap {
  grid-area: tray;
  min-width: 0;
  border-top: 1px solid var(--fr-line);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
}
.fr-switch {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px 8px;
  font-size: 13px;
  color: var(--fr-muted);
}
.fr-tray {
  display: flex;
  gap: 8px;
  margin: 0;
  padding: 0 12px;
  list-style: none;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
}
.fr-tray li { flex: 0 0 auto; }
.fr .fr-tray-item {
  position: relative;
  display: block;
  width: 64px;
  height: 64px;
  min-height: 0;
  padding: 0;
  border: 1px solid var(--fr-line);
  border-radius: 2px;
  overflow: hidden;
  background: var(--fr-surface);
}
.fr-tray-item img { display: block; width: 100%; height: 100%; object-fit: cover; }
.fr-tray-item.is-placed img { opacity: 0.35; }
.fr-tray-item.is-placed::after {
  content: "✓";
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: 14px;
  font-weight: bold;
  color: var(--fr-fg);
}

@media (min-width: 768px) {
  .fr {
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "toolbar toolbar" "tray stage";
  }
  .fr-toolbar {
    grid-template-columns: auto auto 1fr auto auto;
    grid-template-areas: "home title options mode actions";
  }
  .fr-tray-wrap {
    border-top: 0;
    border-right: 1px solid var(--fr-line);
    overflow-y: auto;
  }
  .fr-tray {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    overflow: visible;
  }
  .fr .fr-tray-item { width: 100%; height: auto; aspect-ratio: 1; }
}
