/* 通用基础样式 */
:root {
  --primary-color: #007bff;
  --primary-hover-color: #0056b3;
  --text-color-light: #212529;
  --text-color-dark: #e0e0e0;
  --bg-light: #f9fafb;
  --bg-dark: #1a1a1a;
  --panel-bg-light: #f1f3f5;
  --panel-bg-dark: #2c2c2c;
  --border-light: #dee2e6;
  --border-dark: #404040;
  --button-bg-light: #ffffff;
  --button-bg-dark: #3a3a3a;
  --input-bg-light: #ffffff;
  --input-bg-dark: #333333;
  --user-msg-bg-light: var(--primary-color);
  --user-msg-bg-dark: var(--primary-hover-color);
  --assistant-msg-bg-light: #e9ecef;
  --assistant-msg-bg-dark: #3f3f3f;
  --error-bg-light: #f8d7da;
  --error-text-light: #721c24;
  --error-border-light: #f5c6cb;
  --error-bg-dark: #5a2d2d;
  --error-text-dark: #f8d7da;
  --error-border-dark: #723b3b;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  margin: 0;
  background: var(--bg-light);
  color: var(--text-color-light);
  transition: background 0.3s, color 0.3s;
  line-height: 1.6;
  font-size: 16px;
}

#layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* 左侧历史面板 */
#history-panel {
  width: 280px;
  background: var(--panel-bg-light);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-light);
  transition: transform 0.3s ease, width 0.3s ease;
  flex-shrink: 0;
}

#history-panel .panel-header {
  padding: 15px 20px;
  border-bottom: 1px solid var(--border-light);
  background-color: var(--panel-bg-light); /* Slightly different or same as panel bg */
}
#history-panel .panel-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

#history-panel .panel-content {
  padding: 15px 20px;
  box-sizing: border-box;
  flex-grow: 1;
  overflow-y: auto;
}
.panel-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text-color-light);
}
body.dark .panel-label {
  color: var(--text-color-dark);
}


#history-list button,
.history-action-btn {
  display: block;
  width: 100%;
  margin-bottom: 8px;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--border-light);
  border-radius: 6px;
  background: var(--button-bg-light);
  color: var(--text-color-light);
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  transition: background 0.2s, box-shadow 0.2s, border-color 0.2s;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#history-list button:hover,
.history-action-btn:hover {
  background: #e9ecef; /* Specific hover, not var for simplicity */
  border-color: #adb5bd;
}

.history-action-btn.danger {
    background-color: var(--error-bg-light);
    border-color: var(--error-border-light);
    color: var(--error-text-light);
}
.history-action-btn.danger:hover {
    background-color: #f1b0b7;
    border-color: #e89da5;
}


#history-list button.active-chat {
  background-color: var(--user-msg-bg-light);
  color: white;
  font-weight: 600;
  border-left: 4px solid var(--primary-hover-color);
  padding-left: 9px; /* Adjusted for border */
}

#search-box,
#history-panel select {
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 9px 12px;
  margin-bottom: 10px;
  box-sizing: border-box;
  font-size: 14px;
  width: 100%;
  background-color: var(--input-bg-light);
  color: var(--text-color-light);
}

/* 主聊天区域 */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative; /* For toggle button positioning */
}

#chat {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: var(--bg-light); /* Same as body bg or slightly different */
}

/* 聊天消息样式 */
.message {
  margin-bottom: 12px;
  padding: 10px 15px;
  border-radius: 12px;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-width: 80%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.07);
  line-height: 1.5;
  display: flex; /* To use align-self */
  flex-direction: column; /* Ensure content flows normally */
}

.user {
  background-color: var(--user-msg-bg-light);
  color: #ffffff;
  align-self: flex-end;
  margin-left: auto; /* Pushes to the right */
}

.assistant {
  background-color: var(--assistant-msg-bg-light);
  color: var(--text-color-light);
  align-self: flex-start;
  margin-right: auto; /* Pushes to the left */
}
.message.error-message {
    background-color: var(--error-bg-light);
    color: var(--error-text-light);
    border: 1px solid var(--error-border-light);
    font-size: 0.9em;
}


/* 输入区域 */
#input-container {
  display: flex;
  align-items: flex-end;
  padding: 15px 20px;
  gap: 10px;
  background: var(--panel-bg-light); /* Often white or slightly off-white */
  box-shadow: 0 -2px 6px rgba(0,0,0,0.05);
  border-top: 1px solid var(--border-light);
}

textarea#user-input {
  flex: 1;
  padding: 10px 12px;
  font-size: 16px;
  border-radius: 6px;
  border: 1px solid var(--border-light);
  background-color: var(--input-bg-light);
  color: var(--text-color-light);
  resize: vertical;
  min-height: 46px; /* Match button height */
  max-height: 200px;
  box-sizing: border-box;
  line-height: 1.4;
}

#input-container button#send-button {
  font-size: 16px;
  padding: 0 20px; /* Adjust padding for height */
  height: 46px; /* Fixed height */
  line-height: 44px; /* Align text vertically */
  border-radius: 6px;
  border: 1px solid var(--primary-color);
  background: var(--primary-color);
  color: white;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
  flex-shrink: 0;
}
#input-container button#send-button:hover {
  background: var(--primary-hover-color);
  border-color: var(--primary-hover-color);
}
#input-container button#send-button:disabled {
  background: #cccccc;
  border-color: #cccccc;
  cursor: not-allowed;
}


/* 夜间模式 */
body.dark {
  background: var(--bg-dark);
  color: var(--text-color-dark);
}

body.dark #history-panel {
  background: var(--panel-bg-dark);
  border-right: 1px solid var(--border-dark);
}
body.dark #history-panel .panel-header {
  background-color: var(--panel-bg-dark); /* Darker or same as panel */
  border-bottom: 1px solid var(--border-dark);
}

body.dark #history-list button,
body.dark .history-action-btn {
  background: var(--button-bg-dark);
  color: var(--text-color-dark);
  border: 1px solid var(--border-dark);
}
body.dark #history-list button:hover,
body.dark .history-action-btn:hover {
  background: #454545; /* Specific hover */
  border-color: #606060;
}
body.dark .history-action-btn.danger {
    background-color: var(--error-bg-dark);
    border-color: var(--error-border-dark);
    color: var(--error-text-dark);
}
body.dark .history-action-btn.danger:hover {
    background-color: #6b3939;
}


body.dark #history-list button.active-chat {
  background-color: var(--user-msg-bg-dark);
  border-left-color: var(--primary-color); /* Bright border for contrast */
  color: white;
}

body.dark #search-box,
body.dark #history-panel select {
  background: var(--input-bg-dark);
  color: var(--text-color-dark);
  border: 1px solid var(--border-dark);
}
body.dark #search-box::placeholder,
body.dark textarea#user-input::placeholder {
    color: #888;
}

body.dark #chat {
  background: var(--bg-dark); /* Or slightly different like #212121 */
}

body.dark .user {
  background-color: var(--user-msg-bg-dark);
}

body.dark .assistant {
  background-color: var(--assistant-msg-bg-dark);
  color: var(--text-color-dark);
}
body.dark .message.error-message {
    background-color: var(--error-bg-dark);
    color: var(--error-text-dark);
    border: 1px solid var(--error-border-dark);
}


body.dark #input-container {
  background: var(--panel-bg-dark); /* Usually same as history panel bg */
  border-top: 1px solid var(--border-dark);
}

body.dark textarea#user-input {
  background: var(--input-bg-dark);
  color: var(--text-color-dark);
  border: 1px solid var(--border-dark);
}

body.dark #input-container button#send-button {
  background: var(--primary-color); /* Keep primary color for action */
  border-color: var(--primary-color);
}
body.dark #input-container button#send-button:hover {
  background: var(--primary-hover-color);
  border-color: var(--primary-hover-color);
}
body.dark #input-container button#send-button:disabled {
  background: #555;
  border-color: #555;
}


/* Mobile Toggle Button for History Panel */
#toggle-history-btn {
  display: none; /* Hidden by default, shown by JS based on screen width */
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 1001;
  background: var(--button-bg-light);
  color: var(--text-color-light);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 20px; /* Larger icon */
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
body.dark #toggle-history-btn {
    background: var(--button-bg-dark);
    color: var(--text-color-dark);
    border: 1px solid var(--border-dark);
}


/* Responsive Design */
@media (max-width: 768px) {
  /* #toggle-history-btn display is handled by initialHistoryPanelState in JS */

  #history-panel {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    max-width: 80vw;
    height: 100vh;
    transform: translateX(-100%);
    z-index: 1000;
    border-right: 1px solid var(--border-light);
    box-shadow: 2px 0 10px rgba(0,0,0,0.1);
  }
  body.dark #history-panel {
      border-right: 1px solid var(--border-dark);
  }

  #history-panel.show {
    transform: translateX(0);
  }
  
  /* Add padding to panel header when toggle button is potentially visible on mobile */
  #history-panel .panel-header {
    padding-top: 50px; /* Adjust this value based on toggle button height + desired spacing */
  }


  #main {
    /* No adjustments needed here */
  }

  #input-container {
    padding: 10px; /* Tighter padding on mobile */
  }
  textarea#user-input {
    min-height: 44px; /* Slightly smaller min height */
    max-height: 150px;
  }
  #input-container button#send-button {
    height: 44px;
    line-height: 42px; /* Adjust line height for vertical centering */
    padding: 0 15px;
  }
}
