* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #e5e7eb;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: #1f2937;
}

/* App container: Split LHS (Grid) and RHS (Canvas) */
.app-container {
    display: flex;
    width: 100%;
    height: 100%;
}

/* LHS: Grid panel */
.lhs-panel {
    flex: 0 0 auto;
    width: 42%;
    /* Cap the panel so it stops growing on large screens and the RHS canvas
       keeps all the extra width. ~400px = grid (350px) + horizontal padding
       (32px) + a little buffer for a vertical scrollbar. */
    max-width: 400px;
    min-width: 380px;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 12px 16px;
    background-color: #f3f4f6;
    border-right: 1px solid #d1d5db;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.04);
    user-select: none;
    overflow-y: auto;
}

.grid-header {
    width: 100%;
    max-width: 350px;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 8px;
}

.grid-title {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: #374151;
}

.grid-hints {
    font-size: 11px;
    color: #6b7280;
}

/* Direction & Wrapping Controls: three cells (label | clock chooser | wrap) */
.control-bar {
    width: 100%;
    max-width: 350px;
    background: #ffffff;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 8px 10px;
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

/* The three left-to-right areas of the control bar */
.control-cell {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    min-width: 0;
}

.control-cell--label {
    flex: 1 1 0;
    align-items: flex-start;
}

.control-cell--chooser {
    flex: 0 0 auto;
    align-items: center;
}

.control-cell--wrap {
    flex: 1 1 0;
    align-items: flex-end;
}

.control-label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #4b5563;
}

.control-hint {
    font-size: 10px;
    line-height: 1.2;
    color: #9ca3af;
}

/* Clock-like direction chooser: 8 arrow buttons arranged around a circle */
.direction-clock {
    position: relative;
    width: 92px;
    height: 92px;
    border-radius: 50%;
    background: radial-gradient(circle at 50% 50%, #ffffff 0%, #f1f5f9 62%, #e2e8f0 100%);
    border: 1px solid #e2e8f0;
    box-shadow: inset 0 1px 3px rgba(15, 23, 42, 0.08);
}

/* Small hub dot reinforces the clock-face / compass motif */
.direction-clock::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 8px;
    height: 8px;
    margin: -4px 0 0 -4px;
    border-radius: 50%;
    background: #cbd5e1;
    box-shadow: 0 0 0 3px rgba(203, 213, 225, 0.35);
    pointer-events: none;
}

/* Each direction button sits on the clock face; the arrow rotates to point
   outward, so one SVG serves all eight directions. */
.dir-btn {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 22px;
    height: 22px;
    padding: 0;
    border: 1px solid #e5e7eb;
    border-radius: 50%;
    background: #ffffff;
    color: #64748b;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
    transform: translate(-50%, -50%) rotate(var(--angle, 0deg)) translateY(-34px);
    transition: background-color 0.1s ease, border-color 0.1s ease,
                color 0.1s ease, box-shadow 0.1s ease;
}

.dir-btn:hover {
    background: #f1f5f9;
    border-color: #cbd5e1;
    color: #334155;
}

.dir-btn.active {
    background: #e0f2fe;
    border-color: #0284c7;
    color: #0369a1;
    box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.25);
}

.dir-arrow {
    width: 14px;
    height: 14px;
    display: block;
}

/* Wrap toggle checkbox */
.wrap-toggle-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: #374151;
    padding: 2px 8px;
    background-color: #f9fafb;
    border: 1px solid #d1d5db;
    border-radius: 4px;
}

.wrap-toggle-item input[type="checkbox"] {
    cursor: pointer;
    accent-color: #0284c7;
}

/* Grid wrapper maintains 10:14 aspect ratio and shrinks when needed so lower controls stay visible */
.grid-wrapper {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-width: 350px;
    max-height: 490px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 10 x 14 Letter Grid */
.poem-grid {
    height: 100%;
    max-width: 100%;
    aspect-ratio: 10 / 14;
    display: grid;
    grid-template-columns: repeat(var(--grid-cols, 10), 1fr);
    grid-template-rows: repeat(var(--grid-rows, 14), 1fr);
    gap: 3px;
    padding: 6px;
    background-color: #e5e7eb;
    border-radius: 8px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Grid cell */
.grid-cell {
    background-color: #ffffff;
    border: 1.5px solid #cbd5e1;
    border-radius: 4px;
    color: #0f172a;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: clamp(16px, 3.5vh, 22px);
    font-weight: 800;
    text-transform: uppercase;
    line-height: 1;

    display: flex;
    align-items: center;
    justify-content: center;

    cursor: pointer;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    transition: background-color 0.08s ease, border-color 0.08s ease, transform 0.06s ease;
}

.grid-cell:hover {
    background-color: #f8fafc;
    border-color: #94a3b8;
}

/* Active cursor cell */
.grid-cell.active-cursor {
    background-color: #e0f2fe;
    border-color: #0284c7;
    box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.35);
    transform: scale(1.02);
    z-index: 2;
}

/* RHS: Canvas panel */
.rhs-panel {
    flex: 1 1 auto;
    height: 100%;
    position: relative;
    background-color: #fcfcfd;
    overflow: hidden;
}

#word-cloud-canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* Word cloud test controls (LHS, below the 10x14 grid) */
.cloud-controls {
    flex: 0 0 auto;
    width: 100%;
    max-width: 350px;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px 10px;
    background: #ffffff;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    user-select: none;
}

.cloud-controls-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.cloud-controls button {
    width: 28px;
    height: 26px;
    padding: 0;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    background: #ffffff;
    color: #374151;
    font-size: 15px;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.1s ease, border-color 0.1s ease;
}

.cloud-controls button.cloud-preset-btn {
    flex: 1 1 0;
    width: auto;
    height: 24px;
    font-size: 11px;
    font-weight: 600;
}

.cloud-controls button:hover {
    background: #f1f5f9;
    border-color: #94a3b8;
}

.cloud-controls button:active {
    background: #e2e8f0;
}

.cloud-count {
    min-width: 84px;
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: #4b5563;
}

/* In short viewports (< 640px height), allow grid to expand and fill available vertical space */
@media (max-height: 640px) {
    .lhs-panel {
        padding: 8px 12px;
    }

    .grid-header {
        margin-bottom: 4px;
    }

    .control-bar {
        padding: 4px 8px;
        margin-bottom: 6px;
        gap: 4px;
    }

    .grid-wrapper {
        flex: 1 1 auto;
        height: 100%;
        max-height: calc(100vh - 165px);
        width: auto;
        max-width: 100%;
    }

    /* Shrink the clock a touch so the controls stay compact on short screens */
    .direction-clock {
        width: 78px;
        height: 78px;
    }

    .dir-btn {
        width: 20px;
        height: 20px;
        transform: translate(-50%, -50%) rotate(var(--angle, 0deg)) translateY(-28px);
    }

    .dir-arrow {
        width: 12px;
        height: 12px;
    }
}
