* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { font-size: 16px; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
}

body, input, select, button {
  color: var(--color-text);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1rem;
  line-height: 1.75;
}

a {
  color: inherit;
  text-decoration: none;
  border-bottom: dotted 1px;
  transition: color 0.2s ease, border-color 0.2s ease;
}

a:hover { color: var(--color-text-strong); border-bottom-color: transparent; }

p { margin: 0 0 1.5em; }

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1em;
  padding: 1.5em 2em;
  background: var(--color-bg);
}

.header__title {
  color: var(--color-text-strong);
  font-size: 1.25em;
  font-weight: bold;
  border: 0;
}

.header__link {
  color: var(--color-text-light);
  font-size: 0.7em;
  font-weight: bold;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  border: 0;
}

.header__link:hover { color: var(--color-text-strong); }

@media (max-width: 600px) {
  .header { padding: 1.25em 1em; }
  .header__title { font-size: 1.1em; white-space: nowrap; }
  .header__link { font-size: 0.6em; letter-spacing: 0.15em; text-align: right; }
}

/* ---- Layout: header and workspace fill the window, footer below ---- */

.screen {
  display: flex;
  flex-direction: column;
  height: max(100vh, 48em);
}

.app {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 1.5em;
  padding: 2em;
  background: var(--color-accent1);
}

/* ---- Ribbon (the list on the left) ---- */

.ribbon {
  flex: 0 0 15em;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 1em;
  background: color-mix(in srgb, var(--color-bg-alt) 35%, transparent);
  border: solid 1px var(--color-border);
  border-radius: 0.5em;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}

.ribbon.is-drop-zone {
  border-style: dashed;
  border-color: var(--color-text-light);
}

.ribbon.is-over {
  border-color: var(--color-text-strong);
  background: color-mix(in srgb, var(--color-bg-alt) 60%, transparent);
}

.ribbon__hint {
  margin: 0 0 0.75em;
  color: var(--color-text-light);
  font-size: 0.8em;
  line-height: 1.5;
}

.ribbon__list {
  flex: 1;
  display: grid;
  align-content: start;
  gap: 0.5em;
  margin: 0;
  padding: 0 0.25em 0 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.chip {
  display: grid;
  width: 100%;
  padding: 0.6em 0.85em;
  text-align: left;
  background: var(--color-border-bg);
  border: solid 1px var(--color-border);
  border-radius: 0.25em;
  cursor: grab;
  touch-action: pan-y;   /* vertical swipes scroll the list, sideways ones drag */
  user-select: none;
  -webkit-user-select: none;
  transition: border-color 0.2s ease, opacity 0.2s ease;
}

.chip:hover,
.chip:focus-visible { border-color: var(--color-text-strong); outline: 0; }

.chip.is-dragging { opacity: 0.4; }

.chip__name {
  color: var(--color-text-strong);
  font-weight: bold;
  font-size: 0.9em;
  line-height: 1.4;
}

.chip__units {
  overflow: hidden;
  color: var(--color-text-light);
  font-size: 0.75em;
  line-height: 1.5;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Board (the 3 × 4 grid) ---- */

.board {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 1em;
}

.cell {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 12em;
  background: var(--color-border-bg);
  border: solid 1px var(--color-border);
  border-radius: 0.5em;
  transition: border-color 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
}

.cell--empty {
  align-items: center;
  justify-content: center;
  padding: 1em;
  background: color-mix(in srgb, var(--color-bg-alt) 35%, transparent);
  border: dashed 2px var(--color-border);
}

.cell__hint {
  color: var(--color-text-light);
  font-size: 0.8em;
  text-align: center;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.cell--empty:hover .cell__hint,
body.is-dragging .cell__hint { opacity: 1; }

.cell.is-over {
  border-color: var(--color-text-strong);
  background: color-mix(in srgb, var(--color-bg-alt) 55%, transparent);
}

.cell.is-dragging { opacity: 0.4; }

.cell.is-new { animation: placed 0.6s ease; }

@keyframes placed {
  from { border-color: var(--color-text-strong); }
}

/* ---- A converter inside a cell ---- */

.conv__head {
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 0.6em 1em;
  border-bottom: solid 1px var(--color-border);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: 0;
}

.conv__head:focus-visible { box-shadow: inset 0 0 0 2px var(--color-text-strong); border-radius: 0.5em 0.5em 0 0; }

.conv__grip {
  flex: none;
  width: 0.6em;
  height: 1em;
  background-image: radial-gradient(circle, var(--color-text-light) 1.2px, transparent 1.6px);
  background-size: 0.3em 0.333em;
}

.conv__name {
  overflow: hidden;
  color: var(--color-text-strong);
  font-weight: bold;
  font-size: 0.9em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv__body {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.4em;
  padding: 0.75em 1em 1em;
}

.conv__row {
  display: flex;
  gap: 0.5em;
}

.conv__value,
.conv__unit {
  height: 2.5em;
  color: var(--color-text-strong);
  font-size: 0.95em;
  background: color-mix(in srgb, var(--color-bg-alt) 45%, transparent);
  border: solid 1px var(--color-border);
  border-radius: 0.25em;
  outline: 0;
}

.conv__value {
  flex: 1 1 auto;
  width: 0;
  min-width: 0;
  padding: 0 0.75em;
  font-family: "Courier New", monospace;
}

.conv__unit {
  flex: 0 1 auto;
  max-width: 55%;
  padding: 0 1.8em 0 0.6em;
  appearance: none;
  -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--color-text-light) 50%),
                    linear-gradient(-45deg, transparent 50%, var(--color-text-light) 50%);
  background-position: right 0.95em center, right 0.65em center;
  background-size: 0.3em 0.3em;
  background-repeat: no-repeat;
  cursor: pointer;
  text-overflow: ellipsis;
}

.conv__unit option { color: #000; background: #fff; }

.conv__value:focus,
.conv__unit:focus {
  border-color: var(--color-text-strong);
  box-shadow: 0 0 0 1px var(--color-text-strong);
}

.conv__value.is-invalid { border-color: var(--color-warning); }

.conv__swap {
  align-self: center;
  width: 2em;
  height: 2em;
  padding: 0;
  color: var(--color-text-light);
  font-size: 0.9em;
  line-height: 1;
  background: transparent;
  border: solid 1px var(--color-border);
  border-radius: 50%;
  cursor: pointer;
  transition: color 0.2s ease, border-color 0.2s ease;
}

.conv__swap:hover,
.conv__swap:focus-visible { color: var(--color-text-strong); border-color: var(--color-text-strong); outline: 0; }

/* ---- Dragging ---- */

body.is-dragging,
body.is-dragging * { cursor: grabbing !important; }

body.is-dragging { user-select: none; -webkit-user-select: none; }

.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 0.5em 1em;
  color: var(--color-text-strong);
  font-size: 0.9em;
  font-weight: bold;
  white-space: nowrap;
  background: var(--color-bg);
  border: solid 1px var(--color-text-strong);
  border-radius: 0.25em;
  box-shadow: 0 1em 2em -0.5em rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.board-note {
  position: fixed;
  left: 50%;
  bottom: 1.5em;
  z-index: 100;
  max-width: calc(100% - 2em);
  margin: 0;
  padding: 0.75em 1.25em;
  color: var(--color-text-strong);
  font-size: 0.9em;
  background: var(--color-bg);
  border: solid 1px var(--color-border);
  border-radius: 0.25em;
  opacity: 0;
  transform: translate(-50%, 1em);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

.board-note.is-shown { opacity: 1; transform: translate(-50%, 0); }

/* ---- Smaller screens ---- */

@media (max-width: 1200px) {
  .screen { height: auto; min-height: 100vh; }
  .app { align-items: flex-start; }
  .ribbon { position: sticky; top: 1em; max-height: calc(100vh - 2em); }
  .board { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: none; grid-auto-rows: minmax(12em, auto); }
}

@media (max-width: 760px) {
  .app { flex-direction: column; align-items: stretch; gap: 1em; padding: 1em; }
  .ribbon {
    top: 0;
    z-index: 10;
    flex: none;
    max-height: none;
    padding: 0.75em;
    background: var(--color-accent1);
    box-shadow: 0 0.75em 1em -0.75em rgba(0, 0, 0, 0.4);
  }
  .ribbon__hint { margin-bottom: 0.5em; }
  .ribbon__list {
    display: flex;
    gap: 0.5em;
    padding: 0 0 0.35em;
    overflow-x: auto;
    overflow-y: hidden;
  }
  .ribbon__list li { flex: 0 0 auto; }
  .chip { width: 11em; touch-action: pan-x; }   /* sideways swipes scroll, downward ones drag */
}

@media (max-width: 520px) {
  .board { grid-template-columns: minmax(0, 1fr); grid-auto-rows: minmax(11em, auto); }
}

.footer {
  padding: 2.5em 2em;
  background: var(--color-accent3);
}

.footer .menu {
  display: flex;
  flex-wrap: wrap;
  max-width: 68em;
  margin: 0 auto;
  padding: 0;
  list-style: none;
  font-size: 0.8em;
  color: var(--color-border);
}

.footer .menu li + li {
  margin-left: 1.5em;
  padding-left: 1.5em;
  border-left: solid 1px var(--color-border);
}
