* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1e1e2e;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overscroll-behavior: none;
}

/* ---------- Calculator ---------- */

.calculator {
  width: 100%;
  height: 100dvh;
  background: #2a2a3d;
  padding: env(safe-area-inset-top, 16px) env(safe-area-inset-right, 16px)
    env(safe-area-inset-bottom, 16px) env(safe-area-inset-left, 16px);
  display: flex;
  flex-direction: column;
}

.display {
  color: #fff;
  font-size: clamp(2.2rem, 10vw, 3rem);
  text-align: right;
  padding: 24px 12px;
  word-wrap: break-word;
  word-break: break-all;
  flex: 1;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  min-height: 100px;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding-bottom: 8px;
}

.btn {
  padding: 0;
  aspect-ratio: 1 / 1;
  min-height: 56px;
  font-size: clamp(1rem, 5vw, 1.3rem);
  border: none;
  border-radius: 50%;
  background: #3d3d54;
  color: #fff;
  cursor: pointer;
  transition: filter 0.15s ease;
  touch-action: manipulation;
}

.btn:active {
  filter: brightness(1.3);
}

.btn.operator {
  background: #f0a030;
}

.btn.equals {
  background: #f0a030;
}

.btn.zero {
  grid-column: span 2;
  aspect-ratio: auto;
  border-radius: 30px;
  text-align: left;
  padding-left: 28px;
}

/* ---------- Chat ---------- */

.chat-wrapper {
  width: 100%;
  height: 100dvh;
  background: #2a2a3d;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 14px;
  color: #fff;
  font-size: 1.1rem;
  font-weight: 600;
  border-bottom: 1px solid #3d3d54;
  background: #24243a;
  flex-shrink: 0;
}

.chat-error {
  display: none;
  color: #ffb4b4;
  background: #4a2020;
  font-size: 0.85rem;
  padding: 10px 16px;
  border-bottom: 1px solid #3d3d54;
}

.chat-error.active {
  display: block;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-message {
  background: #3d3d54;
  color: #fff;
  padding: 8px 12px;
  border-radius: 14px;
  max-width: 78%;
  align-self: flex-start;
  overflow-wrap: anywhere;
}

.chat-message .meta {
  font-size: 0.7rem;
  color: #b0b0c0;
  margin-bottom: 2px;
}

.chat-message .text {
  white-space: pre-wrap;
}

.chat-message .photo {
  display: block;
  max-width: 100%;
  width: 100%;
  max-height: 320px;
  object-fit: cover;
  border-radius: 10px;
  margin-top: 4px;
  cursor: pointer;
}

.chat-message.own {
  align-self: flex-end;
  background: #f0a030;
  color: #1e1e2e;
}

.chat-message.own .meta {
  color: #4a3a10;
}

.chat-form {
  display: flex;
  align-items: center;
  padding: calc(env(safe-area-inset-bottom, 0px) + 10px) 10px 10px;
  gap: 6px;
  border-top: 1px solid #3d3d54;
  background: #24243a;
  flex-shrink: 0;
}

.chat-form input[type="text"] {
  flex: 1;
  min-width: 0;
  padding: 12px 16px;
  border-radius: 22px;
  border: none;
  background: #3d3d54;
  color: #fff;
  font-size: 16px;
}

.chat-form input::placeholder {
  color: #b0b0c0;
}

.icon-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: #3d3d54;
  color: #fff;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.icon-btn.send {
  background: #f0a030;
  color: #1e1e2e;
}

.image-preview-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: #24243a;
  border-top: 1px solid #3d3d54;
  flex-shrink: 0;
}

.image-preview-bar.active {
  display: flex;
}

.image-preview-bar img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
}

.image-preview-bar .remove-preview {
  margin-left: auto;
  background: #3d3d54;
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  cursor: pointer;
}

.image-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 24px;
}

.image-lightbox.active {
  display: flex;
}

.image-lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 16px;
}

.modal {
  background: #2a2a3d;
  padding: 24px;
  border-radius: 12px;
  width: 100%;
  max-width: 320px;
  text-align: center;
}

.modal h2 {
  color: #fff;
  margin-top: 0;
  font-size: 1.2rem;
}

.modal input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: none;
  background: #3d3d54;
  color: #fff;
  font-size: 16px;
  margin: 12px 0;
}

.modal button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: none;
  background: #f0a030;
  color: #1e1e2e;
  font-weight: 600;
  cursor: pointer;
  font-size: 16px;
}

/* ---------- Desktop: center as a phone-sized panel ---------- */

@media (min-width: 600px) {
  body {
    padding: 24px;
  }

  .calculator,
  .chat-wrapper {
    width: 380px;
    height: min(100dvh - 48px, 720px);
    border-radius: 20px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  }

  .chat-header {
    padding: 16px;
  }

  .chat-form {
    padding: 10px;
  }
}
