/* AI Tutor — ChatGPT-style side panel */

.tutor-bubble {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 99;
  background: var(--teal-dark);
  color: var(--on-primary, #fff);
  border: none;
  border-radius: 2rem;
  padding: 0.75rem 1.25rem;
  font-family: var(--font-body);
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 0.25rem 1rem rgba(0, 0, 0, 0.3);
  transition: all 0.2s ease;
}

.tutor-bubble:hover {
  background: var(--teal);
}

.tutor-bubble-open {
  display: none;
}

/* Panel */
.tutor-panel {
  position: fixed;
  top: var(--tutor-top-offset, 3.75rem);
  right: 0;
  z-index: 99;
  width: var(--tutor-panel-width, 28rem);
  max-width: 100vw;
  height: calc(100vh - var(--tutor-top-offset, 3.75rem));
  background: var(--ink-light);
  border-left: 1px solid var(--ink-border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}

/* Drag-to-resize handle */
.tutor-resize-handle {
  position: absolute;
  top: 0;
  left: -3px;
  width: 0.375rem;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
}

.tutor-resize-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 2rem;
  background: var(--ink-border);
  border-radius: 1px;
  transition: background 0.15s;
}

.tutor-resize-handle:hover::after {
  background: var(--teal);
}

.tutor-panel.hidden {
  transform: translateX(100%);
  pointer-events: none;
}

/* When tutor is open: push content left of the panel */
body.tutor-open .book-container,
body.tutor-open .layout {
  margin-right: var(--tutor-panel-width, 28rem);
  transition: margin-right 0.25s ease;
}

/* Header */
.tutor-header {
  display: flex;
  align-items: center;
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--ink-border);
  gap: 0.5rem;
  flex-shrink: 0;
}

.tutor-title {
  flex: 1;
  font-family: var(--font-body);
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--ivory);
}

.tutor-menu-btn,
.tutor-header-btn {
  background: none;
  border: none;
  color: var(--ivory-muted);
  font-size: 1.125rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm, 0.25rem);
  line-height: 1;
}

.tutor-menu-btn:hover,
.tutor-header-btn:hover {
  color: var(--ivory);
  background: var(--surface-hover, rgba(255, 255, 255, 0.05));
}

.tutor-menu-btn.active {
  color: var(--teal);
}

.tutor-close-btn {
  font-size: 1.375rem;
}

/* History overlay */
.tutor-history {
  position: absolute;
  top: 3rem;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--ink-light);
  z-index: 10;
  display: flex;
  flex-direction: column;
}

.tutor-history.hidden {
  display: none;
}

.tutor-history-hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--ink-border);
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--ivory);
}

.tutor-history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.tutor-conv-item {
  display: flex;
  align-items: center;
  border-radius: 0.375rem;
  margin-bottom: 0.125rem;
  overflow: hidden;
}

.tutor-conv-item:hover {
  background: var(--surface-hover, rgba(255, 255, 255, 0.05));
}

.tutor-conv-item.active {
  background: var(--teal-glow);
}

.tutor-conv-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding: 0.625rem 0.75rem;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  min-width: 0;
}

.tutor-conv-delete {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--ivory-muted);
  font-size: 1.125rem;
  line-height: 1;
  padding: 0.625rem 0.625rem;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.15s,
    color 0.15s;
}

.tutor-conv-item:hover .tutor-conv-delete {
  opacity: 1;
}

.tutor-conv-delete:hover {
  color: var(--coral);
}

.tutor-conv-title {
  font-size: 0.8125rem;
  color: var(--ivory);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tutor-conv-meta {
  font-size: 0.6875rem;
  color: var(--ivory-muted);
  margin-top: 0.125rem;
}

.tutor-empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--ivory-muted);
  font-size: 0.8125rem;
}

/* Messages */
.tutor-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.tutor-msg {
  max-width: 90%;
  padding: 0.5rem 0.75rem;
  border-radius: 0.625rem;
  font-size: 0.8125rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.tutor-msg-user {
  align-self: flex-end;
  background: var(--teal-dark);
  color: var(--on-primary, #fff);
  border-bottom-right-radius: 0.1875rem;
  white-space: pre-wrap;
}

.tutor-msg-assistant {
  align-self: flex-start;
  background: var(--surface);
  color: var(--ivory-dim);
  border-bottom-left-radius: 0.1875rem;
  border: 1px solid var(--ink-border);
}

/* Markdown in assistant messages */
.tutor-markdown pre {
  background: var(--code-bg);
  border-radius: 0.375rem;
  padding: 0.5rem 0.625rem;
  margin: 0.375rem 0;
  overflow-x: auto;
}

.tutor-markdown code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
}

.tutor-markdown pre code {
  background: none;
  padding: 0;
}

.tutor-markdown code:not(pre code) {
  background: var(--code-inline-bg);
  padding: 0.0625rem 0.25rem;
  border-radius: 0.1875rem;
  color: var(--code-inline-color);
}

.tutor-markdown strong {
  color: var(--ivory);
  font-weight: 600;
}

/* Status */
.tutor-status {
  align-self: flex-start;
  color: var(--ivory-muted);
  font-size: 0.75rem;
  font-style: italic;
  padding: 0.125rem 0;
}

/* Input */
.tutor-input-form {
  display: flex;
  gap: 0.375rem;
  padding: 0.625rem 0.75rem;
  border-top: 1px solid var(--ink-border);
  flex-shrink: 0;
}

.tutor-input {
  flex: 1;
  background: var(--ink);
  border: 1px solid var(--ink-border);
  border-radius: 0.375rem;
  padding: 0.5rem 0.625rem;
  color: var(--ivory);
  font-family: var(--font-body);
  font-size: 0.8125rem;
}

.tutor-input:focus {
  outline: none;
  border-color: var(--teal);
}

.tutor-input::placeholder {
  color: var(--ivory-muted);
}

.tutor-send {
  background: var(--teal-dark);
  color: var(--on-primary, #fff);
  border: none;
  border-radius: 0.375rem;
  width: 2rem;
  height: 2rem;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.tutor-send:hover {
  background: var(--teal);
}

.tutor-cancel {
  background: none;
  border: 1px solid var(--ink-border);
  border-radius: 0.375rem;
  color: var(--ivory-muted);
  padding: 0.125rem 0.5rem;
  font-size: 0.6875rem;
  cursor: pointer;
  flex-shrink: 0;
}

.tutor-cancel:hover {
  border-color: var(--coral);
  color: var(--coral);
}

/* Rate limit message */
.tutor-rate-limit {
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--ink-border);
  border-radius: 0.5rem;
}

.tutor-rate-limit p {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.tutor-rate-limit-cta {
  display: inline-block;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--teal);
  text-decoration: none;
}

.tutor-rate-limit-cta:hover {
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 640px) {
  .tutor-panel {
    width: 100vw;
  }

  body.tutor-open .book-container,
  body.tutor-open .layout {
    width: 100%;
  }
}
