/* =========================================================
   PISCA - ChatGPT-style UI
   Dark + Light Mode
   ========================================================= */

:root {
  color-scheme: dark;

  --bg: #212121;
  --sidebar-bg: #171717;
  --surface: #2f2f2f;
  --surface-hover: #3d3d3d;

  --border: rgba(255, 255, 255, 0.1);

  --text: #ececec;
  --muted: #8e8e8e;

  --accent: #10a37f;
  --accent-hover: #0e8e6e;

  --danger: #ff6b6b;

  --radius: 12px;
}

/* =========================================================
   LIGHT MODE
   ========================================================= */

html[data-theme="light"] {
  color-scheme: light;

  --bg: #ffffff;
  --sidebar-bg: #f9f9f9;
  --surface: #f0f0f0;
  --surface-hover: #e4e4e4;

  --border: rgba(0, 0, 0, 0.1);

  --text: #0d0d0d;
  --muted: #6e6e6e;

  --danger: #dc4c4c;
}

/* =========================================================
   RESET
   ========================================================= */

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);

  font:
    15px/1.6
    Inter,
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;

  transition:
    background 0.2s ease,
    color 0.2s ease;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: 0;

  transition:
    background 0.15s ease,
    color 0.15s ease;
}

/* =========================================================
   APP
   ========================================================= */

.app-shell {
  height: 100vh;
  overflow: hidden;
}

.workspace {
  display: grid;

  grid-template-columns: 260px 1fr;

  height: 100vh;
}

/* =========================================================
   SIDEBAR
   ========================================================= */

.sidebar {
  display: flex;
  flex-direction: column;

  padding: 8px;

  border-right: 1px solid var(--border);

  background: var(--sidebar-bg);

  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  padding: 8px 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  width: 28px;
  height: 28px;

  object-fit: contain;
}

.brand h1 {
  margin: 0;

  font-size: 16px;
  font-weight: 600;
}

.new-chat-button {
  display: flex;
  align-items: center;
  gap: 8px;

  width: 100%;

  padding: 10px 12px;
  margin: 8px 0;

  border: 1px solid var(--border);
  border-radius: var(--radius);

  color: var(--text);

  font-size: 14px;
  text-align: left;
}

.new-chat-button:hover {
  background: var(--surface-hover);
}

.new-chat-icon {
  font-size: 14px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;

  padding: 16px 8px 8px;
}

.section-heading h2 {
  margin: 0;

  color: var(--muted);

  font-size: 12px;
  font-weight: 600;
}

.text-button {
  padding: 4px 8px;

  border-radius: 8px;

  color: var(--muted);

  font-size: 12px;
}

.text-button:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 6px;

  width: 100%;

  padding: 8px 10px;

  border: 0;
  border-radius: 8px;

  color: var(--text);

  text-align: left;
}

.conversation-item:hover {
  background: var(--surface-hover);
}

.conversation-item span {
  flex: 1;

  overflow: hidden;

  font-size: 14px;

  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-mini {
  padding: 2px 6px;

  border-radius: 6px;

  color: var(--muted);

  opacity: 0;
}

.conversation-item:hover .delete-mini {
  opacity: 1;
}

.delete-mini:hover {
  color: var(--danger);
  background: var(--surface);
}

/* =========================================================
   MEMORY
   ========================================================= */

.memory-section {
  margin-top: 8px;
}

.memory-note {
  margin: 4px 8px 8px;

  color: var(--muted);

  font-size: 12px;
  line-height: 1.5;
}

.memory-card {
  margin: 4px 0;
  padding: 10px;

  border: 1px solid var(--border);
  border-radius: var(--radius);

  background: var(--surface);
}

.memory-card header {
  display: flex;
  justify-content: space-between;

  gap: 6px;
}

.memory-card strong {
  font-size: 13px;
}

.memory-card p {
  margin: 4px 0 0;

  color: var(--text);

  font-size: 13px;

  overflow-wrap: anywhere;
}

.memory-card small {
  color: var(--muted);

  font-size: 11px;
  text-transform: capitalize;
}

.memory-actions {
  display: flex;
  gap: 2px;
}

.memory-actions .delete-mini {
  opacity: 1;
  padding: 2px 5px;
}

.empty-state {
  margin: 8px;

  color: var(--muted);

  font-size: 13px;
}

.sidebar-footer {
  margin-top: auto;

  padding: 8px 4px;

  display: flex;
}

.theme-toggle {
  width: 36px;
  height: 36px;

  display: grid;
  place-items: center;

  border-radius: 8px;

  color: var(--text);

  font-size: 16px;
}

.theme-toggle:hover {
  background: var(--surface-hover);
}

/* =========================================================
   SIDEBAR BACKDROP (mobile)
   ========================================================= */

.sidebar-backdrop {
  display: none;

  position: fixed;
  inset: 0;

  background: rgba(0, 0, 0, 0.5);

  z-index: 19;
}

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

.chat-panel {
  display: flex;
  flex-direction: column;

  height: 100vh;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;

  height: 56px;

  padding: 0 12px;

  border-bottom: 1px solid var(--border);
}

.chat-header-logo {
  width: 26px;
  height: 26px;

  display: none;

  object-fit: contain;
}

.icon-button {
  width: 36px;
  height: 36px;

  display: grid;
  place-items: center;

  border-radius: 8px;

  color: var(--text);

  font-size: 18px;
}

.icon-button:hover {
  background: var(--surface-hover);
}

.chat-messages {
  flex: 1;

  overflow-y: auto;

  padding: 24px 0 32px;
}

.chat-messages::-webkit-scrollbar,
.sidebar::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb {
  background: var(--border);

  border-radius: 10px;
}

/* =========================================================
   MESSAGE
   ========================================================= */

.message {
  display: flex;
  gap: 12px;

  max-width: 768px;
  margin: 0 auto 28px;

  padding: 0 16px;

  animation: messageIn 0.2s ease;
}

@keyframes messageIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.avatar {
  width: 28px;
  height: 28px;

  flex: 0 0 auto;

  border-radius: 50%;

  overflow: hidden;
}

.avatar-img {
  display: block;

  width: 100%;
  height: 100%;

  object-fit: cover;
}

.message .content {
  min-width: 0;
  flex: 1;
}

.bubble {
  color: var(--text);

  font-size: 15px;
  line-height: 1.6;

  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.user-message {
  justify-content: flex-end;
}

.user-message .content {
  max-width: 75%;

  padding: 10px 16px;

  border-radius: 18px;

  background: var(--surface);
}

.message time {
  display: block;

  margin-top: 6px;

  color: var(--muted);

  font-size: 11px;
}

.user-message time {
  text-align: right;
}

.typing .bubble {
  color: var(--muted);
}

/* =========================================================
   COMPOSER
   ========================================================= */

.composer {
  width: 100%;
  max-width: 768px;
  margin: 0 auto;

  padding: 8px 16px 20px;
}

.composer-wrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;

  padding: 8px 8px 8px 16px;

  border: 1px solid var(--border);
  border-radius: 24px;

  background: var(--surface);
}

.composer textarea {
  flex: 1;

  min-height: 24px;
  max-height: 200px;

  padding: 8px 0;

  resize: none;
  outline: none;
  border: 0;

  background: transparent;

  color: var(--text);

  font-size: 15px;
  line-height: 1.5;
}

.composer textarea::placeholder {
  color: var(--muted);
}

.send-button {
  width: 32px;
  height: 32px;

  flex: 0 0 auto;

  display: grid;
  place-items: center;

  border-radius: 50%;

  color: var(--bg);

  background: var(--text);

  font-size: 14px;
}

.send-button:hover {
  background: var(--accent);
  color: #fff;
}

.send-button:disabled {
  opacity: 0.4;
  cursor: wait;
}

/* =========================================================
   DIALOG
   ========================================================= */

dialog {
  width: min(440px, calc(100% - 32px));

  padding: 0;

  border: 1px solid var(--border);
  border-radius: 16px;

  color: var(--text);

  background: var(--surface);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

dialog form {
  padding: 24px;
}

.dialog-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;

  margin-bottom: 16px;
}

.dialog-heading h2 {
  margin: 0;

  font-size: 18px;
  font-weight: 600;
}

label {
  display: block;

  margin: 12px 0;

  color: var(--muted);

  font-size: 13px;
}

label input,
label textarea {
  display: block;

  width: 100%;

  margin-top: 6px;
  padding: 10px 12px;

  outline: none;

  border: 1px solid var(--border);
  border-radius: 12px;

  color: var(--text);

  background: var(--bg);
}

label input:focus,
label textarea:focus {
  border-color: var(--accent);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;

  gap: 8px;

  margin-top: 20px;
}

.primary-button,
.secondary-button {
  padding: 8px 16px;

  border-radius: 10px;

  font-size: 14px;
}

.primary-button {
  color: #fff;

  background: var(--accent);
}

.primary-button:hover {
  background: var(--accent-hover);
}

.secondary-button {
  border: 1px solid var(--border);

  color: var(--text);

  background: transparent;
}

.secondary-button:hover {
  background: var(--surface-hover);
}

/* =========================================================
   MOBILE
   ========================================================= */

@media (max-width: 768px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;

    z-index: 20;

    top: 0;
    bottom: 0;
    left: 0;

    width: min(280px, 85vw);

    transform: translateX(-100%);

    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar.open ~ .sidebar-backdrop {
    display: block;
  }

  .chat-header-logo {
    display: block;
  }

  .message {
    padding: 0 12px;
    margin-bottom: 22px;
  }

  .composer {
    padding: 8px 12px 16px;
  }
}

/* =========================================================
   SIDEBAR COLLAPSE (desktop)
   ========================================================= */

@media (min-width: 769px) {
  .app-shell.sidebar-collapsed .sidebar {
    display: none;
  }

  .app-shell.sidebar-collapsed .workspace {
    grid-template-columns: 1fr;
  }

  .app-shell.sidebar-collapsed .chat-header-logo {
    display: block;
  }
}

/* =========================================================
   VERY SMALL SCREEN
   ========================================================= */

@media (max-width: 420px) {
  .user-message .content {
    max-width: 85%;
  }
}