/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #F5F0E8;
  --hub: #C4725A;
  --entry: #8B9E7E;
  --edge-hub: #D4C5A9;
  --edge-cross: #B5A48A;
  --text: #3D3229;
  --text-light: #7A6E62;
  --selected: #D4A853;
  --panel-bg: #FAF6F0;
  --panel-border: #D4C5A9;
  --divider: #D4C5A9;
  --font-heading: 'Libre Baskerville', Georgia, 'Times New Roman', serif;
  --font-body: 'Libre Baskerville', Georgia, 'Times New Roman', serif;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  display: flex;
  flex-direction: column;
}

/* ===== Header ===== */
#page-header {
  flex-shrink: 0;
  padding: 16px 24px;
  border-bottom: 1px solid var(--panel-border);
}

#header-row {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
}

#view-buttons {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

#graph-toggle {
  display: none;
}

.view-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  color: var(--text-light);
  cursor: pointer;
  transition: all 150ms ease-out;
}

.view-btn:hover {
  border-color: var(--text-light);
  color: var(--text);
}

.view-btn.active {
  background: var(--text);
  color: var(--panel-bg);
  border-color: var(--text);
}

#page-title {
  font-family: var(--font-heading);
  font-size: 1.1rem;
  font-variant: small-caps;
  letter-spacing: 0.08em;
  color: var(--text-light);
  font-weight: 400;
}

/* ===== Filter Bar ===== */
#filter-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  flex: 1;
  min-width: 0;
}

.filter-tag {
  font-family: var(--font-body);
  font-size: 0.72rem;
  font-style: italic;
  letter-spacing: 0.03em;
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  color: var(--text-light);
  cursor: pointer;
  transition: all 150ms ease-out;
  user-select: none;
}

.filter-tag:hover {
  border-color: var(--text-light);
  color: var(--text);
}

.filter-tag.active {
  background: var(--text);
  color: var(--panel-bg);
  border-color: var(--text);
}

.filter-tag.active:hover {
  opacity: 0.85;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex: 1;
  min-height: 0;
  transition: all 250ms ease-out;
}

#graph-container {
  flex: 1;
  min-width: 0;
  position: relative;
  transition: all 250ms ease-out;
}

#graph {
  width: 100%;
  height: 100%;
  display: block;
}

/* ===== Graph Nodes ===== */
.node-circle {
  cursor: pointer;
  transition: r 150ms ease-out;
}

.node-circle:hover {
  filter: brightness(1.1);
}

.node-label {
  font-family: var(--font-heading);
  fill: var(--text);
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
}

.node-label-hub {
  font-variant: small-caps;
  letter-spacing: 0.06em;
  fill: var(--text);
}

.edge-hub {
  stroke: var(--edge-hub);
  stroke-width: 1;
}

.edge-cross {
  stroke: var(--edge-cross);
  stroke-width: 1;
  stroke-dasharray: 4 3;
}

/* ===== Filtered / Dimmed State ===== */
.node-group.dimmed {
  opacity: 0.15;
  transition: opacity 250ms ease-out;
}

.node-group:not(.dimmed) {
  transition: opacity 250ms ease-out;
}

.edge-hub.dimmed,
.edge-cross.dimmed {
  opacity: 0.08;
  transition: opacity 250ms ease-out;
}

/* ===== Selected State ===== */
.node-selected {
  stroke: var(--selected);
  stroke-width: 3;
}

@keyframes node-pulse {
  0% { stroke-width: 3; stroke-opacity: 1; }
  50% { stroke-width: 6; stroke-opacity: 0.6; }
  100% { stroke-width: 3; stroke-opacity: 1; }
}

.node-pulse {
  stroke: var(--selected);
  animation: node-pulse 600ms ease-out;
}

/* ===== Side Panel (Desktop) ===== */
#panel {
  width: 350px;
  min-width: 350px;
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-border);
  overflow-y: auto;
  transition: margin-right 250ms ease-out, opacity 250ms ease-out;
  position: relative;
  padding: 24px;
}

#panel.closed {
  margin-right: -350px;
  min-width: 0;
  width: 0;
  padding: 0;
  opacity: 0;
  pointer-events: none;
  border: none;
}

#panel-drag-handle {
  display: none;
}

/* ===== Panel Content ===== */
#panel-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 1.6rem;
  color: var(--text-light);
  cursor: pointer;
  font-family: var(--font-heading);
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
  transition: color 150ms, background 150ms;
}

#panel-close:hover {
  color: var(--text);
  background: rgba(0, 0, 0, 0.05);
}

#panel-image-wrapper {
  margin-bottom: 16px;
  display: none;
}

#panel-image-wrapper.visible {
  display: block;
}

#panel-image {
  width: 100%;
  border-radius: 6px;
  object-fit: cover;
  max-height: 200px;
}

#panel-title {
  font-family: var(--font-heading);
  font-size: 1.5rem;
  font-variant: normal;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 400;
  margin-top: 8px;
  color: var(--text);
}

/* Word type tags in panel */
#panel-word-types {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

#panel-word-types:empty {
  display: none;
}

.word-type-tag {
  font-family: var(--font-body);
  font-size: 0.7rem;
  font-style: italic;
  letter-spacing: 0.03em;
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  color: var(--text-light);
  cursor: pointer;
  transition: color 150ms;
}

.word-type-tag:hover {
  color: var(--text);
  border-color: var(--text-light);
}

/* Hub gets a larger title */
#panel.hub-entry #panel-title {
  font-size: 1.8rem;
  color: var(--hub);
}

#panel-divider {
  border: none;
  border-top: 1px solid var(--divider);
  margin: 12px 0 16px;
}

#panel-description {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 0.95rem;
  line-height: 1.65;
  color: var(--text);
}

.desc-word-types {
  font-weight: 700;
  font-style: normal;
  font-variant: small-caps;
  letter-spacing: 0.04em;
  display: block;
  margin-bottom: 2px;
}

.desc-word-alt {
  display: block;
  font-size: 0.85rem;
  color: var(--text-light);
  margin-bottom: 8px;
}

.desc-label {
  font-style: normal;
  font-variant: small-caps;
  letter-spacing: 0.03em;
}

.desc-word-usage {
  display: block;
  margin-top: 12px;
  font-style: italic;
  color: var(--text-light);
  font-size: 0.88rem;
}

#panel-see-also {
  margin-top: 20px;
  font-size: 0.88rem;
  display: none;
}

#panel-see-also.visible {
  display: block;
}

.see-also-label {
  color: var(--text-light);
  font-style: italic;
}

.see-also-link {
  color: var(--hub);
  cursor: pointer;
  text-decoration: none;
  border-bottom: 1px dotted var(--hub);
  transition: color 150ms;
  margin-left: 4px;
  font-variant: small-caps;
  letter-spacing: 0.04em;
}

.see-also-link:hover {
  color: var(--text);
}

.see-also-separator {
  color: var(--text-light);
  margin: 0 2px;
}

/* ===== List Panel (Left Side) ===== */
#list-container {
  width: 300px;
  min-width: 300px;
  background: var(--panel-bg);
  border-right: 1px solid var(--panel-border);
  overflow-y: auto;
  transition: margin-left 250ms ease-out, opacity 250ms ease-out;
  position: relative;
  padding: 24px;
}

#list-container.closed {
  margin-left: -300px;
  min-width: 0;
  width: 0;
  padding: 0;
  opacity: 0;
  pointer-events: none;
  border: none;
}

#list-sort-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
}

#list-sort-label {
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-variant: small-caps;
  letter-spacing: 0.06em;
  color: var(--text-light);
  margin-right: 4px;
}

.sort-btn {
  font-family: var(--font-body);
  font-size: 0.72rem;
  font-style: italic;
  letter-spacing: 0.03em;
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  color: var(--text-light);
  cursor: pointer;
  transition: all 150ms ease-out;
}

.sort-btn:hover {
  border-color: var(--text-light);
  color: var(--text);
}

.sort-btn.active {
  background: var(--text);
  color: var(--panel-bg);
  border-color: var(--text);
}

#list-entries {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.list-entry {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 150ms ease-out;
}

.list-entry:hover {
  background: rgba(0, 0, 0, 0.04);
}

.list-entry-title {
  font-family: var(--font-heading);
  font-size: 1rem;
  color: var(--text);
  flex-shrink: 0;
}

.list-entry-types {
  font-family: var(--font-body);
  font-size: 0.75rem;
  font-style: italic;
  color: var(--text-light);
  flex: 1;
  min-width: 0;
}

.list-entry-edges {
  font-family: var(--font-body);
  font-size: 0.72rem;
  color: var(--text-light);
  flex-shrink: 0;
  min-width: 30px;
  text-align: right;
}

/* Group headings for word-type sort */
.list-group-heading {
  font-family: var(--font-heading);
  font-size: 0.78rem;
  font-variant: small-caps;
  letter-spacing: 0.08em;
  color: var(--text-light);
  padding: 16px 14px 4px;
  border-bottom: 1px solid var(--divider);
  margin-bottom: 2px;
}

.list-group-heading:first-child {
  padding-top: 0;
}

/* ===== Mobile (Phones) ===== */
@media (max-width: 768px) {
  html, body {
    height: 100dvh;
  }

  /* --- Header --- */
  #page-header {
    padding: 10px 14px;
  }

  #header-row {
    flex-wrap: wrap;
  }

  #page-title {
    order: -1;
    width: 100%;
    text-align: center;
    margin-bottom: 6px;
    font-size: 0.95rem;
  }

  #graph-toggle {
    display: flex;
  }

  #filter-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    padding: 2px 0;
  }

  #filter-bar::-webkit-scrollbar {
    display: none;
  }

  .filter-tag {
    min-height: 34px;
    display: flex;
    align-items: center;
    padding: 4px 12px;
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* --- Layout --- */
  #app {
    flex-direction: column;
    position: relative;
  }

  .mobile-hidden {
    display: none !important;
  }

  #graph-container {
    flex: 1;
    min-height: 0;
  }

  /* --- List (full screen on mobile) --- */
  #list-container {
    width: 100%;
    min-width: 0;
    border-right: none;
    position: absolute;
    inset: 0;
    z-index: 5;
    margin-left: 0;
    opacity: 1;
    transition: none;
  }

  #list-container.closed {
    margin-left: 0;
    width: 100%;
    padding: 24px;
    opacity: 1;
    pointer-events: auto;
    border: none;
  }

  .list-entry {
    min-height: 48px;
    align-items: center;
  }

  .sort-btn {
    min-height: 34px;
    display: flex;
    align-items: center;
  }

  /* --- Detail Panel (Bottom Sheet) --- */
  #panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    min-width: 0;
    height: 50%;
    max-height: 60vh;
    border-left: none;
    border-top: none;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -4px 20px rgba(61, 50, 41, 0.12);
    z-index: 10;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 28px 20px 20px;
    transform: translateY(0);
    transition: transform 250ms ease-out;
    margin-right: 0;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }

  #panel::before {
    display: none;
  }

  #panel-drag-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 28px;
    cursor: grab;
    touch-action: none;
    z-index: 1;
  }

  #panel-drag-handle::after {
    content: '';
    width: 36px;
    height: 4px;
    background: var(--panel-border);
    border-radius: 2px;
  }

  #panel-drag-handle:active {
    cursor: grabbing;
  }

  #panel.dragging {
    transition: none;
  }

  #panel.closed {
    transform: translateY(100%);
    margin-right: 0;
    width: 100%;
    min-width: 0;
    height: 50%;
    padding: 28px 20px 20px;
    opacity: 1;
    pointer-events: none;
  }

  #panel-close {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
