:root {
  --bg-a: #f4eee1;
  --bg-b: #dce9ef;
  --panel: #fffdf8;
  --ink: #1f2a31;
  --muted: #55646d;
  --accent: #0f7d72;
  --accent-dark: #0c655d;
  --danger: #8f2c2c;
  --ok: #136339;
  --grid-cols: 10;
  --grid-rows: 10;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Avenir Next", "Franklin Gothic Medium", "Trebuchet MS", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(900px 520px at -8% -10%, #fff8ed 0%, transparent 60%),
    radial-gradient(620px 460px at 100% -5%, #e6f1f6 0%, transparent 58%),
    linear-gradient(180deg, var(--bg-a), var(--bg-b));
  min-height: 100vh;
}

.app {
  max-width: 1300px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 16px;
  animation: fade-in .32s ease both;
}

.panel {
  background: linear-gradient(180deg, #fffefb, var(--panel));
  border: 1px solid #d6ccba;
  border-radius: 14px;
  box-shadow: 0 12px 24px rgba(21, 34, 44, .15);
}

.left {
  padding: 14px;
  display: grid;
  gap: 12px;
  align-content: start;
}

h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: .02em;
}

label, .mini {
  font-size: .9rem;
  color: #324049;
}

.mini { color: var(--muted); }

select, input, button {
  width: 100%;
  border: 1px solid #c4cecf;
  border-radius: 10px;
  font: inherit;
  font-size: .92rem;
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
}

button {
  cursor: pointer;
  background: #f1f6f5;
  transition: transform .12s ease, border-color .12s ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: #879f9d;
}

button:disabled {
  opacity: .52;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, #0f8f82, var(--accent));
  color: #fff;
  border-color: #0c6961;
}

.btn-primary:hover:not(:disabled) {
  border-color: #0a4f4a;
  background: linear-gradient(180deg, #0e867b, var(--accent-dark));
}

.btn-danger {
  background: #fff4f1;
  color: var(--danger);
  border-color: #dcbbb2;
}

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

.row > * {
  flex: 1;
  min-width: 120px;
}

.status {
  border: 1px solid #c7d3d7;
  border-radius: 12px;
  padding: 10px;
  background: #f8fbfc;
  display: grid;
  gap: 8px;
}

.phase {
  font-size: .98rem;
  font-weight: 700;
}

.instructions {
  margin: 0;
  font-size: .9rem;
  color: #3c4c55;
  line-height: 1.35;
}

.feedback {
  margin: 0;
  padding: 6px 8px;
  font-size: .88rem;
  border-radius: 8px;
  background: #eaf2f7;
  border: 1px solid #c6d8e6;
  color: #254353;
  min-height: 26px;
}

.feedback.ok {
  background: #e9f6ee;
  border-color: #b7dcc4;
  color: var(--ok);
}

.feedback.err {
  background: #ffecec;
  border-color: #e9c0c0;
  color: #7e2121;
}

.elements {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.elements button {
  font-weight: 700;
}

.elements button.active {
  background: #d7eeeb;
  border-color: #6e9f99;
  color: #17453f;
}

.phase-group {
  display: none;
  gap: 8px;
  border: 1px dashed #c5cfce;
  border-radius: 10px;
  padding: 10px;
  background: #fcfefd;
}

.phase-group.active {
  display: grid;
  animation: slide-up .2s ease both;
}

.counter {
  font-size: .9rem;
  border-radius: 8px;
  background: #edf7f6;
  border: 1px solid #c3dfda;
  padding: 7px 8px;
  color: #233d46;
}

.right {
  min-height: 76vh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px;
  border-bottom: 1px solid #c8d2d6;
  background: linear-gradient(180deg, #f7fafb, #edf2f5);
}

.tools button {
  width: auto;
  min-width: 112px;
  padding: 8px 10px;
  font-size: .86rem;
}

.tools button.active {
  background: #dbe8f2;
  border-color: #8099aa;
  color: #21485f;
}

.work-wrap {
  display: grid;
  place-items: center;
  padding: 10px;
  background:
    linear-gradient(180deg, rgba(255,255,255,.62), rgba(238,245,249,.85)),
    repeating-linear-gradient(
      135deg,
      rgba(184,198,208,.14) 0 10px,
      rgba(184,198,208,.08) 10px 20px
    );
  overflow: auto;
}

.workspace {
  width: min(100%, 900px);
  aspect-ratio: 1 / 1;
  max-height: calc(76vh - 84px);
  position: relative;
  border-radius: 16px;
  border: 2px solid #5d6e7c;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.72), 0 14px 28px rgba(22,34,43,.18);
  background:
    linear-gradient(180deg, rgba(255,255,255,.92), rgba(248,252,253,.9)),
    linear-gradient(to right, rgba(97,121,136,.14) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(97,121,136,.14) 1px, transparent 1px);
  background-size:
    auto,
    calc(100% / var(--grid-cols)) calc(100% / var(--grid-rows)),
    calc(100% / var(--grid-cols)) calc(100% / var(--grid-rows));
  overflow: hidden;
  touch-action: none;
}

#bondLayer, #atomLayer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.atom {
  position: absolute;
  width: 44px;
  height: 44px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 2px solid #495861;
  background: radial-gradient(circle at 35% 30%, #fff, #f2f5f7);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.04rem;
  cursor: pointer;
  user-select: none;
  box-shadow: 0 4px 10px rgba(33,45,55,.2);
  animation: pop .16s ease both;
}

.atom.bond-start {
  border-color: #be6a00;
  box-shadow: 0 0 0 4px rgba(253,210,151,.42);
}

.atom.movable {
  cursor: grab;
}

.atom.movable:active {
  cursor: grabbing;
}

.lone-pair {
  position: absolute;
  width: 16px;
  height: 8px;
  transform: translate(-50%, -50%) rotate(var(--pair-rot, 0rad));
}

.lone-pair::before,
.lone-pair::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.8);
  background: #1e2b31;
  box-shadow: 0 1px 2px rgba(0,0,0,.23);
  transform: translateY(-50%);
}

.lone-pair::before {
  left: 0;
}

.lone-pair::after {
  right: 0;
}

.lone-pair.draggable {
  cursor: grab;
}

.lone-pair.draggable::before,
.lone-pair.draggable::after {
  background: #0b5b67;
}

.lone-pair.dragging {
  opacity: .35;
}

.drag-ghost {
  position: fixed;
  width: 16px;
  height: 8px;
  pointer-events: none;
  z-index: 9999;
  transform: translate(-50%, -50%);
}

.drag-ghost::before,
.drag-ghost::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: 1px solid #fff;
  background: #0b5b67;
  transform: translateY(-50%);
  box-shadow: 0 1px 3px rgba(0,0,0,.23);
}

.drag-ghost::before {
  left: 0;
}

.drag-ghost::after {
  right: 0;
}

.bond {
  stroke: #1f303a;
  stroke-width: 3.2;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

.caption {
  position: absolute;
  left: 10px;
  top: 10px;
  font-size: .8rem;
  color: #40535d;
  padding: 4px 6px;
  border-radius: 8px;
  background: rgba(255,255,255,.72);
  border: 1px solid rgba(163,180,193,.56);
  pointer-events: none;
}

details {
  font-size: .86rem;
  color: #3b4a52;
  border-top: 1px solid #d3d9d8;
  padding-top: 8px;
}

summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 6px;
}

.credits-dialog {
  border: 1px solid #c6d2d8;
  border-radius: 14px;
  padding: 0;
  max-width: 460px;
  width: min(calc(100% - 24px), 460px);
  box-shadow: 0 16px 34px rgba(18, 30, 40, .3);
}

.credits-dialog::backdrop {
  background: rgba(20, 35, 46, .48);
}

.credits-card {
  margin: 0;
  padding: 16px;
  display: grid;
  gap: 12px;
  background: linear-gradient(180deg, #ffffff, #f5fafb);
}

.credits-card p {
  margin: 0;
  line-height: 1.4;
  color: #2c3d46;
}

.credits-card button {
  width: auto;
  min-width: 96px;
  justify-self: end;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes pop {
  from { opacity: 0; transform: translate(-50%, -50%) scale(.76); }
  to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes slide-up {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; }
  .right { min-height: 66vh; }
  .workspace {
    width: 100%;
    max-height: calc(66vh - 80px);
  }
}
