/* ============================================================
   RESET & CSS VARIABLES
   ============================================================ */

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

:root {
  --noise-avg: 247;
  --noise-var: 8;
  --mould-stroke: rgb(80, 80, 80);
  --cast-fill-bottom: rgb(80, 80, 80);
  --cast-fill-left: rgb(143, 143, 143);
  --cast-fill-right: rgb(107, 107, 107);
}

:root.dark {
  --noise-avg: 16;
  --noise-var: 2;
  --mould-stroke: rgb(255, 0, 0);
  --cast-fill-bottom: rgb(255, 0, 0);
  --cast-fill-left: rgb(143, 0, 0);
  --cast-fill-right: rgb(191, 0, 0);
}

/* ============================================================
   BASE LAYOUT
   ============================================================ */

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

#crucible {
  width: 100%;
  height: 100%;
  cursor: grab;
  background-repeat: repeat;
}

#crucible.dragging {
  cursor: grabbing;
}

/* ============================================================
   THEME TOGGLE
   ============================================================ */

#theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1000;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: rgba(128, 128, 128, 0.3);
  color: inherit;
  font-size: 20px;
  cursor: pointer;
  transition: background 0.2s;
}

#theme-toggle:hover {
  background: rgba(128, 128, 128, 0.5);
}

:root.dark #theme-toggle {
  color: #eee;
}

/* ============================================================
   CHAT PANEL
   ============================================================ */

#chat-panel {
  position: fixed;
  bottom: 16px;
  right: 16px;
  width: 360px;
  height: 480px;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
  z-index: 2000;
  transition: all 0.2s ease;
}

:root.dark #chat-panel {
  background: rgba(32, 32, 32, 0.95);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

#chat-panel.maximized {
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  border-radius: 0;
}

#chat-panel.minimized {
  height: 44px;
  overflow: hidden;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(128, 128, 128, 0.2);
  flex-shrink: 0;
}

#chat-title {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  font-weight: 600;
  color: #333;
}

:root.dark #chat-title {
  color: #eee;
}

#chat-controls {
  display: flex;
  gap: 8px;
}

#chat-controls button {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: rgba(128, 128, 128, 0.15);
  color: #555;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s;
}

#chat-controls button:hover {
  background: rgba(128, 128, 128, 0.3);
}

:root.dark #chat-controls button {
  color: #ccc;
}

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-message {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 14px;
  border-radius: 12px;
  max-width: 85%;
  word-wrap: break-word;
  user-select: text;
  -webkit-user-select: text;
  cursor: text;
}

.chat-message.user {
  align-self: flex-end;
  background: #007aff;
  color: white;
}

.chat-message.assistant {
  align-self: flex-start;
  background: rgba(128, 128, 128, 0.15);
  color: #333;
}

:root.dark .chat-message.assistant {
  background: rgba(255, 255, 255, 0.1);
  color: #eee;
}

/* Markdown styling in chat messages */
.chat-message p { margin: 0 0 0.5em 0; }
.chat-message p:last-child { margin-bottom: 0; }
.chat-message code {
  background: rgba(128, 128, 128, 0.2);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-family: ui-monospace, monospace;
  font-size: 0.9em;
}
.chat-message pre {
  background: rgba(0, 0, 0, 0.1);
  padding: 0.75em;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0.5em 0;
}
:root.dark .chat-message pre {
  background: rgba(0, 0, 0, 0.3);
}
.chat-message pre code {
  background: none;
  padding: 0;
}
.chat-message ul, .chat-message ol {
  margin: 0.5em 0;
  padding-left: 1.5em;
}
.chat-message li { margin: 0.25em 0; }
.chat-message a { color: #007aff; }
:root.dark .chat-message a { color: #4da3ff; }

#chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid rgba(128, 128, 128, 0.2);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid rgba(128, 128, 128, 0.3);
  border-radius: 20px;
  background: transparent;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: #333;
  resize: none;
  max-height: 120px;
  outline: none;
  transition: border-color 0.15s;
}

#chat-input:focus {
  border-color: #007aff;
}

:root.dark #chat-input {
  color: #eee;
  border-color: rgba(128, 128, 128, 0.4);
}

#chat-input::placeholder {
  color: #999;
}

#chat-send {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: #007aff;
  color: white;
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
}

#chat-send:hover {
  background: #0066dd;
}

#chat-send:disabled {
  background: rgba(128, 128, 128, 0.3);
  cursor: not-allowed;
}

/* ============================================================
   FRAGMENTS - Container & Header
   ============================================================ */

.fragment {
  position: fixed;
  min-width: 280px;
  max-width: 450px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  user-select: text;
}

:root.dark .fragment {
  background: rgba(30, 30, 30, 0.95);
  border-color: rgba(255, 255, 255, 0.2);
}

.fragment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.05);
  cursor: move;
}

:root.dark .fragment-header {
  background: rgba(255, 255, 255, 0.05);
}

.fragment-title {
  font-size: 13px;
  font-weight: 600;
  color: #333;
}

:root.dark .fragment-title {
  color: #eee;
}

.fragment-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.fragment-close {
  width: 20px;
  height: 20px;
  border: none;
  background: transparent;
  font-size: 18px;
  line-height: 1;
  color: #666;
  cursor: pointer;
  border-radius: 4px;
}

.fragment-close:hover {
  background: rgba(0, 0, 0, 0.1);
  color: #333;
}

:root.dark .fragment-close {
  color: #aaa;
}

:root.dark .fragment-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

/* ============================================================
   FRAGMENTS - Pin Button
   ============================================================ */

.fragment-pin {
  width: 20px;
  height: 20px;
  border: none;
  background: transparent;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0.4;
  transition: opacity 0.15s;
}

.fragment-pin:hover {
  opacity: 0.7;
}

.fragment.pinned .fragment-pin {
  opacity: 1;
}

.fragment.pinned {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2), 0 0 0 2px rgba(255, 100, 100, 0.4);
}

/* ============================================================
   FRAGMENTS - Content
   ============================================================ */

.fragment-content {
  padding: 12px;
  max-height: 400px;
  overflow-y: auto;
}

/* ============================================================
   FRAGMENTS - Item Lists (Models & Fields)
   ============================================================ */

.item-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.item-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: 4px;
}

.item-row:hover {
  background: rgba(0, 0, 0, 0.04);
}

:root.dark .item-row:hover {
  background: rgba(255, 255, 255, 0.04);
}

.item-icon {
  flex-shrink: 0;
}

.item-name {
  font-size: 13px;
  color: #333;
  white-space: nowrap;
}

:root.dark .item-name {
  color: #eee;
}

.item-name.clickable,
.model-name {
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}

.item-name.clickable:hover,
.model-name:hover {
  color: #0066dd;
}

.item-type {
  font-size: 12px;
  color: #888;
  white-space: nowrap;
}

.item-type.clickable,
.type-ref.clickable {
  cursor: pointer;
  color: #0066dd;
  text-decoration: underline;
  text-decoration-style: dotted;
}

.item-type.clickable:hover,
.type-ref.clickable:hover {
  color: #0044aa;
}

.item-meta {
  font-size: 11px;
  color: #999;
  margin-left: auto;
}

.required-indicator {
  color: #cc0000;
  margin-left: 2px;
}

/* ============================================================
   FRAGMENTS - Model Sections
   ============================================================ */

.model-section {
  margin-bottom: 8px;
}

.model-header {
  background: rgba(0, 0, 0, 0.03);
  margin-bottom: 2px;
}

:root.dark .model-header {
  background: rgba(255, 255, 255, 0.03);
}

/* ============================================================
   FRAGMENTS - Value Display
   ============================================================ */

.item-value {
  font-size: 12px;
  color: #555;
  margin-left: auto;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

:root.dark .item-value {
  color: #aaa;
}

.value-preview {
  cursor: default;
}

.value-expander {
  border: none;
  background: transparent;
  font-size: 10px;
  cursor: pointer;
  color: #666;
  padding: 0 4px;
}

.value-full {
  font-family: monospace;
  font-size: 11px;
  background: rgba(0, 0, 0, 0.05);
  padding: 8px;
  border-radius: 4px;
  margin-top: 4px;
  max-height: 150px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

:root.dark .value-full {
  background: rgba(255, 255, 255, 0.05);
}

/* ============================================================
   FRAGMENTS - JSON Button & Fragment
   ============================================================ */

.json-btn {
  border: none;
  background: rgba(0, 0, 0, 0.08);
  font-family: monospace;
  font-size: 10px;
  color: #666;
  padding: 2px 5px;
  border-radius: 3px;
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
}

.json-btn:hover {
  background: rgba(0, 0, 0, 0.15);
  color: #333;
}

:root.dark .json-btn {
  background: rgba(255, 255, 255, 0.1);
  color: #aaa;
}

:root.dark .json-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.json-fragment {
  min-width: 280px;
  max-width: 500px;
}

.json-fragment .fragment-content {
  background: #f5f5f5;
  padding: 8px;
}

:root.dark .json-fragment .fragment-content {
  background: #2a2a2a;
}

.json-content pre {
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  padding: 12px;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  background: #fff;
  border-radius: 4px;
  color: #333;
  max-height: 400px;
  overflow: auto;
}

:root.dark .json-content pre {
  background: #1a1a1a;
  color: #ddd;
}

/* ============================================================
   FRAGMENTS - Dangling Section
   ============================================================ */

.dangling-separator {
  font-size: 11px;
  color: #999;
  text-align: center;
  padding: 8px 0;
  margin: 8px 0;
  border-top: 1px dashed rgba(0, 0, 0, 0.15);
}

:root.dark .dangling-separator {
  border-top-color: rgba(255, 255, 255, 0.15);
}

.dangling-list .model-section {
  opacity: 0.7;
}

.dangling-list .model-header {
  background: rgba(255, 150, 50, 0.1);
}

:root.dark .dangling-list .model-header {
  background: rgba(255, 150, 50, 0.15);
}
