*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: "Segoe UI", "Heebo", "Arial Hebrew", Arial, sans-serif;
  background: #e3f2fd;
  color: #222;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  max-width: 100vw;
  max-height: 100dvh;
  height: 100dvh;
}

/* ─── Toolbar ─── */
.toolbar {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px;
  padding-top: max(4px, env(safe-area-inset-top));
  background: linear-gradient(180deg, #4FC3F7 0%, #29B6F6 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  z-index: 2;
}

.toolbar.collapsed .sizes,
.toolbar.collapsed .actions,
.toolbar.collapsed .colors {
  display: none;
}

/* ─── Button row: toggle + sizes + actions ─── */
.bar,
.sizes,
.actions {
  display: flex;
  gap: 4px;
}

.bar {
  gap: 8px;
}

.sizes,
.actions {
  flex: 3 1 0;
}

.tool-btn {
  flex: 1 1 0;
  min-width: 44px;
  height: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0 2px;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1.1;
  border: none;
  border-radius: 12px;
  background: #fff;
  color: #1565C0;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  transition: transform 0.08s;
}

.tool-btn .ico {
  font-size: 1.15rem;
  line-height: 1;
}

.tool-btn:active {
  transform: scale(0.94);
}

.toggle-btn {
  color: #0d47a1;
}

.toolbar.collapsed .toggle-btn {
  flex: 0 0 auto;
  flex-direction: row;
  gap: 0.4rem;
  padding: 0 1rem;
  font-size: 1rem;
}

.size-btn {
  background: rgba(255, 255, 255, 0.7);
  color: #0d47a1;
}

.size-btn.selected {
  background: #fff;
  color: #01579b;
  box-shadow: inset 0 0 0 3px #0d47a1;
}

.action-btn.danger {
  color: #c62828;
}

.action-btn.save {
  color: #2e7d32;
}

/* ─── Color palette: 18 swatches → 2 rows of 9 ─── */
.colors {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 46px));
  justify-content: center;
  gap: 3px;
}

.color-btn {
  width: 100%;
  aspect-ratio: 1;
  min-height: 36px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.1s;
}

.color-btn:active {
  transform: scale(0.92);
}

.color-btn.selected {
  border-color: #fff;
  transform: scale(1.06);
  box-shadow: 0 0 0 2px #0d47a1, 0 3px 6px rgba(0, 0, 0, 0.3);
}

.color-btn.eraser {
  background: #fff !important;
  font-size: 1.15rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ─── Canvas ─── */
.canvas-wrap {
  flex: 1;
  position: relative;
  min-height: 0;
  background: #fff;
  overflow: hidden;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  background: #fff;
}

/* ─── Confirm modal ─── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-box {
  background: #fff;
  border-radius: 24px;
  padding: 1.75rem 1.5rem;
  max-width: 320px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  animation: pop 0.2s ease-out;
}

@keyframes pop {
  from { transform: scale(0.85); opacity: 0; }
  to   { transform: scale(1);    opacity: 1; }
}

.modal-title {
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 1.5rem;
  color: #333;
}

.modal-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.modal-btn {
  min-width: 100px;
  min-height: 56px;
  font-size: 1.3rem;
  font-weight: 800;
  border: none;
  border-radius: 16px;
  cursor: pointer;
  transition: transform 0.08s;
}

.modal-btn:active {
  transform: scale(0.94);
}

.modal-btn.yes {
  background: #ef5350;
  color: #fff;
}

.modal-btn.no {
  background: #e0e0e0;
  color: #333;
}

/* ─── Landscape compact ─── */
@media (orientation: landscape) and (max-height: 500px) {
  .toolbar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 4px 8px;
    padding-left: max(4px, env(safe-area-inset-left));
    padding-right: max(4px, env(safe-area-inset-right));
  }
  .sizes,
  .actions {
    flex: none;
  }
  .tool-btn {
    flex: 0 0 48px;
    height: 40px;
  }
  .colors {
    grid-template-columns: repeat(9, 36px);
  }
}
