:root {
  --bg-start: #f0f4f8;
  --bg-end: #d9e2ec;
  --panel: #ffffff;
  --panel-border: #cbd2d9;
  --text: #102a43;
  --muted: #486581;
  --primary: #1f7a8c;
  --primary-strong: #135c6b;
  --accent: #f0a500;
  --danger: #cc2936;
  --button: #f8fafc;
  --button-hover: #dfe9f3;
  --shadow: 0 18px 40px rgba(16, 42, 67, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Avenir Next", "Segoe UI", "Helvetica Neue", sans-serif;
  color: var(--text);
  background: linear-gradient(130deg, var(--bg-start), var(--bg-end));
}

.page {
  min-height: 100vh;
  display: grid;
  place-items: start center;
  padding: 24px;
}

.calculator {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.left-panel {
  flex-shrink: 0;
  width: 420px;
}

.display-wrap {
  background: linear-gradient(180deg, #f9fbfd, #edf3f8);
  border: 1px solid #d7e2ec;
  border-radius: 12px;
  padding: 6px 10px;
  min-height: 56px;
}

.expression {
  margin: 0;
  min-height: 16px;
  text-align: right;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.01em;
}

.display {
  margin: 2px 0 1px;
  min-height: 24px;
  text-align: right;
  font-size: clamp(1rem, 3vw, 1.3rem);
  font-weight: 700;
}

.status {
  margin: 0;
  min-height: 16px;
  font-size: 0.75rem;
  color: var(--muted);
}

.status.error {
  color: var(--danger);
  font-weight: 600;
}

.buttons {
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.btn {
  border: 1px solid #d5dee8;
  border-radius: 10px;
  background: var(--button);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  min-height: 28px;
  cursor: pointer;
  transition:
    transform 120ms ease,
    background-color 120ms ease,
    box-shadow 120ms ease;
}

.btn:hover:not(:disabled) {
  background: var(--button-hover);
  transform: translateY(-1px);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-op {
  background: color-mix(in srgb, var(--primary) 12%, white);
}

.btn-equals {
  background: color-mix(in srgb, var(--accent) 22%, white);
}

.btn-clear {
  background: color-mix(in srgb, var(--danger) 16%, white);
}

.btn-zero {
  grid-column: span 4;
}

.chat-panel {
  margin-top: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 14px;
  background: linear-gradient(180deg, #f9fbfd, #edf3f8);
}

.chat-title {
  margin: 0;
  font-size: 0.94rem;
  letter-spacing: 0.02em;
  color: var(--text);
}

.chat-subtitle {
  margin: 2px 0 10px;
  font-size: 0.78rem;
  color: var(--muted);
}

.chat-input-wrap {
  display: flex;
  gap: 8px;
}

.chat-input {
  flex: 1;
  border: 1px solid #d5dee8;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 0.92rem;
  font-family: inherit;
  color: var(--text);
  background: #ffffff;
  outline: none;
}

.chat-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(31, 122, 140, 0.15);
}

.chat-response-wrap {
  margin-top: 10px;
}

.chat-response-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 4px;
}

.chat-response {
  min-height: 44px;
  padding: 10px 12px;
  border: none;
  border-left: 3px solid var(--primary);
  border-radius: 0 8px 8px 0;
  background: color-mix(in srgb, var(--primary) 6%, white);
  font-size: 0.9rem;
  line-height: 1.45;
  color: var(--text);
}

.chat-response .chat-placeholder {
  margin: 0;
  color: var(--muted);
  font-style: italic;
  font-size: 0.85rem;
}

.chat-response.loading {
  opacity: 0.6;
}

.chat-response.error {
  border-left-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 6%, white);
  color: var(--danger);
}

.chat-send {
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #ffffff;
  font-size: 0.92rem;
  font-weight: 700;
  padding: 10px 18px;
  cursor: pointer;
  transition: background-color 120ms ease;
}

.chat-send:hover:not(:disabled) {
  background: var(--primary-strong);
}

.chat-send:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.console-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  border: 1px solid #11304f;
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(180deg, #0e2237, #0b1a2c);
  color: #d9e6f5;
}

.console-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(205, 224, 245, 0.2);
}

.console-title {
  margin: 0;
  font-size: 0.94rem;
  letter-spacing: 0.02em;
}

.console-controls {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.console-connection {
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  background: #1f3b58;
  color: #d9e6f5;
}

.console-connection.connected {
  background: color-mix(in srgb, var(--primary) 70%, #ffffff 30%);
  color: #ffffff;
}

.console-connection.connecting {
  background: #2a4765;
  color: #deebfb;
}

.console-connection.disconnected {
  background: color-mix(in srgb, var(--danger) 75%, #ffffff 25%);
  color: #ffffff;
}

.console-clear {
  border: 1px solid rgba(205, 224, 245, 0.4);
  border-radius: 9px;
  background: transparent;
  color: #deebfb;
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1;
  padding: 7px 10px;
  cursor: pointer;
}

.console-clear:hover {
  background: rgba(220, 234, 251, 0.16);
}

.console-help {
  margin: 0;
  padding: 8px 12px;
  color: #abc4df;
  font-size: 0.8rem;
  border-bottom: 1px solid rgba(205, 224, 245, 0.15);
}

.console-list {
  list-style: none;
  margin: 0;
  padding: 10px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

.console-entry {
  border: 1px solid rgba(205, 224, 245, 0.2);
  border-radius: 10px;
  background: rgba(232, 244, 255, 0.06);
  padding: 8px;
}

.console-entry.error {
  border-color: rgba(255, 167, 176, 0.55);
  background: rgba(219, 76, 96, 0.14);
}

.console-meta {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 0.72rem;
  color: #a7bfd8;
}

.console-time {
  color: #98b8d8;
}

.console-component {
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border: 1px solid rgba(194, 216, 239, 0.35);
  border-radius: 999px;
  padding: 1px 6px;
}

.console-event {
  color: #b8d2eb;
  font-weight: 700;
}

.console-summary {
  margin: 6px 0 0;
  font-size: 0.74rem;
  font-weight: 400;
  color: #e9f2fc;
}

.console-explanation {
  margin: 4px 0 0;
  font-size: 0.71rem;
  line-height: 1.35;
  color: #bfd4ea;
}

.console-payload {
  margin: 6px 0 0;
  padding: 8px 10px;
  border: 1px solid rgba(100, 160, 220, 0.3);
  border-left: 3px solid rgba(100, 160, 220, 0.6);
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.25);
  font-size: 0.72rem;
  line-height: 1.5;
  color: #a8d4f5;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}

body.loading .calculator {
  animation: pulse 1s ease-in-out infinite alternate;
}

@keyframes pulse {
  from {
    box-shadow: var(--shadow);
  }
  to {
    box-shadow: 0 24px 48px rgba(16, 42, 67, 0.2);
  }
}

@media (max-width: 640px) {
  .calculator {
    flex-direction: column;
    border-radius: 18px;
    padding: 16px;
  }

  .left-panel {
    width: 100%;
  }

  .console-panel {
    min-height: 300px;
  }

  .btn {
    min-height: 36px;
  }
}
