:root {
    --cream: rgb(241, 218, 168);
    --moss: rgb(14, 56, 2);
    --panel: rgba(0, 0, 0, 0.90);
    --ink: #2b2a22;
    --note: 200px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    scrollbar-color: var(--cream) var(--moss);
}

body.sb-page {
    margin: 0;
    width: 100%;
    min-height: 100vh;
    overflow: hidden;
    font-family: 'Warbler', Georgia, serif;
    color: var(--cream);
    background-color: var(--moss);
    background-image: url('../img/forest-floor-texture.webp');
    background-size: 520px;
    background-blend-mode: darken;
}

button,
input,
textarea {
    font-family: inherit;
    color: inherit;
}

.sb-box {
    background: var(--panel);
    border: 1px solid var(--cream);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.sb-btn {
    min-height: 44px;
    padding: 0.6em 1.1em;
    border: 1px solid var(--cream);
    background: var(--panel);
    font-size: 1.05rem;
    letter-spacing: 1px;
    cursor: pointer;
}

.sb-btn:hover:not(:disabled) {
    background: var(--cream);
    color: #0b2402;
}

.sb-btn.sb-primary {
    background: var(--cream);
    color: #0b2402;
}

.sb-btn.sb-primary:hover:not(:disabled) {
    background: #fff4dc;
}

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

.sb-btn.sb-small {
    min-height: 36px;
    padding: 0.35em 0.8em;
    font-size: 0.95rem;
}

.sb-btn[aria-pressed="true"] {
    background: var(--cream);
    color: #0b2402;
}

.sb-btn:focus-visible,
.sb-back:focus-visible,
.sb-help-toggle:focus-visible,
.sb-note:focus-visible,
.sb-tabs button:focus-visible,
.sb-swatch:focus-visible {
    outline: 3px solid var(--cream);
    outline-offset: 3px;
}

/* Fixed back button */
.sb-back {
    position: fixed;
    top: calc(1rem + env(safe-area-inset-top, 0px));
    left: calc(1rem + env(safe-area-inset-left, 0px));
    z-index: 40000000;
    min-height: 44px;
    padding: 0.55rem 1rem;
    font-size: 1.15rem;
    cursor: pointer;
}

.sb-back:hover {
    background: var(--cream);
    color: #0b2402;
}

/* Keep the oversized board out of the document's mobile layout viewport. */
.sb-viewport {
    position: fixed;
    inset: 0;
    overflow: auto;
    overscroll-behavior: none;
    overflow-anchor: none;
}

/* The world: sized by JS so it always contains every note */
.sb-world {
    position: relative;
    display: block;
    overflow: hidden;
}

.sb-layer {
    position: absolute;
    top: 0;
    left: 0;
}

/* Notes */
.sb-note {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--note);
    height: var(--note);
    transform-origin: center;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.sb-note-face {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    box-shadow: 2px 7px 14px rgba(0, 0, 0, 0.5);
}

.sb-note--text .sb-note-face,
.sb-note--draw .sb-note-face {
    background-image: linear-gradient(rgba(0, 0, 0, 0.07), rgba(0, 0, 0, 0) 20%);
}

.sb-note--photo .sb-note-face {
    padding: 10px;
    background: #fbf8f0;
}

.sb-note-face img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
}

.sb-note-text {
    position: absolute;
    inset: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    overflow: hidden;
}

.sb-note-text span {
    display: block;
    width: 100%;
    color: var(--ink);
    line-height: 1.2;
    text-align: center;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.sb-note.is-mine {
    cursor: pointer;
}

.sb-note.is-active {
    cursor: grab;
    touch-action: none;
}

.sb-note.is-active .sb-note-face {
    outline: 2px dashed var(--cream);
    outline-offset: 6px;
}

.sb-note.is-dragging {
    cursor: grabbing;
}

.sb-note.is-dragging .sb-note-face {
    box-shadow: 6px 18px 30px rgba(0, 0, 0, 0.55);
}

.sb-rotate {
    position: absolute;
    top: -52px;
    left: 50%;
    width: 30px;
    height: 30px;
    margin-left: -15px;
    border: 2px solid var(--moss);
    border-radius: 50%;
    background: var(--cream);
    cursor: grab;
    touch-action: none;
}

.sb-rotate::before {
    content: '';
    position: absolute;
    inset: -10px;
}

.sb-rotate::after {
    content: '';
    position: absolute;
    top: 100%;
    left: 50%;
    width: 2px;
    height: 22px;
    background: var(--cream);
    transform: translateX(-50%);
}

/* "Display note" ghost: always above every other note */
.sb-ghost {
    pointer-events: none;
}

.sb-ghost .sb-note-face {
    opacity: 0.9;
    outline: 3px dashed var(--cream);
    outline-offset: 6px;
    animation: sb-pulse 1.6s ease-in-out infinite;
}

.sb-ghost-label {
    position: absolute;
    bottom: 100%;
    left: 50%;
    margin-bottom: 14px;
    padding: 0.15em 0.6em;
    background: var(--cream);
    color: #0b2402;
    font-size: 0.9rem;
    letter-spacing: 1px;
    white-space: nowrap;
    transform: translateX(-50%);
}

@keyframes sb-pulse {
    50% {
        outline-color: rgba(241, 218, 168, 0.35);
    }
}

@media (prefers-reduced-motion: reduce) {
    .sb-ghost .sb-note-face {
        animation: none;
    }
}

.sb-loading {
    position: fixed;
    top: calc(5rem + env(safe-area-inset-top, 0px));
    left: 50%;
    z-index: 40000001;
    width: min(20rem, calc(100vw - 2rem));
    padding: 0.8rem 1rem;
    text-align: center;
    transform: translateX(-50%);
    pointer-events: none;
}

.sb-loading-track {
    height: 4px;
    margin-top: 0.65rem;
    overflow: hidden;
    background: rgba(241, 218, 168, 0.2);
}

.sb-loading-track::before {
    content: '';
    display: block;
    width: 40%;
    height: 100%;
    background: var(--cream);
    animation: sb-loading 1.2s ease-in-out infinite alternate;
}

@keyframes sb-loading {
    from { transform: translateX(0); }
    to { transform: translateX(150%); }
}

@media (prefers-reduced-motion: reduce) {
    .sb-loading-track::before {
        width: 100%;
        animation: none;
    }
}

/* Center hub */
.sb-hub {
    position: absolute;
    z-index: 10000000;
    width: min(20rem, calc(100vw - 2rem));
    transform: translate(-50%, -50%);
}

.sb-hub-main {
    padding: 1.4rem 1.25rem 1.1rem;
    text-align: center;
}

.sb-hub h1 {
    margin: 0 0 0.35rem;
    font-family: 'Sabbath Black', serif;
    font-size: 2.1rem;
    font-weight: normal;
}

.sb-hub-sub,
.sb-reset {
    margin: 0;
    font-size: 0.95rem;
    letter-spacing: 1px;
    opacity: 0.85;
}

.sb-hub-actions {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin: 1rem 0 0.75rem;
}

.sb-hub-row {
    display: flex;
    gap: 0.6rem;
}

.sb-hub-row .sb-btn {
    flex: 1;
    padding-inline: 0.5em;
    font-size: 0.95rem;
}

.sb-hub-status {
    min-height: 1.3em;
    margin: 0 0 0.5rem;
    font-size: 0.95rem;
    line-height: 1.4;
}

.sb-hub-status:empty {
    display: none;
}

.sb-help-toggle {
    position: absolute;
    top: calc(100% + 14px);
    left: 50%;
    width: 48px;
    height: 48px;
    font-family: 'Sabbath Black', serif;
    font-size: 1.6rem;
    cursor: pointer;
    transform: translateX(-50%);
}

.sb-help-toggle:hover {
    background: var(--cream);
    color: #0b2402;
}

/* Placement toolbar */
.sb-editbar {
    position: fixed;
    bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
    left: 50%;
    z-index: 40000000;
    width: max-content;
    max-width: calc(100vw - 1.5rem - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
    padding: 0.6rem 0.75rem 0.75rem;
    transform: translateX(-50%);
}

.sb-editbar-hint {
    margin: 0 0 0.5rem;
    font-size: 0.85rem;
    letter-spacing: 1px;
    text-align: center;
    opacity: 0.85;
}

.sb-editbar-buttons {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
}

.sb-icon {
    width: 44px;
    padding: 0;
    font-size: 1.4rem;
}

.sb-toast {
    position: fixed;
    top: 1rem;
    left: 50%;
    z-index: 40000001;
    max-width: calc(100vw - 10rem);
    padding: 0.6rem 1rem;
    border: 1px solid var(--cream);
    background: var(--panel);
    letter-spacing: 1px;
    text-align: center;
    transform: translateX(-50%);
}

/* Dialogs */
.sb-dialog {
    width: min(34rem, calc(100vw - 1.5rem));
    max-height: calc(100vh - 1.5rem);
    padding: 1.1rem 1.25rem 1.25rem;
    border: 1px solid var(--cream);
    background: var(--panel);
    color: var(--cream);
}

.sb-dialog::backdrop {
    background: rgba(0, 0, 0, 0.65);
}

.sb-dialog-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.sb-dialog h2 {
    margin: 0;
    font-family: 'Sabbath Black', serif;
    font-size: 1.8rem;
    font-weight: normal;
}

.sb-dialog h3 {
    margin: 0 0 0.4rem;
    font-size: 1.1rem;
}

.sb-dialog p,
.sb-dialog li {
    line-height: 1.5;
    letter-spacing: 1px;
}

.sb-dialog ul {
    padding-left: 1.2rem;
}

.sb-dialog li {
    margin-bottom: 0.4rem;
}

.sb-x {
    width: 40px;
    height: 40px;
    border: 0;
    background: none;
    font-size: 1.8rem;
    cursor: pointer;
}

.sb-dialog-foot {
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
    margin-top: 1rem;
}

.sb-tabs {
    display: flex;
    margin-bottom: 1rem;
    border-bottom: 1px solid rgba(241, 218, 168, 0.4);
}

.sb-tabs button {
    flex: 1;
    min-height: 44px;
    border: 0;
    border-bottom: 3px solid transparent;
    background: none;
    font-size: 1.1rem;
    letter-spacing: 1px;
    cursor: pointer;
}

.sb-tabs button[aria-selected="true"] {
    border-bottom-color: var(--cream);
    font-weight: bold;
}

.sb-square {
    display: block;
    width: min(100%, 22rem, 46vh);
    aspect-ratio: 1;
    height: auto;
    margin: 0 auto;
}

#sb-text {
    padding: 1.2rem;
    border: 0;
    background: #f6e07a;
    color: var(--ink);
    font-size: 1.35rem;
    line-height: 1.3;
    text-align: center;
    resize: none;
}

#sb-text:focus-visible {
    outline: 3px solid var(--cream);
    outline-offset: 3px;
}

#sb-draw {
    background: #f6e07a;
    cursor: crosshair;
    touch-action: none;
}

#sb-crop {
    background: #111;
    cursor: move;
    touch-action: none;
}

.sb-file {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px dashed var(--cream);
    font-size: 1.2rem;
    letter-spacing: 1px;
    cursor: pointer;
}

.sb-file:hover,
.sb-file:focus-within {
    background: rgba(241, 218, 168, 0.1);
}

.sb-file input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
}

.sb-counter {
    margin: 0.4rem 0 0;
    font-size: 0.9rem;
    text-align: center;
    opacity: 0.85;
}

.sb-tools {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.6rem;
    margin-top: 0.75rem;
}

.sb-range {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.sb-range input {
    width: 7rem;
    accent-color: var(--cream);
}

.sb-swatches {
    display: flex;
    gap: 0.4rem;
}

.sb-swatch {
    width: 30px;
    height: 30px;
    padding: 0;
    border: 2px solid rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    cursor: pointer;
}

.sb-swatch[aria-pressed="true"] {
    outline: 3px solid var(--cream);
    outline-offset: 2px;
}

.sb-error {
    min-height: 1.2em;
    margin: 0.6rem 0 0;
    color: #ffb4a2;
    text-align: center;
}

.sb-noscript {
    position: fixed;
    inset: 40% 0 auto;
    text-align: center;
}

@media (max-width: 767px) {
    .sb-back {
        top: calc(0.6rem + env(safe-area-inset-top, 0px));
        left: calc(0.6rem + env(safe-area-inset-left, 0px));
    }

    .sb-hub h1 {
        font-size: 1.7rem;
    }

    .sb-editbar {
        bottom: calc(0.6rem + env(safe-area-inset-bottom, 0px));
        width: calc(100vw - 1.2rem - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
        padding: 0.5rem;
    }

    .sb-editbar-hint {
        font-size: 0.75rem;
        letter-spacing: 0;
    }

    .sb-editbar-buttons {
        gap: 0.35rem;
    }

    .sb-editbar-buttons .sb-btn:not(.sb-icon) {
        min-width: 0;
        padding-inline: 0.5rem;
        font-size: 0.9rem;
        letter-spacing: 0;
    }
}
